• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
LeftyGuitar

Crashes When Character Walks

12 posts in this topic

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
0

Share this post


Link to post
Share on other sites
Also seems likely you AR accessing your arrays out of bounds with current frame too
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
	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.

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.LeftImg[0] = IMG_Load("Hero\\w17.png");
	gPlayer.LeftTex[0] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[0]);
	gPlayer.LeftImg[1] = IMG_Load("Hero\\w18.png");
	gPlayer.LeftTex[1] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[1]);
	gPlayer.LeftImg[2] = IMG_Load("Hero\\w19.png");
	gPlayer.LeftTex[2] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[2]);
	gPlayer.LeftImg[3] = IMG_Load("Hero\\w20.png");
	gPlayer.LeftTex[3] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[3]);
	gPlayer.LeftImg[4] = IMG_Load("Hero\\w21.png");
	gPlayer.LeftTex[4] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[4]);
	gPlayer.LeftImg[5] = IMG_Load("Hero\\w22.png");
	gPlayer.LeftTex[5] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[5]);
	gPlayer.LeftImg[6] = IMG_Load("Hero\\w23.png");
	gPlayer.LeftTex[6] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[6]);
	gPlayer.LeftImg[7] = IMG_Load("Hero\\w24.png");
	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.

0

Share this post


Link to post
Share on other sites

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!
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0