Calculating frames per second in a game

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

    How i do it!

    boolean run = false;
    
    int ticks = 0;
    
    long tickstart;
    
    int fps;
    
    public void loop()
    {
    if(this.ticks==0)
    {
    this.tickstart = System.currentTimeMillis();
    }
    this.ticks++;
    this.fps = (int)this.ticks / (System.currentTimeMillis()-this.tickstart);
    }
    

    In words, a tick clock tracks ticks. If it is the first time, it takes the current time and puts it in 'tickstart'. After the first tick, it makes the variable 'fps' equal how many ticks of the tick clock divided by the time minus the time of the first tick.

    Fps is an integer, hence "(int)".

    0 讨论(0)
提交回复
热议问题