A newbie problem with SDL and events

Started by
6 comments, last by BeerNutts 12 years, 9 months ago
I am trying to make a game with SDL for my course in C++ programming. But the program crashes every time I try to add some sort of code for keyevents.

The program runs succesfully if the only event I look for is a SDL_QUIT but as soon as I add the keypresses for the UP and DOWN keys the game crashes.

MY Main (GameEngine is where all SDL related code exists):



int main(int argc, char * args[])
{
GameEngine * g = new GameEngine();
Image * i = new Image(false, "pong_player.bmp");
g -> createSprite(i, 0, 0);

bool quit = false;

while(quit == false)
{
//Update screen and rect
g -> update();
g -> updateRect();

//Go through the events and check to see if the game should exit or react to a keypress
quit = g -> eventLoop();

//Controll the framerate
g -> checkFPS();
}

g -> exit();

return 0;
}




The Event method in GameEngine:



bool GameEngine::eventLoop()
{
bool quit = false;

while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit = true;
}
if(event.type == SDL_KEYDOWN)
{
int yVel = player -> getYVel();

switch(event.key.keysym.sym)
{
case SDLK_UP:
yVel -= 10;
player -> setYVel(yVel);
quit = false;
break;
case SDLK_DOWN:
yVel += 10;
player -> setYVel(yVel);
quit = false;
break;
}
}
if(event.type == SDL_KEYUP)
{
int yVel = player -> getYVel();

switch(event.key.keysym.sym)
{
case SDLK_UP:
yVel += 10;
player -> setYVel(yVel);
quit = false;
break;
case SDLK_DOWN:
yVel -= 10;
player -> setYVel(yVel);
quit = false;
break;
}
}
}

return quit;
}



I have been stuck with this problem for a week now and I really have a tight deadline for this game.
Advertisement
Did you create player? It could be that it's pointing to NULL/Bad Memory.

But what you really want to do is get Visual Studio Express (Link) and use the debugger.
Using a debugger would solve this problem super fast and is necessary to continue progressing as a programmer.
I don't know what's making your program crash, but the fact that you have two uses of `new' and none of `delete' in that code doesn't make me very confident that you know what you are doing with memory management.

I very rarely use `new' myself. I prefer to use objects on the stack (`GameEngine g;' instead of `GameEngine * g = new GameEngine();', and similarly for your `Image i'). I also use standard-library containers, which handle dynamic memory allocation and release for me. In situations where I want polymorphic behavior, I make a factory function that returns a shared_ptr to the base class, so I can be sure that the object will be destroyed and its memory deleted when nobody is using it anymore.

Chances are you would have fewer unexplained crashes if you were to adhere to similar guidelines.

I don't know what's making your program crash, but the fact that you have two uses of `new' and none of `delete' in that code doesn't make me very confident that you know what you are doing with memory management.

I very rarely use `new' myself. I prefer to use objects on the stack (`GameEngine g;' instead of `GameEngine * g = new GameEngine();', and similarly for your `Image i'). I also use standard-library containers, which handle dynamic memory allocation and release for me. In situations where I want polymorphic behavior, I make a factory function that returns a shared_ptr to the base class, so I can be sure that the object will be destroyed and its memory deleted when nobody is using it anymore.

Chances are you would have fewer unexplained crashes if you were to adhere to similar guidelines.




Honestly, my knowledge of proper mempory allocation is a bit foggy. Could you clarify what you mean by using standard-librari containers? Give me an example.

He is talking about the Standard Template Library (Link).

In this case, you don't do anything that would require these things however he does make a good point that if you allocate 'GameEngine' and 'Image' on the stack it will be freed for you.

Like so:

GameEngine g;
Image i(false, "pong_player.bmp");

Also, he made a point that you don't delete the objects you new.

Like so:

int main(int argc, char * args[])
{
GameEngine * g = new GameEngine();
Image * i = new Image(false, "pong_player.bmp");
g -> createSprite(i, 0, 0);

bool quit = false;

while(quit == false)
{
//Update screen and rect
g -> update();
g -> updateRect();

//Go through the events and check to see if the game should exit or react to a keypress
quit = g -> eventLoop();

//Controll the framerate
g -> checkFPS();
}

g -> exit();

delete g; <-----
delete i; <-----

return 0;
}

Can you show us your GameEngine class?

He is talking about the Standard Template Library (Link).

In this case, you don't do anything that would require these things however he does make a good point that if you allocate 'GameEngine' and 'Image' on the stack it will be freed for you.

Like so:

GameEngine g;
Image i(false, "pong_player.bmp");

Also, he made a point that you don't delete the objects you new.

Like so:

int main(int argc, char * args[])
{
GameEngine * g = new GameEngine();
Image * i = new Image(false, "pong_player.bmp");
g -> createSprite(i, 0, 0);

bool quit = false;

while(quit == false)
{
//Update screen and rect
g -> update();
g -> updateRect();

//Go through the events and check to see if the game should exit or react to a keypress
quit = g -> eventLoop();

//Controll the framerate
g -> checkFPS();
}

g -> exit();

delete g; <-----
delete i; <-----

return 0;
}





Thanks for the clarification!
Will try that.

Dave, that's not going to fix your problem. the deleting is just a maintenance thing.

Where does it die? put some printf's in there, so you can tell how far it gets before it dies.

What is "player"? Where is it defined and instantiated?

[quote name='James Leighe' timestamp='1310922641' post='4836419']
He is talking about the Standard Template Library (Link).

In this case, you don't do anything that would require these things however he does make a good point that if you allocate 'GameEngine' and 'Image' on the stack it will be freed for you.

Like so:

GameEngine g;
Image i(false, "pong_player.bmp");

Also, he made a point that you don't delete the objects you new.

Like so:

int main(int argc, char * args[])
{
GameEngine * g = new GameEngine();
Image * i = new Image(false, "pong_player.bmp");
g -> createSprite(i, 0, 0);

bool quit = false;

while(quit == false)
{
//Update screen and rect
g -> update();
g -> updateRect();

//Go through the events and check to see if the game should exit or react to a keypress
quit = g -> eventLoop();

//Controll the framerate
g -> checkFPS();
}

g -> exit();

delete g; <-----
delete i; <-----

return 0;
}





Thanks for the clarification!
Will try that.


[/quote]

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement