SDL - CPU usage

Recommended Posts

Vexorian    152
I was testing my just converted SDL game, well after all the work the game works correctly, the problem is that if you switch to other app that app freezes/becomes horribly slow. I succesfully disabled the game loop for non active times, but the problem is still there, so I am sure that it is nothing on the game itself but something related with SDL perhaps PollEvent doesn't sleep or something? Any way to fix this? Edit: Wait a second It was my mistake Edit 2: Just in case any body finds it useful in the future:
while (!done)
{
// message processing loop
while (SDL_PollEvent(&event)) //Poll event doesn't sleep by itself
{
....
}
SDL_Delay(10); //Makes it sleep for a small time
}


Edit 2: There is a major improvement if you increase the delay to 1000 during app inactivity [Edited by - Vexorian on November 25, 2005 7:09:35 AM]

Share on other sites
Gorg    248
You want SDL_WaitEvent in your pause loop.

SDL_pollevent is there so you can check if there is something in the event queue and do something else than event handling if there are none.

Share on other sites
Spippo    158
I think there is a small problem in your solution:

If you use SDL_Delay(1000);
It will wait sometime before it checks again to see if an event happend. But the whole program/game will wait for that amount of time, so you get a lower framerate.

Or am I wrong?

Share on other sites
Kylotan    10010
You're right. Every frame will take a second, but he did say 'during inactivity'.

Generally using 100% CPU for a game is not a problem. But depending on the kind of game, you might want to get around this, either by pausing the game completely - especially when it's not focused, or perhaps using SDL_Delay(0) (which should work ok on Windows, at least).

Share on other sites
Vexorian    152
Quote:
 Original post by SpippoI think there is a small problem in your solution:If you use SDL_Delay(1000);It will wait sometime before it checks again to see if an event happend. But the whole program/game will wait for that amount of time, so you get a lower framerate.Or am I wrong?

It was rather:

st=SDL_GetAppState();
if ((st==SDL_APPACTIVE) || (! st) ) SDL_Delay(1000)
else SDL_Delay(0)
PollEvents...
...

...

And so and so, it only waited for 1 second when the app was not active.

I am gonna try the WaitEvent thing. SDL_Delay(0) didn't help a lot other graphic apps were (kind-of) freezing anyways. The thing was only fixed correctly after the 1000 thing, although when focus was back on the application next frame took some time

Share on other sites
steffenomak    100
Oh, this helped me, thank you , when I tried whit a normal window, the window used a 100% on one core, but when i added SDL_Delay( 0 ); it made the load go down to 0% when nothing happaned.

Share on other sites
TheBuzzSaw    143
I use SDL_Delay(1) in my program. What is the fundamental difference between that at SDL_Delay(0)?

Share on other sites
rip-off    10979
Quote:
 Original post by TheBuzzSawI use SDL_Delay(1) in my program. What is the fundamental difference between that at SDL_Delay(0)?

I believe that on some implementations of SDL, SDL_Delay(0) may return immediately, whereas SDL_Delay(1) will always sleep for some period (generally a timeslice, which could have a granularity of 10 milliseconds for example on Windows).

Create an account

Register a new account