add fps counter

main
Benjamin Kraft 1 year ago
parent 47258f88af
commit f66c5743e6
  1. 31
      src/FPS.cpp
  2. 20
      src/FPS.h
  3. 23
      src/GLWidget.cpp
  4. 9
      src/GLWidget.h
  5. 3
      src/MainWindow.cpp
  6. 3
      src/MainWindow.h
  7. 3
      src/Pendulum.cpp
  8. 1
      src/Simulation.cpp

@ -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();
};

@ -6,19 +6,26 @@
#include "Simulation.h" #include "Simulation.h"
#include <QApplication> #include <QApplication>
#include <QDialog> #include <QDialog>
#include "FPS.h"
GLWidget::GLWidget(Simulation * simulation) : simulation(simulation) { GLWidget::GLWidget(Simulation * simulation) : simulation(simulation) {
startTimer(1000 / 60); startTimer(1000 / 144);
previousFrame = high_resolution_clock::now(); fps = new FPS;
fps->setUpdateInterval(500);
} }
void GLWidget::paintEvent(QPaintEvent *e) { void GLWidget::paintEvent(QPaintEvent *e) {
fps->newFrame();
updateFPS(); QString fpsString = "FPS: " + QString::fromStdString(std::to_string(fps->current));
auto p = new QPainter(this); auto p = new QPainter(this);
p->setRenderHint(QPainter::Antialiasing); p->setRenderHint(QPainter::Antialiasing);
p->fillRect(e->rect(), QColor(30, 30, 30)); p->fillRect(e->rect(), QColor(30, 30, 30));
p->setPen(Qt::white);
auto font = p->font();
font.setPixelSize(20);
p->setFont(font);
p->drawText(0, 0, 400, 400, Qt::AlignTop | Qt::AlignLeft, fpsString);
p->translate(e->rect().center()); p->translate(e->rect().center());
simulation->draw(p, std::min(width(), height())); simulation->draw(p, std::min(width(), height()));
@ -31,14 +38,6 @@ void GLWidget::timerEvent(QTimerEvent *e) {
update(); update();
} }
void GLWidget::updateFPS() {
auto thisFrame = high_resolution_clock::now();
auto diff = (int)duration_cast<milliseconds>(thisFrame - previousFrame).count();
currentFPS = 1000 / (diff == 0 ? 1 : diff);
// std::cout << currentFPS << std::endl;
previousFrame = thisFrame;
}
bool GLWidget::AnyDialogOpen() { bool GLWidget::AnyDialogOpen() {
for (auto widget : QApplication::topLevelWidgets()) for (auto widget : QApplication::topLevelWidgets())
if (auto dialog = qobject_cast<QDialog*>(widget)) if (auto dialog = qobject_cast<QDialog*>(widget))

@ -1,10 +1,10 @@
#include <QOpenGLWidget> #include <QOpenGLWidget>
#include <QOpenGLFunctions> #include <QOpenGLFunctions>
#include <chrono>
using namespace std::chrono;
class Simulation; class Simulation;
class FPS;
class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions { class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions {
public: public:
@ -14,9 +14,6 @@ protected:
void timerEvent(QTimerEvent* e) override; void timerEvent(QTimerEvent* e) override;
private: private:
Simulation * simulation; Simulation * simulation;
time_point<system_clock> previousFrame; FPS * fps;
int currentFPS;
void updateFPS();
static bool AnyDialogOpen(); static bool AnyDialogOpen();
}; };

@ -437,8 +437,7 @@ void MainWindow::toggleSimulation() {
simulation->isPlaying = !simulation->isPlaying; simulation->isPlaying = !simulation->isPlaying;
} }
void MainWindow::closeEvent(QCloseEvent *e) { MainWindow::~MainWindow() {
simulationThread->quit(); simulationThread->quit();
simulationThread->wait(); simulationThread->wait();
e->accept();
} }

@ -13,8 +13,7 @@ class MainWindow : public QWidget {
Q_OBJECT Q_OBJECT
public: public:
explicit MainWindow(); explicit MainWindow();
protected: ~MainWindow() override;
void closeEvent(QCloseEvent*) override;
private: private:
void buildUI(); void buildUI();
QWidget * buildAddUI(); QWidget * buildAddUI();

@ -30,9 +30,10 @@ void Pendulum::draw(QPainter *p, double scale) const {
} }
p->drawPath(path); p->drawPath(path);
p->setPen(Qt::NoPen);
p->setBrush(Qt::white); p->setBrush(Qt::white);
for (int i = 0; i < M.size(); i++){ for (int i = 0; i < M.size(); i++){
double r = sqrt(M[i]) * scale / 10; double r = sqrt(M[i]) * scale / 30;
p->drawEllipse(QPointF(X[i].x, X[i].y) * scale, r * 2, r * 2); p->drawEllipse(QPointF(X[i].x, X[i].y) * scale, r * 2, r * 2);
} }
} }

@ -24,7 +24,6 @@ void Simulation::draw(QPainter *p, int screenSize) {
void Simulation::update() { void Simulation::update() {
auto thisUpdate = high_resolution_clock::now(); auto thisUpdate = high_resolution_clock::now();
auto ms = (int)duration_cast<milliseconds>(thisUpdate - lastUpdate).count(); auto ms = (int)duration_cast<milliseconds>(thisUpdate - lastUpdate).count();
// std::cout << ms << std::endl;
lastUpdate = thisUpdate; lastUpdate = thisUpdate;
if (!isPlaying) if (!isPlaying)

Loading…
Cancel
Save