diff options
-rw-r--r-- | eng/inc/_.h | 3 | ||||
-rw-r--r-- | eng/inc/entityeng.h | 4 | ||||
-rw-r--r-- | eng/inc/entityimpl.h | 48 | ||||
-rw-r--r-- | eng/inc/gameng.h | 2 | ||||
-rw-r--r-- | eng/inc/vtable.h | 20 | ||||
-rw-r--r-- | eng/src/vtable.c | 18 | ||||
-rw-r--r-- | inc/entity.h | 11 | ||||
-rw-r--r-- | inc/game.h | 4 | ||||
-rw-r--r-- | src/player.c | 1 |
9 files changed, 109 insertions, 2 deletions
diff --git a/eng/inc/_.h b/eng/inc/_.h index 9e14d77..adc619d 100644 --- a/eng/inc/_.h +++ b/eng/inc/_.h @@ -3,6 +3,9 @@ #include "game.h" #include "cfg.h" #include "TZR.h" +#include "entityimpl.h" +#include "vec2.h" +#include "entityeng.h" #include <time.h> #include <errno.h> diff --git a/eng/inc/entityeng.h b/eng/inc/entityeng.h new file mode 100644 index 0000000..994e184 --- /dev/null +++ b/eng/inc/entityeng.h @@ -0,0 +1,4 @@ +#pragma once +#include "entity.h" + +typedef struct Entity Entity; diff --git a/eng/inc/entityimpl.h b/eng/inc/entityimpl.h new file mode 100644 index 0000000..06df273 --- /dev/null +++ b/eng/inc/entityimpl.h @@ -0,0 +1,48 @@ +#pragma once +#include "vtable.h" + +#define NAME(X) \ +static Vtable _vtable = {0}; \ +__attribute__((constructor(101))) \ +static void \ +_register_vtable(void) \ +{ \ + _vtable.id = ++___vtable_i; \ + if (___vtables == NULL) \ + ___vtables = &_vtable; \ + else { \ + Vtable *tail = ___vtables; \ + while (tail->next != NULL) \ + tail = tail->next; \ + tail->next = &_vtable; \ + } \ +} \ +SET(name, #X) + +#define SET(x, X) \ +__attribute__((constructor(102))) \ +static void \ +_set_##x(void) \ +{ \ + _vtable.x = X; \ +} + +#define IMPL(X) \ +static void _##X(Entity *this); \ +SET(X, _##X); \ +static void \ +_##X(Entity *this) + +#define PARENT(X) SET(parent, #X) +#define INIT() IMPL(init) +#define DEINIT() IMPL(deinit) +#define DRAW() IMPL(draw) +#define UPDATE() IMPL(update) +#define SUPER(X) vtable_get(_vtable.parent)->X(this) +#define T this +#define G this->g +#define POS this->pos +#define DIM this->dim +#define VEL this->vel +#define REM this->rem +#define SQRT2 1.414214 diff --git a/eng/inc/gameng.h b/eng/inc/gameng.h index 9fc48a3..e808186 100644 --- a/eng/inc/gameng.h +++ b/eng/inc/gameng.h @@ -1,6 +1,8 @@ #pragma once #include "game.h" +typedef struct Game Game; + /* calls fatal on failure, consider it never returns NULL */ Game *game_create(void); diff --git a/eng/inc/vtable.h b/eng/inc/vtable.h new file mode 100644 index 0000000..8811bce --- /dev/null +++ b/eng/inc/vtable.h @@ -0,0 +1,20 @@ +#pragma once + +struct Entity; + +typedef struct Vtable Vtable; +struct Vtable { + const char *name; + const char *parent; + unsigned long id; + void (*init)(struct Entity *this); + void (*deinit)(struct Entity *this); + void (*update)(struct Entity *this); + void (*draw)(struct Entity *this); + Vtable *next; +}; + +extern Vtable *___vtables; +extern unsigned long ___vtable_i; + +Vtable *vtable_get(const char *name); diff --git a/eng/src/vtable.c b/eng/src/vtable.c new file mode 100644 index 0000000..daad451 --- /dev/null +++ b/eng/src/vtable.c @@ -0,0 +1,18 @@ +#include "vtable.h" + +Vtable *___vtables = NULL; +unsigned long ___vtable_i = 0; + +Vtable * +vtable_get(const char *name) +{ + if (name == NULL) { + plog("name is NULL"); + return NULL; + } + + foreach (e, ___vtables) + if (strcasecmp(e->name, name) == 0) + return e; + return NULL; +} diff --git a/inc/entity.h b/inc/entity.h new file mode 100644 index 0000000..fb70067 --- /dev/null +++ b/inc/entity.h @@ -0,0 +1,11 @@ +#pragma once + +struct Entity { + bool active; + bool persist; + Vtable *table; + struct Game *g; + unsigned long uuid; + char name[256]; + unsigned int tile; +}; @@ -1,5 +1,5 @@ #pragma once -typedef struct Game { +struct Game { int x, y; -} Game; +}; diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..c667c13 --- /dev/null +++ b/src/player.c @@ -0,0 +1 @@ +NAME(player); |