-
Notifications
You must be signed in to change notification settings - Fork 6
/
fpstext.cpp
48 lines (38 loc) · 1 KB
/
fpstext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "fpstext.h"
FPSText::FPSText(QQuickItem *parent)
: QQuickPaintedItem(parent)
, _currentFPS(0)
, _cacheCount(0)
{
_times.clear();
setFlag(QQuickItem::ItemHasContents);
}
FPSText::~FPSText() {}
void FPSText::recalculateFPS()
{
qint64 currentTime = QDateTime::currentDateTime().toMSecsSinceEpoch();
_times.push_back(currentTime);
while (_times[0] < currentTime - 1000) {
_times.pop_front();
}
int currentCount = _times.length();
_currentFPS = (currentCount + _cacheCount) / 2;
if (currentCount != _cacheCount) {
emit fpsChanged(_currentFPS);
}
_cacheCount = currentCount;
}
int FPSText::fps() const
{
return _currentFPS;
}
void FPSText::paint(QPainter *painter)
{
recalculateFPS();
QBrush brush(Qt::yellow);
painter->setBrush(brush);
painter->setPen(Qt::NoPen);
painter->setRenderHint(QPainter::Antialiasing);
painter->drawRoundedRect(0, 0, boundingRect().width(), boundingRect().height(), 0, 0);
update();
}