Created
February 4, 2012 23:22
-
-
Save torarnv/1741055 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "fpsitem.h" | |
#include <QElapsedTimer> | |
FPSItem::FPSItem(QQuickItem *parent) | |
: QQuickItem(parent) | |
, m_averageMsPerFrame(0) | |
{ | |
setFlag(ItemHasContents); | |
} | |
int FPSItem::fps() const | |
{ | |
return 1000 / m_averageMsPerFrame; | |
} | |
class AutoStartingElapsedTimer : public QElapsedTimer | |
{ | |
public: | |
AutoStartingElapsedTimer() : QElapsedTimer() { start(); } | |
}; | |
QSGNode* FPSItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) | |
{ | |
Q_UNUSED(data); | |
static AutoStartingElapsedTimer timer; | |
static qreal smoothingFactor = 0.1; | |
m_averageMsPerFrame = (m_averageMsPerFrame * (1.0 - smoothingFactor)) | |
+ (timer.restart() * smoothingFactor); | |
emit fpsChanged(); | |
update(); // Ensure that we'll be called again for the next frame | |
return oldNode; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FPSITEM_H | |
#define FPSITEM_H | |
#include <QQuickItem> | |
class FPSItem : public QQuickItem | |
{ | |
Q_OBJECT | |
Q_PROPERTY(int fps READ fps NOTIFY fpsChanged) | |
public: | |
explicit FPSItem(QQuickItem *parent = 0); | |
int fps() const; | |
signals: | |
void fpsChanged(); | |
protected: | |
virtual QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *); | |
private: | |
qreal m_averageMsPerFrame; | |
}; | |
QML_DECLARE_TYPE(FPSItem) | |
#endif // FPSITEM_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment