feat(render): added render depth to behaviour entities
This commit is contained in:
parent
7851fc5a24
commit
ac8cacd2fc
|
@ -9,6 +9,7 @@ typedef struct {
|
||||||
void (*const update)(void* self, float dt);
|
void (*const update)(void* self, float dt);
|
||||||
void (*const start)(void* self);
|
void (*const start)(void* self);
|
||||||
void (*const draw)(void* self);
|
void (*const draw)(void* self);
|
||||||
|
long (*const get_depth)(void* self);
|
||||||
} IEntityBehaviour;
|
} IEntityBehaviour;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -17,15 +18,17 @@ typedef struct {
|
||||||
IDrop const* drop;
|
IDrop const* drop;
|
||||||
} BehaviourEntity;
|
} BehaviourEntity;
|
||||||
|
|
||||||
#define impl_BehaviourEntity_for(T, start_f, update_f, draw_f)\
|
#define impl_BehaviourEntity_for(T, start_f, update_f, draw_f, get_depth_f)\
|
||||||
static inline BehaviourEntity T##_as_BehaviourEntity(T* x) {\
|
static inline BehaviourEntity T##_as_BehaviourEntity(T* x) {\
|
||||||
TC_FN_TYPECHECK(void, start_f, T*);\
|
TC_FN_TYPECHECK(void, start_f, T*);\
|
||||||
TC_FN_TYPECHECK(void, update_f, T*, float);\
|
TC_FN_TYPECHECK(void, update_f, T*, float);\
|
||||||
TC_FN_TYPECHECK(void, draw_f, T*);\
|
TC_FN_TYPECHECK(void, draw_f, T*);\
|
||||||
|
TC_FN_TYPECHECK(long, get_depth_f, T*);\
|
||||||
static IEntityBehaviour const tc = {\
|
static IEntityBehaviour const tc = {\
|
||||||
.update = (void(*const)(void*, float)) update_f,\
|
.update = (void(*const)(void*, float)) update_f,\
|
||||||
.start = (void(*const)(void*)) start_f,\
|
.start = (void(*const)(void*)) start_f,\
|
||||||
.draw = (void(*const)(void*)) draw_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(Drop, T##_as_Drop, T*);\
|
||||||
IDrop const* drop = T##_as_Drop(x).tc;\
|
IDrop const* drop = T##_as_Drop(x).tc;\
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include "behaviour_entity.h"
|
#include "behaviour_entity.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "program.h"
|
#include "program.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
static List _add_queue;
|
static List _add_queue;
|
||||||
static List _remove_queue;
|
static List _remove_queue;
|
||||||
|
@ -73,8 +74,29 @@ void game_world_update() {
|
||||||
_internal_clear_removed();
|
_internal_clear_removed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
int _internal_compare_depth(const BehaviourEntity* a, const BehaviourEntity* b) {
|
||||||
|
return b->tc->get_depth(b->data) - a->tc->get_depth(a->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define AS_COMPARISON(__FN) ((int(*)(const void*, const void*))__FN)
|
||||||
|
|
||||||
void game_world_draw() {
|
void game_world_draw() {
|
||||||
list_foreach(BehaviourEntity*, entity, &_game_entities) {
|
List draw_order = list_copy(&_game_entities);
|
||||||
|
|
||||||
|
LOG_INFO("before");
|
||||||
|
list_foreach(BehaviourEntity*, entity, &draw_order) {
|
||||||
|
LOG_INFO("Entity: %p depth %d", entity->data, entity->tc->get_depth(entity->data));
|
||||||
|
}
|
||||||
|
|
||||||
|
qsort(_game_entities.data, _game_entities.len, _game_entities.element_size,
|
||||||
|
AS_COMPARISON(_internal_compare_depth));
|
||||||
|
|
||||||
|
LOG_INFO("after");
|
||||||
|
list_foreach(BehaviourEntity*, entity, &draw_order) {
|
||||||
|
LOG_INFO("Entity: %p depth %d", entity->data, entity->tc->get_depth(entity->data));
|
||||||
entity->tc->draw(entity->data);
|
entity->tc->draw(entity->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
list_empty(&draw_order);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue