From 239845a2796f632d561f107df2a9d47c37081eaa Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 24 Feb 2016 23:12:13 +0000 Subject: [PATCH] Array resize bug fixes. --- src/battle/battle.h | 3 +++ src/battle/bullets.c | 8 ++++++-- src/battle/debris.c | 8 ++++---- src/battle/effects.c | 14 +++++++++++--- src/battle/effects.h | 3 ++- src/system/io.c | 8 +++++++- 6 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/battle/battle.h b/src/battle/battle.h index b5c8a0f..b680472 100644 --- a/src/battle/battle.h +++ b/src/battle/battle.h @@ -78,6 +78,9 @@ extern void doDebris(void); extern void drawDebris(void); extern void doLocations(void); extern void drawLocations(void); +extern void destroyDebris(void); +extern void destroyBullets(void); +extern void destroyEffects(void); extern App app; extern Battle battle; diff --git a/src/battle/bullets.c b/src/battle/bullets.c index 629ac37..aeab981 100644 --- a/src/battle/bullets.c +++ b/src/battle/bullets.c @@ -78,7 +78,7 @@ void doBullets(void) incomingMissile = 0; - memset(bulletsToDraw, 0, sizeof(Bullet*) * MAX_BULLETS_TO_DRAW); + memset(bulletsToDraw, 0, sizeof(Bullet*) * drawCapacity); for (b = battle.bulletHead.next ; b != NULL ; b = b->next) { @@ -143,7 +143,7 @@ static void resizeDrawList(void) n = drawCapacity + INITIAL_BULLET_DRAW_CAPACITY; - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Resizing bullet draw capacity: %d -> %d\n", drawCapacity, n); + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing bullet draw capacity: %d -> %d\n", drawCapacity, n); bullets = malloc(sizeof(Bullet*) * n); memset(bullets, 0, sizeof(Bullet*) * n); @@ -407,6 +407,10 @@ void fireMissile(Entity *owner) } void destroyBulletDefs(void) +{ +} + +void destroyBullets(void) { free(bulletsToDraw); diff --git a/src/battle/debris.c b/src/battle/debris.c index b9c3343..55206c3 100644 --- a/src/battle/debris.c +++ b/src/battle/debris.c @@ -36,7 +36,7 @@ void initDebris(void) debrisTexture[4] = getTexture("gfx/debris/debris5.png"); debrisTexture[5] = getTexture("gfx/debris/debris6.png"); - drawCapacity = INITIAL_BULLET_DRAW_CAPACITY; + drawCapacity = INITIAL_DEBRIS_DRAW_CAPACITY; debrisToDraw = malloc(sizeof(Bullet*) * drawCapacity); memset(debrisToDraw, 0, sizeof(Bullet*) * drawCapacity); @@ -71,7 +71,7 @@ void doDebris(void) int i; Debris *d, *prev; - memset(debrisToDraw, 0, sizeof(Debris*) * MAX_DEBRIS_TO_DRAW); + memset(debrisToDraw, 0, sizeof(Debris*) * drawCapacity); prev = &battle.debrisHead; @@ -124,9 +124,9 @@ static void resizeDrawList(void) int i, n; Debris **debris; - n = drawCapacity + INITIAL_BULLET_DRAW_CAPACITY; + n = drawCapacity + INITIAL_DEBRIS_DRAW_CAPACITY; - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Resizing debris draw capacity: %d -> %d\n", drawCapacity, n); + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing debris draw capacity: %d -> %d\n", drawCapacity, n); debris = malloc(sizeof(Debris*) * n); memset(debris, 0, sizeof(Debris*) * n); diff --git a/src/battle/effects.c b/src/battle/effects.c index 0cdef59..2296fa7 100644 --- a/src/battle/effects.c +++ b/src/battle/effects.c @@ -22,11 +22,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void setRandomFlameHue(Effect *e); static void setRandomShieldHue(Effect *e); +static void resizeDrawList(void); static SDL_Texture *explosionTexture; static SDL_Texture *shieldHitTexture; static SDL_Texture *haloTexture; static Effect **effectsToDraw; +static int drawCapacity; void initEffects(void) { @@ -34,15 +36,21 @@ void initEffects(void) shieldHitTexture = getTexture("gfx/effects/shieldHit.png"); haloTexture = getTexture("gfx/effects/halo.png"); - drawCapacity = INITIAL_EFFECTS_TO_DRAW; + drawCapacity = INITIAL_EFFECT_DRAW_CAPACITY; effectsToDraw = malloc(sizeof(Effect*) * drawCapacity); + memset(effectsToDraw, 0, sizeof(Effect*) * drawCapacity); } void doEffects(void) { + int i; Effect *e; Effect *prev = &battle.effectHead; + + i = 0; + + memset(effectsToDraw, 0, sizeof(Effect*) * drawCapacity); for (e = battle.effectHead.next ; e != NULL ; e = e->next) { @@ -69,7 +77,7 @@ void doEffects(void) } else { - if (e->type == EFFECT_LINE || collision(e->x - (e->size / 2) - battle.camera.x, e->y - (b->size / 2) - battle.camera.y, e->size * 2, e->size * 2, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)) + if (e->type == EFFECT_LINE || 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)) { effectsToDraw[i++] = e; @@ -91,7 +99,7 @@ static void resizeDrawList(void) n = drawCapacity + INITIAL_EFFECT_DRAW_CAPACITY; - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Resizing effect draw capacity: %d -> %d\n", drawCapacity, n); + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing effect draw capacity: %d -> %d\n", drawCapacity, n); effects = malloc(sizeof(Effect*) * n); memset(effects, 0, sizeof(Effect*) * n); diff --git a/src/battle/effects.h b/src/battle/effects.h index a938b6c..cd6db9f 100644 --- a/src/battle/effects.h +++ b/src/battle/effects.h @@ -20,11 +20,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../common.h" -#define INITIAL_EFFECTS_TO_DRAW 128 +#define INITIAL_EFFECT_DRAW_CAPACITY 128 extern void blitScaled(SDL_Texture *texture, int x, int y, int w, int h); 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 App app; extern Battle battle; diff --git a/src/system/io.c b/src/system/io.c index 4096232..97847fd 100644 --- a/src/system/io.c +++ b/src/system/io.c @@ -99,6 +99,9 @@ char **getFileList(char *dir, int *count) struct dirent *ent; char **filenames; + i = 0; + filenames = NULL; + if ((d = opendir(dir)) != NULL) { while ((ent = readdir(d)) != NULL) @@ -136,7 +139,10 @@ char **getFileList(char *dir, int *count) *count = i; - qsort(filenames, i, sizeof(char*), stringComparator); + if (filenames) + { + qsort(filenames, i, sizeof(char*), stringComparator); + } return filenames; }