summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kdx.42@42l.fr>2023-03-26 07:26:47 +0000
committerkdx <kdx.42@42l.fr>2023-03-26 07:26:47 +0000
commit09438266c3987c89a1e7f0abf9042dd3528ac343 (patch)
tree5bc3888bfcdd122f8734cbb0414a71597fb2c254
parent83f5fc33df8ea2b8df661f921f157080d472a0ae (diff)
downloadhyperultra-09438266c3987c89a1e7f0abf9042dd3528ac343.tar.gz
overengineer entityimpl a bit more
-rw-r--r--src/deathpart.c20
-rw-r--r--src/entity.c3
-rw-r--r--src/entityimpl.h24
-rw-r--r--src/entitytag.h2
-rw-r--r--src/exit.c11
-rw-r--r--src/player.c32
-rw-r--r--src/spike.c8
7 files changed, 50 insertions, 50 deletions
diff --git a/src/deathpart.c b/src/deathpart.c
index bdc3cdf..3a94ac8 100644
--- a/src/deathpart.c
+++ b/src/deathpart.c
@@ -3,6 +3,16 @@
#include "entityimpl.h"
#include <stdlib.h>
+IMPL_INIT(deathpart) {
+ this->ignore_solids = true;
+ this->vel[0] = (float)(rand() % 1024) / 1024;
+ this->vel[1] = -(float)(rand() % 1024) / 512;
+ this->vel[0] *= this->vel[0];
+ if (rand() % 2)
+ this->vel[0] *= -1;
+ this->width = 1 + rand() % 2;
+}
+
IMPL(update) {
if (this->deathpart.skip == 0) {
this->vel[1] *= AIR_RESISTANCE;
@@ -21,13 +31,3 @@ IMPL(draw) {
(void)LZY_DrawRect(this->pos[0], this->pos[1],
this->width, this->width);
}
-
-IMPL_INIT(deathpart) {
- this->ignore_solids = true;
- this->vel[0] = (float)(rand() % 1024) / 1024;
- this->vel[1] = -(float)(rand() % 1024) / 512;
- this->vel[0] *= this->vel[0];
- if (rand() % 2)
- this->vel[0] *= -1;
- this->width = 1 + rand() % 2;
-} IMPL_END
diff --git a/src/entity.c b/src/entity.c
index 00e6719..044bb7b 100644
--- a/src/entity.c
+++ b/src/entity.c
@@ -100,5 +100,6 @@ entity_move(Entity *this, Game *g)
Entity *
entity_init(Entity *this, unsigned int type, int x, int y)
{
- return entitytags[type - 1].init(this, x, y);
+ entitytags[type - 1].init(this, x, y);
+ return this;
}
diff --git a/src/entityimpl.h b/src/entityimpl.h
index 3d62172..1fd65ba 100644
--- a/src/entityimpl.h
+++ b/src/entityimpl.h
@@ -5,22 +5,26 @@
#include "entitytag.h"
#include <string.h>
-#define IMPL(X) static void \
-X([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g)
+[[maybe_unused]] static void *_draw;
+[[maybe_unused]] static void *_update;
-#define IMPL_INIT(X) static Entity *init(Entity *this, int x, int y); \
+#define IMPL(X) static void X(Entity *this, Game *g); \
+__attribute__((constructor)) static void init_##X() { _##X = X; } \
+static void X([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g)
+
+#define IMPL_INIT(X) static void init(Entity *this, int x, int y); \
__attribute__((constructor)) static void init_tag() { \
entitytags[num_entitytags].init = init; \
entitytags[num_entitytags++].name = #X; \
} \
-static Entity * \
-init(Entity *this, int x, int y) { do { \
+static void _init(Entity *this); \
+static void init(Entity *this, int x, int y) { \
memset(this, 0, sizeof(*this)); \
- this->update = update; \
- this->draw = draw; \
+ this->update = _update; \
+ this->draw = _draw; \
this->pos[0] = x; \
this->pos[1] = y; \
this->type = entity_type(#X); \
-} while(0);
-
-#define IMPL_END return this; }
+ _init(this); \
+} \
+static void _init([[maybe_unused]] Entity *this)
diff --git a/src/entitytag.h b/src/entitytag.h
index dcbad2d..154023c 100644
--- a/src/entitytag.h
+++ b/src/entitytag.h
@@ -3,7 +3,7 @@
typedef struct {
const char *name;
- Entity *(*init)(Entity *this, int x, int y);
+ void (*init)(Entity *this, int x, int y);
} EntityTag;
extern unsigned int num_entitytags;
diff --git a/src/exit.c b/src/exit.c
index 3aaad42..64ed0a9 100644
--- a/src/exit.c
+++ b/src/exit.c
@@ -1,7 +1,10 @@
#include "entityimpl.h"
#include "rotrect.h"
-IMPL(update) {
+IMPL_INIT(exit) {
+ this->width = 12;
+ this->height = 12;
+ this->exit.dir = 1;
}
IMPL(draw) {
@@ -9,9 +12,3 @@ IMPL(draw) {
rotrect(this->pos[0], this->pos[1],
this->width, this->height, 0.2 * this->exit.dir);
}
-
-IMPL_INIT(exit) {
- this->width = 12;
- this->height = 12;
- this->exit.dir = 1;
-} IMPL_END
diff --git a/src/player.c b/src/player.c
index 5456d0f..9434b3c 100644
--- a/src/player.c
+++ b/src/player.c
@@ -5,6 +5,22 @@
#include "rotrect.h"
#include <math.h>
+IMPL_INIT(player) {
+ this->width = 10;
+ this->height = 10;
+ this->player.scale_x = 1.0;
+ this->player.scale_y = 1.0;
+ this->player.dirx = 1;
+}
+
+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(update) {
const int on_ground = entity_collide(this, g, 0, 1);
@@ -72,19 +88,3 @@ IMPL(update) {
if (entity_place_meeting(this, g, entity_type("exit")) != NULL)
g->queue_next_scene = true;
}
-
-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_INIT(player) {
- this->width = 10;
- this->height = 10;
- this->player.scale_x = 1.0;
- this->player.scale_y = 1.0;
- this->player.dirx = 1;
-} IMPL_END
diff --git a/src/spike.c b/src/spike.c
index 3ab6e54..2f178e9 100644
--- a/src/spike.c
+++ b/src/spike.c
@@ -1,13 +1,11 @@
#include "entityimpl.h"
#include "rotrect.h"
-IMPL(update) {}
+IMPL_INIT(spike) {
+ this->height = this->width = 8;
+}
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_INIT(spike) {
- this->height = this->width = 8;
-} IMPL_END