From ac8cacd2fce568025817034a760f50df05596ff7 Mon Sep 17 00:00:00 2001 From: Sara Date: Sat, 25 Nov 2023 11:33:11 +0100 Subject: [PATCH] feat(render): added render depth to behaviour entities --- core/src/behaviour_entity.h | 5 ++++- core/src/game_world.c | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/core/src/behaviour_entity.h b/core/src/behaviour_entity.h index e9d5938..c5b8391 100644 --- a/core/src/behaviour_entity.h +++ b/core/src/behaviour_entity.h @@ -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;\ diff --git a/core/src/game_world.c b/core/src/game_world.c index b93a029..8c83b08 100644 --- a/core/src/game_world.c +++ b/core/src/game_world.c @@ -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); }