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.