From 3fb3d12d6a984c660c3fd18bc74216fceab8467f Mon Sep 17 00:00:00 2001 From: kdx Date: Fri, 31 Mar 2023 02:32:32 +0200 Subject: draw begin+end & update begin+end --- src/entity.h | 4 ++++ src/entityimpl.h | 8 ++++++++ src/game.c | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/entity.h b/src/entity.h index 6b8d2e7..bf246d0 100644 --- a/src/entity.h +++ b/src/entity.h @@ -6,8 +6,12 @@ struct Game; typedef struct Entity Entity; struct Entity { unsigned long uuid; + void (*update_begin)(Entity *this, struct Game *g); void (*update)(Entity *this, struct Game *g); + void (*update_end)(Entity *this, struct Game *g); + void (*draw_begin)(Entity *this, struct Game *g); void (*draw)(Entity *this, struct Game *g); + void (*draw_end)(Entity *this, struct Game *g); unsigned int type; const char *name; int pos[2]; diff --git a/src/entityimpl.h b/src/entityimpl.h index 397e58b..87eeb4c 100644 --- a/src/entityimpl.h +++ b/src/entityimpl.h @@ -5,8 +5,12 @@ #include "entitytag.h" #include +[[maybe_unused]] static void *_draw_begin; [[maybe_unused]] static void *_draw; +[[maybe_unused]] static void *_draw_end; +[[maybe_unused]] static void *_update_begin; [[maybe_unused]] static void *_update; +[[maybe_unused]] static void *_update_end; #define IMPL(X) static void X(Entity *this, Game *g); \ __attribute__((constructor)) static void init_##X() { _##X = X; } \ @@ -22,8 +26,12 @@ __attribute__((constructor)) static void init_tag(void) { \ static void _init(Entity *this); \ static void init(Entity *this, const char *name, int x, int y, int w, int h) { \ memset(this, 0, sizeof(*this)); \ + this->update_begin = _update_begin; \ this->update = _update; \ + this->update_end = _update_end; \ + this->draw_begin = _draw_begin; \ this->draw = _draw; \ + this->draw_end = _draw_end; \ this->name = name; \ this->pos[0] = x; \ this->pos[1] = y; \ diff --git a/src/game.c b/src/game.c index ea98bec..13b4432 100644 --- a/src/game.c +++ b/src/game.c @@ -37,22 +37,42 @@ game_update(Game *this) if (--this->queue_restart_scene == 0) game_restart_scene(this); } + for (__auto_type i = 0; i < MAX_ENTITIES; i++) { + const __auto_type e = &this->entities[i]; + if (e->type != 0 && e->update_begin != NULL) + e->update_begin(e, this); + } for (__auto_type i = 0; i < MAX_ENTITIES; i++) { const __auto_type e = &this->entities[i]; if (e->type != 0 && e->update != NULL) e->update(e, this); } + for (__auto_type i = 0; i < MAX_ENTITIES; i++) { + const __auto_type e = &this->entities[i]; + if (e->type != 0 && e->update_end != NULL) + e->update_end(e, this); + } } void game_draw(Game *this) { map_draw(); + for (int i = 0; i < MAX_ENTITIES; i++) { + const __auto_type e = &this->entities[i]; + if (e->type != 0 && e->draw_begin != NULL) + e->draw_begin(e, this); + } for (int i = 0; i < MAX_ENTITIES; i++) { const __auto_type e = &this->entities[i]; if (e->type != 0 && e->draw != NULL) e->draw(e, this); } + for (int i = 0; i < MAX_ENTITIES; i++) { + const __auto_type e = &this->entities[i]; + if (e->type != 0 && e->draw_end != NULL) + e->draw_end(e, this); + } } void -- cgit v1.2.3