diff --git a/src/corelib/corelib.h b/src/corelib/corelib.h new file mode 100644 index 0000000..fe92539 --- /dev/null +++ b/src/corelib/corelib.h @@ -0,0 +1,6 @@ +#include "object.h" +#include "render.h" +#include "assets.h" +#include "physics.h" +#include "world.h" +#include "entry.h" diff --git a/src/engine.c b/src/corelib/entry.c similarity index 95% rename from src/engine.c rename to src/corelib/entry.c index a1c857f..4489250 100644 --- a/src/engine.c +++ b/src/corelib/entry.c @@ -1,8 +1,9 @@ -#include "engine.h" +#include "entry.h" #include "world.h" -#include "corelib/assets.h" -#include "corelib/render.h" -#include "corelib/input.h" +#include "assets.h" +#include "render.h" +#include "input.h" + #include "time.h" static double _delta_time = 0; diff --git a/src/engine.h b/src/corelib/entry.h similarity index 82% rename from src/engine.h rename to src/corelib/entry.h index 8667836..db8843a 100644 --- a/src/engine.h +++ b/src/corelib/entry.h @@ -1,10 +1,6 @@ #ifndef _engine_h #define _engine_h -#ifdef __cplusplus -extern "C" { -#endif - extern float delta_time(); extern void set_frame_interval(double frame_interval); extern void set_frame_rate_limit(int fps); @@ -16,8 +12,4 @@ extern void update_game(); extern void update_ui(); extern void game_exit(); -#ifdef __cplusplus -} -#endif - #endif /* _engine_h */ diff --git a/src/corelib/object.h b/src/corelib/object.h index 9d26ab9..ea38c6e 100644 --- a/src/corelib/object.h +++ b/src/corelib/object.h @@ -24,5 +24,9 @@ struct object_t { object_t object_default(); void object_draw_sprite(object_t* object); +static inline +int object_is_valid(const object_t* object) { + return object != NULL && object->active != 0; +} -#endif /* _object_h */ \ No newline at end of file +#endif /* _object_h */ diff --git a/src/corelib/physics.c b/src/corelib/physics.c index 001348c..6ac2844 100644 --- a/src/corelib/physics.c +++ b/src/corelib/physics.c @@ -24,7 +24,7 @@ void object_broadcast_collision(object_t* this, object_t* other) { } } short can_collide(const object_t* this) { - return this->active && this->enabled; + return object_is_valid(this) && this->enabled; } static inline @@ -191,9 +191,9 @@ void solve_collision_slide(object_t* left, object_t* right) { static inline void _solve_move(object_t* this) { // loop over all objects and check collision if applicable - for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { + for(int i = 0; i < world_num_objects(); ++i) { // get pointer to other object - object_t* other = g_objects + i; + object_t* other = world_get_object(i); // check collision, return if found if(can_collide(other) && this != other && _collision_check(other, this)) { object_broadcast_collision(other, this); diff --git a/src/corelib/world.c b/src/corelib/world.c index 341607e..7eb4ddb 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -1,26 +1,64 @@ #include "world.h" +#include "memory.h" +#include "assert.h" #include "math/vec.h" +#include "object.h" -object_t g_objects[WORLD_NUM_OBJECTS]; +struct { + size_t num; + object_t** objects; +} _world_objects = { + .num = 0, + .objects = NULL +}; -void world_clear() { - for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { - g_objects[i].active = 0; - g_objects[i].enabled = 0; +static inline +int _expand_world() { + size_t new_num = _world_objects.num * 2; + if(new_num == 0) { + new_num = 16; } + object_t** new_list = realloc(_world_objects.objects, new_num * sizeof(object_t*)); + + if(new_list == NULL) { + assert(!"ERROR: Out of memory"); + exit(-10); + } + + for(size_t i = _world_objects.num; i < new_num; ++i) { + new_list[i] = NULL; + } + + _world_objects.objects = new_list; + _world_objects.num = new_num; + return 1; } -object_t* _find_free_object() { - for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { - if(g_objects[i].active == 0) { - return g_objects + i; +static inline +size_t _find_free_object() { + for(int i = 0; i < _world_objects.num; ++i) { + if(!object_is_valid(_world_objects.objects[i])) { + return i; } } - return NULL; + size_t num = _world_objects.num; + _expand_world(); + return num; +} + +void world_clear() { + for(int i = 0; i < _world_objects.num; ++i) { + _world_objects.objects[i]->active = 0; + _world_objects.objects[i]->enabled = 0; + } } object_t* make_object() { - object_t* o = _find_free_object(); + size_t index = _find_free_object(); + if(_world_objects.objects[index] == NULL) { + _world_objects.objects[index] = malloc(sizeof(object_t)); + } + object_t* o = _world_objects.objects[index]; *o = object_default(); return o; } @@ -33,21 +71,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_objects.num; ++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_objects.num; ++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(); + } +} diff --git a/src/corelib/world.h b/src/corelib/world.h index c86124a..a98865f 100644 --- a/src/corelib/world.h +++ b/src/corelib/world.h @@ -3,18 +3,21 @@ #include "object.h" -#define WORLD_NUM_OBJECTS 255 +#define BASE_WORLD_SIZE 24 typedef struct object_t object_t; -extern object_t g_objects[WORLD_NUM_OBJECTS]; +extern object_t* make_object(); +extern object_t* instantiate_object(const object_t* original); -object_t* make_object(); -object_t* instantiate_object(const object_t* original); +extern void world_clear(); -void world_clear(); +extern void world_update(); +extern void world_draw(); -void world_update(); -void world_draw(); +extern object_t* world_get_object(size_t at); + +extern size_t world_num_objects(); +extern void world_reserve_objects(size_t min); #endif /* _world_h */ diff --git a/src/game.c b/src/game.c index 81b040f..37f1f8e 100644 --- a/src/game.c +++ b/src/game.c @@ -1,5 +1,5 @@ -#include "engine.h" -#include "world.h" +#include "corelib/entry.h" +#include "corelib/world.h" #include "corelib/input.h" #include "corelib/render.h" #include "corelib/assets.h"