Calculating frames per second in a game

后端 未结 19 748
天命终不由人
天命终不由人 2020-12-04 05:10

What\'s a good algorithm for calculating frames per second in a game? I want to show it as a number in the corner of the screen. If I just look at how long it took to render

19条回答
  •  半阙折子戏
    2020-12-04 05:37

    You need a smoothed average, the easiest way is to take the current answer (the time to draw the last frame) and combine it with the previous answer.

    // eg.
    float smoothing = 0.9; // larger=more smoothing
    measurement = (measurement * smoothing) + (current * (1.0-smoothing))
    

    By adjusting the 0.9 / 0.1 ratio you can change the 'time constant' - that is how quickly the number responds to changes. A larger fraction in favour of the old answer gives a slower smoother change, a large fraction in favour of the new answer gives a quicker changing value. Obviously the two factors must add to one!

提交回复
热议问题