From 6c8349781c96117948e79cac3c98e071ba080cd2 Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 9 Apr 2016 18:13:59 +0100 Subject: [PATCH] Use quadtree for drawing, now that quadtree bug is squashed (hopefully). --- src/battle/entities.c | 69 +++++++------------------------------------ src/battle/entities.h | 4 +-- src/galaxy/mission.c | 2 +- src/galaxy/mission.h | 2 +- 4 files changed, 13 insertions(+), 64 deletions(-) diff --git a/src/battle/entities.c b/src/battle/entities.c index 61a8ce4..79ed9f5 100644 --- a/src/battle/entities.c +++ b/src/battle/entities.c @@ -23,22 +23,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void drawEntity(Entity *e); static void doEntity(void); static void alignComponents(void); -static void drawEntity(Entity *e); static void activateEpicFighters(int n, int side); static void restrictToBattleArea(Entity *e); static void drawTargetRects(Entity *e); static int drawComparator(const void *a, const void *b); static void notifyNewArrivals(void); static int isComponent(Entity *e); -static void resizeDrawList(void); static Entity deadHead; static Entity *deadTail; static int disabledGlow; static int disabledGlowDir; -static Entity **entsToDraw; -static int drawCapacity; -static int numEntsToDraw; void initEntities(void) { @@ -48,11 +43,6 @@ void initEntities(void) disabledGlow = DISABLED_GLOW_MAX; disabledGlowDir = -DISABLED_GLOW_SPEED; - - drawCapacity = INITIAL_ENTITY_DRAW_CAPACITY; - - entsToDraw = malloc(sizeof(Entity*) * drawCapacity); - memset(entsToDraw, 0, sizeof(Entity*) * drawCapacity); } Entity *spawnEntity(void) @@ -83,10 +73,6 @@ void doEntities(void) player->health = player->maxHealth; player->shield = player->maxShield; } - - numEntsToDraw = 0; - - memset(entsToDraw, 0, sizeof(Entity*) * drawCapacity); for (e = battle.entityHead.next ; e != NULL ; e = e->next) { @@ -179,16 +165,6 @@ void doEntities(void) { addToQuadtree(e, &battle.quadtree); } - - if (isOnBattleScreen(e->x, e->y, e->w, e->h)) - { - entsToDraw[numEntsToDraw++] = e; - - if (numEntsToDraw == drawCapacity) - { - resizeDrawList(); - } - } } else { @@ -284,19 +260,6 @@ void doEntities(void) } } -static void resizeDrawList(void) -{ - int n; - - n = drawCapacity + INITIAL_ENTITY_DRAW_CAPACITY; - - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing entity draw capacity: %d -> %d", drawCapacity, n); - - entsToDraw = resize(entsToDraw, sizeof(Entity*) * drawCapacity, sizeof(Entity*) * n); - - drawCapacity = n; -} - static void restrictToBattleArea(Entity *e) { float force; @@ -398,12 +361,17 @@ static int isComponent(Entity *e) void drawEntities(void) { - Entity *e; int i; + Entity *e, **candidates; - qsort(entsToDraw, numEntsToDraw, sizeof(Entity*), drawComparator); + candidates = getAllEntsWithin(battle.camera.x, battle.camera.y, SCREEN_WIDTH, SCREEN_HEIGHT, NULL); + + /* counting entities to draw */ + for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) {}; + + qsort(candidates, i, sizeof(Entity*), drawComparator); - for (i = 0, e = entsToDraw[i] ; e != NULL ; e = entsToDraw[++i]) + for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) { self = e; @@ -589,28 +557,15 @@ void countNumEnemies(void) SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "battle.numInitialEnemies=%d", battle.numInitialEnemies); } -void addVisibleEntsToDrawList(void) +void addAllToQuadtree(void) { Entity *e; - battle.camera.x = player->x - (SCREEN_WIDTH / 2); - battle.camera.y = player->y - (SCREEN_HEIGHT / 2); - - numEntsToDraw = 0; - for (e = battle.entityHead.next ; e != NULL ; e = e->next) { if (e->active) { - if (isOnBattleScreen(e->x, e->y, e->w, e->h)) - { - entsToDraw[numEntsToDraw++] = e; - - if (numEntsToDraw == drawCapacity) - { - resizeDrawList(); - } - } + addToQuadtree(e, &battle.quadtree); } } } @@ -635,8 +590,4 @@ void destroyEntities(void) } deadTail = &deadHead; - - free(entsToDraw); - - entsToDraw = NULL; } diff --git a/src/battle/entities.h b/src/battle/entities.h index 55f2b99..67f801a 100644 --- a/src/battle/entities.h +++ b/src/battle/entities.h @@ -20,7 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../common.h" -#define INITIAL_ENTITY_DRAW_CAPACITY 8 #define DISABLED_GLOW_SPEED 3 #define DISABLED_GLOW_MIN 128 #define DISABLED_GLOW_MAX 255 @@ -35,8 +34,7 @@ extern void drawShieldHitEffect(Entity *e); extern void removeFromQuadtree(Entity *e, Quadtree *root); extern void addToQuadtree(Entity *e, Quadtree *root); extern void updateCapitalShipComponentProperties(Entity *parent, long flags); -extern void *resize(void *array, int oldSize, int newSize); -extern int isOnBattleScreen(int x, int y, int w, int h); +extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern App app; extern Battle battle; diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index 50ee935..3c0af6c 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -231,7 +231,7 @@ void loadMission(char *filename) initMissionInfo(); - addVisibleEntsToDrawList(); + addAllToQuadtree(); playMusic(music); } diff --git a/src/galaxy/mission.h b/src/galaxy/mission.h index b1a4d0a..2bab095 100644 --- a/src/galaxy/mission.h +++ b/src/galaxy/mission.h @@ -49,7 +49,7 @@ extern char *getPlanetTextureName(int n); extern char *getMusicFilename(int n); extern int getJSONValue(cJSON *node, char *name, int defValue); extern char *getJSONValueStr(cJSON *node, char *name, char *defValue); -extern void addVisibleEntsToDrawList(void); +extern void addAllToQuadtree(void); extern void loadObjectives(cJSON *node); extern void loadPlayer(cJSON *node); extern void loadCapitalShips(cJSON *node);