How to make timer for a game loop?

Neomex picture Neomex · Mar 26, 2011 · Viewed 20.3k times · Source

I want to time fps count, and set it's limit to 60 and however i've been looking throught some code via google, I completly don't get it.

Answer

MarcB picture MarcB · Mar 26, 2011

If you want 60 FPS, you need to figure out how much time you have on each frame. In this case, 16.67 milliseconds. So you want a loop that completes every 16.67 milliseconds.

Usually it goes (simply put): Get input, do physics stuff, render, pause until 16.67ms has passed.

Its usually done by saving the time at the top of the loop and then calculating the difference at the end and sleeping or looping doing nothing for that duration.

This article describes a few different ways of doing game loops, including the one you want, although I'd use one of the more advanced alternatives in this article.