diff --git a/src/FPS.cpp b/src/FPS.cpp new file mode 100644 index 0000000..0014f59 --- /dev/null +++ b/src/FPS.cpp @@ -0,0 +1,31 @@ +#include "FPS.h" +#include + +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(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); +} diff --git a/src/FPS.h b/src/FPS.h new file mode 100644 index 0000000..28ab80b --- /dev/null +++ b/src/FPS.h @@ -0,0 +1,20 @@ +#include +#include + +class QTimer; + +using namespace std::chrono; + +class FPS : public QObject { + QTimer * timer; + time_point previousFrame; + milliseconds timeSinceUpdate; + size_t framesSinceUpdate; +public: + FPS(); + size_t current; + void newFrame(); + void setUpdateInterval(int milli); +private slots: + void updateCurrent(); +}; \ No newline at end of file diff --git a/src/GLWidget.cpp b/src/GLWidget.cpp index 512d20f..e83081e 100644 --- a/src/GLWidget.cpp +++ b/src/GLWidget.cpp @@ -6,19 +6,26 @@ #include "Simulation.h" #include #include +#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(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(widget)) diff --git a/src/GLWidget.h b/src/GLWidget.h index e606a45..c740c18 100644 --- a/src/GLWidget.h +++ b/src/GLWidget.h @@ -1,10 +1,10 @@ #include #include -#include -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 previousFrame; - int currentFPS; - void updateFPS(); - + FPS * fps; static bool AnyDialogOpen(); }; \ No newline at end of file diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index a17517f..3f6caf6 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -437,8 +437,7 @@ void MainWindow::toggleSimulation() { simulation->isPlaying = !simulation->isPlaying; } -void MainWindow::closeEvent(QCloseEvent *e) { +MainWindow::~MainWindow() { simulationThread->quit(); simulationThread->wait(); - e->accept(); } diff --git a/src/MainWindow.h b/src/MainWindow.h index 433dcce..103b6d8 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -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(); diff --git a/src/Pendulum.cpp b/src/Pendulum.cpp index 6c598bd..3b6fd85 100644 --- a/src/Pendulum.cpp +++ b/src/Pendulum.cpp @@ -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); } } diff --git a/src/Simulation.cpp b/src/Simulation.cpp index a0d8a6b..c076a03 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -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(thisUpdate - lastUpdate).count(); - // std::cout << ms << std::endl; lastUpdate = thisUpdate; if (!isPlaying)