Sign in to follow this  
CraazyDave

A newbie problem with SDL and events

Recommended Posts

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):

[code]

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;
}

[/code]


The Event method in GameEngine:

[code]

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;
}
[/code]


I have been stuck with this problem for a week now and I really have a tight deadline for this game.

Share this post


Link to post
Share on other sites
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 [url="http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express"](Link[/url]) and use the debugger.
Using a debugger would solve this problem super fast and is necessary to continue progressing as a programmer.

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1310918793' post='4836394']
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.

[/quote]


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.

Share this post


Link to post
Share on other sites
He is talking about the Standard Template Library ([url="http://en.wikipedia.org/wiki/Standard_Template_Library"]Link[/url]).

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:
[code]
GameEngine g;
Image i(false, "pong_player.bmp");
[/code]
Also, he made a point that you don't delete the objects you new.

Like so:
[code]
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;
}

[/code]

Share this post


Link to post
Share on other sites
[quote name='James Leighe' timestamp='1310922641' post='4836419']
He is talking about the Standard Template Library ([url="http://en.wikipedia.org/wiki/Standard_Template_Library"]Link[/url]).

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:
[code]
GameEngine g;
Image i(false, "pong_player.bmp");
[/code]
Also, he made a point that you don't delete the objects you new.

Like so:
[code]
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;
}

[/code]
[/quote]


Thanks for the clarification!
Will try that.

Share this post


Link to post
Share on other sites
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='CraazyDave' timestamp='1310978983' post='4836719']
[quote name='James Leighe' timestamp='1310922641' post='4836419']
He is talking about the Standard Template Library ([url="http://en.wikipedia.org/wiki/Standard_Template_Library"]Link[/url]).

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:
[code]
GameEngine g;
Image i(false, "pong_player.bmp");
[/code]
Also, he made a point that you don't delete the objects you new.

Like so:
[code]
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;
}

[/code]
[/quote]


Thanks for the clarification!
Will try that.


[/quote]

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