diff --git a/src/Game.cpp b/src/Game.cpp index 73a8e02..b468fd4 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,10 +1,12 @@ #include #include "Game.h" #include "InputWindow.h" +#include "Window.h" void Game::draw(QPixmap &output) { QPainter p(&output); + p.drawText(50, 50, QString::fromStdString(std::to_string(int(window->currentFPS)))); for (auto & spectator : spectators) spectator.draw(p); player.draw(p); diff --git a/src/Game.h b/src/Game.h index 11715db..15fe7de 100644 --- a/src/Game.h +++ b/src/Game.h @@ -9,10 +9,12 @@ #define GROUND_Y 400 #define SPEC_Y 150 +class Window; class Game { Player player; std::vector spectators {5}; public: + Window * window; float eTime = 0; static Game * instance; static GameQueue * eventQueue; diff --git a/src/Window.cpp b/src/Window.cpp index 93a9da3..f87a718 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -5,9 +5,9 @@ Window::Window() { timer = new QTimer(this); timer->setInterval(17); connect(timer, &QTimer::timeout, this, [this]{ - update(); game.update(17.f / 1000); }); + game.window = this; timer->start(); label = new QLabel(this); label->resize(size()); @@ -18,6 +18,12 @@ void Window::paintEvent(QPaintEvent *event) { output.fill(Qt::white); game.draw(output); label->setPixmap(output.scaled(label->size())); + update(); + auto now = std::chrono::high_resolution_clock::now(); + auto diff = duration_cast((now - tp)); + double seconds = double(diff.count()) / 1000. / 1000. / 1000. ; + currentFPS = 1 / seconds; + tp = now; } void Window::resizeEvent(QResizeEvent *e) { diff --git a/src/Window.h b/src/Window.h index 5b4e614..a38b1e3 100644 --- a/src/Window.h +++ b/src/Window.h @@ -8,11 +8,15 @@ #include "Game.h" #include +using namespace std::chrono; + class Window : public InputWindow { Game game; QLabel * label; QTimer * timer; + time_point> tp; public: + double currentFPS = 0; explicit Window(); protected: void paintEvent(QPaintEvent *) override;