Skip to main content
gamedev.net
🔒 Locked

SDL event loop

Started by Kalazart 5 replies 2.8k views
Original Post
Kalazart
Kalazart
Which one should be used and why?

while(!done) {
    while(SDL_PollEvent(&event)) {
        // ...
    }

    // ...
}
OR

while(!done) {
    if(SDL_PollEvent(&event)) {
        // ...
    }

    // ...
}
Chad Smith
Chad Smith
I hope this is correct.


The while loop one because it will keep on looping through your events. The if only checks it once per frame I believe.


I hope that helps.

Chad
iliak
iliak
The while loop is better.
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
nullsquared
nullsquared
The while(...) {...} version. It handles all events while the other will handle 1 per frame... Which'll mess up if you use the mouse or have any other thing that generates a lot of events.
M2tM
M2tM
Just be careful you don't ignore all events but the last one to be seen by the while loop or you will end up with dropped events.

ie: deal with all events, or manage the events yourself somehow that makes sense for your application... But don't do the while loop and then process only the last event because you'll have missed all the other ones for the sake of emptying the SDL event queue.

Typically how I handle it is I have at the top of my game loop the sdl event loop (the while loop) which grabs all events, interprets them in some form for my own event manager and then each frame I process events from my event manager as required. Some events are dealt with immediately while others can acceptably be left for a frame or two and so it isn't just a dumb event queue like you get from SDL, though I do maintain order of recieved events when necessary...

Hopefully that explains my view at least... It may not be the best way of handling things, it's just how I do.
_____"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
nullsquared
nullsquared
You can also make an event filter:
int filter(SDL_Event* e) {    if (e->type == SDL_MOUSEMOTIONEVENT)        return 0; // drop    return 1; // keep}SDL_SetEventFilter(&filter);
SigmaX
SigmaX
The while loop. When I first started with SDL I read a book that showed the IF method. It caused a lot of slowdown with handling events. Just changing to the while loops sped up the event handling.
-)------ Ed

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.