Compare commits

..

No commits in common. '2890ec0d0ab4e32112760d55f8b9ac2c82bc26fd' and '78252c94e3a3c67b173b84e60517a8a86b939211' have entirely different histories.

  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. 38
      src/Queue.h
  8. 2
      src/main.cpp

@ -51,14 +51,18 @@ 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,12 +66,8 @@ 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);
auto logger = new Logger; Game::eventQueue->registerListener(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);
else if (auto wallCrashEvent = dynamic_cast<WallCrashEvent *>(event)) if (auto wallCrashEvent = dynamic_cast<WallCrashEvent *>(event))
OnWallCrashed(wallCrashEvent); OnWallCrashed(wallCrashEvent);
else if (auto coinCollectEvent = dynamic_cast<CoinCollectEvent *>(event)) 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);
else if (auto mouseRelease = dynamic_cast<MouseReleaseEvent *>(event)) if (auto mouseRelease = dynamic_cast<MouseReleaseEvent *>(event))
mouseReleased(mouseRelease); mouseReleased(mouseRelease);
else if (auto mouseDoubleClick = dynamic_cast<MouseDoubleClickEvent *>(event)) if (auto mouseDoubleClick = dynamic_cast<MouseDoubleClickEvent *>(event))
mouseDoubleClicked(mouseDoubleClick); mouseDoubleClicked(mouseDoubleClick);
else if (auto mouseMove = dynamic_cast<MouseMoveEvent *>(event)) if (auto mouseMove = dynamic_cast<MouseMoveEvent *>(event))
mouseMoved(mouseMove); mouseMoved(mouseMove);
else if (auto keyPress = dynamic_cast<KeyPressEvent *>(event)) if (auto keyPress = dynamic_cast<KeyPressEvent *>(event))
keyPressed(keyPress); keyPressed(keyPress);
else if (auto keyRelease = dynamic_cast<KeyReleaseEvent *>(event)) if (auto keyRelease = dynamic_cast<KeyReleaseEvent *>(event))
keyReleased(keyRelease); keyReleased(keyRelease);
else if (auto wheel = dynamic_cast<WheelEvent *>(event)) 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 T> template <typename EventType>
class Listener { class Listener {
public: public:
virtual void accept(T *) {} virtual void accept(EventType *) {}
}; };
class InputListener : public Listener<InputEvent> { class InputListener : public Listener<InputEvent> {

@ -6,21 +6,18 @@ 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", sprintf(buffer, "Game event: %*s | time: %6.2f | value: %d\n", eventStringMax, "Coin", event->time, event->value);
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", sprintf(buffer, "Game event: %*s | time: %6.2f | wall_height: %6.2f\n", eventStringMax, "Jump", event->time, event->wallHeight);
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", sprintf(buffer, "Game event: %*s | time: %6.2f | wall_height: %6.2f\n", eventStringMax, "Crash", event->time, event->wallHeight);
eventStringMax, "Crash", event->time, event->wallHeight);
FileManager::instance->writeFile("log.txt", buffer, true); FileManager::instance->writeFile("log.txt", buffer, true);
} }

@ -35,6 +35,7 @@ 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 {
@ -42,6 +43,7 @@ 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);
} }
} }
@ -54,6 +56,7 @@ 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,18 +17,29 @@ class Queue {
std::mutex mutex; std::mutex mutex;
void run() { void run() {
while (!QApplication::closingDown()){ while (!QApplication::closingDown()){
if (tail != head) if (!isEmpty())
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];
@ -42,21 +53,18 @@ 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 T> template <typename EventType>
class EventQueue : private Queue<T>{ class EventQueue : protected Queue<EventType>{
std::set<Listener<T> *> listeners; std::set<Listener<EventType> *> listeners;
void update() override { void update() override {
T * event = this->pop(); EventType * event = this->pop();
for (auto listener : listeners) for (auto listener : listeners)
listener->accept(event); listener->accept(event);
@ -64,13 +72,13 @@ class EventQueue : private Queue<T>{
delete event; delete event;
} }
public: public:
void submitEvent(T * event){ void submitEvent(EventType * event){
this->push(event); this->push(event);
} }
void registerListener(Listener<T> * listener){ void registerListener(Listener<EventType> * listener){
listeners.insert(listener); listeners.insert(listener);
} }
void unregisterListener(Listener<T> * listener){ void unregisterListener(Listener<EventType> * 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(WIDTH, HEIGHT); w.resize(800, 400);
w.show(); w.show();
return QApplication::exec(); return QApplication::exec();

Loading…
Cancel
Save