Compare commits

..

9 Commits

  1. 4
      src/Event.h
  2. 6
      src/Game.cpp
  3. 16
      src/Listener.cpp
  4. 4
      src/Listener.h
  5. 9
      src/Logger.cpp
  6. 3
      src/Player.cpp
  7. 34
      src/Queue.h
  8. 2
      src/main.cpp

@ -51,18 +51,14 @@ struct GameEvent {
virtual void print() {}; virtual void print() {};
}; };
class Player;
struct WallJumpEvent : GameEvent { struct WallJumpEvent : GameEvent {
double wallHeight = 0; double wallHeight = 0;
Player * player = nullptr;
}; };
struct WallCrashEvent : GameEvent { struct WallCrashEvent : GameEvent {
double wallHeight = 0; double wallHeight = 0;
Player * player = nullptr;
}; };
struct CoinCollectEvent : GameEvent { struct CoinCollectEvent : GameEvent {
Player * player = nullptr;
int value = 0; int value = 0;
}; };

@ -66,8 +66,12 @@ Game::Game() {
for (auto & spec : spectators) for (auto & spec : spectators)
Game::eventQueue->registerListener(&spec); Game::eventQueue->registerListener(&spec);
InputWindow::inputQueue->registerListener(&player); InputWindow::inputQueue->registerListener(&player);
Game::eventQueue->registerListener(new Logger); auto logger = new Logger;
Game::eventQueue->registerListener(logger);
Game::eventQueue->unregisterListener(logger);
Game::eventQueue->registerListener(logger);
Game::eventQueue->registerListener(new RecordTracker); Game::eventQueue->registerListener(new RecordTracker);
instance = this; instance = this;
} }

@ -3,25 +3,25 @@
void GameListener::accept(GameEvent *event) { void GameListener::accept(GameEvent *event) {
if (auto wallJumpEvent = dynamic_cast<WallJumpEvent *>(event)) if (auto wallJumpEvent = dynamic_cast<WallJumpEvent *>(event))
OnWallJumped(wallJumpEvent); OnWallJumped(wallJumpEvent);
if (auto wallCrashEvent = dynamic_cast<WallCrashEvent *>(event)) else if (auto wallCrashEvent = dynamic_cast<WallCrashEvent *>(event))
OnWallCrashed(wallCrashEvent); OnWallCrashed(wallCrashEvent);
if (auto coinCollectEvent = dynamic_cast<CoinCollectEvent *>(event)) else if (auto coinCollectEvent = dynamic_cast<CoinCollectEvent *>(event))
OnCoinCollected(coinCollectEvent); OnCoinCollected(coinCollectEvent);
} }
void InputListener::accept(InputEvent *event) { void InputListener::accept(InputEvent *event) {
if (auto mousePress = dynamic_cast<MousePressEvent *>(event)) if (auto mousePress = dynamic_cast<MousePressEvent *>(event))
mousePressed(mousePress); mousePressed(mousePress);
if (auto mouseRelease = dynamic_cast<MouseReleaseEvent *>(event)) else if (auto mouseRelease = dynamic_cast<MouseReleaseEvent *>(event))
mouseReleased(mouseRelease); mouseReleased(mouseRelease);
if (auto mouseDoubleClick = dynamic_cast<MouseDoubleClickEvent *>(event)) else if (auto mouseDoubleClick = dynamic_cast<MouseDoubleClickEvent *>(event))
mouseDoubleClicked(mouseDoubleClick); mouseDoubleClicked(mouseDoubleClick);
if (auto mouseMove = dynamic_cast<MouseMoveEvent *>(event)) else if (auto mouseMove = dynamic_cast<MouseMoveEvent *>(event))
mouseMoved(mouseMove); mouseMoved(mouseMove);
if (auto keyPress = dynamic_cast<KeyPressEvent *>(event)) else if (auto keyPress = dynamic_cast<KeyPressEvent *>(event))
keyPressed(keyPress); keyPressed(keyPress);
if (auto keyRelease = dynamic_cast<KeyReleaseEvent *>(event)) else if (auto keyRelease = dynamic_cast<KeyReleaseEvent *>(event))
keyReleased(keyRelease); keyReleased(keyRelease);
if (auto wheel = dynamic_cast<WheelEvent *>(event)) else if (auto wheel = dynamic_cast<WheelEvent *>(event))
mouseWheel(wheel); mouseWheel(wheel);
} }

@ -3,10 +3,10 @@
#include <iostream> #include <iostream>
#include "Event.h" #include "Event.h"
template <typename EventType> template <typename T>
class Listener { class Listener {
public: public:
virtual void accept(EventType *) {} virtual void accept(T *) {}
}; };
class InputListener : public Listener<InputEvent> { class InputListener : public Listener<InputEvent> {

@ -6,18 +6,21 @@ constexpr int eventStringMax = 6;
void Logger::OnCoinCollected(CoinCollectEvent *event) { void Logger::OnCoinCollected(CoinCollectEvent *event) {
char buffer[MAX_STRING]; char buffer[MAX_STRING];
sprintf(buffer, "Game event: %*s | time: %6.2f | value: %d\n", eventStringMax, "Coin", event->time, event->value); sprintf(buffer, "Game event: %*s | time: %6.2f | value: %d\n",
eventStringMax, "Coin", event->time, event->value);
FileManager::instance->writeFile("log.txt", buffer, true); FileManager::instance->writeFile("log.txt", buffer, true);
} }
void Logger::OnWallJumped(WallJumpEvent *event) { void Logger::OnWallJumped(WallJumpEvent *event) {
char buffer[MAX_STRING]; char buffer[MAX_STRING];
sprintf(buffer, "Game event: %*s | time: %6.2f | wall_height: %6.2f\n", eventStringMax, "Jump", event->time, event->wallHeight); sprintf(buffer, "Game event: %*s | time: %6.2f | wall_height: %6.2f\n",
eventStringMax, "Jump", event->time, event->wallHeight);
FileManager::instance->writeFile("log.txt", buffer, true); FileManager::instance->writeFile("log.txt", buffer, true);
} }
void Logger::OnWallCrashed(WallCrashEvent *event) { void Logger::OnWallCrashed(WallCrashEvent *event) {
char buffer[MAX_STRING]; char buffer[MAX_STRING];
sprintf(buffer, "Game event: %*s | time: %6.2f | wall_height: %6.2f\n", eventStringMax, "Crash", event->time, event->wallHeight); sprintf(buffer, "Game event: %*s | time: %6.2f | wall_height: %6.2f\n",
eventStringMax, "Crash", event->time, event->wallHeight);
FileManager::instance->writeFile("log.txt", buffer, true); FileManager::instance->writeFile("log.txt", buffer, true);
} }

@ -35,7 +35,6 @@ void Player::update(float dTime, std::vector<Wall> &walls, std::vector<Coin>& co
wall.jumped = true; wall.jumped = true;
auto e = new WallJumpEvent; auto e = new WallJumpEvent;
e->time = Game::instance->eTime; e->time = Game::instance->eTime;
e->player = this;
e->wallHeight = wall.size.height(); e->wallHeight = wall.size.height();
Game::eventQueue->submitEvent(e); Game::eventQueue->submitEvent(e);
} else { } else {
@ -43,7 +42,6 @@ void Player::update(float dTime, std::vector<Wall> &walls, std::vector<Coin>& co
auto e = new WallCrashEvent; auto e = new WallCrashEvent;
e->time = Game::instance->eTime; e->time = Game::instance->eTime;
e->wallHeight = wall.size.height(); e->wallHeight = wall.size.height();
e->player = this;
Game::eventQueue->submitEvent(e); Game::eventQueue->submitEvent(e);
} }
} }
@ -56,7 +54,6 @@ void Player::update(float dTime, std::vector<Wall> &walls, std::vector<Coin>& co
coin.collected = true; coin.collected = true;
auto e = new CoinCollectEvent; auto e = new CoinCollectEvent;
e->time = Game::instance->eTime; e->time = Game::instance->eTime;
e->player = this;
e->value = coin.value; e->value = coin.value;
Game::eventQueue->submitEvent(e); Game::eventQueue->submitEvent(e);
} }

@ -17,29 +17,18 @@ class Queue {
std::mutex mutex; std::mutex mutex;
void run() { void run() {
while (!QApplication::closingDown()){ while (!QApplication::closingDown()){
if (!isEmpty()) if (tail != head)
update(); update();
std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} }
}; };
virtual void update() {} virtual void update() {}
// First in: points to next free slot
// First in
size_t tail = 0; size_t tail = 0;
// First out: points to leaving element's position
// First out
size_t head = 0; size_t head = 0;
T * container[MAX_QUEUE_SIZE]; T * container[MAX_QUEUE_SIZE];
protected: protected:
bool isEmpty() const {
return tail == head;
}
size_t size() const {
if (tail > head)
return tail - head;
return MAX_QUEUE_SIZE - head + tail;
}
T * pop(){ T * pop(){
mutex.lock(); mutex.lock();
T * element = container[head]; T * element = container[head];
@ -53,18 +42,21 @@ public:
} }
void push(T * element){ void push(T * element){
mutex.lock(); mutex.lock();
if ((tail + 1) % MAX_QUEUE_SIZE != head){
container[tail] = element; container[tail] = element;
tail = (tail + 1) % MAX_QUEUE_SIZE; tail = (tail + 1) % MAX_QUEUE_SIZE;
} else
std::cout << "Queue is full!\n";
mutex.unlock(); mutex.unlock();
} }
}; };
template <typename EventType> template <typename T>
class EventQueue : protected Queue<EventType>{ class EventQueue : private Queue<T>{
std::set<Listener<EventType> *> listeners; std::set<Listener<T> *> listeners;
void update() override { void update() override {
EventType * event = this->pop(); T * event = this->pop();
for (auto listener : listeners) for (auto listener : listeners)
listener->accept(event); listener->accept(event);
@ -72,13 +64,13 @@ class EventQueue : protected Queue<EventType>{
delete event; delete event;
} }
public: public:
void submitEvent(EventType * event){ void submitEvent(T * event){
this->push(event); this->push(event);
} }
void registerListener(Listener<EventType> * listener){ void registerListener(Listener<T> * listener){
listeners.insert(listener); listeners.insert(listener);
} }
void unregisterListener(Listener<EventType> * listener){ void unregisterListener(Listener<T> * listener){
listeners.erase(listener); listeners.erase(listener);
} }
}; };

@ -8,7 +8,7 @@ int main(int argc, char * argv[]){
QApplication app(argc, argv); QApplication app(argc, argv);
Window w; Window w;
w.resize(800, 400); w.resize(WIDTH, HEIGHT);
w.show(); w.show();
return QApplication::exec(); return QApplication::exec();

Loading…
Cancel
Save