fencer/core/src/behaviour_entity.h
Sara 0c6f1dd8cf feat: reworked typeclasses to forward-declare then define
forward declarations are simplified with decl_typeclass_impl
impl_Typeclass_for now instead only define
static inline impl_Typeclass_for can be used to achieve the old behaviour
2024-01-12 09:02:42 +01:00

42 lines
1.3 KiB
C

#ifndef _update_entity_h
#define _update_entity_h
#include "drop.h"
#include "mirror.h"
#include "typeclass_helpers.h"
typedef struct {
void (*const update)(void* self, float dt);
void (*const start)(void* self);
void (*const draw)(void* self);
long (*const get_depth)(void* self);
} IEntityBehaviour;
typedef struct {
void* data;
IEntityBehaviour const* tc;
IDrop const* drop;
IMirror const* mirror;
} BehaviourEntity;
#define impl_BehaviourEntity_for(T, start_f, update_f, draw_f, get_depth_f)\
BehaviourEntity T##_as_BehaviourEntity(T* x) {\
TC_FN_TYPECHECK(void, start_f, T*);\
TC_FN_TYPECHECK(void, update_f, T*, float);\
TC_FN_TYPECHECK(void, draw_f, T*);\
TC_FN_TYPECHECK(long, get_depth_f, T*);\
static IEntityBehaviour const tc = {\
.update = (void(*const)(void*, float)) update_f,\
.start = (void(*const)(void*)) start_f,\
.draw = (void(*const)(void*)) draw_f,\
.get_depth=(long(*const)(void*)) get_depth_f,\
};\
TC_FN_TYPECHECK(Drop, T##_as_Drop, T*);\
TC_FN_TYPECHECK(Mirror, T##_as_Mirror, T*);\
IDrop const* drop = T##_as_Drop(x).tc;\
IMirror const* mirror = T##_as_Mirror(x).tc;\
return (BehaviourEntity){.data = x, .tc = &tc, .drop = drop, .mirror = mirror};\
}\
#endif // !_update_entity_h