Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL_PollEvent vs SDL_WaitEvent

Tags:

So I was reading this article which contains 'Tips and Advice for Multithreaded Programming in SDL' - https://vilimpoc.org/research/portmonitorg/sdl-tips-and-tricks.html

It talks about SDL_PollEvent being inefficient as it can cause excessive CPU usage and so recommends using SDL_WaitEvent instead.

It shows an example of both loops but I can't see how this would work with a game loop. Is it the case that SDL_WaitEvent should only be used by things which don't require constant updates ie if you had a game running you would perform game logic each frame.

The only things I can think it could be used for are programs like a paint program where there is only action required on user input.

Am I correct in thinking I should continue to use SDL_PollEvent for generic game programming?

like image 223
Zammalad Avatar asked Sep 17 '13 21:09

Zammalad


1 Answers

If your game only updates/repaints on user input, then you could use SDL_WaitEvent. However, most games have animation/physics going on even when there is no user input. So I think SDL_PollEvent would be best for most games.

One case in which SDL_WaitEvent might be useful is if you have it in one thread and your animation/logic on another thread. That way even if SDL_WaitEvent waits for a long time, your game will continue painting/updating. (EDIT: This may not actually work. See Henrik's comment below)

As for SDL_PollEvent using 100% CPU as the article indicated, you could mitigate that by adding a sleep in your loop when you detect that your game is running more than the required frames-per-second.

like image 116
priomsrb Avatar answered Sep 18 '22 15:09

priomsrb