Sdl Event Queue Help, Inputs Going Out Of Sync


iatneH

Still Fresh
Joined
Dec 21, 2005
Messages
62
Location
Vancouver, BC, Canada
Website
Visit site
I've been working on a game as my first project for a few weeks now using SDL, I just have a question about the SDL event queue, more specifically how exactly it works.

Just for context, I'm making a 2D scrolling shooter. I noticed that if I really quickly mash around the directional keys and then let go, the ship keeps moving around for a while after I stop.
What it looks like is that the inputs aren't being interpreted in real time, but what is strange is that the screen keeps scrolling at the same speed it always has.

So are all the events are just being shoved into a queue and being interpreted at a later time? But how can I make it so that inputs are always registered in one VBlank period and interpreted for the next VBlank?

What the general form of the code looks like now is this:
Code:
while (!done)
{
  SDL_PollEvent(&event)
  switch (event.type)
  {
	// (key up and key down stuff, basically I'm just putting 1's and 0's into an array here)
  }
  // other game logic
  // including interpreting what was found in the controls array
  SDL_Flip(screen);
}

I have no clue what could be causing the huge input lags... the controls are fine if I make only a few inputs, it's just that it lags to Hell when I mash buttons - the ship could continue moving for several seconds after I stop inputting controls.

Any ideas would be much appreciated, thanks!
 
Code:
while (!done)
{
  while (SDL_PollEvent(&event))
  {
	switch (event.type)
	{
	  // (key up and key down stuff, basically I'm just putting 1's and 0's into an array here)
	}
  }
  // other game logic
  // including interpreting what was found in the controls array
  SDL_Flip(screen);
}
 
I have a related problem...

I ended up removing the while loop because I found it wasn't responding to a button being held down. Is there something I need to do to 'free' up the event?

I am also having problems firing at the same time as moving? Any ideas? My code currently looks like this:-

Code:
SDL_PollEvent(&event)
					switch( event.type )
					{
						case SDL_JOYBUTTONDOWN:

							switch( event.jbutton.button )
							{

								case GP2X_BUTTON_LEFT :
								{
									//NavSound();
									Player.DecX();
									break;
								}
								case GP2X_BUTTON_RIGHT :
								{
									//NavSound();
									Player.IncX();
									break;
								}
								case GP2X_BUTTON_START :
								{
									PlayMode = PLAY_MODE_MENU;
									Mix_PlayMusic(music, 1);
									break;
								}
								case GP2X_BUTTON_A :
								{
									for (int i=0;i<3;i++)
									{
										if (SDL_GetTicks() > (lFire + 300))
										{
											if (PlayerLaser[i].IsAlive() == false)
											{
												//RE-INIT
												lFire = SDL_GetTicks();
												//drawText(screen, 1, "ALIVE", 120, 130, 255, 255, 255);
												PlayerLaser[i].SetAlive(true);
												PlayerLaser[i].SetX(Player.GetX()+12);
												PlayerLaser[i].SetY(Player.GetY()-33);
												//drawTextInt(screen, 1, PlayerLaser[i].GetY(), 120, 150, 255, 255, 255);
												LaserSound();
												break;
											}
										}
									}
									break;
								}
								default:
									break;
							}

					}
				//}
				SDL_Flip(screen);
 
The problem is due to the way that SDL works with key up and down.

When you press right, it triggers the event for key down for right, after that, the state of the joystick does not change as long as right is held down therefore no events are triggered. When you let go of right, the state of the joystick has changed and triggers the key up event.

theoddbot example code will fix your second problem. His code basically says, process all events that have been triggered since the last frame now. Yours just processes one event per frame.
 
Back
Top