main
Benjamin Kraft 2 years ago
parent 3a1e1ce094
commit 01fcc2ac84
  1. 2
      src/Game.cpp
  2. 2
      src/Game.h
  3. 8
      src/Window.cpp
  4. 4
      src/Window.h

@ -1,10 +1,12 @@
#include <QPainter> #include <QPainter>
#include "Game.h" #include "Game.h"
#include "InputWindow.h" #include "InputWindow.h"
#include "Window.h"
void Game::draw(QPixmap &output) { void Game::draw(QPixmap &output) {
QPainter p(&output); QPainter p(&output);
p.drawText(50, 50, QString::fromStdString(std::to_string(int(window->currentFPS))));
for (auto & spectator : spectators) for (auto & spectator : spectators)
spectator.draw(p); spectator.draw(p);
player.draw(p); player.draw(p);

@ -9,10 +9,12 @@
#define GROUND_Y 400 #define GROUND_Y 400
#define SPEC_Y 150 #define SPEC_Y 150
class Window;
class Game { class Game {
Player player; Player player;
std::vector<Spectator> spectators {5}; std::vector<Spectator> spectators {5};
public: public:
Window * window;
float eTime = 0; float eTime = 0;
static Game * instance; static Game * instance;
static GameQueue * eventQueue; static GameQueue * eventQueue;

@ -5,9 +5,9 @@ Window::Window() {
timer = new QTimer(this); timer = new QTimer(this);
timer->setInterval(17); timer->setInterval(17);
connect(timer, &QTimer::timeout, this, [this]{ connect(timer, &QTimer::timeout, this, [this]{
update();
game.update(17.f / 1000); game.update(17.f / 1000);
}); });
game.window = this;
timer->start(); timer->start();
label = new QLabel(this); label = new QLabel(this);
label->resize(size()); label->resize(size());
@ -18,6 +18,12 @@ void Window::paintEvent(QPaintEvent *event) {
output.fill(Qt::white); output.fill(Qt::white);
game.draw(output); game.draw(output);
label->setPixmap(output.scaled(label->size())); label->setPixmap(output.scaled(label->size()));
update();
auto now = std::chrono::high_resolution_clock::now();
auto diff = duration_cast<std::chrono::nanoseconds>((now - tp));
double seconds = double(diff.count()) / 1000. / 1000. / 1000. ;
currentFPS = 1 / seconds;
tp = now;
} }
void Window::resizeEvent(QResizeEvent *e) { void Window::resizeEvent(QResizeEvent *e) {

@ -8,11 +8,15 @@
#include "Game.h" #include "Game.h"
#include <QLabel> #include <QLabel>
using namespace std::chrono;
class Window : public InputWindow { class Window : public InputWindow {
Game game; Game game;
QLabel * label; QLabel * label;
QTimer * timer; QTimer * timer;
time_point<system_clock, duration<double>> tp;
public: public:
double currentFPS = 0;
explicit Window(); explicit Window();
protected: protected:
void paintEvent(QPaintEvent *) override; void paintEvent(QPaintEvent *) override;

Loading…
Cancel
Save