added expandable world objects array

pull/12/head
Sara 2023-07-12 02:07:29 +02:00
parent 3ed5755e52
commit e6b66884ec
1 changed files with 37 additions and 14 deletions

View File

@ -1,6 +1,7 @@
#include "world.h" #include "world.h"
#include "memory.h" #include "memory.h"
#include "math/vec.h" #include "math/vec.h"
#include "object.h"
struct { struct {
size_t num; size_t num;
@ -26,12 +27,16 @@ int _expand_world() {
static inline static inline
object_t* _find_free_object() { object_t* _find_free_object() {
for(int i = 0; i < _world_objects.num; ++i) { 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]; return _world_objects.objects[i];
} }
} }
_expand_world(); size_t num = _world_objects.num;
if(_expand_world()) {
return _world_objects.objects[num];
} else {
return NULL; return NULL;
}
} }
void world_clear() { void world_clear() {
@ -55,21 +60,39 @@ object_t* instantiate_object(const object_t *original) {
} }
void world_update() { void world_update() {
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { for(int i = 0; i < world_num_objects(); ++i) {
if(g_objects[i].active == 1 object_t* object = world_get_object(i);
&& g_objects[i].enabled == 1 if(!object_is_valid(object)
&& g_objects[i].evt_update != NULL) { && object->evt_update != NULL) {
g_objects[i].evt_update(g_objects + i); object->evt_update(object);
} }
} }
} }
void world_draw() { void world_draw() {
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { for(int i = 0; i < world_num_objects(); ++i) {
if(g_objects[i].active == 1 object_t* object = world_get_object(i);
&& g_objects[i].enabled == 1 if(!object_is_valid(object)
&& g_objects[i].evt_draw != NULL) { && object->evt_draw != NULL) {
g_objects[i].evt_draw(g_objects + i); object->evt_draw(object);
} }
} }
} }
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();
}
}