Start of bullet, debris, and effect performance updates. Dynamic array sizing.

This commit is contained in:
Sweeney 2016-02-24 15:02:51 +00:00
parent ba41d615a2
commit 56540a56ac
7 changed files with 397 additions and 275 deletions

View File

@ -429,4 +429,10 @@ void destroyBattle(void)
destroyScript();
destroyQuadtree();
destroyDebris();
destroyBullets();
destroyEffects();
}

View File

@ -22,16 +22,21 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void huntTarget(Bullet *b);
static void checkCollisions(Bullet *b);
static void resizeDrawList(void);
static Bullet bulletDef[BT_MAX];
static Bullet *bulletsToDraw[MAX_BULLETS_TO_DRAW];
static Bullet **bulletsToDraw;
static int incomingMissile;
static int drawCapacity;
void initBullets(void)
{
incomingMissile = 0;
memset(bulletsToDraw, 0, sizeof(Bullet*) * MAX_BULLETS_TO_DRAW);
drawCapacity = INITIAL_BULLET_DRAW_CAPACITY;
bulletsToDraw = malloc(sizeof(Bullet*) * drawCapacity);
memset(bulletsToDraw, 0, sizeof(Bullet*) * drawCapacity);
}
void initBulletDefs(void)
@ -119,10 +124,10 @@ void doBullets(void)
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))
{
bulletsToDraw[i++] = b;
if (i >= MAX_BULLETS_TO_DRAW)
if (i == drawCapacity)
{
printf("Too many bullets to draw\n");
exit(1);
resizeDrawList();
}
}
}
@ -131,6 +136,29 @@ void doBullets(void)
}
}
static void resizeDrawList(void)
{
int i, n;
Bullet **bullets;
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);
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;
drawCapacity = n;
}
static void checkCollisions(Bullet *b)
{
Entity *e, **candidates;
@ -380,4 +408,7 @@ void fireMissile(Entity *owner)
void destroyBulletDefs(void)
{
free(bulletsToDraw);
bulletsToDraw = NULL;
}

View File

@ -24,7 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define TURN_SPEED 2
#define TURN_THRESHOLD 3
#define MAX_BULLETS_TO_DRAW 512
#define INITIAL_BULLET_DRAW_CAPACITY 32
extern SDL_Texture *getTexture(char *filename);
extern void blitRotated(SDL_Texture *texture, int x, int y, float angle);

View File

@ -21,20 +21,25 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "debris.h"
static void changeCourse(Debris *d);
static void resizeDrawList(void);
static Debris *debrisToDraw[MAX_DEBRIS_TO_DRAW];
static Debris **debrisToDraw;
static SDL_Texture *debrisTexture[MAX_DEBRIS_TEXTURES];
static int drawCapacity;
void initDebris(void)
{
memset(debrisToDraw, 0, sizeof(Debris*) * MAX_DEBRIS_TO_DRAW);
debrisTexture[0] = getTexture("gfx/debris/debris1.png");
debrisTexture[1] = getTexture("gfx/debris/debris2.png");
debrisTexture[2] = getTexture("gfx/debris/debris3.png");
debrisTexture[3] = getTexture("gfx/debris/debris4.png");
debrisTexture[4] = getTexture("gfx/debris/debris5.png");
debrisTexture[5] = getTexture("gfx/debris/debris6.png");
drawCapacity = INITIAL_BULLET_DRAW_CAPACITY;
debrisToDraw = malloc(sizeof(Bullet*) * drawCapacity);
memset(debrisToDraw, 0, sizeof(Bullet*) * drawCapacity);
}
void addDebris(int x, int y, int amount)
@ -102,10 +107,10 @@ void doDebris(void)
if (collision(d->x - 16 - battle.camera.x, d->y - 16 - battle.camera.y, 32, 32, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
{
debrisToDraw[i++] = d;
if (i >= MAX_DEBRIS_TO_DRAW)
if (i == drawCapacity)
{
printf("Too much debris to draw\n");
exit(1);
resizeDrawList();
}
}
}
@ -114,6 +119,29 @@ void doDebris(void)
}
}
static void resizeDrawList(void)
{
int i, n;
Debris **debris;
n = drawCapacity + INITIAL_BULLET_DRAW_CAPACITY;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "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;
drawCapacity = n;
}
static void changeCourse(Debris *d)
{
float dir;
@ -139,3 +167,10 @@ void drawDebris(void)
blitRotated(d->texture, d->x - battle.camera.x, d->y - battle.camera.y, d->angle);
}
}
void destroyDebris(void)
{
free(debrisToDraw);
debrisToDraw = NULL;
}

View File

@ -22,7 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../json/cJSON.h"
#define MAX_DEBRIS_TO_DRAW 512
#define INITIAL_DEBRIS_DRAW_CAPACITY 32
#define MAX_DEBRIS_TEXTURES 6
extern float mod(float n, float x);

View File

@ -26,12 +26,17 @@ static void setRandomShieldHue(Effect *e);
static SDL_Texture *explosionTexture;
static SDL_Texture *shieldHitTexture;
static SDL_Texture *haloTexture;
static Effect **effectsToDraw;
void initEffects(void)
{
explosionTexture = getTexture("gfx/effects/explosion.png");
shieldHitTexture = getTexture("gfx/effects/shieldHit.png");
haloTexture = getTexture("gfx/effects/halo.png");
drawCapacity = INITIAL_EFFECTS_TO_DRAW;
effectsToDraw = malloc(sizeof(Effect*) * drawCapacity);
}
void doEffects(void)
@ -62,18 +67,54 @@ void doEffects(void)
free(e);
e = prev;
}
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))
{
effectsToDraw[i++] = e;
if (i == drawCapacity)
{
resizeDrawList();
}
}
}
prev = e;
}
}
static void resizeDrawList(void)
{
int i, n;
Effect **effects;
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);
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;
drawCapacity = n;
}
void drawEffects(void)
{
int i;
Effect *e;
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND);
for (e = battle.effectHead.next ; e != NULL ; e = e->next)
for (i = 0, e = effectsToDraw[i] ; e != NULL ; e = effectsToDraw[++i])
{
SDL_SetRenderDrawColor(app.renderer, e->r, e->g, e->b, e->a);
@ -478,3 +519,10 @@ static void setRandomShieldHue(Effect *e)
break;
}
}
void destroyEffects(void)
{
free(effectsToDraw);
effectsToDraw = NULL;
}

View File

@ -20,6 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h"
#define INITIAL_EFFECTS_TO_DRAW 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);