My SDL app hogs the CPU unless in a state where I’m waiting for user
input. Since I’m using SDL_PollEvent, I
never miss an event, but CPU is at 100% the whole time.
If I put a 100ms delay every pass of the loop, CPU drops to ~25%
which is fine.
So…how do I get CPU usage down to the ~25% figure without using a
delay in the loop?
This isn’t a Windows app, just a console app.
Any help muchly appreciated!
This is an issue we were also working on at our project (OpenLieroX, a
Like Alvin said, use SDL_WaitEvent() where you can. You have to
separate a bit code which has to be executed frequently and code that
only has to be executed if an event occurs (for example the user has
pressed a key or your application got a quit signal or a socket
received some data).
Though, if your app is not event-based this needs some work to do. In
other cases, a delay is the correct way to let your app sleep a bit.
It means that you give some time away for other processes which means
that you don’t use 100% CPU time.
X% CPU time just means that your application always do something and
(In some implementations, SDL_WaitEvent() simply is a loop where it
checks SDL_PollEvent() and if no event has occurred, it just sleeps a
bit and tries again.)
We made it like this: All menu code uses SDL_WaitEvent() and only
while the game itself is running, we use SDL_PollEvent() with a
SDL_Delay(). You can set a MaxFPS value and SDL_Delay() sleeps exactly
so long that the FPS is not higher than the MaxFPS value.
AlbertAm 08.02.2008 um 19:43 schrieb Edward Byard: