Code for handling events looks like this(requested):
void gameEngine::HandleEvents()
{
if( SDL_PollEvent(&gameEvent) != 0 )
{
HandleEngineEvents();
gameLevel.HandleEvents(gameEvent);
}
}
HandleEngineEvents() :
void gameEngine::HandleEngineEvents()
{
if( gameEvent.type == SDL_QUIT)
{
running = false;
}
else if( gameEvent.type == SDL_KEYDOWN && gameEvent.key.repeat == 0 )
{
if( gameEvent.key.keysym.sym == SDLK_ESCAPE )
{
running = false;
}
}
}
Level::HandleEvents() :
void Level::HandleEvents(SDL_Event& gameEvent)
{
player.HandleEvents(gameEvent);
}
Player::HandleEvents() :
void Player::HandleEvents(SDL_Event& gameEvent)
{
if(gameEvent.type == SDL_KEYDOWN && gameEvent.key.repeat == 0)
{
switch(gameEvent.key.keysym.sym)
{
case SDLK_RIGHT:
velocity += 1;
break;
case SDLK_LEFT:
velocity -= 1;
break;
default:
break;
}
}
else if(gameEvent.type == SDL_KEYUP && gameEvent.key.repeat == 0)
{
switch(gameEvent.key.keysym.sym)
{
case SDLK_RIGHT:
velocity -= 1;
break;
case SDLK_LEFT:
velocity += 1;
break;
default:
break;
}
}
}