From 83f5fc33df8ea2b8df661f921f157080d472a0ae Mon Sep 17 00:00:00 2001 From: kdx Date: Sun, 26 Mar 2023 07:11:55 +0000 Subject: simplify entityimpl a bit --- src/deathpart.c | 8 ++++---- src/entity.c | 4 ++-- src/entity.h | 4 ++-- src/entityimpl.h | 11 ++++------- src/entitytag.c | 3 +-- src/entitytag.h | 9 ++++++--- src/exit.c | 8 ++++---- src/player.c | 8 ++++---- src/spike.c | 7 +++---- 9 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/deathpart.c b/src/deathpart.c index b397133..bdc3cdf 100644 --- a/src/deathpart.c +++ b/src/deathpart.c @@ -3,7 +3,7 @@ #include "entityimpl.h" #include -IMPL_UPDATE() { +IMPL(update) { if (this->deathpart.skip == 0) { this->vel[1] *= AIR_RESISTANCE; this->vel[1] += GRAVITY; @@ -11,16 +11,16 @@ IMPL_UPDATE() { this->deathpart.skip = 3; } else this->deathpart.skip -= 1; -} IMPL_END +} -IMPL_DRAW() { +IMPL(draw) { LZY_DrawSetColor(BLACK); if (this->width == 1) (void)LZY_DrawPoint(this->pos[0], this->pos[1]); else (void)LZY_DrawRect(this->pos[0], this->pos[1], this->width, this->width); -} IMPL_END +} IMPL_INIT(deathpart) { this->ignore_solids = true; diff --git a/src/entity.c b/src/entity.c index 8fdaa9e..00e6719 100644 --- a/src/entity.c +++ b/src/entity.c @@ -20,7 +20,7 @@ unsigned int entity_type(const char *type) { for (unsigned int i = 0; i < num_entitytags; i++) - if (strcmp(type, entitytags[i]) == 0) + if (strcmp(type, entitytags[i].name) == 0) return i + 1; fprintf(stderr, "unknown type '%s'\n", type); return 0; @@ -100,5 +100,5 @@ entity_move(Entity *this, Game *g) Entity * entity_init(Entity *this, unsigned int type, int x, int y) { - return entityinits[type - 1](this, x, y); + return entitytags[type - 1].init(this, x, y); } diff --git a/src/entity.h b/src/entity.h index 464b81f..0051639 100644 --- a/src/entity.h +++ b/src/entity.h @@ -11,8 +11,8 @@ enum { ET_NONE = 0 }; typedef struct Entity Entity; struct Entity { unsigned long uuid; - Entity *(*update)(Entity *this, struct Game *g); - Entity *(*draw)(Entity *this, struct Game *g); + void (*update)(Entity *this, struct Game *g); + void (*draw)(Entity *this, struct Game *g); unsigned int type; int id; int pos[2]; diff --git a/src/entityimpl.h b/src/entityimpl.h index 971ffc5..3d62172 100644 --- a/src/entityimpl.h +++ b/src/entityimpl.h @@ -5,16 +5,13 @@ #include "entitytag.h" #include -#define IMPL_UPDATE() static Entity * \ -update([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) { - -#define IMPL_DRAW() static Entity * \ -draw([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) { +#define IMPL(X) static void \ +X([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) #define IMPL_INIT(X) static Entity *init(Entity *this, int x, int y); \ __attribute__((constructor)) static void init_tag() { \ - entityinits[num_entitytags] = init; \ - entitytags[num_entitytags++] = #X; \ + entitytags[num_entitytags].init = init; \ + entitytags[num_entitytags++].name = #X; \ } \ static Entity * \ init(Entity *this, int x, int y) { do { \ diff --git a/src/entitytag.c b/src/entitytag.c index ce65a24..de56455 100644 --- a/src/entitytag.c +++ b/src/entitytag.c @@ -1,5 +1,4 @@ #include "entitytag.h" unsigned int num_entitytags = 0; -const char *entitytags[256] = {}; -EntityInit entityinits[256] = {}; +EntityTag entitytags[256] = {}; diff --git a/src/entitytag.h b/src/entitytag.h index 946ed56..dcbad2d 100644 --- a/src/entitytag.h +++ b/src/entitytag.h @@ -1,7 +1,10 @@ #pragma once #include "entity.h" -typedef Entity *(*EntityInit)(Entity *this, int x, int y); +typedef struct { + const char *name; + Entity *(*init)(Entity *this, int x, int y); +} EntityTag; + extern unsigned int num_entitytags; -extern const char *entitytags[256]; -extern EntityInit entityinits[256]; +extern EntityTag entitytags[256]; diff --git a/src/exit.c b/src/exit.c index c6ad4cb..3aaad42 100644 --- a/src/exit.c +++ b/src/exit.c @@ -1,14 +1,14 @@ #include "entityimpl.h" #include "rotrect.h" -IMPL_UPDATE() { -} IMPL_END +IMPL(update) { +} -IMPL_DRAW() { +IMPL(draw) { LZY_DrawSetColor(BLACK); rotrect(this->pos[0], this->pos[1], this->width, this->height, 0.2 * this->exit.dir); -} IMPL_END +} IMPL_INIT(exit) { this->width = 12; diff --git a/src/player.c b/src/player.c index c6a3323..5456d0f 100644 --- a/src/player.c +++ b/src/player.c @@ -5,7 +5,7 @@ #include "rotrect.h" #include -IMPL_UPDATE() { +IMPL(update) { const int on_ground = entity_collide(this, g, 0, 1); this->vel[0] = 2.0 * this->player.dirx; @@ -71,15 +71,15 @@ IMPL_UPDATE() { if (entity_place_meeting(this, g, entity_type("exit")) != NULL) g->queue_next_scene = true; -} IMPL_END +} -IMPL_DRAW() { +IMPL(draw) { LZY_DrawSetColor(BLACK); const double width = this->width * this->player.scale_x + 2; const double height = this->height * this->player.scale_y + 2; rotrect(this->pos[0] - 1, this->pos[1] - 1, width, height, this->player.angle); -} IMPL_END +} IMPL_INIT(player) { this->width = 10; diff --git a/src/spike.c b/src/spike.c index 3492a5f..3ab6e54 100644 --- a/src/spike.c +++ b/src/spike.c @@ -1,13 +1,12 @@ #include "entityimpl.h" #include "rotrect.h" -IMPL_UPDATE() { -} IMPL_END +IMPL(update) {} -IMPL_DRAW() { +IMPL(draw) { rotrect_draw(g->spike_rect, this->pos[0], this->pos[1]); rotrect_draw(g->spike_irect, this->pos[0], this->pos[1]); -} IMPL_END +} IMPL_INIT(spike) { this->height = this->width = 8; -- cgit v1.2.3