Jump to content
  • Advertisement
Sign in to follow this  
slicer4ever

using sleep in a game?

This topic is 2537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello all, i have a design decision to make in my game, which is becoming hard to overcome.

i'm writing a game utilizing openGL, on windows, and basically, i find my program having irratic framerate because i call sleep at the end of each game loop, so that i don't utilize the entire cpu.

if i don't call sleep, my framerate skyrockets(with vsync off), but when i do call sleep, it wreaks havoc on my framerate(sometimes dipping down to 20-30 fps, or popping up to 100fps), i currently utilize a timer to lock my game to a maximum of 60 fps. my gpu can easily handle what i'm pushing down it, since without sleep, i can maintain fps's in the thousands.

i could use a variable step approach to fix the irratic fps issue, but i was hoping to find an alternative solution to my problem.

the only reason i can foresee sleep being a problem is because it's not guaranteed to sleep for the amount of time i request.
is their an alternative to sleep that doesn't cause the cpu to be used constantly, or should i simply not care that i'm utilizing 100% of the resources?

Share this post


Link to post
Share on other sites
Advertisement
It depends on the operating system, really, as the OS will control how time slices get used, and sleep (or their equivalents) on two different OSs may be different.

On Windows, Sleep(numMSecs) will guarantee that your thread sleeps for at least numMSecs, which means it really could be far from what you requested. Even calling Sleep(0) (which is a bit of a special case) is not totally deterministic in the amount of time that will elapse before the function exits, though it is definitely more reliable than Sleep(1).

That being said, I say don't worry about using 100% of the CPU. I'd suggest reading this though, as it may help.

Share this post


Link to post
Share on other sites
Sleep, is not really a good idea. There is nothing wrong with taking up the CPU when your game is being played. Now with that being said, you need to ensure that if your game is minimized that you pause it and give up the CPU, same goes if it is a windowed game and the window get's pushed back, then it is time to give up most of the CPU cycles.

Share this post


Link to post
Share on other sites
The only valid reason to use Sleep is if you are targetting a mobile device and want to save power. Otherwise - your CPU is a resource that's there to be used, and if you're not using it then it's going to waste. So use it!

Share this post


Link to post
Share on other sites

hello all, i have a design decision to make in my game, which is becoming hard to overcome.

i'm writing a game utilizing openGL, on windows, and basically, i find my program having irratic framerate because i call sleep at the end of each game loop, so that i don't utilize the entire cpu.

if i don't call sleep, my framerate skyrockets(with vsync off), but when i do call sleep, it wreaks havoc on my framerate(sometimes dipping down to 20-30 fps, or popping up to 100fps), i currently utilize a timer to lock my game to a maximum of 60 fps. my gpu can easily handle what i'm pushing down it, since without sleep, i can maintain fps's in the thousands.

i could use a variable step approach to fix the irratic fps issue, but i was hoping to find an alternative solution to my problem.

the only reason i can foresee sleep being a problem is because it's not guaranteed to sleep for the amount of time i request.
is their an alternative to sleep that doesn't cause the cpu to be used constantly, or should i simply not care that i'm utilizing 100% of the resources?


Only use sleep to save power (Useful for laptops when running on battery) or if the game is paused or in a state where framerate is irrelevant. any power saving mode should be optional for the end user, (Personally i'd use 3 settings for power saving, off(default), on, and auto where the auto setting enables power saving whenever the system runs on battery(Which can be detected quite easily using the OS API)

Update the logic at a fixed rate and render as fast as possible, if you want to limit the framerate you should do so by enabling v-sync, If the game runs in fullscreen it really doesn't matter if it uses 100% of the resources, the user normally doesn't care about anything else anyway and if he does he can reduce your applications priority himself aswell.

Share this post


Link to post
Share on other sites
The real problem you're seeing is that sleep() is not accurate. It has a maximum resolution of 15ms at most and it's very sensitive to current OS state, which causes your framerate to fluctuate. The correct way to limit framerate is to use the system's high-resolution timer via QueryPerformanceCounter() and QueryPerformanceFrequency() which is accurate to the nanosecond (roughly, not that the CPU is fast enough to take advantage of that much accuracy). Or, you could just use VSync to limit the framerate to some multiple of the monitor's refresh rate, but then you'd need a separate thread for user input otherwise you'll get some input lag.

I am personally against having the CPU and GPU work more than they need to. Having 2000 fps and 100% cpu utilization while you could have 60 fps and 15% CPU utilization makes no sense other than generating heat and, in the case of many GPU's. pushing thousands of frames per second creates a very annoying coil whine which is disturbing to hear and can frighten some users.

And of course process the logic in a framerate-independent way, there's nothing more annoying than having one's game going slow-mo when framerate dips a bit.

Share this post


Link to post
Share on other sites
thank you all for your comments, i've decided to use sleep only when the window is not active, and implement a semi-time step(per CornStalk's linked article), to maintain that the game runs at the same interval on all machines.

Share this post


Link to post
Share on other sites
You could always use the GLFW openGL framework. It has a very high resolution timer and is cross platform. I use it to maintain constant framerate in my games while freeing as much cpu power to the system as possible. Works like a charm, and I see no reason why not to do so.

Cheers,
Mike

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!