From a3b166001342bb6c93ddf474ffcf5d2786b845aa Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Thu, 15 Sep 2022 01:46:11 +0800 Subject: [PATCH] SP3-108 Removed old event register method added new events define --- SHADE_Engine/SHADE_Engine.vcxproj | 4 +- SHADE_Engine/SHADE_Engine.vcxproj.filters | 4 +- SHADE_Engine/src/Events/SHEvent.cpp | 49 ---------------------- SHADE_Engine/src/Events/SHEvent.h | 43 +++++-------------- SHADE_Engine/src/Events/SHEventDefines.h | 10 +++++ SHADE_Engine/src/Events/SHEventManager.cpp | 49 +++++++++++----------- SHADE_Engine/src/Events/SHEventManager.h | 23 ++++------ SHADE_Engine/src/Events/SHEventReceiver.h | 10 ++--- 8 files changed, 61 insertions(+), 131 deletions(-) delete mode 100644 SHADE_Engine/src/Events/SHEvent.cpp create mode 100644 SHADE_Engine/src/Events/SHEventDefines.h diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 8da6f80c..9d619853 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -118,6 +118,7 @@ + @@ -204,7 +205,6 @@ - @@ -292,4 +292,4 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index f0d769f0..3cfa1b29 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -400,6 +400,7 @@ Tools + @@ -423,9 +424,6 @@ Engine - - Events - Events diff --git a/SHADE_Engine/src/Events/SHEvent.cpp b/SHADE_Engine/src/Events/SHEvent.cpp deleted file mode 100644 index 4ac7c612..00000000 --- a/SHADE_Engine/src/Events/SHEvent.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/****************************************************************************** - * \file SHEvent.cpp - * \author Loh Xiao Qi - * \brief Implementation of SHEvent.h Initialise data member of smart pointer - * to package data. Return the package pointer and package type - * accordingly. - * - * \copyright Copyright (c) 2021 Digipen Institute of Technology. Reproduction - * or disclosure of this file or its contents without the prior - * written consent of Digipen Institute of Technology is prohibited - ******************************************************************************/ -#include "SHpch.h" -#include "SHEvent.h" -#include - -namespace SHADE -{ - /**************************************************************************** - * \param PackagePtr - smart pointer to package data. - - * \brief Constructor for Event. User needs to dynamically allocate memory - * for package data and pass in the smart pointer so the data member - * can be initialised properly. - ****************************************************************************/ - SHEvent::SHEvent(PackagePtr pkg) - : package{ std::move(pkg) } - { - - } - - /**************************************************************************** - * \brief Get pointer to data package. - * - * \return Smart pointer to Package Base, needs to be cast to correct - * derived class. - ****************************************************************************/ - ConstPackagePtr& SHEvent::GetData() const - { - return package; - } - - /**************************************************************************** - * \brief Gets the type of package. - ****************************************************************************/ - SHPackageType SHEvent::GetType() const - { - return package->GetPackageType(); - } -} \ No newline at end of file diff --git a/SHADE_Engine/src/Events/SHEvent.h b/SHADE_Engine/src/Events/SHEvent.h index fc128a17..c6d5c97b 100644 --- a/SHADE_Engine/src/Events/SHEvent.h +++ b/SHADE_Engine/src/Events/SHEvent.h @@ -1,45 +1,22 @@ /****************************************************************************** - * \file SHEventBase.h + * \file SHEvent.h * \author Loh Xiao Qi - * \brief Event base abstract class declaration + * \brief Event class declaration * - * \copyright Copyright (c) 2021 Digipen Institute of Technology. Reproduction + * \copyright Copyright (c) 2022 Digipen Institute of Technology. Reproduction * or disclosure of this file or its contents without the prior written consent * of Digipen Institute of Technology is prohibited. ******************************************************************************/ -#ifndef SH_EVENT_BASE_H -#define SH_EVENT_BASE_H +#pragma once + +#include "SHEventDefines.h" namespace SHADE { - class SHEvent + struct SHEvent { - public: - /**************************************************************************** - * \param PackagePtr - smart pointer to package data. - - * \brief Constructor for Event. User needs to dynamically allocate memory - * for package data and pass in the smart pointer so the data member - * can be initialised properly. - ****************************************************************************/ - SHEvent(PackagePtr); - - /**************************************************************************** - * \brief Get pointer to data package. - * - * \return Smart pointer to Package Base, needs to be cast to correct - * derived class. - ****************************************************************************/ - PackagePtr const& GetData() const; - - /**************************************************************************** - * \brief Gets the type of package. - ****************************************************************************/ - SHPackageType GetType() const; - private: - PackagePtr package; + SHEventIdentifier type; + SHEventDataPtr data; + SHEventHandle handle; }; } - - -#endif // !SH_EVENT_BASE_H diff --git a/SHADE_Engine/src/Events/SHEventDefines.h b/SHADE_Engine/src/Events/SHEventDefines.h new file mode 100644 index 00000000..4087caa0 --- /dev/null +++ b/SHADE_Engine/src/Events/SHEventDefines.h @@ -0,0 +1,10 @@ +#pragma once +#include "SHpch.h" + +typedef uint32_t SHEventIdentifier; +typedef uint32_t SHEventHandle; +typedef void* SHEventDataPtr; + +//Add your event identifiers here: + +constexpr SHEventIdentifier SH_EXAMPLE_EVENT{0}; \ No newline at end of file diff --git a/SHADE_Engine/src/Events/SHEventManager.cpp b/SHADE_Engine/src/Events/SHEventManager.cpp index b1038da8..7798c944 100644 --- a/SHADE_Engine/src/Events/SHEventManager.cpp +++ b/SHADE_Engine/src/Events/SHEventManager.cpp @@ -72,20 +72,33 @@ namespace SHADE /**************************************************************************** * \param ResponseFunction - function pointer from receiver to be passed * into event manager to be called when events are broadcasted. - * \param SHPackageType - package type that corresponding subscriber is + * \param SHEventIdentifier - package type that corresponding subscriber is * subscribing to. * \brief Links a function pointer from a subscriber to a particular * package type ****************************************************************************/ - void SHEventManager::SubscribeTo(SHPackageType pkgType, ReceiverPtr receiver) + void SHEventManager::SubscribeTo(SHEventIdentifier pkgType, ReceiverPtr receiver) { SHEventManager::GetEventManagerInstance().RegisterReceiverToType(pkgType, receiver); } - void SHEventManager::StaticSubscribeTo(SHPackageType pkgType, ResponseFunction func) + template + T* SHEventManager::BroadcastData(T data, SHEventIdentifier eventType) { - SHEventManager::GetEventManagerInstance().RegisterStaticReceiverToType(pkgType, func); + SHEventDataPtr ptr = new std::byte[sizeof(data)]; + + std::memcpy(ptr, &data, sizeof(data)); + + CatchEvent( + { + eventType, + ptr, + handleCounter++ + } + ); + + return reinterpret_cast(ptr); } /**************************************************************************** @@ -96,7 +109,7 @@ namespace SHADE * \brief Registers receiver as a subscriber to listener in the registry. ****************************************************************************/ void SHEventManager::RegisterReceiverToType( - SHPackageType pkgType, ReceiverPtr receiver) + SHEventIdentifier pkgType, ReceiverPtr receiver) { if (packageReceiverRegistry.contains(pkgType)) { @@ -108,18 +121,6 @@ namespace SHADE } } - void SHEventManager::RegisterStaticReceiverToType(SHPackageType pkgType, ResponseFunction func) - { - if (staticPackageReceiverRegistry.contains(pkgType)) - { - staticPackageReceiverRegistry[pkgType].emplace_back(func); - } - else - { - staticPackageReceiverRegistry.emplace(pkgType, std::vector{ func }); - } - } - /**************************************************************************** * \param ListenerConstPtr - Const pointer to listener that sent event. * \param EventType - Event data @@ -129,16 +130,16 @@ namespace SHADE ****************************************************************************/ void SHEventManager::Broadcast(SHEvent const& event) { - ResponseVec& receivers{ packageReceiverRegistry[event.GetType()] }; + ResponseVec& receivers{ packageReceiverRegistry[event.type] }; for (auto& receiver : receivers) { - receiver->Receive(event.GetData()); + receiver->Receive(event.data); } - auto& funcs{ staticPackageReceiverRegistry[event.GetType()] }; - for (auto func : funcs) - { - func(event.GetData()); - } + //auto& funcs{ staticPackageReceiverRegistry[event.GetType()] }; + //for (auto func : funcs) + //{ + // func(event.GetData()); + //} } } diff --git a/SHADE_Engine/src/Events/SHEventManager.h b/SHADE_Engine/src/Events/SHEventManager.h index e8d499dc..4f9b4350 100644 --- a/SHADE_Engine/src/Events/SHEventManager.h +++ b/SHADE_Engine/src/Events/SHEventManager.h @@ -12,13 +12,12 @@ #include "SHEvent.h" #include "SHEventReceiver.h" -#include #include #include namespace SHADE { - using ResponseFunction = std::function; + using ResponseFunction = std::function; using ReceiverPtr = std::shared_ptr; using ResponseVec = std::vector; using StaticResponseVec = std::vector; @@ -68,22 +67,19 @@ namespace SHADE * \brief Links a function pointer from a subscriber to a particular * package type ****************************************************************************/ - void SubscribeTo(SHPackageType, ReceiverPtr); + void SubscribeTo(SHEventIdentifier, ReceiverPtr); - void StaticSubscribeTo(SHPackageType, ResponseFunction); + template + T* BroadcastData(T data, SHEventIdentifier eventType); private: // Singleton instance static SHEventManager* instance; // Registry for broadcasters and subscribers - std::unordered_map< - SHPackageType, - ResponseVec> packageReceiverRegistry; + std::unordered_map packageReceiverRegistry; + std::unordered_map dataEventMap; - // Registry for static broadcasters and subscribers - std::unordered_map< - SHPackageType, - StaticResponseVec> staticPackageReceiverRegistry; + SHEventHandle handleCounter; /**************************************************************************** * \param ListenerConstPtr - Const pointer to listener that sent event. @@ -101,12 +97,9 @@ namespace SHADE * \brief Registers receiver as a subscriber to listener in the registry. ****************************************************************************/ - void RegisterReceiverToType(SHPackageType, ReceiverPtr); - - void RegisterStaticReceiverToType(SHPackageType, ResponseFunction); + void RegisterReceiverToType(SHEventIdentifier, ReceiverPtr); }; - } #endif // !SH_EVENTS_MANAGER_H diff --git a/SHADE_Engine/src/Events/SHEventReceiver.h b/SHADE_Engine/src/Events/SHEventReceiver.h index 387b8a97..ab379901 100644 --- a/SHADE_Engine/src/Events/SHEventReceiver.h +++ b/SHADE_Engine/src/Events/SHEventReceiver.h @@ -9,7 +9,7 @@ namespace SHADE { private: public: - virtual void Receive(PackagePtr) = 0; + virtual void Receive(SHEventDataPtr) = 0; }; template @@ -17,16 +17,16 @@ namespace SHADE { private: T* object; - void(T::*callback)(PackagePtr); + void(T::*callback)(SHEventDataPtr); public: - SHEventReceiverSpec(T* obj, void(T::*cb)(PackagePtr)) - :SHEventReceiver(), object{obj}, callback{ cb } + SHEventReceiverSpec(T* obj, void(T::* cb)(SHEventDataPtr)) + :SHEventReceiver(), object{ obj }, callback{ cb } { } - void Receive(PackagePtr evt) + void Receive(SHEventDataPtr evt) override { (object->*callback)(evt); }