SDL - CPU usage

Started by
6 comments, last by rip-off 14 years, 1 month ago
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]
------ XYE - A new edition of the classic Kye
Advertisement
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.
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?
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
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).
Quote:Original post by Spippo
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?


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
------ XYE - A new edition of the classic Kye
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.
I use SDL_Delay(1) in my program. What is the fundamental difference between that at SDL_Delay(0)?
Amateurs practice until they do it right.Professionals practice until they never do it wrong.
Quote:Original post by TheBuzzSaw
I 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).

This topic is closed to new replies.

Advertisement