Calculating frames per second in a game

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

    This is what I have used in many games.

    #define MAXSAMPLES 100
    int tickindex=0;
    int ticksum=0;
    int ticklist[MAXSAMPLES];
    
    /* need to zero out the ticklist array before starting */
    /* average will ramp up until the buffer is full */
    /* returns average ticks per frame over the MAXSAMPLES last frames */
    
    double CalcAverageTick(int newtick)
    {
        ticksum-=ticklist[tickindex];  /* subtract value falling off */
        ticksum+=newtick;              /* add new value */
        ticklist[tickindex]=newtick;   /* save new value so it can be subtracted later */
        if(++tickindex==MAXSAMPLES)    /* inc buffer index */
            tickindex=0;
    
        /* return average */
        return((double)ticksum/MAXSAMPLES);
    }
    

提交回复
热议问题