C ++:计算移动FPS

时间:2011-01-14 02:27:54

标签: c++ frame-rate

我想计算游戏最后2-4秒的FPS。最好的方法是什么?

感谢。

编辑:更具体地说,我只能以一秒的增量访问计时器。

4 个答案:

答案 0 :(得分:10)

近期发布的一篇文章。请参阅我在那里使用指数加权移动平均线的回应。

C++: Counting total frames in a game

这是示例代码。

最初:

avgFps = 1.0; // Initial value should be an estimate, but doesn't matter much.

每秒(假设最后一秒的总帧数在framesThisSecond):

// Choose alpha depending on how fast or slow you want old averages to decay.
// 0.9 is usually a good choice.
avgFps = alpha * avgFps + (1.0 - alpha) * framesThisSecond;

答案 1 :(得分:1)

可以为最后100帧保留帧时间的循环缓冲区,并将它们平均吗?这将是“过去100帧的FPS”。 (或者说,99,因为你不会分享最新的时间和最老的时间。)

调用一些准确的系统时间,毫秒或更好。

答案 2 :(得分:1)

这是一个可能适合您的解决方案。我会用伪/ C写这个,但你可以将这个想法改编成你的游戏引擎。

const int trackedTime = 3000; // 3 seconds
int frameStartTime; // in milliseconds
int queueAggregate = 0;
queue<int> frameLengths;

void onFrameStart()
{
    frameStartTime = getCurrentTime();
}

void onFrameEnd()
{
    int frameLength = getCurrentTime() - frameStartTime;

    frameLengths.enqueue(frameLength);
    queueAggregate += frameLength;

    while (queueAggregate > trackedTime)
    {
        int oldFrame = frameLengths.dequeue();
        queueAggregate -= oldFrame;
    }

    setAverageFps(frameLength.count() / 3); // 3 seconds
}

答案 3 :(得分:0)

你真正想要的是这样的(在你的mainLoop中):

frames++;
if(time<secondsTimer()){
  time = secondsTimer();
  printf("Average FPS from the last 2 seconds: %d",(frames+lastFrames)/2);
  lastFrames = frames;
  frames = 0;
}

如果你知道,如何处理结构/数组,你应该很容易将这个例子扩展到4秒而不是2秒。但如果你想要更详细的帮助,你应该提一下你为什么没有访问权限一个精确的计时器(建筑,语言) - 否则一切都像猜测...

相关问题