Advertisement

SDL_PollEvent Question

Started by December 14, 2004 11:04 AM
4 comments, last by Drew_Benton 20 years, 2 months ago
Hey guys its me again [wink]. As you can see, I'm busy as a bee! Ok my question is this: Name: SDL_PollEvent -- Polls for currently pending events. Description Polls for currently pending events, and returns 1 if there are any pending events, or 0 if there are none available. If event is not NULL, the next event is removed from the queue and stored in that area. Example:

while( SDL_PollEvent(&event) )
{
   // Process events
}


My question is that in my engine's loop, instead of calling SDL_PollEvent via a while loop, I am doing it via a if statement.

if ( SDL_PollEvent(&event) )
{
... Process Event...
}
else
{
... Draw Update Etc ...
}


As you can see, it will perform the same functionality as the while loop, becauseas long as there is an event, nothing will be draw, but is this correct? I was thinking I should draw every frame, whether there is a event or not, but I don't know if that is logical. I was also thinking, should I just use the while loop, or is the if/else loop faster? In the engine itself, it is only processing 2 events - event.type == SDL_QUIT and event.type == SDL_ACTIVEEVENT to know when to exit and know when *not* to draw/update. Thanks! [Edited by - Drew_Benton on May 25, 2005 9:29:51 PM]
I don't think that'll work because if you move the window(if the game is in windowed mode) then instead of rendering the game scene it will go to the first if statement and that evidently will mess up your game. Another example is when the user presses -ANY- key on the keyboard this will halt rendering. Try it.

[Edited by - Khaosifix on December 14, 2004 1:17:49 PM]
---http://www.michaelbolton.comI constantly dream about Michael Bolton.
Advertisement
Quote:
Original post by Khaosifix
I don't think that'll work because if you move the window(if the game is in windowed mode) then instead of rendering the game scene it will go to the first if statement and that evidently will mess up your game. Another example is when the user press -ANY- key on the keyboard this will half rendering. Try it.


Well that's the thing, it logically seems incorrect, but in application it works (but you are entirely correct). What I did was put two counters in each statement to see what was being executed. The resutls were (~10 second test):

With no mouse moving, keys, etc:
ctr1 - 9
ctr2 - 16828

With lots of mouse moving, keys, etc:
ctr1 - 1566
ctr2 - 17368

But not difference in terms of "messing up drawing"

One thing I noticed was the fps counter was a little off in timings on the second test (which means update code would be messed up), but I saw no difference in rendeing since the calls of processing events are so small. I will go ahead and use the while loop and draw every frame! Thanks for the advice! I needed to talk this problem out [wink].

[Edited by - Drew_Benton on May 25, 2005 9:47:59 PM]
Quote:
Original post by Drew_Benton
In the engine itself, it is only processing 2 events - event.type == SDL_QUIT and event.type == SDL_ACTIVEEVENT to know when to exit and know when *not* to draw/update. Thanks!

How do you plan to get user input?

The problem that I personally ran into when trying if(SDL_PollEvent(&ev)) is that SDL_PollEvent() removes ONE event from the top of the queue and stores it into the SDL_Event. Queues are a FIFO (First-in First-out) Structure, meaning that the events are removed from the queue in the order they are triggered.

Doing while(SDL_PollEvent(&ev)) will remove ALL events from the queue, so if you need to test for a lot of user input this is probably the way to go.

The reason that I think your if() { } else { } still seems to be drawing perfectly fine is that SDL resolution on bringing events to the queue may not be to-the-millisecond accurate. How fast is the fps? If its like 200+, I would think that it would be nearly impossible to trigger even 100+ events in one second, which means that your game would draw, update, ect 100 a second like Khaosifix mentioned. Even 30-60 redraws per second would probably look fine unless your game is a 3D FPS.
doing it in an if statement will only give you a single input event each frame. doing it in a loop will get all of the input for that frame. in short, theres absolutely no reason at all to use an if statement. this is not an optimization and just makes your input and game less responsive.
FTA, my 2D futuristic action MMORPG
Well for input I have made an separate independent class wrapping SDL_GetKeyState as well as the mouse functions (from Cone3D's site). I can see now why I should use the while loop (I have). If you guys want to take a look at my engine so far it is here. It comes with documentation and everything. So far there is no input, b/c it is a separate component, but you can close it. Thanks!

[Edited by - Drew_Benton on May 25, 2005 9:40:07 PM]

This topic is closed to new replies.

Advertisement