Added array resize function (not using realloc because it doesn't zero the new array).
This commit is contained in:
parent
ac59559d90
commit
e28390a496
|
@ -138,24 +138,14 @@ void doBullets(void)
|
||||||
|
|
||||||
static void resizeDrawList(void)
|
static void resizeDrawList(void)
|
||||||
{
|
{
|
||||||
int i, n;
|
int n;
|
||||||
Bullet **bullets;
|
|
||||||
|
|
||||||
n = drawCapacity + INITIAL_BULLET_DRAW_CAPACITY;
|
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);
|
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing bullet draw capacity: %d -> %d\n", drawCapacity, n);
|
||||||
|
|
||||||
bullets = malloc(sizeof(Bullet*) * n);
|
bulletsToDraw = resize(bulletsToDraw, sizeof(Bullet*) * drawCapacity, sizeof(Bullet*) * n);
|
||||||
memset(bullets, 0, sizeof(Bullet*) * n);
|
|
||||||
|
|
||||||
for (i = 0 ; i < drawCapacity ; i++)
|
|
||||||
{
|
|
||||||
bullets[i] = bulletsToDraw[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
free(bulletsToDraw);
|
|
||||||
|
|
||||||
bulletsToDraw = bullets;
|
|
||||||
drawCapacity = n;
|
drawCapacity = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...);
|
||||||
extern void playSound(int id);
|
extern void playSound(int id);
|
||||||
extern char *getTranslatedString(char *string);
|
extern char *getTranslatedString(char *string);
|
||||||
|
extern void *resize(void *array, int oldSize, int newSize);
|
||||||
|
|
||||||
extern Battle battle;
|
extern Battle battle;
|
||||||
extern Colors colors;
|
extern Colors colors;
|
||||||
|
|
|
@ -121,24 +121,13 @@ void doDebris(void)
|
||||||
|
|
||||||
static void resizeDrawList(void)
|
static void resizeDrawList(void)
|
||||||
{
|
{
|
||||||
int i, n;
|
int n;
|
||||||
Debris **debris;
|
|
||||||
|
|
||||||
n = drawCapacity + INITIAL_DEBRIS_DRAW_CAPACITY;
|
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);
|
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing debris draw capacity: %d -> %d\n", drawCapacity, n);
|
||||||
|
|
||||||
debris = malloc(sizeof(Debris*) * n);
|
debrisToDraw = resize(debrisToDraw, sizeof(Debris*) * drawCapacity, sizeof(Debris*) * n);
|
||||||
memset(debris, 0, sizeof(Debris*) * n);
|
|
||||||
|
|
||||||
for (i = 0 ; i < drawCapacity ; i++)
|
|
||||||
{
|
|
||||||
debris[i] = debrisToDraw[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
free(debrisToDraw);
|
|
||||||
|
|
||||||
debrisToDraw = debris;
|
|
||||||
drawCapacity = n;
|
drawCapacity = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 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 SDL_Texture *getTexture(char *filename);
|
||||||
extern void addDebrisFire(int x, int y);
|
extern void addDebrisFire(int x, int y);
|
||||||
|
extern void *resize(void *array, int oldSize, int newSize);
|
||||||
|
|
||||||
extern Battle battle;
|
extern Battle battle;
|
||||||
|
|
|
@ -117,24 +117,14 @@ static int pointOnScreen(float x, float y)
|
||||||
|
|
||||||
static void resizeDrawList(void)
|
static void resizeDrawList(void)
|
||||||
{
|
{
|
||||||
int i, n;
|
int n;
|
||||||
Effect **effects;
|
|
||||||
|
|
||||||
n = drawCapacity + INITIAL_EFFECT_DRAW_CAPACITY;
|
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);
|
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing effect draw capacity: %d -> %d\n", drawCapacity, n);
|
||||||
|
|
||||||
effects = malloc(sizeof(Effect*) * n);
|
effectsToDraw = resize(effectsToDraw, sizeof(Effect*) * drawCapacity, sizeof(Effect*) * n);
|
||||||
memset(effects, 0, sizeof(Effect*) * n);
|
|
||||||
|
|
||||||
for (i = 0 ; i < drawCapacity ; i++)
|
|
||||||
{
|
|
||||||
effects[i] = effectsToDraw[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
free(effectsToDraw);
|
|
||||||
|
|
||||||
effectsToDraw = effects;
|
|
||||||
drawCapacity = n;
|
drawCapacity = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 SDL_Texture *getTexture(char *name);
|
||||||
extern void blit(SDL_Texture *t, int x, int y, int center);
|
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 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 App app;
|
||||||
extern Battle battle;
|
extern Battle battle;
|
||||||
|
|
|
@ -133,24 +133,13 @@ void addToQuadtree(Entity *e, Quadtree *root)
|
||||||
|
|
||||||
static void resizeQTEntCapacity(Quadtree *root)
|
static void resizeQTEntCapacity(Quadtree *root)
|
||||||
{
|
{
|
||||||
int i, n;
|
int n;
|
||||||
Entity **ents;
|
|
||||||
|
|
||||||
n = root->capacity + QT_INITIAL_CAPACITY;
|
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);
|
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing QT node: %d -> %d\n", root->capacity, n);
|
||||||
|
|
||||||
ents = malloc(sizeof(Entity*) * n);
|
root->ents = resize(root->ents, sizeof(Entity*) * root->capacity, 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->capacity = 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)
|
static void resizeCandidates(void)
|
||||||
{
|
{
|
||||||
int i, n;
|
int n;
|
||||||
Entity **ents;
|
|
||||||
|
|
||||||
n = cCapacity + QT_INITIAL_CAPACITY;
|
n = cCapacity + QT_INITIAL_CAPACITY;
|
||||||
|
|
||||||
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing candidates: %d -> %d\n", cCapacity, n);
|
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing candidates: %d -> %d\n", cCapacity, n);
|
||||||
|
|
||||||
ents = malloc(sizeof(Entity*) * n);
|
candidates = resize(candidates, sizeof(Entity*) * cCapacity, sizeof(Entity*) * n);
|
||||||
memset(ents, 0, sizeof(Entity*) * n);
|
|
||||||
|
|
||||||
for (i = 0 ; i < cCapacity ; i++)
|
|
||||||
{
|
|
||||||
ents[i] = candidates[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
free(candidates);
|
|
||||||
|
|
||||||
candidates = ents;
|
|
||||||
cCapacity = n;
|
cCapacity = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,4 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
#define QT_MAX_DEPTH 6
|
#define QT_MAX_DEPTH 6
|
||||||
#define QT_INITIAL_CAPACITY 8
|
#define QT_INITIAL_CAPACITY 8
|
||||||
|
|
||||||
|
extern void *resize(void *array, int oldSize, int newSize);
|
||||||
|
|
||||||
extern Battle battle;
|
extern Battle battle;
|
||||||
|
|
|
@ -129,3 +129,15 @@ int getJSONValue(cJSON *node, char *name, int defValue)
|
||||||
|
|
||||||
return 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;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue