SDL event loop

Started by
5 comments, last by SigmaX 17 years, 7 months ago
Which one should be used and why?

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

    // ...
}
OR

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

    // ...
}
Advertisement
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
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 ]
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.
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
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);
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

This topic is closed to new replies.

Advertisement