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 <QApplication>
#include <QDialog>
#include "FPS.h"
GLWidget::GLWidget(Simulation * simulation) : simulation(simulation) {
startTimer(1000 / 60);
previousFrame = high_resolution_clock::now();
startTimer(1000 / 144);
fps = new FPS;
fps->setUpdateInterval(500);
}
void GLWidget::paintEvent(QPaintEvent *e) {
updateFPS();
fps->newFrame();
QString fpsString = "FPS: " + QString::fromStdString(std::to_string(fps->current));
auto p = new QPainter(this);
p->setRenderHint(QPainter::Antialiasing);
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());
simulation->draw(p, std::min(width(), height()));
@ -31,14 +38,6 @@ void GLWidget::timerEvent(QTimerEvent *e) {
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() {
for (auto widget : QApplication::topLevelWidgets())
if (auto dialog = qobject_cast<QDialog*>(widget))

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

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

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

@ -30,9 +30,10 @@ void Pendulum::draw(QPainter *p, double scale) const {
}
p->drawPath(path);
p->setPen(Qt::NoPen);
p->setBrush(Qt::white);
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);
}
}

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

Loading…
Cancel
Save