diff --git a/src/battle/bullets.c b/src/battle/bullets.c index ef11737..73aa328 100644 --- a/src/battle/bullets.c +++ b/src/battle/bullets.c @@ -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; diff --git a/src/battle/bullets.h b/src/battle/bullets.h index e859ad6..2b51cf6 100644 --- a/src/battle/bullets.h +++ b/src/battle/bullets.h @@ -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; diff --git a/src/battle/debris.c b/src/battle/debris.c index 25b1103..b6947a5 100644 --- a/src/battle/debris.c +++ b/src/battle/debris.c @@ -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; diff --git a/src/battle/debris.h b/src/battle/debris.h index d6cddf1..c723129 100644 --- a/src/battle/debris.h +++ b/src/battle/debris.h @@ -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; diff --git a/src/battle/effects.c b/src/battle/effects.c index 330fc7a..cbc68fc 100644 --- a/src/battle/effects.c +++ b/src/battle/effects.c @@ -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; } diff --git a/src/battle/effects.h b/src/battle/effects.h index 485d530..f5da065 100644 --- a/src/battle/effects.h +++ b/src/battle/effects.h @@ -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; diff --git a/src/battle/entities.c b/src/battle/entities.c index 4b41669..c10ebdb 100644 --- a/src/battle/entities.c +++ b/src/battle/entities.c @@ -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(); + } + } } } } diff --git a/src/battle/entities.h b/src/battle/entities.h index 6466510..740df75 100644 --- a/src/battle/entities.h +++ b/src/battle/entities.h @@ -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; diff --git a/src/draw/draw.c b/src/draw/draw.c index 73f42d2..2ab53f8 100644 --- a/src/draw/draw.c +++ b/src/draw/draw.c @@ -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; diff --git a/src/draw/draw.h b/src/draw/draw.h index 8a895c0..3a9e254 100644 --- a/src/draw/draw.h +++ b/src/draw/draw.h @@ -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; diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index de77caf..df5960b 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -225,7 +225,7 @@ void loadMission(char *filename) initMissionInfo(); - addAllEntsToQuadtree(); + addVisibleEntsToDrawList(); playMusic(music); } diff --git a/src/galaxy/mission.h b/src/galaxy/mission.h index aa13bd7..1565040 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 addAllEntsToQuadtree(void); +extern void addVisibleEntsToDrawList(void); extern void loadObjectives(cJSON *node); extern void loadPlayer(cJSON *node); extern void loadCapitalShips(cJSON *node);