From 20d86d093f1783a2a8135c27bcea5e41cce24bb5 Mon Sep 17 00:00:00 2001 From: kdx Date: Sun, 26 Mar 2023 05:45:46 +0000 Subject: entity init by name --- src/deathpart.h | 5 ----- src/entity.c | 6 ++++++ src/entity.h | 1 + src/entityimpl.h | 14 ++++++++++---- src/entitytag.c | 3 ++- src/entitytag.h | 3 +++ src/exit.h | 5 ----- src/game.c | 13 +++++++------ src/input.c | 2 +- src/player.c | 4 ++-- src/player.h | 5 ----- src/spike.h | 6 ------ 12 files changed, 32 insertions(+), 35 deletions(-) delete mode 100644 src/spike.h diff --git a/src/deathpart.h b/src/deathpart.h index e62b8ef..e3316ac 100644 --- a/src/deathpart.h +++ b/src/deathpart.h @@ -3,8 +3,3 @@ typedef struct { int skip; } DeathPart; - -struct Entity; -struct Game; - -struct Entity *deathpart_init(struct Entity *this, int x, int y); diff --git a/src/entity.c b/src/entity.c index 65af918..8fdaa9e 100644 --- a/src/entity.c +++ b/src/entity.c @@ -96,3 +96,9 @@ 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); +} diff --git a/src/entity.h b/src/entity.h index 532bd12..464b81f 100644 --- a/src/entity.h +++ b/src/entity.h @@ -34,3 +34,4 @@ bool entity_collide(Entity *this, struct Game *g, int ox, int oy); bool entity_meet(Entity *this, Entity *other); Entity *entity_place_meeting(Entity *this, struct Game *g, unsigned int type); void entity_move(Entity *this, struct Game *g); +Entity *entity_init(Entity *this, unsigned int type, int x, int y); diff --git a/src/entityimpl.h b/src/entityimpl.h index adc4c09..971ffc5 100644 --- a/src/entityimpl.h +++ b/src/entityimpl.h @@ -6,13 +6,18 @@ #include #define IMPL_UPDATE() static Entity * \ - update([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) { +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_INIT(X) __attribute__((constructor)) static void init_tag() { \ +draw([[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; \ } \ -Entity *X##_init(Entity *this, int x, int y) { do { \ +static Entity * \ +init(Entity *this, int x, int y) { do { \ memset(this, 0, sizeof(*this)); \ this->update = update; \ this->draw = draw; \ @@ -20,4 +25,5 @@ Entity *X##_init(Entity *this, int x, int y) { do { \ this->pos[1] = y; \ this->type = entity_type(#X); \ } while(0); + #define IMPL_END return this; } diff --git a/src/entitytag.c b/src/entitytag.c index 0c0e952..ce65a24 100644 --- a/src/entitytag.c +++ b/src/entitytag.c @@ -1,4 +1,5 @@ #include "entitytag.h" unsigned int num_entitytags = 0; -const char *entitytags[256] = {0}; +const char *entitytags[256] = {}; +EntityInit entityinits[256] = {}; diff --git a/src/entitytag.h b/src/entitytag.h index 2b423d2..946ed56 100644 --- a/src/entitytag.h +++ b/src/entitytag.h @@ -1,4 +1,7 @@ #pragma once +#include "entity.h" +typedef Entity *(*EntityInit)(Entity *this, int x, int y); extern unsigned int num_entitytags; extern const char *entitytags[256]; +extern EntityInit entityinits[256]; diff --git a/src/exit.h b/src/exit.h index 5ea3418..c35a83c 100644 --- a/src/exit.h +++ b/src/exit.h @@ -3,8 +3,3 @@ typedef struct { int dir; } Exit; - -struct Entity; -struct Game; - -struct Entity *exit_init(struct Entity *this, int x, int y); diff --git a/src/game.c b/src/game.c index d35d7a9..5467798 100644 --- a/src/game.c +++ b/src/game.c @@ -1,10 +1,8 @@ #include "game.h" -#include "exit.h" +#include "entity.h" #include "map.h" -#include "player.h" #include "cfg.h" #include "rotrect.h" -#include "spike.h" #include void @@ -71,16 +69,19 @@ game_restart_scene(Game *this) Entity *e; switch (map_get(x, y)) { case 2: - e = player_init(game_create_entity(this), dx, dy); + e = entity_init(game_create_entity(this), + entity_type("player"), dx, dy); e->player.dirx = this->player_dir; break; case 3: case 4: - e = exit_init(game_create_entity(this), dx, dy); + e = entity_init(game_create_entity(this), + entity_type("exit"), dx, dy); e->exit.dir = (map_get(x, y) == 3) ? -1 : 1; break; case 5: - spike_init(game_create_entity(this), dx, dy); + e = entity_init(game_create_entity(this), + entity_type("spike"), dx, dy); break; default: break; diff --git a/src/input.c b/src/input.c index 7235675..032bf92 100644 --- a/src/input.c +++ b/src/input.c @@ -3,7 +3,7 @@ static const unsigned int keys[6] = {LZYK_LEFT, LZYK_RIGHT, LZYK_UP, LZYK_DOWN, LZYK_O, LZYK_X}; -static int states[6] = {0}; +static int states[6] = {}; void input_update(void) { diff --git a/src/player.c b/src/player.c index 4b1d65f..d355e48 100644 --- a/src/player.c +++ b/src/player.c @@ -1,4 +1,3 @@ -#include "deathpart.h" #include "entity.h" #include "entityimpl.h" #include "game.h" @@ -59,7 +58,8 @@ IMPL_UPDATE() { for (int y = 0; y < 7; y++) { int dx = this->pos[0] - 6; for (int x = 0; x < 7; x++) { - deathpart_init(game_create_entity(g), dx, dy); + entity_init(game_create_entity(g), + entity_type("deathpart"), dx, dy); dx += 2; } dy += 2; diff --git a/src/player.h b/src/player.h index 381fe46..a405e97 100644 --- a/src/player.h +++ b/src/player.h @@ -10,8 +10,3 @@ typedef struct { double angle; int dirx; } Player; - -struct Entity; -struct Game; - -struct Entity *player_init(struct Entity *this, int x, int y); diff --git a/src/spike.h b/src/spike.h deleted file mode 100644 index 514af83..0000000 --- a/src/spike.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -struct Entity; -struct Game; - -struct Entity *spike_init(struct Entity *this, int x, int y); -- cgit v1.2.3