Calculating frames per second in a game

后端 未结 19 766
天命终不由人
天命终不由人 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:52

    In (c++ like) pseudocode these two are what I used in industrial image processing applications that had to process images from a set of externally triggered camera's. Variations in "frame rate" had a different source (slower or faster production on the belt) but the problem is the same. (I assume that you have a simple timer.peek() call that gives you something like the nr of msec (nsec?) since application start or the last call)

    Solution 1: fast but not updated every frame

    do while (1)
    {
        ProcessImage(frame)
        if (frame.framenumber%poll_interval==0)
        {
            new_time=timer.peek()
            framerate=poll_interval/(new_time - last_time)
            last_time=new_time
        }
    }
    

    Solution 2: updated every frame, requires more memory and CPU

    do while (1)
    {
       ProcessImage(frame)
       new_time=timer.peek()
       delta=new_time - last_time
       last_time = new_time
       total_time += delta
       delta_history.push(delta)
       framerate= delta_history.length() / total_time
       while (delta_history.length() > avg_interval)
       {
          oldest_delta = delta_history.pop()
          total_time -= oldest_delta
       }
    } 
    

提交回复
热议问题