From f115334644b05d6251906e72245de29dab834ab7 Mon Sep 17 00:00:00 2001 From: Sara Date: Mon, 26 Jun 2023 17:42:02 +0200 Subject: [PATCH 01/21] moved engine module to corelib --- src/{ => corelib}/engine.c | 0 src/{ => corelib}/engine.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/{ => corelib}/engine.c (100%) rename src/{ => corelib}/engine.h (100%) diff --git a/src/engine.c b/src/corelib/engine.c similarity index 100% rename from src/engine.c rename to src/corelib/engine.c diff --git a/src/engine.h b/src/corelib/engine.h similarity index 100% rename from src/engine.h rename to src/corelib/engine.h -- 2.34.1 From 528e35537f20e33458f600b869f697dd275b82e0 Mon Sep 17 00:00:00 2001 From: Sara Date: Mon, 26 Jun 2023 17:43:15 +0200 Subject: [PATCH 02/21] renamed engine module to entry module --- src/corelib/{engine.c => entry.c} | 0 src/corelib/{engine.h => entry.h} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/corelib/{engine.c => entry.c} (100%) rename src/corelib/{engine.h => entry.h} (100%) diff --git a/src/corelib/engine.c b/src/corelib/entry.c similarity index 100% rename from src/corelib/engine.c rename to src/corelib/entry.c diff --git a/src/corelib/engine.h b/src/corelib/entry.h similarity index 100% rename from src/corelib/engine.h rename to src/corelib/entry.h -- 2.34.1 From f395f27532f6bacbbba23fcc7273a7d84f56606c Mon Sep 17 00:00:00 2001 From: Sara Date: Tue, 27 Jun 2023 08:16:31 +0200 Subject: [PATCH 03/21] moved entry to corelib --- src/corelib/entry.c | 9 +++++---- src/corelib/entry.h | 8 -------- src/game.c | 4 ++-- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/corelib/entry.c b/src/corelib/entry.c index a1c857f..4489250 100644 --- a/src/corelib/entry.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/corelib/entry.h b/src/corelib/entry.h index 8667836..db8843a 100644 --- a/src/corelib/entry.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/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" -- 2.34.1 From de7d14ae925ea4e1223ace1a747b0eabaf6f6e09 Mon Sep 17 00:00:00 2001 From: Sara Date: Tue, 27 Jun 2023 08:16:45 +0200 Subject: [PATCH 04/21] added corelib.h include for minimal setup --- src/corelib/corelib.h | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/corelib/corelib.h 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" -- 2.34.1 From 1eb8ab2fd9973db749252780eb7e6969e87f0515 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 28 Jun 2023 08:54:22 +0200 Subject: [PATCH 05/21] WIP: world array is now an array of pointers to objects --- src/corelib/world.c | 38 ++++++++++++++++++++++++++++++-------- src/corelib/world.h | 17 ++++++++++------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/corelib/world.c b/src/corelib/world.c index 341607e..eac5005 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -1,24 +1,46 @@ #include "world.h" +#include "memory.h" #include "math/vec.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; + object_t** new_list = realloc(_world_objects.objects, new_num * sizeof(object_t*)); + if(new_list == NULL) { + return 0; + } else { + _world_objects.objects = new_list; + _world_objects.num = new_num; + return 1; } } +static inline object_t* _find_free_object() { - for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { - if(g_objects[i].active == 0) { - return g_objects + i; + for(int i = 0; i < _world_objects.num; ++i) { + if(_world_objects.objects[i]->active == 0) { + return _world_objects.objects[i]; } } + _expand_world(); return NULL; } +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(); *o = object_default(); 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 */ -- 2.34.1 From a5c6a7ecebee65371a123cc15cb8a0bec2a85476 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:06:40 +0200 Subject: [PATCH 06/21] added objecadded object_is_valid --- src/corelib/object.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/corelib/object.h b/src/corelib/object.h index 9d26ab9..913b670 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(object_t* object) { + return object != NULL || object->active <= 0; +} -#endif /* _object_h */ \ No newline at end of file +#endif /* _object_h */ -- 2.34.1 From 3ed5755e527cfe56eb8f1e9958c54da3194fe9e7 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:07:08 +0200 Subject: [PATCH 07/21] physics.h no longer depends on g_objects being public, or WORLD_NUM_OBJECTS being constant --- src/corelib/physics.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/corelib/physics.c b/src/corelib/physics.c index 001348c..02890e3 100644 --- a/src/corelib/physics.c +++ b/src/corelib/physics.c @@ -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); -- 2.34.1 From e6b66884ec068beb6c543f7c3bb9abaf7f623913 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:07:29 +0200 Subject: [PATCH 08/21] 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(); + } +} -- 2.34.1 From 544f18e9c6cc591f4eb4730eddeaff5176516f1b Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:16:05 +0200 Subject: [PATCH 09/21] added error when failing to expand world --- src/corelib/world.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/corelib/world.c b/src/corelib/world.c index af89ea5..a7ce34b 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -16,6 +16,7 @@ int _expand_world() { size_t new_num = _world_objects.num * 2; object_t** new_list = realloc(_world_objects.objects, new_num * sizeof(object_t*)); if(new_list == NULL) { + printf("ERROR: failed to expand world when requesting additional objects\n"); return 0; } else { _world_objects.objects = new_list; -- 2.34.1 From c1fa6e2956ed5520fde067f61a49618ba541698f Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:19:32 +0200 Subject: [PATCH 10/21] now creating world array with 16 elements if world array length is 0 --- src/corelib/world.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/corelib/world.c b/src/corelib/world.c index a7ce34b..e2457eb 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -14,6 +14,9 @@ struct { 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) { printf("ERROR: failed to expand world when requesting additional objects\n"); -- 2.34.1 From d915047bd19a5706f204fd2a456bbd9eeaa8e043 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:25:11 +0200 Subject: [PATCH 11/21] removed uses of world_num_objects from world.c and replaced them with _world_objects.num --- src/corelib/world.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/corelib/world.c b/src/corelib/world.c index e2457eb..47ea660 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -64,7 +64,7 @@ object_t* instantiate_object(const object_t *original) { } void world_update() { - for(int i = 0; i < world_num_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) { @@ -74,7 +74,7 @@ void world_update() { } void world_draw() { - for(int i = 0; i < world_num_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) { -- 2.34.1 From 6a52f2795d06e05718368965edd54ab60c8b87ee Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:25:32 +0200 Subject: [PATCH 12/21] object_is_valid now does not attempt to validate internals if object is NULL --- src/corelib/object.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/corelib/object.h b/src/corelib/object.h index 913b670..14baea3 100644 --- a/src/corelib/object.h +++ b/src/corelib/object.h @@ -26,7 +26,7 @@ object_t object_default(); void object_draw_sprite(object_t* object); static inline int object_is_valid(object_t* object) { - return object != NULL || object->active <= 0; + return object != NULL && object->active <= 0; } #endif /* _object_h */ -- 2.34.1 From a139e4f3bb810ac815800b09caf4830d8809b429 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:28:13 +0200 Subject: [PATCH 13/21] now initializing newly expanded world space with nullptrs --- src/corelib/world.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/corelib/world.c b/src/corelib/world.c index 47ea660..2005a73 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -22,6 +22,10 @@ int _expand_world() { printf("ERROR: failed to expand world when requesting additional objects\n"); return 0; } else { + for(size_t i = _world_objects.num; i < new_num; ++i) { + _world_objects.objects[i] = NULL; + } + _world_objects.objects = new_list; _world_objects.num = new_num; return 1; -- 2.34.1 From 30d4e4c39a277e9df4d83766cb96e2b3421ad628 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:29:33 +0200 Subject: [PATCH 14/21] replaced initialization code of _world_objects.objects with new_list --- src/corelib/world.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/corelib/world.c b/src/corelib/world.c index 2005a73..41722ff 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -23,7 +23,7 @@ int _expand_world() { return 0; } else { for(size_t i = _world_objects.num; i < new_num; ++i) { - _world_objects.objects[i] = NULL; + new_list[i] = NULL; } _world_objects.objects = new_list; -- 2.34.1 From 3e3f322529f5c19888f9887ac46936b42fa1a8b6 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:38:26 +0200 Subject: [PATCH 15/21] _find_free_object now returns the index of the found object / make_object now allocates the object if the found slot is NULL --- src/corelib/world.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/corelib/world.c b/src/corelib/world.c index 41722ff..4f78b6c 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -33,15 +33,15 @@ int _expand_world() { } static inline -object_t* _find_free_object() { +size_t _find_free_object() { for(int i = 0; i < _world_objects.num; ++i) { if(object_is_valid(_world_objects.objects[i])) { - return _world_objects.objects[i]; + return i; } } size_t num = _world_objects.num; if(_expand_world()) { - return _world_objects.objects[num]; + return num; } else { return NULL; } @@ -55,7 +55,11 @@ void world_clear() { } 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; } -- 2.34.1 From 5e001bc345813c3933797c59120129fd98924f5c Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:43:53 +0200 Subject: [PATCH 16/21] object_is_valid now confirms if an object is valid, as opposed to if an object is invalid --- src/corelib/object.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/corelib/object.h b/src/corelib/object.h index 14baea3..ffa0659 100644 --- a/src/corelib/object.h +++ b/src/corelib/object.h @@ -26,7 +26,7 @@ object_t object_default(); void object_draw_sprite(object_t* object); static inline int object_is_valid(object_t* object) { - return object != NULL && object->active <= 0; + return object == NULL || object->active == 0; } #endif /* _object_h */ -- 2.34.1 From 59de57fe3adc00cfb98aca3326a67f9152b2da5b Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:44:21 +0200 Subject: [PATCH 17/21] simplified error checking, out of memory now results in an immediate crash when trying to expand world --- src/corelib/world.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/corelib/world.c b/src/corelib/world.c index 4f78b6c..0036b7f 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -1,5 +1,6 @@ #include "world.h" #include "memory.h" +#include "assert.h" #include "math/vec.h" #include "object.h" @@ -18,18 +19,19 @@ int _expand_world() { new_num = 16; } object_t** new_list = realloc(_world_objects.objects, new_num * sizeof(object_t*)); - if(new_list == NULL) { - printf("ERROR: failed to expand world when requesting additional objects\n"); - return 0; - } else { - 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; + 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; } static inline @@ -40,11 +42,8 @@ size_t _find_free_object() { } } size_t num = _world_objects.num; - if(_expand_world()) { - return num; - } else { - return NULL; - } + _expand_world(); + return num; } void world_clear() { -- 2.34.1 From 78c0e3a9c0561e14c3c5cf243a927e77183f023a Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:46:26 +0200 Subject: [PATCH 18/21] object_is_valid now takes a constant pointer --- src/corelib/object.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/corelib/object.h b/src/corelib/object.h index ffa0659..362efb6 100644 --- a/src/corelib/object.h +++ b/src/corelib/object.h @@ -25,7 +25,7 @@ object_t object_default(); void object_draw_sprite(object_t* object); static inline -int object_is_valid(object_t* object) { +int object_is_valid(const object_t* object) { return object == NULL || object->active == 0; } -- 2.34.1 From 8a6a0e74936abbeb00d0bb59381ac06ed6639c30 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:47:31 +0200 Subject: [PATCH 19/21] physics.c: can_collide now checks for object_is_valid before comfirming other variables --- src/corelib/physics.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/corelib/physics.c b/src/corelib/physics.c index 02890e3..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 -- 2.34.1 From 0698121853076d92d0243da5f0953c1c8e2a7a31 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:49:36 +0200 Subject: [PATCH 20/21] object_is_valid now confirms that an object is not null and is active --- src/corelib/object.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/corelib/object.h b/src/corelib/object.h index 362efb6..ea38c6e 100644 --- a/src/corelib/object.h +++ b/src/corelib/object.h @@ -26,7 +26,7 @@ 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; + return object != NULL && object->active != 0; } #endif /* _object_h */ -- 2.34.1 From ec50000130e34a4597f9933fde2147396fb35cac Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 12 Jul 2023 02:52:04 +0200 Subject: [PATCH 21/21] un-negated uses of object_is_valid that shouldn't be, and negated ones that should be --- src/corelib/world.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/corelib/world.c b/src/corelib/world.c index 0036b7f..7eb4ddb 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -37,7 +37,7 @@ int _expand_world() { static inline size_t _find_free_object() { for(int i = 0; i < _world_objects.num; ++i) { - if(object_is_valid(_world_objects.objects[i])) { + if(!object_is_valid(_world_objects.objects[i])) { return i; } } @@ -73,7 +73,7 @@ object_t* instantiate_object(const object_t *original) { void world_update() { for(int i = 0; i < _world_objects.num; ++i) { object_t* object = world_get_object(i); - if(!object_is_valid(object) + if(object_is_valid(object) && object->evt_update != NULL) { object->evt_update(object); } @@ -83,7 +83,7 @@ void world_update() { void world_draw() { for(int i = 0; i < _world_objects.num; ++i) { object_t* object = world_get_object(i); - if(!object_is_valid(object) + if(object_is_valid(object) && object->evt_draw != NULL) { object->evt_draw(object); } -- 2.34.1