From 6ba7b3ae436bb33375201fb3ffb7dc9e72548f34 Mon Sep 17 00:00:00 2001 From: BoredGuy Date: Wed, 27 Aug 2025 16:09:56 +0300 Subject: Y SORTINGGGGGG! - And magit works now thank god --- CMakeLists.txt | 1 + src/game.c | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e1f84a2..5f93b7a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ endif() if (UNIX) target_link_libraries(game m) + target_compile_options(game PRIVATE -O3) endif() if (MSVC) diff --git a/src/game.c b/src/game.c index 59747c6..df904d7 100644 --- a/src/game.c +++ b/src/game.c @@ -1,5 +1,6 @@ #include #include +#include #include "stdio.h" #include "constants.h" @@ -12,6 +13,18 @@ Game game; +int compare(const void* a, const void* b) { + const Entity* eA = *(Entity**)a; + const Entity* eB = *(Entity**)b; + + if (!PhysicsEnabled(eA)) + return 1; + else if (!PhysicsEnabled(eB)) + return -1; + + return GetPhysicsColliderGlobal(eA).y < GetPhysicsColliderGlobal(eB).y ? -1 : 1; +} + void AddEntity(Entity* e) { static int nextId = 0; @@ -106,9 +119,9 @@ void DrawEntity(const Entity* e, DrawLayer toLayer) { } } -void DrawEntitiesInLayer(int layer) { +void DrawEntitiesInLayer(Entity* drawOrder[], int layer) { for (int i = 0; i < MAX_ENTITY_COUNT; i++) { - Entity* currentEntity = &game.entities[i]; + Entity* currentEntity = drawOrder[i]; if (ShouldDrawEntity(currentEntity, layer)) DrawEntity(currentEntity, layer); @@ -116,18 +129,26 @@ void DrawEntitiesInLayer(int layer) { } void DrawGame() { + Entity* drawOrder[MAX_ENTITY_COUNT]; + + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + drawOrder[i] = &game.entities[i]; + } + + qsort(drawOrder, MAX_ENTITY_COUNT, sizeof(Entity*), compare); + BeginDrawing(); ClearBackground(RAYWHITE); BeginMode2D(game.camera); for (DrawLayer layer = Background_Layer; layer <= Foreground_Layer; layer++) - DrawEntitiesInLayer(layer); + DrawEntitiesInLayer(drawOrder, layer); #ifdef BEATEMUP_DEBUG if (game.enableDebugOverlay) { for (int i = 0; i < MAX_ENTITY_COUNT; i++) { - DebugHighlights(&game.entities[i]); + DebugHighlights(drawOrder[i]); } } #endif -- cgit v1.2.3