• Create Account

## Crashes When Character Walks

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1LeftyGuitar  Members

Posted 12 August 2014 - 02:57 PM

Hello,

Whenever I try to move my character using the keyboard, it crashes after a few second. I'll post the code and where it says the crash is coming from.

gPlayer.XPos = MAX_WIDTH / 2;
gPlayer.YPos = MAX_HEIGHT / 2;
gPlayer.XVel = 0.010f;
gPlayer.YVel = 0.010f;
gPlayer.CurrentFrame = 1;

for(int i = 0; i <= 8; i++)
{
gPlayer.LeftRect[i].x = gPlayer.XPos;
gPlayer.LeftRect[i].y = gPlayer.YPos;
gPlayer.LeftRect[i].w = gPlayer.Width;
gPlayer.LeftRect[i].h = gPlayer.Height;
}

else if(event.key.keysym.sym == SDLK_LEFT)
{
gPlayer.Facing = gPlayer.LEFT;
gPlayer.CurrentState = gPlayer.WALK_LEFT;
}

else if(event.key.keysym.sym == SDLK_LEFT)
{
gPlayer.CurrentState = gPlayer.IDLE;
gPlayer.Facing = gPlayer.LEFT;
gPlayer.XVel = 0.0f;
}

if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
{
gPlayer.LeftRect[gPlayer.CurrentFrame].x -= 3;
}

if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
{
SDL_RenderCopy(GameRend,gPlayer.LeftTex[gPlayer.CurrentFrame],NULL,&gPlayer.LeftRect[gPlayer.CurrentFrame]);

gPlayer.CurrentFrame++; //This is where the debugger points to when it crashes
gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;
}


I've only posted bits and pieces of my code. I can post more if it is needed. My problem is it runs fine when first executed, but when I try to move the character, it shows the animation play, then crashes after a couple of seconds. NOTE: There are 8 animations for the character walking.

Edited by LeftyGuitar, 12 August 2014 - 02:58 PM.

### #2DrEvil  Members

Posted 12 August 2014 - 03:29 PM

How big is your leftrect array?

<= 8 looks suspicious

### #3DrEvil  Members

Posted 12 August 2014 - 03:30 PM

Also seems likely you AR accessing your arrays out of bounds with current frame too

### #4ApochPiQ  Moderators

Posted 12 August 2014 - 03:34 PM

POPULAR

Wielder of the Sacred Wands

### #5LeftyGuitar  Members

Posted 12 August 2014 - 03:42 PM

The link on the what the debugger numbers mean is down. Also, The LeftRect array is 8. I tried changing it to 9, but still nothing. Here is what the debugger error message says

First-chance exception at 0x6C778E1B (SDL2.dll) in KnightsReverie.exe: 0xC0000005: Access violation reading location 0x00000200.
Unhandled exception at 0x6C778E1B (SDL2.dll) in KnightsReverie.exe: 0xC0000005: Access violation reading location 0x00000200.

I tried looking those up, but I couldn't really find anything.

	for(int i = 0; i == 8; i++)
{
gPlayer.LeftRect[i].x = gPlayer.XPos;
gPlayer.LeftRect[i].y = gPlayer.YPos;
gPlayer.LeftRect[i].w = gPlayer.Width;
gPlayer.LeftRect[i].h = gPlayer.Height;
}


I also tried this, and still nothing. The idle animations play fine, but when I try to move the character with walking animations, it crashes.

### #6DrEvil  Members

Posted 12 August 2014 - 04:47 PM

What's the value of gPlayer.CurrentFrame when it crashes? Go over the values of all your variables in the debugger when it bombs and you should get a hint.

### #7LeftyGuitar  Members

Posted 12 August 2014 - 06:19 PM

I tried setting a watch on the variable, but it says unrecognized token for gPlayer.CurrentFrame. I also noticed these two errors   SDL2.dll!6c778e1b()    Unknown and      [Frames below may be incorrect and/or missing, no symbols loaded for SDL2.dll]

I also added this to code, but it dosen't seem to help.

	if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
{
SDL_RenderCopy(GameRend,gPlayer.LeftTex[gPlayer.CurrentFrame],NULL,&gPlayer.LeftRect[gPlayer.CurrentFrame]);

gPlayer.CurrentFrame++;
gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;

if(gPlayer.CurrentFrame >= 8)
{
gPlayer.CurrentFrame = 1;
}
}


I think I am getting closer to fixing the problem.

Edited by LeftyGuitar, 12 August 2014 - 06:21 PM.

### #8ApochPiQ  Moderators

Posted 12 August 2014 - 06:26 PM

This crash is inside SDL, not your code. You're probably misreading the debugger's indicator (it shows the next line that will execute, not the line that caused the crash). I would verify that you're not passing something bogus to SDL_RenderCopy.
Wielder of the Sacred Wands

### #9LeftyGuitar  Members

Posted 12 August 2014 - 06:53 PM

	if(gPlayer.Facing == gPlayer.RIGHT)
{
SDL_RenderCopyEx(GameRend,gPlayer.IdleSideTex[gPlayer.CurrentFrame],NULL,&gPlayer.IdleSideRect[gPlayer.CurrentFrame],NULL,NULL,SDL_FLIP_HORIZONTAL); //Could it be something to do with this?

gPlayer.CurrentFrame++;
gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 9;
}

if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
{
SDL_RenderCopy(GameRend,gPlayer.LeftTex[gPlayer.CurrentFrame],NULL,&gPlayer.LeftRect[gPlayer.CurrentFrame]); //Or this?

gPlayer.CurrentFrame++;
gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;

if(gPlayer.CurrentFrame >= 8)
{
gPlayer.CurrentFrame = 1;
}
}


I looked over my SDL_RenderCopy(s), and I didn't notice anything bogus. Everything inside those calls has values, which can and are being used. One is RenderCopyEx, to flip the image when needed. Should I make them all RenderCopyEx(s)? I doubled check, and the images for walking is only 8 frames. However for standing idle, left or right, there is 9 images.

### #10Lactose  GDNet+

Posted 13 August 2014 - 04:50 AM

Print the value of gPlayer.CurrentFrame (right before each RenderCopy call) so you can see in a log/output window what the last value is.

You could also hover the variable when the application crashes, to see its value. This might be IDE specific, but the ones I know about support this.

You're only posting snippets, which makes it impossible to guess the overall flow of the thing, but if the last code you posted is directly copied, I think i see the reason for it, plus some other issues:

gPlayer.CurrentFrame++;
gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8; //Also in the facing code, with the % 9


You first increment the frame by one, but this is a complete waste. The very next line you set the variable directly, overwriting the increment operation.

gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;
if (gPlayer.CurrentFrame >= 8)
// ...


If I'm reading this correctly (I can't recall the operator precedence rules here), this if will never trigger.

I'm reading it as equivalent to (SDL_GetTicks() / 150) % 8, which means that the result will always be between (and including) 0 and 7. Your if check checks for 8 and higher, which is never true.

if (gPlayer.Facing == gPlayer.RIGHT)
//stuff
if (gPlayer.CurrentState == gPlayer.WALK_LEFT)
//Stuff that crashes


I'm assuming you also have a facing == gPlayer.LEFT somewhere. If you actually execute the facing (idle?) drawing always, and the overwrite with the walking animation, this will result in out of bounds reading.

The % 9 in the facing/idle code will make gPlayer.CurrentFrame be between (and including) 0 and 8. If it's set to 8, and then the walking animation code is run directly after, you will be indexing into your 8 large array (with max valid index = 7) with the invalid index

--> Crash, access violation.

Hello to all my stalkers.

### #11LeftyGuitar  Members

Posted 13 August 2014 - 12:37 PM

When I hover over the gPlayer.CurrentFrame, it says gPlayer.CurrentFrame = 0x00000008. I assume this means, its already set the CurrentFrame to 8?

gPlayer.XPos = MAX_WIDTH / 2;
gPlayer.YPos = MAX_HEIGHT / 2;
gPlayer.XVel = 0.010f;
gPlayer.YVel = 0.010f;
gPlayer.CurrentFrame = 1;
gPlayer.TotalFrames = 8;
gPlayer.Width = 48;
gPlayer.Height = 63;
gPlayer.LastFrame = gPlayer.CurrentFrame;
gPlayer.CurrentState = gPlayer.IDLE;
gPlayer.Facing = gPlayer.DOWN;

gPlayer.LeftTex[0] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[0]);
gPlayer.LeftTex[1] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[1]);
gPlayer.LeftTex[2] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[2]);
gPlayer.LeftTex[3] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[3]);
gPlayer.LeftTex[4] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[4]);
gPlayer.LeftTex[5] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[5]);
gPlayer.LeftTex[6] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[6]);
gPlayer.LeftTex[7] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[7]);

for(int i = 0; i <= 8; i++)
{

gPlayer.LeftRect[i].x = gPlayer.XPos;
gPlayer.LeftRect[i].y = gPlayer.YPos;
gPlayer.LeftRect[i].w = gPlayer.Width;
gPlayer.LeftRect[i].h = gPlayer.Height;
}

void RunGame()
{
while(GameRunning)
{
ThisTime = SDL_GetTicks();
DeltaTime = (float)(ThisTime - LastTime) / MAX_TICKS;
LastTime = ThisTime;

while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
GameRunning = false;
}

if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
{
GameRunning = false;
}

if(event.key.keysym.sym == SDLK_UP)
{
gPlayer.Facing = gPlayer.UP;
}
else if(event.key.keysym.sym == SDLK_DOWN)
{
gPlayer.Facing = gPlayer.DOWN;
}
else if(event.key.keysym.sym == SDLK_LEFT)
{
gPlayer.Facing = gPlayer.LEFT;
gPlayer.CurrentState = gPlayer.WALK_LEFT;
}
else if(event.key.keysym.sym == SDLK_RIGHT)
{
gPlayer.Facing = gPlayer.RIGHT;
}
}

if(event.type == SDL_KEYUP)
{
if(event.key.keysym.sym == SDLK_UP)
{
gPlayer.YVel = 0.0f;
}
else if(event.key.keysym.sym == SDLK_DOWN)
{
gPlayer.YVel = 0.0f;
}
else if(event.key.keysym.sym == SDLK_LEFT)
{
gPlayer.CurrentState = gPlayer.IDLE;
gPlayer.Facing = gPlayer.LEFT;
gPlayer.XVel = 0.0f;
}
else if(event.key.keysym.sym == SDLK_RIGHT)
{
gPlayer.XVel = 0.0f;
}
}
}

UpdateGame();

DrawGame();
}
}

void UpdateGame()
{
if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
{
gPlayer.LeftRect[gPlayer.CurrentFrame].x -= gPlayer.XVel * DeltaTime;
}
}

void DrawGame()
{
SDL_RenderClear(GameRend);

SDL_RenderCopy(GameRend,BackdropTex,NULL,&BackdropRect);

if(gPlayer.Facing == gPlayer.DOWN)
{
SDL_RenderCopy(GameRend,gPlayer.IdleFrontTex[gPlayer.CurrentFrame],NULL,&gPlayer.IdleFrontRect[gPlayer.CurrentFrame]);

gPlayer.CurrentFrame++;
gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;
}

if(gPlayer.Facing == gPlayer.UP)
{
SDL_RenderCopy(GameRend,gPlayer.IdleBackTex[gPlayer.CurrentFrame],NULL,&gPlayer.IdleBackRect[gPlayer.CurrentFrame]);

gPlayer.CurrentFrame++;
gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;
}

if(gPlayer.Facing == gPlayer.LEFT)
{
SDL_RenderCopy(GameRend,gPlayer.IdleSideTex[gPlayer.CurrentFrame],NULL,&gPlayer.IdleSideRect[gPlayer.CurrentFrame]);

gPlayer.CurrentFrame++;
gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 9;
}

if(gPlayer.Facing == gPlayer.RIGHT)
{
SDL_RenderCopyEx(GameRend,gPlayer.IdleSideTex[gPlayer.CurrentFrame],NULL,&gPlayer.IdleSideRect[gPlayer.CurrentFrame],NULL,NULL,SDL_FLIP_HORIZONTAL);

gPlayer.CurrentFrame++;
gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 9;
}

if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
{
SDL_RenderCopy(GameRend,gPlayer.LeftTex[gPlayer.CurrentFrame],NULL,&gPlayer.LeftRect[gPlayer.CurrentFrame]);

gPlayer.CurrentFrame++;
gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;

if(gPlayer.CurrentFrame >= 8)
{
gPlayer.CurrentFrame = 1;
}
}

SDL_RenderPresent(GameRend);
}


I have posted a rather large chunk of the code. Maybe now I can get closer to solving the problem.

### #12Lactose  GDNet+

Posted 13 August 2014 - 12:50 PM

gPlayer.CurrentFrame = 0x00000008 means gPlayer.CurrentFrame is equal to hexadecimal (hex) 8. Hex 8 is equal to 8 in our normal counting system as well. You might want to brush up on hexadecimal numbers if this is new to you. You might also have the option to turn off hex display of values in your IDE, which can make some values easier to read (although some numbers are easier to read in hex in certain contexts).

This means, as I guessed, that you're indexing into the array which has size 8 (and maximum index 7) with the invalid index 8.

The cause is also what I guessed, the appropriate "facing/idle" if checks are always executed, even when the player is moving. This causes the gPlayer.CurrentFrame value to increment (although in a strange matter, which I also touched on in the previous post), up to 8.

After the facing/idle checks, you do the movement anim checks. This part will simply explode if gPlayer.CurrentFrame is 8, and as we see above, it clearly can be.

A simple way to verify (verifying is not fixing) this would be to put the gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8 (or 9) stuff directly before the SDL_RenderCopy calls, forcing the gPlayer.CurrentFrame value to be valid before you use it.

It should no longer crash, but your code should still be fixed properly. I'd recommend trying to find a tutorial or similar for this. The way you're doing animations and drawing now doesn't really work well for a game, in my opinion.

Edited by Lactose!, 13 August 2014 - 12:51 PM.

Hello to all my stalkers.

### #13LeftyGuitar  Members

Posted 13 August 2014 - 01:23 PM

Thanks it dosen't crash. However you do have a valid argument, as this isn't the best way for animation and movement, as I am noticing. I am going to look up some tutorials on how to do this better.

if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
{
//gPlayer.CurrentFrame++;
//gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;
SDL_RenderCopy(GameRend,gPlayer.LeftTex[gPlayer.CurrentFrame  % 8],NULL,&gPlayer.LeftRect[gPlayer.CurrentFrame  % 8]);
if(gPlayer.CurrentFrame >= 8)
{
gPlayer.CurrentFrame = 1;
}
}


I noticed that when I put the % 8 inside the []'s it works a tiny bit better. However the animation is still a little off. The Player moves, but only so far, then it gets stuck.

Edited by LeftyGuitar, 13 August 2014 - 04:05 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.