Don't use quadtree for drawing.

This commit is contained in:
Steve 2016-04-02 16:37:49 +01:00
parent c555aeccbe
commit 4a1958319c
12 changed files with 90 additions and 19 deletions

View File

@ -121,7 +121,7 @@ void doBullets(void)
}
else
{
if (collision(b->x - (b->w / 2) - battle.camera.x, b->y - (b->h / 2) - battle.camera.y, b->w * 2, b->h * 2, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
if (isOnBattleScreen(b->x, b->y, b->w, b->h))
{
bulletsToDraw[i++] = b;

View File

@ -43,6 +43,7 @@ extern void drawText(int x, int y, int size, int align, SDL_Color c, const char
extern void playSound(int id);
extern char *getTranslatedString(char *string);
extern void *resize(void *array, int oldSize, int newSize);
extern int isOnBattleScreen(int x, int y, int w, int h);
extern Battle battle;
extern Colors colors;

View File

@ -104,7 +104,7 @@ void doDebris(void)
}
else
{
if (collision(d->x - 16 - battle.camera.x, d->y - 16 - battle.camera.y, 32, 32, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
if (isOnBattleScreen(d->x, d->y, 32, 32))
{
debrisToDraw[i++] = d;

View File

@ -31,5 +31,6 @@ extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int
extern SDL_Texture *getTexture(char *filename);
extern void addDebrisFire(int x, int y);
extern void *resize(void *array, int oldSize, int newSize);
extern int isOnBattleScreen(int x, int y, int w, int h);
extern Battle battle;

View File

@ -91,7 +91,7 @@ void doEffects(void)
break;
default:
onScreen = collision(e->x - (e->size / 2) - battle.camera.x, e->y - (e->size / 2) - battle.camera.y, e->size * 2, e->size * 2, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
onScreen = isOnBattleScreen(e->x, e->y, e->size, e->size);
break;
}

View File

@ -27,6 +27,7 @@ extern SDL_Texture *getTexture(char *name);
extern void blit(SDL_Texture *t, int x, int y, int center);
extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
extern void *resize(void *array, int oldSize, int newSize);
extern int isOnBattleScreen(int x, int y, int w, int h);
extern App app;
extern Battle battle;

View File

@ -30,11 +30,15 @@ static void drawTargetRects(Entity *e);
static int drawComparator(const void *a, const void *b);
static void notifyNewArrivals(void);
static int isCapitalShipComponent(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)
{
@ -44,6 +48,11 @@ 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)
@ -75,6 +84,10 @@ 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)
{
@ -167,6 +180,16 @@ 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
{
@ -262,6 +285,19 @@ 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\n", drawCapacity, n);
entsToDraw = resize(entsToDraw, sizeof(Entity*) * drawCapacity, sizeof(Entity*) * n);
drawCapacity = n;
}
static void restrictToBattleArea(Entity *e)
{
float force;
@ -363,17 +399,12 @@ static int isCapitalShipComponent(Entity *e)
void drawEntities(void)
{
Entity *e, **candidates;
Entity *e;
int i;
qsort(entsToDraw, numEntsToDraw, sizeof(Entity*), drawComparator);
candidates = getAllEntsWithin(battle.camera.x, battle.camera.y, SCREEN_WIDTH, SCREEN_HEIGHT, NULL);
/* count number of candidates for use with qsort */
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])
for (i = 0, e = entsToDraw[i] ; e != NULL ; e = entsToDraw[++i])
{
self = e;
@ -559,15 +590,27 @@ void countNumEnemies(void)
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "battle.numInitialEnemies=%d", battle.numInitialEnemies);
}
void addAllEntsToQuadtree(void)
void addVisibleEntsToDrawList(void)
{
int numEntsToDraw = 0;
Entity *e;
battle.camera.x = player->x - (SCREEN_WIDTH / 2);
battle.camera.y = player->y - (SCREEN_HEIGHT / 2);
for (e = battle.entityHead.next ; e != NULL ; e = e->next)
{
if (e->active)
{
addToQuadtree(e, &battle.quadtree);
if (isOnBattleScreen(e->x, e->y, e->w, e->h))
{
entsToDraw[numEntsToDraw++] = e;
if (numEntsToDraw == drawCapacity)
{
resizeDrawList();
}
}
}
}
}

View File

@ -20,9 +20,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h"
#define DISABLED_GLOW_SPEED 3
#define DISABLED_GLOW_MIN 128
#define DISABLED_GLOW_MAX 255
#define INITIAL_ENTITY_DRAW_CAPACITY 8
#define DISABLED_GLOW_SPEED 3
#define DISABLED_GLOW_MIN 128
#define DISABLED_GLOW_MAX 255
extern void blitRotated(SDL_Texture *texture, int x, int y, float angle);
extern void doFighter(void);
@ -35,6 +36,9 @@ 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 int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
extern void *resize(void *array, int oldSize, int newSize);
extern int isOnBattleScreen(int x, int y, int w, int h);
extern App app;
extern Battle battle;

View File

@ -183,6 +183,25 @@ void drawBackground(SDL_Texture *texture)
}
}
int isOnBattleScreen(int x, int y, int w, int h)
{
x -= (w / 2);
x -= (SCREEN_WIDTH / 2);
x -= battle.camera.x;
y -= (h / 2);
y -= (SCREEN_HEIGHT / 2);
y -= battle.camera.y;
w *= 2;
w += SCREEN_WIDTH;
h *= 2;
h += SCREEN_HEIGHT;
return collision(x, y, w, h, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
void saveScreenshot(void)
{
static int i = 0;

View File

@ -22,7 +22,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...);
extern void drawMouse(void);
extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
extern App app;
extern Battle battle;
extern Colors colors;
extern Dev dev;

View File

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

View File

@ -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 addAllEntsToQuadtree(void);
extern void addVisibleEntsToDrawList(void);
extern void loadObjectives(cJSON *node);
extern void loadPlayer(cJSON *node);
extern void loadCapitalShips(cJSON *node);