Sign in to follow this  
Vexorian

SDL - CPU usage

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this