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 }
SDL - CPU usage
Started by Vexorian, Nov 25 2005 12:53 AM
7 replies to this topic
#1 Members - Reputation: 152
Posted 25 November 2005 - 12:53 AM
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:
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]
Sponsor:
#3 Members - Reputation: 158
Posted 30 November 2005 - 11:58 PM
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?
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?
#4 Moderators - Reputation: 2959
Posted 01 December 2005 - 01:28 AM
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).
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).
#5 Members - Reputation: 152
Posted 02 December 2005 - 02:30 AM
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
#8 Moderators - Reputation: 5048
Posted 13 March 2010 - 09:07 AM
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).






