簡述FPS的計算方法

來源:互聯網
上載者:User

FPS是Frame per second的縮寫,即每秒的幀數.這一術語廣泛的應用於電腦圖形學,視頻採集,遊戲等。

這裡主要介紹一下視頻遊戲中的幀率,第一個First person shooter game的幀率只有大概6FPS,但是依然很成功。不過隨著硬體裝置,尤其是顯卡效能的加強,現在遊戲的幀率一般在30FPS~100FPS之間。由於每幀映像所消耗的時間不一樣,造成幀率是在不斷變化的,所以每個遊戲都會設定一個最大的幀率,以保證平滑的切換。

下面就以一段QT中的代碼介紹一下幀率的計算。

        // Timing...
        static QTime time;
        static int frames = 0;
        static bool started = false;

        if (!started || time.elapsed() > 1000) {
            qreal fps = frames * 1000. / time.elapsed();
            if (fps == 0)
                m_current_fps = "counting fps...";
            else
                m_current_fps = QString::fromLatin1("%3 FPS").arg((int) qRound(fps));

            time.start();
            started = true;
            frames = 0;

        } else {
            ++frames;

            p.setOpacity(1);
            p.setFont(QFont("times", 30));
            p.fillRect(5, height() - 40, 250, 40, Qt::white);
            p.drawText(10, height() - 8, m_current_fps);
        }
    }

 

 

 

由於一般即時的遊戲都已一個定時器不斷地重新整理畫面,所以每一幀的輸出都是通過Calibri" lang="en-US">paintEvent來完成的。將上面這段代碼放入paintEvent就可以統計出每秒的幀率。

那麼怎麼控制最大的幀率呢?

其實也很簡單,就是通過設定定時器的interval來完成的,考慮到現在顯示器的顯示頻率一般在60HZSimSun" lang="zh-CN">,所以interval一般設定為Calibri" lang="en-US">1000/60ms 比較好,即60FPS是一個理論上最大的幀率。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.