From e6b66884ec068beb6c543f7c3bb9abaf7f623913 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:07:29 +0200 Subject: [PATCH] added expandable world objects array --- src/corelib/world.c | 51 ++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/corelib/world.c b/src/corelib/world.c index eac5005..af89ea5 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -1,6 +1,7 @@ #include "world.h" #include "memory.h" #include "math/vec.h" +#include "object.h" struct { size_t num; @@ -26,12 +27,16 @@ int _expand_world() { static inline object_t* _find_free_object() { for(int i = 0; i < _world_objects.num; ++i) { - if(_world_objects.objects[i]->active == 0) { + if(object_is_valid(_world_objects.objects[i])) { return _world_objects.objects[i]; } } - _expand_world(); - return NULL; + size_t num = _world_objects.num; + if(_expand_world()) { + return _world_objects.objects[num]; + } else { + return NULL; + } } void world_clear() { @@ -55,21 +60,39 @@ object_t* instantiate_object(const object_t *original) { } void world_update() { - for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { - if(g_objects[i].active == 1 - && g_objects[i].enabled == 1 - && g_objects[i].evt_update != NULL) { - g_objects[i].evt_update(g_objects + i); + for(int i = 0; i < world_num_objects(); ++i) { + object_t* object = world_get_object(i); + if(!object_is_valid(object) + && object->evt_update != NULL) { + object->evt_update(object); } } } void world_draw() { - for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { - if(g_objects[i].active == 1 - && g_objects[i].enabled == 1 - && g_objects[i].evt_draw != NULL) { - g_objects[i].evt_draw(g_objects + i); + for(int i = 0; i < world_num_objects(); ++i) { + object_t* object = world_get_object(i); + if(!object_is_valid(object) + && object->evt_draw != NULL) { + object->evt_draw(object); } } -} \ No newline at end of file +} + +object_t* world_get_object(size_t at) { + if(at < _world_objects.num) { + return _world_objects.objects[at]; + } else { + return NULL; + } +} + +size_t world_num_objects() { + return _world_objects.num; +} + +void world_reserve_objects(size_t min) { + while(_world_objects.num < min) { + _expand_world(); + } +}