[SDL] CPU usage
Albert Zeyer
albert.zeyer at rwth-aachen.de
Fri Feb 8 10:33:36 PST 2008
Am 08.02.2008 um 19:43 schrieb Edward Byard:
> Hi
>
> 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!
>
> Ed
Hi,
This is an issue we were also working on at our project (OpenLieroX, a
game) recently.
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
never sleeps.
(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.
Greetings,
Albert
More information about the SDL
mailing list