From 375b25ff590c4d7394a36c61374856af9ba83e20 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Thu, 20 Oct 2022 09:29:02 -0700 Subject: [PATCH] Added event system --- src/dawn/display/RenderPipeline.hpp | 2 +- src/dawn/event/Event.hpp | 119 +++++++++++++++++++++++ src/dawnpokergame/game/DawnPokerGame.cpp | 1 + 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/dawn/event/Event.hpp diff --git a/src/dawn/display/RenderPipeline.hpp b/src/dawn/display/RenderPipeline.hpp index 7c70530c..3ab436fd 100644 --- a/src/dawn/display/RenderPipeline.hpp +++ b/src/dawn/display/RenderPipeline.hpp @@ -47,7 +47,7 @@ namespace Dawn { * @param scene Scene to render. * @param camera Camera within the scene to render. */ - virtual void renderSceneCamera(Scene &scene, Camera &camera) = 0; + virtual void renderSceneCamera(Scene &scene, Camera &camera); /** * Cleanup a render pipeline that has been initialized. diff --git a/src/dawn/event/Event.hpp b/src/dawn/event/Event.hpp new file mode 100644 index 00000000..85f22bd6 --- /dev/null +++ b/src/dawn/event/Event.hpp @@ -0,0 +1,119 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" + +namespace Dawn { + + template + struct IEventListener { + /** + * Abstracted method for C++ template reasons. Invokes the listener. + * + * @param args Arguments to pass to the listener. + */ + virtual void invoke(A... args) = 0; + }; + + template + struct EventListener : public IEventListener { + T *instance; + void (T::*callback)(A... args); + + /** + * Construct a new event listener structure. + * + * @param instance Instance that the callback belongs to. + * @param callback Callback method that invokes back. + */ + EventListener(T *instance, void (T::*callback)(A... args)) : + instance(instance), + callback(callback) + { + } + + void invoke(A... args) { + ((*this->instance).*(this->callback))(args...); + } + }; + + template + class Event { + private: + std::vector*> listeners; + + public: + /** + * Add a listener to this event. + * + * @tparam T The class that will receive the event. + * @param instance Instance of type T that will receive the callback. + * @param callback Callback method attached to T to receive the event. + * @return The initialized event listener. You don't really need this. + */ + template + EventListener * addListener( + T *instance, + void (T::*callback)(A... args) + ) { + auto listener = new EventListener(instance, callback); + this->listeners.push_back(listener); + return listener; + } + + /** + * Removes an event listener from this event. + * + * @tparam T The class that was once receiving the event. + * @param instance Instance of type T that did receive the callback. + * @param callback Callback method attached to T for the event. + */ + template + void removeListener( + T *instance, + void (T::*callback)(A... args) + ) { + auto it = this->listeners.begin(); + while(it != this->listeners.end()) { + auto listener = static_cast*>(*it); + + if(listener->instance != instance || listener->callback != callback) { + ++it; + continue; + } + + this->listeners.erase(it); + delete listener; + break; + } + } + + /** + * Invokes the event and emits to all of the listeners. + * + * @param args Arguments for this event to pass to the listeners. + */ + void invoke(A... args) { + auto it = this->listeners.begin(); + while(it != this->listeners.end()) { + (*it)->invoke(args...); + ++it; + } + } + + /** + * Disposes the event instance. Will also destroy all of the event + * listeners. + */ + ~Event() { + auto it = this->listeners.begin(); + while(it != this->listeners.end()) { + delete *it; + ++it; + } + } + }; +} \ No newline at end of file diff --git a/src/dawnpokergame/game/DawnPokerGame.cpp b/src/dawnpokergame/game/DawnPokerGame.cpp index e0f694be..6c59d1b9 100644 --- a/src/dawnpokergame/game/DawnPokerGame.cpp +++ b/src/dawnpokergame/game/DawnPokerGame.cpp @@ -4,6 +4,7 @@ // https://opensource.org/licenses/MIT #include "DawnPokerGame.hpp" +#include "event/Event.hpp" using namespace Dawn;