Qt: Show FPS in title bar
Jeffrey Pfau jeffrey@endrift.com
Thu, 27 Nov 2014 17:35:25 -0800
2 files changed,
34 insertions(+),
0 deletions(-)
M
src/platform/qt/Window.cpp
→
src/platform/qt/Window.cpp
@@ -67,6 +67,7 @@ });
#endif connect(m_controller, SIGNAL(gameUnpaused(GBAThread*)), m_display, SLOT(unpauseDrawing())); connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&))); + connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(recordFrame())); connect(m_logView, SIGNAL(levelsSet(int)), m_controller, SLOT(setLogLevel(int))); connect(m_logView, SIGNAL(levelsEnabled(int)), m_controller, SLOT(enableLogLevel(int))); connect(m_logView, SIGNAL(levelsDisabled(int)), m_controller, SLOT(disableLogLevel(int)));@@ -76,8 +77,10 @@ connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide())); connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int))); connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float))); + connect(&m_fpsTimer, SIGNAL(timeout()), this, SLOT(showFPS())); m_logView->setLevels(GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL); + m_fpsTimer.setInterval(FPS_TIMER_INTERVAL); setupMenu(menuBar()); }@@ -277,6 +280,8 @@ GBAGetGameTitle(context->gba, title);
setWindowTitle(tr(PROJECT_NAME " - %1").arg(title)); attachWidget(m_display); m_screenWidget->setScaledContents(true); + + m_fpsTimer.start(); } void Window::gameStopped() {@@ -287,6 +292,8 @@ setWindowTitle(tr(PROJECT_NAME));
detachWidget(m_display); m_screenWidget->setScaledContents(false); redoLogo(); + + m_fpsTimer.stop(); } void Window::redoLogo() {@@ -296,6 +303,22 @@ }
QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio()); m_screenWidget->setPixmap(logo); +} + +void Window::recordFrame() { + m_frameList.append(QDateTime::currentDateTime()); + while (m_frameList.count() > FRAME_LIST_SIZE) { + m_frameList.removeFirst(); + } +} + +void Window::showFPS() { + qint64 interval = m_frameList.first().msecsTo(m_frameList.last()); + float fps = (m_frameList.count() - 1) * 10000.f / interval; + fps = round(fps) / 10.f; + char title[13] = { '\0' }; + GBAGetGameTitle(m_controller->thread()->gba, title); + setWindowTitle(tr(PROJECT_NAME " - %1 (%2 fps)").arg(title).arg(fps)); } void Window::openStateWindow(LoadSave ls) {
M
src/platform/qt/Window.h
→
src/platform/qt/Window.h
@@ -2,7 +2,10 @@ #ifndef QGBA_WINDOW
#define QGBA_WINDOW #include <QAudioOutput> +#include <QDateTime> +#include <QList> #include <QMainWindow> +#include <QTimer> extern "C" { #include "gba.h"@@ -76,7 +79,13 @@ void gameStarted(GBAThread*);
void gameStopped(); void redoLogo(); + void recordFrame(); + void showFPS(); + private: + static const int FPS_TIMER_INTERVAL = 2000; + static const int FRAME_LIST_SIZE = 120; + void setupMenu(QMenuBar*); void openStateWindow(LoadSave);@@ -92,6 +101,8 @@ WindowBackground* m_screenWidget;
QPixmap m_logo; ConfigController* m_config; InputController m_inputController; + QList<QDateTime> m_frameList; + QTimer m_fpsTimer; #ifdef USE_FFMPEG VideoView* m_videoView;