better fps counter
This commit is contained in:
parent
9e3db3865a
commit
620d7df720
|
@ -1,59 +0,0 @@
|
|||
import QtQuick 2.0
|
||||
import QtQuick.Window 2.2
|
||||
|
||||
Rectangle {
|
||||
id: root
|
||||
property int frameCounter: 0
|
||||
property int frameCounterAvg: 0
|
||||
property int counter: 0
|
||||
property int fps: 0
|
||||
property int fpsAvg: 0
|
||||
|
||||
readonly property real dp: Screen.pixelDensity * 25.4/160
|
||||
|
||||
color: "black"
|
||||
width: childrenRect.width + 10*dp;
|
||||
height: childrenRect.height + 10*dp;
|
||||
|
||||
Image {
|
||||
id: spinnerImage
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
x: 4 * dp
|
||||
width: 36 * dp
|
||||
height: width
|
||||
source: "images/spinner.png"
|
||||
NumberAnimation on rotation {
|
||||
from:0
|
||||
to: 360
|
||||
duration: 800
|
||||
loops: Animation.Infinite
|
||||
}
|
||||
onRotationChanged: frameCounter++;
|
||||
}
|
||||
|
||||
Text {
|
||||
anchors.left: spinnerImage.right
|
||||
anchors.leftMargin: 8 * dp
|
||||
anchors.verticalCenter: spinnerImage.verticalCenter
|
||||
color: "#c0c0c0"
|
||||
font.pixelSize: 18 * dp
|
||||
text: "Ø " + root.fpsAvg + " | " + root.fps + " fps"
|
||||
}
|
||||
|
||||
Timer {
|
||||
interval: 2000
|
||||
repeat: true
|
||||
running: true
|
||||
onTriggered: {
|
||||
frameCounterAvg += frameCounter;
|
||||
root.fps = frameCounter/2;
|
||||
counter++;
|
||||
frameCounter = 0;
|
||||
if (counter >= 3) {
|
||||
root.fpsAvg = frameCounterAvg/(2*counter)
|
||||
frameCounterAvg = 0;
|
||||
counter = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ CONFIG += c++11
|
|||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
SOURCES += \
|
||||
frequencymonitor.cpp \
|
||||
main.cpp
|
||||
|
||||
RESOURCES += qml.qrc
|
||||
|
@ -21,3 +22,6 @@ QML_DESIGNER_IMPORT_PATH =
|
|||
qnx: target.path = /tmp/$${TARGET}/bin
|
||||
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
||||
!isEmpty(target.path): INSTALLS += target
|
||||
|
||||
HEADERS += \
|
||||
frequencymonitor.h
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
#include "frequencymonitor.h"
|
||||
#include <QQuickWindow>
|
||||
|
||||
FrequencyMonitor::FrequencyMonitor(QQuickItem *parent) :
|
||||
QQuickItem(parent),
|
||||
m_counter(0),
|
||||
m_refreshPeriod(1000)
|
||||
{
|
||||
connect(this, &FrequencyMonitor::windowChanged, this, &FrequencyMonitor::handleWindowChanged);
|
||||
}
|
||||
|
||||
int FrequencyMonitor::fps() const
|
||||
{
|
||||
return m_fps;
|
||||
}
|
||||
|
||||
void FrequencyMonitor::setFps(int fps)
|
||||
{
|
||||
if (m_fps == fps)
|
||||
return;
|
||||
|
||||
m_fps = fps;
|
||||
emit fpsChanged();
|
||||
}
|
||||
|
||||
int FrequencyMonitor::refreshPeriod() const
|
||||
{
|
||||
return m_refreshPeriod;
|
||||
}
|
||||
|
||||
void FrequencyMonitor::setRefreshPeriod(int msec)
|
||||
{
|
||||
if (m_refreshPeriod == msec)
|
||||
return;
|
||||
|
||||
m_refreshPeriod = msec;
|
||||
emit refreshPeriodChanged();
|
||||
}
|
||||
|
||||
void FrequencyMonitor::handleAfterRendering()
|
||||
{
|
||||
const int elapsedMsec = m_time.elapsed();
|
||||
|
||||
m_counter++;
|
||||
|
||||
if (elapsedMsec >= m_refreshPeriod)
|
||||
{
|
||||
setFps(m_counter / (elapsedMsec / 1000.0));
|
||||
|
||||
m_counter = 0;
|
||||
m_time.restart();
|
||||
}
|
||||
}
|
||||
|
||||
void FrequencyMonitor::handleWindowChanged(QQuickWindow *window)
|
||||
{
|
||||
if (m_windowConnection)
|
||||
this->disconnect(m_windowConnection);
|
||||
|
||||
if (window)
|
||||
{
|
||||
m_time.restart();
|
||||
m_windowConnection = connect(window, &QQuickWindow::afterRendering, this, &FrequencyMonitor::handleAfterRendering);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
#ifndef FREQUENCYMONITOR_H
|
||||
#define FREQUENCYMONITOR_H
|
||||
|
||||
#include <QQuickItem>
|
||||
#include <QTime>
|
||||
#include <QMetaObject>
|
||||
|
||||
class QQuickWindow;
|
||||
|
||||
class FrequencyMonitor : public QQuickItem
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(int fps READ fps NOTIFY fpsChanged)
|
||||
Q_PROPERTY(int refreshPeriod READ refreshPeriod WRITE setRefreshPeriod NOTIFY refreshPeriodChanged)
|
||||
public:
|
||||
explicit FrequencyMonitor(QQuickItem *parent = 0);
|
||||
|
||||
int fps() const;
|
||||
void setFps(int fps);
|
||||
|
||||
// by default refresh one time per 1000 msec
|
||||
int refreshPeriod() const;
|
||||
void setRefreshPeriod(int msec);
|
||||
|
||||
signals:
|
||||
void fpsChanged();
|
||||
void refreshPeriodChanged();
|
||||
|
||||
public slots:
|
||||
void handleAfterRendering();
|
||||
void handleWindowChanged(QQuickWindow * window);
|
||||
|
||||
protected:
|
||||
QMetaObject::Connection m_windowConnection;
|
||||
QTime m_time;
|
||||
int m_fps;
|
||||
int m_counter;
|
||||
int m_refreshPeriod;
|
||||
|
||||
};
|
||||
|
||||
#endif // FREQUENCYMONITOR_H
|
|
@ -1,12 +1,18 @@
|
|||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include "frequencymonitor.h"
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
||||
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
|
||||
qmlRegisterType<FrequencyMonitor>("be.mindgoo.tools", 0, 1, "FrequencyMonitor");
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
const QUrl url(QStringLiteral("qrc:/main.qml"));
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import QtQuick 2.12
|
||||
import QtQuick.Window 2.12
|
||||
import QtQuick.Controls 2.3
|
||||
import be.mindgoo.tools 0.1
|
||||
|
||||
|
||||
Window {
|
||||
|
@ -54,13 +55,14 @@ Window {
|
|||
onClicked: rootwin.onChecked(checked)
|
||||
}
|
||||
|
||||
FpsItem {
|
||||
id: fpsItem
|
||||
anchors.verticalCenterOffset: 106
|
||||
anchors.horizontalCenterOffset: -239
|
||||
anchors.centerIn: parent
|
||||
FrequencyMonitor {
|
||||
id: monitor
|
||||
refreshPeriod: 500
|
||||
}
|
||||
|
||||
Text {
|
||||
text: monitor.fps
|
||||
}
|
||||
}
|
||||
|
||||
/*##^##
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>main.qml</file>
|
||||
<file>FpsItem.qml</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
|
Loading…
Reference in New Issue