Jump to content
  • Advertisement
Sign in to follow this  
Kalazart

SDL event loop

This topic is 4471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Which one should be used and why?
while(!done) {
    while(SDL_PollEvent(&event)) {
        // ...
    }

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

    // ...
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!