The thing is, when an event happens the event is added to the event queue. When you call SDL_PollEvent it removes one event from the event queue (if the queue is not empty). So what happens is that Player.Update(); will handle all the events leaving the event queue empty so Player2.Update(); has no events to handle. They simply steal events from each other.
You will have the same problem if you try to handle other kinds of events like SDL_QUIT somewhere else in your program.
To fix all this you should only have one loop that handles all the events in the program. In that loop can of course call other functions. You can make the update function take an event as argument and pass the event from the event loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
while(!Done)
{
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
Done = true;
break;
}
Player.Update(event);
Player2.Update(event);
}
Player.Draw();
Player2.Draw();
}
|
This will call Player::Update() each time you receive an event. That means that it might be called 0 or many times in each iteration of the outer loop. If you only want to have it called exactly once every iteration you could call the update function outside the event loop. In that case the update function should not handle events(!). Instead you can have a special Player member function for handling key events. Here is an example of what I mean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
while(!Done)
{
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
Done = true;
break;
case SDL_KEYDOWN:
Player.onKeyDown(event.key.keysym.sym);
Player2.onKeyDown(event.key.keysym.sym);
break;
}
}
Player.Update();
Player2.Update();
Player.Draw();
Player2.Draw();
}
|
onKeyDown takes a SDLKey so it might look something like this
1 2 3 4 5 6 7 8 9 10 11
|
void Player::onKeyDown(SDLKey key)
{
switch(key)
{
case SDLK_w:
upup = false;
break;
case SDLK_s:
downup = false;
break;
...
|
The actual movement you probably want to handle in the update function to get a smooth movement.
EDIT:
I can also mention that if all you want to do is check if a key is down or not you can use SDL_GetKeyState for that.
http://www.libsdl.org/cgi/docwiki.cgi/SDL_GetKeyState
1 2 3 4 5 6 7 8 9
|
Uint8* keystate = SDL_GetKeyState(0);
if (keystate[SDLK_w])
{
// the w key is down
}
if (keystate[SDLK_s])
{
// the s key is down
}
|
This will not affect the event queue so it is safe to use anywhere. If you use SDL_GetKeyState it is important that you call SDL_PollEvent or SDL_PumpEvents somewhere in you program because otherwise the key state will not get updated.