parent
47258f88af
commit
f66c5743e6
8 changed files with 69 additions and 24 deletions
@ -0,0 +1,31 @@ |
||||
#include "FPS.h" |
||||
#include <QTimer> |
||||
|
||||
FPS::FPS() { |
||||
previousFrame = high_resolution_clock::now(); |
||||
timeSinceUpdate = milliseconds(0); |
||||
framesSinceUpdate = 0; |
||||
current = 0; |
||||
|
||||
timer = new QTimer(this); |
||||
timer->setInterval(250); |
||||
connect(timer, &QTimer::timeout, this, &FPS::updateCurrent); |
||||
timer->start(); |
||||
} |
||||
|
||||
void FPS::newFrame() { |
||||
framesSinceUpdate++; |
||||
auto thisFrame = high_resolution_clock::now(); |
||||
timeSinceUpdate += duration_cast<milliseconds>(thisFrame - previousFrame); |
||||
previousFrame = thisFrame; |
||||
} |
||||
|
||||
void FPS::setUpdateInterval(int milli) { |
||||
timer->setInterval(milli); |
||||
} |
||||
|
||||
void FPS::updateCurrent() { |
||||
current = framesSinceUpdate * 1000 / timeSinceUpdate.count(); |
||||
framesSinceUpdate = 0; |
||||
timeSinceUpdate = milliseconds(0); |
||||
} |
@ -0,0 +1,20 @@ |
||||
#include <chrono> |
||||
#include <QObject> |
||||
|
||||
class QTimer; |
||||
|
||||
using namespace std::chrono; |
||||
|
||||
class FPS : public QObject { |
||||
QTimer * timer; |
||||
time_point<system_clock> previousFrame; |
||||
milliseconds timeSinceUpdate; |
||||
size_t framesSinceUpdate; |
||||
public: |
||||
FPS(); |
||||
size_t current; |
||||
void newFrame(); |
||||
void setUpdateInterval(int milli); |
||||
private slots: |
||||
void updateCurrent(); |
||||
}; |
Loading…
Reference in new issue