Ensure that sprite frames don't overrun array bounds.

This commit is contained in:
Steve 2018-02-24 15:59:26 +00:00
parent 891d551179
commit 9c279da3a2
7 changed files with 49 additions and 13 deletions

View File

@ -645,17 +645,30 @@ static void die(void)
static SDL_Rect *getCurrentSprite(void) static SDL_Rect *getCurrentSprite(void)
{ {
if (world.bob->alive == ALIVE_ALIVE && world.bob->stunTimer <= 0) Sprite *s;
s = (world.bob->alive == ALIVE_ALIVE && world.bob->stunTimer <= 0) ? world.bob->sprite[world.bob->facing] : world.bob->sprite[FACING_DIE];
{ {
return &world.bob->sprite[world.bob->facing]->frames[world.bob->spriteFrame]->rect; return &world.bob->sprite[world.bob->facing]->frames[world.bob->spriteFrame]->rect;
} }
return &world.bob->sprite[FACING_DIE]->frames[world.bob->spriteFrame]->rect; if (world.bob->spriteFrame >= s->numFrames)
{
world.bob->spriteFrame = 0;
}
return &s->frames[world.bob->spriteFrame]->rect;
} }
static void animate(void) static void animate(void)
{ {
if (world.bob->dx != 0 || world.bob->stunTimer > 0 || world.bob->flags & EF_WEIGHTLESS || world.bob->health <= 0) if (world.bob->stunTimer > 0 || world.bob->alive != ALIVE_ALIVE)
{
world.bob->facing = FACING_DIE;
superAnimate();
}
else if (world.bob->dx != 0 || world.bob->flags & EF_WEIGHTLESS)
{ {
superAnimate(); superAnimate();
} }

View File

@ -160,9 +160,22 @@ static void changeEnvironment(void)
{ {
} }
void entityIdle(void)
{
}
static SDL_Rect *getCurrentSprite(void) static SDL_Rect *getCurrentSprite(void)
{ {
return &self->sprite[self->facing]->frames[self->spriteFrame]->rect; Sprite *s;
s = self->sprite[self->facing];
if (self->spriteFrame >= s->numFrames)
{
self->spriteFrame = 0;
}
return &s->frames[self->spriteFrame]->rect;
} }
static void load(cJSON *root) static void load(cJSON *root)

View File

@ -312,7 +312,13 @@ static void die(void)
static void animate(void) static void animate(void)
{ {
if (self->dx != 0 || self->health <= 0) if (self->alive != ALIVE_ALIVE)
{
self->facing = FACING_DIE;
superAnimate();
}
else if (self->dx != 0)
{ {
superAnimate(); superAnimate();
} }

View File

@ -24,14 +24,14 @@ extern void dropCarriedItem(void);
extern int getDistance(int x1, int y1, int x2, int y2); extern int getDistance(int x1, int y1, int x2, int y2);
extern double randF(void); extern double randF(void);
extern void throwFleshChunks(float x, float y, int amount); extern void throwFleshChunks(float x, float y, int amount);
extern void addRandomWeapon(float x, float y); extern void addRandomWeapon(int x, int y);
extern float limit(float i, float a, float b); extern float limit(float i, float a, float b);
extern void playSound(int snd, int ch); extern void playSound(int snd, int ch);
extern void addBloodDecal(int x, int y); extern void addBloodDecal(int x, int y);
extern void updateObjective(char *targetName); extern void updateObjective(char *targetName);
extern int enemyCanSeePlayer(Entity *e); extern int enemyCanSeePlayer(Entity *e);
extern void fireTriggers(char *name); extern void fireTriggers(char *name);
extern void addRandomItems(float x, float y); extern void addRandomItems(int x, int y);
extern int rrnd(int low, int high); extern int rrnd(int low, int high);
extern Unit *createUnit(void); extern Unit *createUnit(void);

View File

@ -85,7 +85,7 @@ static void touch(Entity *other)
addRandomWeapon(u->x, u->y); addRandomWeapon(u->x, u->y);
} }
addRandomItems((int) u->x, (int) u->y); addRandomItems(u->x, u->y);
updateObjective(u->name); updateObjective(u->name);
updateObjective("ENEMY"); updateObjective("ENEMY");

View File

@ -23,13 +23,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern void dropCarriedItem(void); extern void dropCarriedItem(void);
extern int getDistance(int x1, int y1, int x2, int y2); extern int getDistance(int x1, int y1, int x2, int y2);
extern double randF(void); extern double randF(void);
extern void addRandomWeapon(float x, float y); extern void addRandomWeapon(int x, int y);
extern float limit(float i, float a, float b); extern float limit(float i, float a, float b);
extern void playSound(int snd, int ch); extern void playSound(int snd, int ch);
extern void updateObjective(char *targetName); extern void updateObjective(char *targetName);
extern int enemyCanSeePlayer(Entity *e); extern int enemyCanSeePlayer(Entity *e);
extern void fireTriggers(char *name); extern void fireTriggers(char *name);
extern void addRandomItems(float x, float y); extern void addRandomItems(int x, int y);
extern int rrnd(int low, int high); extern int rrnd(int low, int high);
extern void addExplosion(float x, float y, int radius, Entity *owner); extern void addExplosion(float x, float y, int radius, Entity *owner);
extern void throwDebris(float x, float y, int amount); extern void throwDebris(float x, float y, int amount);

View File

@ -284,12 +284,16 @@ static int canFire(Entity *target)
static SDL_Rect *getCurrentSprite(void) static SDL_Rect *getCurrentSprite(void)
{ {
if (self->alive == ALIVE_ALIVE) Sprite *s;
s = (self->alive == ALIVE_ALIVE) ? self->sprite[self->facing] : self->sprite[FACING_DIE];
if (self->spriteFrame >= s->numFrames)
{ {
return &self->sprite[self->facing]->frames[self->spriteFrame]->rect; self->spriteFrame = 0;
} }
return &self->sprite[FACING_DIE]->frames[self->spriteFrame]->rect; return &s->frames[self->spriteFrame]->rect;
} }
static void load(cJSON *root) static void load(cJSON *root)