diff --git a/src/battle/bullets.c b/src/battle/bullets.c index 0264cba..d7e233f 100644 --- a/src/battle/bullets.c +++ b/src/battle/bullets.c @@ -138,24 +138,14 @@ void doBullets(void) static void resizeDrawList(void) { - int i, n; - Bullet **bullets; + int n; n = drawCapacity + INITIAL_BULLET_DRAW_CAPACITY; 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); - - for (i = 0 ; i < drawCapacity ; i++) - { - bullets[i] = bulletsToDraw[i]; - } - - free(bulletsToDraw); - - bulletsToDraw = bullets; + bulletsToDraw = resize(bulletsToDraw, sizeof(Bullet*) * drawCapacity, sizeof(Bullet*) * n); + drawCapacity = n; } diff --git a/src/battle/bullets.h b/src/battle/bullets.h index a5934c3..e859ad6 100644 --- a/src/battle/bullets.h +++ b/src/battle/bullets.h @@ -42,6 +42,7 @@ extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); extern void playSound(int id); extern char *getTranslatedString(char *string); +extern void *resize(void *array, int oldSize, int newSize); extern Battle battle; extern Colors colors; diff --git a/src/battle/debris.c b/src/battle/debris.c index 55206c3..25b1103 100644 --- a/src/battle/debris.c +++ b/src/battle/debris.c @@ -121,24 +121,13 @@ void doDebris(void) static void resizeDrawList(void) { - int i, n; - Debris **debris; + int n; n = drawCapacity + INITIAL_DEBRIS_DRAW_CAPACITY; 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); - - for (i = 0 ; i < drawCapacity ; i++) - { - debris[i] = debrisToDraw[i]; - } - - free(debrisToDraw); - - debrisToDraw = debris; + debrisToDraw = resize(debrisToDraw, sizeof(Debris*) * drawCapacity, sizeof(Debris*) * n); drawCapacity = n; } diff --git a/src/battle/debris.h b/src/battle/debris.h index 2ab0432..d6cddf1 100644 --- a/src/battle/debris.h +++ b/src/battle/debris.h @@ -30,5 +30,6 @@ extern void blitRotated(SDL_Texture *texture, int x, int y, float angle); extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2); extern SDL_Texture *getTexture(char *filename); extern void addDebrisFire(int x, int y); +extern void *resize(void *array, int oldSize, int newSize); extern Battle battle; diff --git a/src/battle/effects.c b/src/battle/effects.c index 4858bb9..29f1152 100644 --- a/src/battle/effects.c +++ b/src/battle/effects.c @@ -117,24 +117,14 @@ static int pointOnScreen(float x, float y) static void resizeDrawList(void) { - int i, n; - Effect **effects; + int n; n = drawCapacity + INITIAL_EFFECT_DRAW_CAPACITY; 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); - - for (i = 0 ; i < drawCapacity ; i++) - { - effects[i] = effectsToDraw[i]; - } - - free(effectsToDraw); - - effectsToDraw = effects; + effectsToDraw = resize(effectsToDraw, sizeof(Effect*) * drawCapacity, sizeof(Effect*) * n); + drawCapacity = n; } diff --git a/src/battle/effects.h b/src/battle/effects.h index cd6db9f..485d530 100644 --- a/src/battle/effects.h +++ b/src/battle/effects.h @@ -26,6 +26,7 @@ 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 void *resize(void *array, int oldSize, int newSize); extern App app; extern Battle battle; diff --git a/src/battle/quadtree.c b/src/battle/quadtree.c index 7f34218..303c96a 100644 --- a/src/battle/quadtree.c +++ b/src/battle/quadtree.c @@ -133,24 +133,13 @@ void addToQuadtree(Entity *e, Quadtree *root) static void resizeQTEntCapacity(Quadtree *root) { - int i, n; - Entity **ents; + int n; n = root->capacity + QT_INITIAL_CAPACITY; SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing QT node: %d -> %d\n", root->capacity, n); - ents = malloc(sizeof(Entity*) * n); - memset(ents, 0, sizeof(Entity*) * n); - - for (i = 0 ; i < root->capacity ; i++) - { - ents[i] = root->ents[i]; - } - - free(root->ents); - - root->ents = ents; + root->ents = resize(root->ents, sizeof(Entity*) * root->capacity, sizeof(Entity*) * n); root->capacity = n; } @@ -274,24 +263,13 @@ static void getAllEntsWithinNode(int x, int y, int w, int h, Entity *ignore, Qua static void resizeCandidates(void) { - int i, n; - Entity **ents; + int n; n = cCapacity + QT_INITIAL_CAPACITY; SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing candidates: %d -> %d\n", cCapacity, n); - ents = malloc(sizeof(Entity*) * n); - memset(ents, 0, sizeof(Entity*) * n); - - for (i = 0 ; i < cCapacity ; i++) - { - ents[i] = candidates[i]; - } - - free(candidates); - - candidates = ents; + candidates = resize(candidates, sizeof(Entity*) * cCapacity, sizeof(Entity*) * n); cCapacity = n; } diff --git a/src/battle/quadtree.h b/src/battle/quadtree.h index c4552cd..fef2e03 100644 --- a/src/battle/quadtree.h +++ b/src/battle/quadtree.h @@ -23,4 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define QT_MAX_DEPTH 6 #define QT_INITIAL_CAPACITY 8 +extern void *resize(void *array, int oldSize, int newSize); + extern Battle battle; diff --git a/src/system/util.c b/src/system/util.c index 949d779..35d4e12 100644 --- a/src/system/util.c +++ b/src/system/util.c @@ -129,3 +129,15 @@ int getJSONValue(cJSON *node, char *name, int defValue) return defValue; } + +void *resize(void *array, int oldSize, int newSize) +{ + void **newArray; + + newArray = malloc(newSize); + memset(newArray, 0, newSize); + memcpy(newArray, array, oldSize); + free(array); + + return newArray; +}