From 2a81321b7e3aae9f375a9fd51b430dbf867d2d98 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Tue, 2 Apr 2024 15:04:05 +0800 Subject: [PATCH] Make it not crash when player is deleted --- app_render.cpp | 8 +++++--- entity.c | 20 ++++++++++++++++++++ entity.h | 8 ++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/app_render.cpp b/app_render.cpp index 6d9c360..462d789 100644 --- a/app_render.cpp +++ b/app_render.cpp @@ -85,9 +85,11 @@ void app_Render(App *app) { } } } - setlinecolor(RGB(255, 0, 255)); - Vec2 respawn = camera_TransformVec2(app->camera, app->player->player->hazardRespawn); - circle((int)round(respawn.x), (int)round(respawn.y), 6); + if (app->player->player) { + setlinecolor(RGB(255, 0, 255)); + Vec2 respawn = camera_TransformVec2(app->camera, app->player->player->hazardRespawn); + circle((int)round(respawn.x), (int)round(respawn.y), 6); + } setfillcolor(RGB(255, 255, 255)); diff --git a/entity.c b/entity.c index 69ce489..1f8a509 100644 --- a/entity.c +++ b/entity.c @@ -123,3 +123,23 @@ void entity_Advance(System_Entity *sys, Duration deltaTime) { e->thinker(sys->super, e, deltaTime); } } + + +void entity_Clear(System_Entity *sys) { + // Build a list of all entity IDs + vector_Clear(sys->flaggedDelete); + for (tree_Node *i = tree_FirstNode(sys->entities); + i != NULL; + i = tree_Node_Next(i)) { + Entity *e = (Entity *)i->data; + vector_Push(sys->flaggedDelete, &e->id); + } + + // _Delete all of them + for (int i = 0; i < vector_Size(sys->flaggedDelete); i++) { + _entity_Delete(sys, *(uintptr_t *)(vector_At(sys->flaggedDelete, i))); + } + vector_Clear(sys->flaggedDelete); + + ASSERT(tree_Count(sys->entities) == 0); +} diff --git a/entity.h b/entity.h index 3bfe180..40e91c3 100644 --- a/entity.h +++ b/entity.h @@ -65,6 +65,14 @@ void entity_Delete(System_Entity *sys, uintptr_t id); void entity_Advance(System_Entity *sys, Duration deltaTime); +// Clears all the entities. Also calls other systems +// to update these removals. +// +// Should load all other new entities and the player +// before any game logic after this, or this probably +// will crash. Sketchy. +void entity_Clear(System_Entity *sys); + #ifdef __cplusplus } #endif