Sign in to follow this  
MoBaT

SDL Event Queue

Recommended Posts

I have a problem with the SDL Events. I have a code which looks like:

while (SDL_PollEvent(&Event)){
	OnEvent(&Event);
}

 

then the OnEvent processes the event and directs it to the proper virtual function. So If I were to hold down the right key button, it would go here.
 


void Engine::OnKeyDown(SDLKey sym){
	switch(sym) {
		case SDLK_RIGHT:
			x1 += 1;
			break;
	}
}

 

The problem is that when I am holding the right button key, it is only running one time. The queue does not stack and keep processing the keydown button. What could be the problem?

Share this post


Link to post
Share on other sites

What you should do is keep track of when the key gets pressed and when it gets released. You could have a simple array of bools, one for each key. When you get an SDL_Keydown event, switch the key in your bool array to true. When you get an SDL_Keyup event, switch the key in your bool array to false. Then in your update loop, you can just check your bool array to see if x is pressed and move your sprite accordingly.

Edited by kidman171

Share this post


Link to post
Share on other sites

SDL_PollEvent() Only signals when a Key State changes, so, you'll only get it on Press and Release (as kidman points out).  If you would rather poll the KeyState, you can do that instead.  Check SDL manual for function, I don't remeber exactly which one it is.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this