How to make timer for a game loop?

前端 未结 5 1761
别跟我提以往
别跟我提以往 2021-02-06 16:21

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.

5条回答
  •  花落未央
    2021-02-06 17:03

    There are many good reasons why you should not limit your frame rate in such a way. One reason being as stijn pointed out, not every monitor may run at exactly 60fps, another reason being that the resolution of timers is not sufficient, yet another reason being that even given sufficient resolutions, two separate timers (monitor refresh and yours) running in parallel will always get out of sync with time (they must!) due to random inaccuracies, and the most important reason being that it is not necessary at all.

    Note that the default timer resolution under Windows is 15ms, and the best possible resolution you can get (by using timeBeginPeriod) is 1ms. Thus, you can (at best) wait 16ms or 17ms. One frame at 60fps is 16.6666ms How do you wait 16.6666ms?

    If you want to limit your game's speed to the monitor's refresh rate, enable vertical sync. This will do what you want, precisely, and without sync issues. Vertical sync does have its pecularities too (such as the funny surprise you get when a frame takes 16.67ms), but it is by far the best available solution.

    If the reason why you wanted to do this was to fit your simulation into the render loop, this is a must read for you.

提交回复
热议问题