From a80adc6f44cb63a7fb5fff2bb5f4bda89eeacae4 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Mon, 23 Jan 2023 14:47:17 +0100 Subject: [PATCH] better window and singletons for queues --- src/Audio.cpp | 4 +++- src/Audio.h | 7 ++++--- src/Game.cpp | 15 +++++++++++++-- src/Game.h | 9 +++++++-- src/InputWindow.cpp | 4 +++- src/InputWindow.h | 2 +- src/Player.cpp | 24 ++++++++++++++++-------- src/Player.h | 13 ++++++++++--- src/Queue.h | 2 +- src/Window.cpp | 10 ++++++---- src/main.cpp | 1 + 11 files changed, 65 insertions(+), 26 deletions(-) diff --git a/src/Audio.cpp b/src/Audio.cpp index 33a66ae..9749022 100644 --- a/src/Audio.cpp +++ b/src/Audio.cpp @@ -12,5 +12,7 @@ void Audio::update() { void Audio::playSound(double volume, unsigned int soundId) { auto command = new AudioCommand {volume, soundId}; - push(command); + instance->push(command); } + +Audio * Audio::instance = new Audio; diff --git a/src/Audio.h b/src/Audio.h index f3e5b2a..4686053 100644 --- a/src/Audio.h +++ b/src/Audio.h @@ -6,9 +6,10 @@ struct AudioCommand { double volume; unsigned soundId; }; - class Audio : private Queue { + static Audio * instance; void update() override; public: - void playSound(double volume, unsigned soundId); -}; \ No newline at end of file + static void playSound(double volume, unsigned soundId); +}; + diff --git a/src/Game.cpp b/src/Game.cpp index 78a717f..eb25d1a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,10 +1,21 @@ #include #include "Game.h" +#include "InputWindow.h" void Game::draw(QPixmap &output) { QPainter p(&output); double w = output.size().width(); double h = output.size().height(); - p.fillRect(w / 2 - 10, h / 2 - 10, 20, 20, Qt::red); - p.drawEllipse(0, 0, 20, 20); + + player.draw(p); +} + +void Game::update(float dTime) { + player.update(dTime); } + +Game::Game() { + InputWindow::inputQueue->registerListener(&player); +} + +GameQueue * Game::eventQueue = new GameQueue; diff --git a/src/Game.h b/src/Game.h index 28b3d99..d825e8e 100644 --- a/src/Game.h +++ b/src/Game.h @@ -1,10 +1,15 @@ #pragma once #include "Queue.h" +#include "Player.h" +#include "Spectator.h" class Game { - GameQueue * eventQueue = new GameQueue; - + Player player; + std::vector spectators; public: + static GameQueue * eventQueue; + explicit Game(); void draw(QPixmap & output); + void update(float dTime); }; diff --git a/src/InputWindow.cpp b/src/InputWindow.cpp index 25ee65c..778b449 100644 --- a/src/InputWindow.cpp +++ b/src/InputWindow.cpp @@ -26,4 +26,6 @@ void InputWindow::keyPressEvent(QKeyEvent *event) { void InputWindow::keyReleaseEvent(QKeyEvent *event) { inputQueue->submitEvent(new KeyReleaseEvent(event->key())); -} \ No newline at end of file +} + +InputQueue * InputWindow::inputQueue = new InputQueue ; \ No newline at end of file diff --git a/src/InputWindow.h b/src/InputWindow.h index 8fde279..2c07857 100644 --- a/src/InputWindow.h +++ b/src/InputWindow.h @@ -5,7 +5,7 @@ class InputWindow : public QWidget { public: - InputQueue * inputQueue = new InputQueue; + static InputQueue * inputQueue; protected: void mousePressEvent(QMouseEvent * event) override; void mouseReleaseEvent(QMouseEvent * event) override; diff --git a/src/Player.cpp b/src/Player.cpp index 3daf971..91b7fd0 100644 --- a/src/Player.cpp +++ b/src/Player.cpp @@ -2,17 +2,25 @@ #include void Player::mousePressed(MouseEvent *event) { - std::cout << event->pos.x() << " pressed\n"; + if (pos.y() == ground) + vel += {0, -300}; } -void Player::mouseReleased(MouseEvent *event) { - std::cout << event->pos.x() << " released\n"; -} +void Player::draw(QPainter &painter) { + painter.save(); + + painter.translate(pos.toPoint()); + painter.fillRect(-5, -5, 10, 10, Qt::black); -void Player::mouseDoubleClicked(MouseEvent * event) { - std::cout << event->pos.x() << " double clicked\n"; + painter.restore(); } -void Player::mouseWheel(WheelEvent * event) { - std::cout << event->delta << " wheel" << std::endl; +void Player::update(float dTime) { + pos += vel * dTime; + vel += acc * dTime; + + if (pos.y() > ground){ + pos.setY(ground); + vel = {}; + } } diff --git a/src/Player.h b/src/Player.h index 37e4bf5..bedeea1 100644 --- a/src/Player.h +++ b/src/Player.h @@ -3,11 +3,18 @@ #include "Listener.h" #include "Event.h" #include "Entity.h" +#include class Player : public Entity, public GameListener, public InputListener { + QVector2D pos {50, 50}; + QVector2D vel {}; + QVector2D acc {0, 9.81 * 30}; + + float ground = 400; +public: + void draw(QPainter &painter); + void update(float dTime); + protected: void mousePressed(MouseEvent *) override; - void mouseReleased(MouseEvent *) override; - void mouseDoubleClicked(MouseEvent *) override; - void mouseWheel(WheelEvent *) override; }; \ No newline at end of file diff --git a/src/Queue.h b/src/Queue.h index b5810cc..6241308 100644 --- a/src/Queue.h +++ b/src/Queue.h @@ -69,4 +69,4 @@ public: }; using InputQueue = EventQueue; -using GameQueue = EventQueue; +using GameQueue = EventQueue; \ No newline at end of file diff --git a/src/Window.cpp b/src/Window.cpp index 0e03a56..015b0a9 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -4,18 +4,20 @@ Window::Window() { timer = new QTimer(this); timer->setInterval(17); - connect(timer, &QTimer::timeout, this, [this]{ update(); }); + connect(timer, &QTimer::timeout, this, [this]{ + update(); + game.update(17.f / 1000); + }); timer->start(); - label = new QLabel(this); label->resize(size()); } void Window::paintEvent(QPaintEvent *event) { - QPixmap output(label->size()); + QPixmap output(1000, 500); output.fill(Qt::white); game.draw(output); - label->setPixmap(output); + label->setPixmap(output.scaled(label->size())); } void Window::resizeEvent(QResizeEvent *e) { diff --git a/src/main.cpp b/src/main.cpp index dabe693..f1fccea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ int main(int argc, char * argv[]){ QApplication app(argc, argv); Window w; + w.resize(800, 400); w.show(); return QApplication::exec();