Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


using sleep in a game?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 slicer4ever   Crossbones+   -  Reputation: 3986

Like
0Likes
Like

Posted 31 December 2011 - 03:52 PM

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?
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 31 December 2011 - 04:50 PM

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.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 TheTroll   Members   -  Reputation: 882

Like
0Likes
Like

Posted 31 December 2011 - 04:54 PM

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.

#4 mhagain   Crossbones+   -  Reputation: 8282

Like
0Likes
Like

Posted 31 December 2011 - 06:13 PM

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!

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#5 SimonForsman   Crossbones+   -  Reputation: 6322

Like
0Likes
Like

Posted 31 December 2011 - 06:18 PM

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.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#6 Bacterius   Crossbones+   -  Reputation: 9293

Like
0Likes
Like

Posted 31 December 2011 - 09:23 PM

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.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#7 slicer4ever   Crossbones+   -  Reputation: 3986

Like
0Likes
Like

Posted 01 January 2012 - 01:08 AM

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.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#8 h4tt3n   Members   -  Reputation: 1290

Like
0Likes
Like

Posted 01 January 2012 - 07:16 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS