• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
slicer4ever

using sleep in a game?

7 posts in this topic

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?
0

Share this post


Link to post
Share on other sites
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 [i]at least[/i] 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 [url="http://gafferongames.com/game-physics/fix-your-timestep/"]this[/url] though, as it may help.
0

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.
0

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!
0

Share this post


Link to post
Share on other sites
[quote name='slicer4ever' timestamp='1325368347' post='4898536']
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?
[/quote]

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.
0

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.
0

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.
0

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
0

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  
Followers 0