feat(render): added render depth to behaviour entities

This commit is contained in:
Sara 2023-11-25 11:33:11 +01:00
parent 7851fc5a24
commit ac8cacd2fc
2 changed files with 27 additions and 2 deletions

View file

@ -9,6 +9,7 @@ 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 {
@ -17,15 +18,17 @@ typedef struct {
IDrop const* drop;
} 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) {\
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*);\
IDrop const* drop = T##_as_Drop(x).tc;\

View file

@ -3,6 +3,7 @@
#include "behaviour_entity.h"
#include "list.h"
#include "program.h"
#include "debug.h"
static List _add_queue;
static List _remove_queue;
@ -73,8 +74,29 @@ void game_world_update() {
_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() {
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);
}
list_empty(&draw_order);
}