Tight game loop in Windows

Started by
2 comments, last by frob 18 years, 4 months ago
I'm very new to this. The game i'm creating is a very simple 2D sidescroller in Windows. What would be the optimal method of delaying at the end of a game loop iteration so the game doesn't chew up the computer's CPU? I've experimented with Sleep() but i'm getting incosistent performance and i'm curious if this is the preffered way. Thank you much!
Advertisement
My view is that using nearly 100% of the CPU time is a good thing to do, as long as you don't bring down the rest of the system.

Maybe your game should switch to GetMessage instead of PeekMessage when the window isn't active, but that's about it.
Here is a nice tutorial on that.

Tutorial
SDBradley
CGP
"A person who won't read has no advantage over one who can't read." ~Mark Twain
Quote:Original post by durnew
I'm very new to this. The game i'm creating is a very simple 2D sidescroller in Windows. What would be the optimal method of delaying at the end of a game loop iteration so the game doesn't chew up the computer's CPU? I've experimented with Sleep() but i'm getting incosistent performance and i'm curious if this is the preffered way.

Thank you much!


Timers are a problem on Windows in general. See this article by Jon Watte for some a description of problems, and this implementation by Charles Bloom for some solution code.

Sleep() on windows isn't very accurate. It has a 10ms minimum granularity, IIRC.

You can use WaitSingleObject() and timers.

If you decoupled display from physics like you ought to, the issue becomes less severe since you can render frequently but keep the physics at a fixed step.

frob.

This topic is closed to new replies.

Advertisement