Use quadtree for drawing, now that quadtree bug is squashed (hopefully).

This commit is contained in:
Steve 2016-04-09 18:13:59 +01:00
parent bea850f755
commit 6c8349781c
4 changed files with 13 additions and 64 deletions

View File

@ -23,22 +23,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void drawEntity(Entity *e); static void drawEntity(Entity *e);
static void doEntity(void); static void doEntity(void);
static void alignComponents(void); static void alignComponents(void);
static void drawEntity(Entity *e);
static void activateEpicFighters(int n, int side); static void activateEpicFighters(int n, int side);
static void restrictToBattleArea(Entity *e); static void restrictToBattleArea(Entity *e);
static void drawTargetRects(Entity *e); static void drawTargetRects(Entity *e);
static int drawComparator(const void *a, const void *b); static int drawComparator(const void *a, const void *b);
static void notifyNewArrivals(void); static void notifyNewArrivals(void);
static int isComponent(Entity *e); static int isComponent(Entity *e);
static void resizeDrawList(void);
static Entity deadHead; static Entity deadHead;
static Entity *deadTail; static Entity *deadTail;
static int disabledGlow; static int disabledGlow;
static int disabledGlowDir; static int disabledGlowDir;
static Entity **entsToDraw;
static int drawCapacity;
static int numEntsToDraw;
void initEntities(void) void initEntities(void)
{ {
@ -48,11 +43,6 @@ void initEntities(void)
disabledGlow = DISABLED_GLOW_MAX; disabledGlow = DISABLED_GLOW_MAX;
disabledGlowDir = -DISABLED_GLOW_SPEED; disabledGlowDir = -DISABLED_GLOW_SPEED;
drawCapacity = INITIAL_ENTITY_DRAW_CAPACITY;
entsToDraw = malloc(sizeof(Entity*) * drawCapacity);
memset(entsToDraw, 0, sizeof(Entity*) * drawCapacity);
} }
Entity *spawnEntity(void) Entity *spawnEntity(void)
@ -84,10 +74,6 @@ void doEntities(void)
player->shield = player->maxShield; player->shield = player->maxShield;
} }
numEntsToDraw = 0;
memset(entsToDraw, 0, sizeof(Entity*) * drawCapacity);
for (e = battle.entityHead.next ; e != NULL ; e = e->next) for (e = battle.entityHead.next ; e != NULL ; e = e->next)
{ {
removeFromQuadtree(e, &battle.quadtree); removeFromQuadtree(e, &battle.quadtree);
@ -179,16 +165,6 @@ void doEntities(void)
{ {
addToQuadtree(e, &battle.quadtree); addToQuadtree(e, &battle.quadtree);
} }
if (isOnBattleScreen(e->x, e->y, e->w, e->h))
{
entsToDraw[numEntsToDraw++] = e;
if (numEntsToDraw == drawCapacity)
{
resizeDrawList();
}
}
} }
else 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) static void restrictToBattleArea(Entity *e)
{ {
float force; float force;
@ -398,12 +361,17 @@ static int isComponent(Entity *e)
void drawEntities(void) void drawEntities(void)
{ {
Entity *e;
int i; int i;
Entity *e, **candidates;
qsort(entsToDraw, numEntsToDraw, sizeof(Entity*), drawComparator); candidates = getAllEntsWithin(battle.camera.x, battle.camera.y, SCREEN_WIDTH, SCREEN_HEIGHT, NULL);
for (i = 0, e = entsToDraw[i] ; e != NULL ; e = entsToDraw[++i]) /* 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 = candidates[i] ; e != NULL ; e = candidates[++i])
{ {
self = e; self = e;
@ -589,28 +557,15 @@ void countNumEnemies(void)
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "battle.numInitialEnemies=%d", battle.numInitialEnemies); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "battle.numInitialEnemies=%d", battle.numInitialEnemies);
} }
void addVisibleEntsToDrawList(void) void addAllToQuadtree(void)
{ {
Entity *e; 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) for (e = battle.entityHead.next ; e != NULL ; e = e->next)
{ {
if (e->active) if (e->active)
{ {
if (isOnBattleScreen(e->x, e->y, e->w, e->h)) addToQuadtree(e, &battle.quadtree);
{
entsToDraw[numEntsToDraw++] = e;
if (numEntsToDraw == drawCapacity)
{
resizeDrawList();
}
}
} }
} }
} }
@ -635,8 +590,4 @@ void destroyEntities(void)
} }
deadTail = &deadHead; deadTail = &deadHead;
free(entsToDraw);
entsToDraw = NULL;
} }

View File

@ -20,7 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h" #include "../common.h"
#define INITIAL_ENTITY_DRAW_CAPACITY 8
#define DISABLED_GLOW_SPEED 3 #define DISABLED_GLOW_SPEED 3
#define DISABLED_GLOW_MIN 128 #define DISABLED_GLOW_MIN 128
#define DISABLED_GLOW_MAX 255 #define DISABLED_GLOW_MAX 255
@ -35,8 +34,7 @@ extern void drawShieldHitEffect(Entity *e);
extern void removeFromQuadtree(Entity *e, Quadtree *root); extern void removeFromQuadtree(Entity *e, Quadtree *root);
extern void addToQuadtree(Entity *e, Quadtree *root); extern void addToQuadtree(Entity *e, Quadtree *root);
extern void updateCapitalShipComponentProperties(Entity *parent, long flags); extern void updateCapitalShipComponentProperties(Entity *parent, long flags);
extern void *resize(void *array, int oldSize, int newSize); extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore);
extern int isOnBattleScreen(int x, int y, int w, int h);
extern App app; extern App app;
extern Battle battle; extern Battle battle;

View File

@ -231,7 +231,7 @@ void loadMission(char *filename)
initMissionInfo(); initMissionInfo();
addVisibleEntsToDrawList(); addAllToQuadtree();
playMusic(music); playMusic(music);
} }

View File

@ -49,7 +49,7 @@ extern char *getPlanetTextureName(int n);
extern char *getMusicFilename(int n); extern char *getMusicFilename(int n);
extern int getJSONValue(cJSON *node, char *name, int defValue); extern int getJSONValue(cJSON *node, char *name, int defValue);
extern char *getJSONValueStr(cJSON *node, char *name, char *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 loadObjectives(cJSON *node);
extern void loadPlayer(cJSON *node); extern void loadPlayer(cJSON *node);
extern void loadCapitalShips(cJSON *node); extern void loadCapitalShips(cJSON *node);