summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoredGuy <osome3717@gmail.com>2025-08-27 16:09:56 +0300
committerBoredGuy <osome3717@gmail.com>2025-08-27 16:09:56 +0300
commit6ba7b3ae436bb33375201fb3ffb7dc9e72548f34 (patch)
tree76ca20358c34bd513fe0a7a111240e6e5365860d
parentf95068d73410aa6d01e8ba91b430ca05d404a57b (diff)
Y SORTINGGGGGG!
- And magit works now thank god
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/game.c29
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 <raylib.h>
#include <string.h>
+#include <stdlib.h>
#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