From 9de4bf625547f646b139fa209401084ae7114d83 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 9 Nov 2015 22:48:59 +0000 Subject: [PATCH] Sort entities before drawing, using their type number as the order. --- src/battle/entities.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/battle/entities.c b/src/battle/entities.c index f1a965f..3a2f775 100644 --- a/src/battle/entities.c +++ b/src/battle/entities.c @@ -24,6 +24,7 @@ static void drawEntity(Entity *e); static void doEntity(void); static void activateEpicFighters(int n, int side); static void restrictToGrid(Entity *e); +static int drawComparator(const void *a, const void *b); Entity *spawnEntity(void) { @@ -200,8 +201,21 @@ void drawEntities(void) { Entity *e, **candidates; int i; - + candidates = getAllEntsWithin(battle.camera.x, battle.camera.y, SCREEN_WIDTH, SCREEN_HEIGHT, NULL); + + i = 0; + e = candidates[i]; + + while (e) + { + i++; + + e = (i < MAX_GRID_CANDIDATES) ? candidates[i] : NULL; + } + + qsort(candidates, i, sizeof(Entity*), drawComparator); + i = 0; e = candidates[i]; @@ -265,3 +279,11 @@ static void activateEpicFighters(int n, int side) } } } + +static int drawComparator(const void *a, const void *b) +{ + Entity *e1 = *((Entity**)a); + Entity *e2 = *((Entity**)b); + + return e2->type - e1->type; +}