簡體   English   中英

C ++:計算移動FPS

[英]C++: Calculating Moving FPS

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

謝謝。

編輯:更具體地說,我只能訪問一個增量為1秒的計時器。

差點錯過最近發布的帖子。 請參閱我在那里使用指數加權移動平均線的回答。

C ++:計算游戲中的總幀數

這是示例代碼。

原來:

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;

可以保留最后100幀幀時間的循環緩沖區,並將它們平均嗎? 這將是“過去100幀的FPS”。 (或者說,99,因為你不會分享最新的時間和最老的。)

調用一些准確的系統時間,毫秒或更好。

這是一個可能適合您的解決方案。 我會用偽/ 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
}

你真正想要的是這樣的(在你的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秒。但如果你想要更詳細的幫助,你應該提到為什么你無法獲得精確的計時器(哪種架構,語言) - 否則一切都像猜測......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM