Original Post
I am trying to make a game in SDL but I am avoiding at all costs to have any part of SDL in the main. Instead I have a class GameEngine with all the SDL functionality.
My problem is that I need to tie together the keyinputs that I check in GameEngine and call in the main method to the blitting of the Sprite.
It all should be straightforward but for some reason it does not work for me.
Now the problem I am having is that the x and y values that I use to change the position of the Sprite does not update with my keypresses.
The Method that checks the events:
Any help with figuring out why it does not work would be very helpfull.
My problem is that I need to tie together the keyinputs that I check in GameEngine and call in the main method to the blitting of the Sprite.
It all should be straightforward but for some reason it does not work for me.
Now the problem I am having is that the x and y values that I use to change the position of the Sprite does not update with my keypresses.
The Method that checks the events:
bool GameEngine::events()
{
std::cout << spriteField.size() << std::endl;
int xVel = 0;
int yVel = 0;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
exit();
return true;
}
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_UP:
yVel -= 5;
break;
case SDLK_DOWN:
yVel += 5;
break;
case SDLK_RIGHT:
xVel -= 5;
break;
case SDLK_LEFT:
xVel += 5;
break;
}
}
if(event.type == SDL_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_UP:
yVel += 5;
break;
case SDLK_DOWN:
yVel -= 5;
break;
case SDLK_RIGHT:
xVel += 5;
break;
case SDLK_LEFT:
xVel -= 5;
break;
}
}
for(int i = 0; i < spriteField.size(); i++)
{
spriteField.setVel(xVel, yVel);
spriteField.move();
}
}
return false;
}
Any help with figuring out why it does not work would be very helpfull.