diff options
-rw-r--r-- | src/crepe/api/EventManager.cpp | 5 | ||||
-rw-r--r-- | src/crepe/api/EventManager.h | 9 | ||||
-rw-r--r-- | src/example/events.cpp | 3 |
3 files changed, 10 insertions, 7 deletions
diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 872c6eb..42bff12 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -1,5 +1,10 @@ #include "EventManager.h" +EventManager & EventManager::get_instance() { + static EventManager instance; + return instance; +} + void EventManager::dispatch_events() { for (std::vector<std::tuple<std::unique_ptr<Event>, int, std::type_index>>::iterator event_it = this->events_queue.begin(); event_it != this->events_queue.end();) { std::unique_ptr<Event>& event = std::get<0>(*event_it); diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index d3a14da..b963ce3 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -34,10 +34,7 @@ public: * * \return Reference to the EventManager instance. */ - static EventManager & get_instance() { - static EventManager instance; - return instance; - } + static EventManager & get_instance(); /** * \brief Subscribe to an event. @@ -150,7 +147,7 @@ void EventManager::trigger_event(const EventType & event, int channel) { std::type_index event_type = std::type_index(typeid(EventType)); if (channel > 0) { - std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>& handlers_map = subscribers_by_event_id[event_type]; + std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>& handlers_map = this->subscribers_by_event_id[event_type]; std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator handlers_it = handlers_map.find(channel); if (handlers_it != handlers_map.end()) { @@ -165,7 +162,7 @@ void EventManager::trigger_event(const EventType & event, int channel) { } } } else { - std::vector<std::unique_ptr<IEventHandlerWrapper>>& handlers = subscribers[event_type]; + std::vector<std::unique_ptr<IEventHandlerWrapper>>& handlers = this->subscribers[event_type]; for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it = handlers.begin(); it != handlers.end();) { // erases callback if callback function returns true if ((*it)->exec(event)) { diff --git a/src/example/events.cpp b/src/example/events.cpp index ff97cf1..af6f294 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -9,13 +9,14 @@ #include <crepe/api/GameObject.h> #include <crepe/api/Script.h> #include <crepe/api/Transform.h> - #include "crepe/api/Event.h" #include "crepe/api/EventManager.h" #include "crepe/api/IKeyListener.h" #include "crepe/api/IMouseListener.h" + using namespace crepe; using namespace std; + class MyScript : public Script, public IKeyListener,public IMouseListener{ void update() { // Retrieve component from the same GameObject this script is on |