GP2X Sdl Event Polling


A_SN

Active Member
Joined
Jun 8, 2006
Messages
899
So far I've always used an if statement in my event polling routine, but a couple of people on #gp2xdev strongly advised me to make it be a while statement to poll all the events in queue before going on with the program.

While it makes sense to me, I can see a problem with it, you could have key presses being ignored because their release would be pulled out of the event queue right after the press event and thus not get to meet the code destinated to handle that key press.

What do you guys think about that and how do you handle it most of the time?
 
I use while, it just makes sure all the events are handled before the end of the main loop and the next render. There's no reason to be ignoring events, you still handle all the events in the while i.e

Code:
while (SDL_PollEvent(&event))
{
	  switch (event.type)
	  {
		  case SDL_KEYDOWN:
				do_down_stuff();
				break;

	   case SDL_KEYUP:
				do_up_stuff();
				break;
}

With if, you just do one event per cycle of the main loop

Edit: thought of a good reason to use while

Using if. If you press 2 buttons you will render the screen not knowing about one of the actions until the next render therefore one render will show action A then action B on the next render. With while, both actions will be simultaneous
 
for sure while would be a better option

altough joystick handling does not change fast enough to be unusable by 60 ifs per second, still while would be much more precise and logical to use, even more if you use a mouse

and while in do_down_stuff() example, always check for diagonals first, to avoid calling some joy_down_reaction routine and right after joy_diagonal_lowerleft_reaction when user just pressed diagonal lower-left (as gp2x uses a stick this is not much of a problem, but by doing this your program will be a bit more compatible)
 
I use while, it just makes sure all the events are handled before the end of the main loop and the next render. There's no reason to be ignoring events, you still handle all the events in the while i.e

Code:
while (SDL_PollEvent(&event))
{
	  switch (event.type)
	  {
		  case SDL_KEYDOWN:
				do_down_stuff();
				break;

	   case SDL_KEYUP:
				do_up_stuff();
				break;
}

With if, you just do one event per cycle of the main loop

Edit: thought of a good reason to use while

Using if. If you press 2 buttons you will render the screen not knowing about one of the actions until the next render therefore one render will show action A then action B on the next render. With while, both actions will be simultaneous

I don't do it like this, that's why, I don't call functions but rather set values to variables. That's why a while statement could miss something if it sets a variable to 1 and right after sets it back to 0.

However you make a point that it'll have to wait until next frame in order to take the other action in account, however I think it's better to have it slightly delayed than to have some key presses missing (however if you have more iterations of your main loop than actually displayed frames, some kind of frameskip allowing you to for example simulate more physics with more time domain precision than displayed it wouldn't be a problem).

You of course don't have to choose between missing key presses vs. have actions delayed in your code snippet, but the way I do things I don't think I can afford to call functions instead of setting variables.
 
Last edited by a moderator:
I don't do it like this, that's why, I don't call functions but rather set values to variables. That's why a while statement could miss something if it sets a variable to 1 and right after sets it back to 0.
I set values to variables also, but I've never had any problems with missing events. I think your framerate would have to be pretty low for that to happen (1-2 fps? Somebody do a test :p).
 
If you're setting variables based on the events them later doing an if and doing a while are similar; the if would be handling the first event in the queue and the while would be handling the last event (of that type). Either way you could be missing events (may come up if the user's pressing several buttons at once), but the while would be missing fewer since there's only overlap with events that use the same variables you're setting.
 
I don't do it like this, that's why, I don't call functions but rather set values to variables. That's why a while statement could miss something if it sets a variable to 1 and right after sets it back to 0.
I set values to variables also, but I've never had any problems with missing events. I think your framerate would have to be pretty low for that to happen (1-2 fps? Somebody do a test :p).

1-2 fps? yeah, if it takes you half a second to press and release a button. reminds me that I wanted to make a simple test program to test such things as how fast can people press buttons and stuff (mainly in order to make AI's act more humanly by not allowing to press buttons more often than we can and such)

If you're setting variables based on the events them later doing an if and doing a while are similar; the if would be handling the first event in the queue and the while would be handling the last event (of that type). Either way you could be missing events (may come up if the user's pressing several buttons at once), but the while would be missing fewer since there's only overlap with events that use the same variables you're setting.


I don't see how on earth you could miss key presses using the if statement. You might not know it but events get queued until they get polled. Makes me wonder tho how big can the queue be.
 
Last edited by a moderator:
I meant, you'll miss them that frame. Although of course you're right, I'm sure the queue will overflow eventually.
 
Back
Top