From 33c1a5e9d24c542e05e2c7ee6067108682d4a200 Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 16 Jul 2023 00:31:13 +0200 Subject: [PATCH] changed the way objects are notified of destruction, simplified game.c preset --- src/corelib/object.c | 9 ++++----- src/corelib/object.h | 6 +++--- src/corelib/physics.c | 2 +- src/corelib/render.h | 1 - src/corelib/world.c | 17 +++++++++++++++-- src/game.c | 2 -- 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/corelib/object.c b/src/corelib/object.c index 5664fbc..93ecb72 100644 --- a/src/corelib/object.c +++ b/src/corelib/object.c @@ -3,11 +3,10 @@ object_t object_default() { return (object_t){ .active = 1, - .enabled = 1, - .physics = physics_default(), - .evt_draw = &object_draw_sprite, - .evt_update = NULL, - .sprite = sprite_default(), + .physics = physics_default(), + .evt_draw = &object_draw_sprite, + .evt_update = NULL, + .sprite = sprite_default(), }; } diff --git a/src/corelib/object.h b/src/corelib/object.h index ea38c6e..09a78f9 100644 --- a/src/corelib/object.h +++ b/src/corelib/object.h @@ -11,8 +11,8 @@ typedef void(*draw_fn)(struct object_t*); struct object_t { sprite_t sprite; - int active; // 1 if this object is in use and should not be overriden. - int enabled; // 1 if this object's events should be triggered. + int active; // 1 if this object's events should be triggered by the game world update. + int wants_to_be_deleted; physics_t physics; // the collider to use for this object's physics interaction. uintptr_t timer; // free to use for whatever @@ -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->wants_to_be_deleted == 0; } #endif /* _object_h */ diff --git a/src/corelib/physics.c b/src/corelib/physics.c index 6ac2844..f9ee2b4 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 object_is_valid(this) && this->enabled; + return object_is_valid(this); } static inline diff --git a/src/corelib/render.h b/src/corelib/render.h index d2fc2e4..6b32700 100644 --- a/src/corelib/render.h +++ b/src/corelib/render.h @@ -104,7 +104,6 @@ extern sprite_t make_sprite(const char* file, float x, float y); extern sprite_t sprite_from_spritesheet(spritesheet_t* sheet, int index); extern text_style_t make_text_style(const char* font, SDL_Color color, int dpi, float size); extern SDL_Rect get_srcrect_from(spritesheet_t* sheet, int index); -extern void set_active_view(const view_t* view); #define no_sprite (sprite_t){NULL, 0.0, 0.0, {0.0, 0.0}, 0.0, 0.0, 0.0, 0, {0, 0, 0, 0}} diff --git a/src/corelib/world.c b/src/corelib/world.c index 7eb4ddb..4a91a23 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -47,23 +47,36 @@ size_t _find_free_object() { } void world_clear() { + // free all allocated objects for(int i = 0; i < _world_objects.num; ++i) { - _world_objects.objects[i]->active = 0; - _world_objects.objects[i]->enabled = 0; + object_t* object = _world_objects.objects[i]; + if(object != NULL) { + free(object); + } } + // free world array + free(_world_objects.objects); + // reset world objects struct + _world_objects.objects = NULL; + _world_objects.num = 0; } object_t* make_object() { + // acquire pointer to empty slot size_t index = _find_free_object(); + // allocate new object if(_world_objects.objects[index] == NULL) { _world_objects.objects[index] = malloc(sizeof(object_t)); } + // initialize object to default object_t* o = _world_objects.objects[index]; *o = object_default(); + return o; } object_t* instantiate_object(const object_t *original) { + // create new object with default settings object_t* obj = make_object(); *obj = *original; obj->active = 1; diff --git a/src/game.c b/src/game.c index 9c5d0ed..2d1f110 100644 --- a/src/game.c +++ b/src/game.c @@ -4,8 +4,6 @@ #include "corelib/render.h" #include "corelib/assets.h" #include "corelib/layers.h" -#include "SDL2/SDL_mouse.h" -#include "SDL2/SDL_scancode.h" void start_game() { // called when the game is done initializing the backend and ready to start