SDL Event Queue

Started by
2 comments, last by Chad Smith 11 years, 2 months ago

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?

Advertisement

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.

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.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This tutorial will help you if you're interested in the keystates likes has been just described: http://lazyfoo.net/SDL_tutorials/lesson10/index.php

This topic is closed to new replies.

Advertisement