From 23a599abe80ede12ae76429e810a18c39509b35c Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Tue, 13 Sep 2022 16:34:05 +0800 Subject: [PATCH 1/6] SP3-107 SP3-108 Initial port from 200 Removed unused classes Changing Event package for better ease of use --- SHADE_Engine/SHADE_Engine.vcxproj | 8 +- SHADE_Engine/SHADE_Engine.vcxproj.filters | 24 ++++ SHADE_Engine/src/Common/SHCommonTypes.h | 28 ++++ SHADE_Engine/src/Events/SHEvent.cpp | 49 +++++++ SHADE_Engine/src/Events/SHEvent.h | 45 +++++++ SHADE_Engine/src/Events/SHEventManager.cpp | 144 +++++++++++++++++++++ SHADE_Engine/src/Events/SHEventManager.h | 112 ++++++++++++++++ SHADE_Engine/src/Events/SHEventReceiver.h | 35 +++++ SHADE_Engine/src/SHpch.h | 3 + 9 files changed, 447 insertions(+), 1 deletion(-) create mode 100644 SHADE_Engine/src/Common/SHCommonTypes.h create mode 100644 SHADE_Engine/src/Events/SHEvent.cpp create mode 100644 SHADE_Engine/src/Events/SHEvent.h create mode 100644 SHADE_Engine/src/Events/SHEventManager.cpp create mode 100644 SHADE_Engine/src/Events/SHEventManager.h create mode 100644 SHADE_Engine/src/Events/SHEventReceiver.h diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index e54c82ec..8da6f80c 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -102,6 +102,7 @@ + @@ -116,6 +117,9 @@ + + + @@ -200,6 +204,8 @@ + + @@ -286,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 7486fad4..f0d769f0 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -1,6 +1,9 @@ + + {8EEA3EAC-7A8C-6982-6347-7DD64F88F0D2} + {1AB26817-067F-C322-2F98-B1CA1BC4F8B0} @@ -19,6 +22,9 @@ {DBC7D3B0-C769-FE86-B024-12DB9C6585D7} + + {3A8963B1-262B-8E87-0FE6-A1DBFB2615D8} + {8A8E2B37-7646-6D84-DF4D-46E0CB240875} @@ -117,6 +123,9 @@ + + Common + ECS_Base\Components @@ -159,6 +168,15 @@ Engine + + Events + + + Events + + + Events + Filesystem @@ -405,6 +423,12 @@ Engine + + Events + + + Events + Filesystem diff --git a/SHADE_Engine/src/Common/SHCommonTypes.h b/SHADE_Engine/src/Common/SHCommonTypes.h new file mode 100644 index 00000000..97ef7928 --- /dev/null +++ b/SHADE_Engine/src/Common/SHCommonTypes.h @@ -0,0 +1,28 @@ +/************************************************************************************//*! +\file SHCommonTypes.h +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Sep 8, 2022 +\brief Contains the definitions of type alias for commonly used units for + clarity and convenience. + + +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. +*//*************************************************************************************/ +#pragma once + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*---------------------------------------------------------------------------------*/ + /***********************************************************************************/ + /*! + \brief + Type used to mark a value that is supposed to represent a size in bytes. + */ + /***********************************************************************************/ + using Byte = size_t; +} diff --git a/SHADE_Engine/src/Events/SHEvent.cpp b/SHADE_Engine/src/Events/SHEvent.cpp new file mode 100644 index 00000000..4ac7c612 --- /dev/null +++ b/SHADE_Engine/src/Events/SHEvent.cpp @@ -0,0 +1,49 @@ +/****************************************************************************** + * \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 new file mode 100644 index 00000000..fc128a17 --- /dev/null +++ b/SHADE_Engine/src/Events/SHEvent.h @@ -0,0 +1,45 @@ +/****************************************************************************** + * \file SHEventBase.h + * \author Loh Xiao Qi + * \brief Event base abstract class declaration + * + * \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. +******************************************************************************/ +#ifndef SH_EVENT_BASE_H +#define SH_EVENT_BASE_H + +namespace SHADE +{ + class 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; + }; +} + + +#endif // !SH_EVENT_BASE_H diff --git a/SHADE_Engine/src/Events/SHEventManager.cpp b/SHADE_Engine/src/Events/SHEventManager.cpp new file mode 100644 index 00000000..b1038da8 --- /dev/null +++ b/SHADE_Engine/src/Events/SHEventManager.cpp @@ -0,0 +1,144 @@ +/****************************************************************************** + * \file SHEventManager.cpp + * \author Loh Xiao Qi + * \brief Function Implmentations for SHEventManager + * + * \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 "SHEventManager.h" + +namespace SHADE +{ + SHEventManager* SHEventManager::instance; + + /**************************************************************************** + * \brief Ctor and Dtor. Dtor will delete instance of self stored at + * data member instance. + ****************************************************************************/ + SHEventManager::SHEventManager() + { + + } + + /**************************************************************************** + * \brief Exit function to terminate the manager properly and deallocate + * memory. + ****************************************************************************/ + void SHEventManager::Exit() + { + delete instance; + } + + /**************************************************************************** + * \brief Returns singleton instance of event manager in heap memory. If + * instance does not exist, one is created immediately. + ****************************************************************************/ + SHEventManager& SHEventManager::GetEventManagerInstance() + { + // Create instance of event manager and return + if (!instance) + { + instance = new SHEventManager(); + } + + return *instance; + } + + /**************************************************************************** + * \brief Returns function pointer to entry point for events. + ****************************************************************************/ + EventManagerListener SHEventManager::GetListenerFunction() + { + return &(instance->CatchEvent); + } + + /**************************************************************************** + * \param ListenerConstPtr - Const pointer to listener that sent event. + * \param EventType - Templated type for every type of event + + * \brief Receives event from the listeners. + ****************************************************************************/ + void SHEventManager::CatchEvent(SHEvent event) + { + + // Do something with the event + + instance->Broadcast(event); + } + + /**************************************************************************** + * \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 + * subscribing to. + + * \brief Links a function pointer from a subscriber to a particular + * package type + ****************************************************************************/ + void SHEventManager::SubscribeTo(SHPackageType pkgType, ReceiverPtr receiver) + { + SHEventManager::GetEventManagerInstance().RegisterReceiverToType(pkgType, receiver); + } + + void SHEventManager::StaticSubscribeTo(SHPackageType pkgType, ResponseFunction func) + { + SHEventManager::GetEventManagerInstance().RegisterStaticReceiverToType(pkgType, func); + } + + /**************************************************************************** + * \param ReceiverPtr - Pointer to receiver + * \param ListenerConstPtr - Const pointer to listener that receiver is + * subscribing to. + + * \brief Registers receiver as a subscriber to listener in the registry. + ****************************************************************************/ + void SHEventManager::RegisterReceiverToType( + SHPackageType pkgType, ReceiverPtr receiver) + { + if (packageReceiverRegistry.contains(pkgType)) + { + packageReceiverRegistry[pkgType].emplace_back(receiver); + } + else + { + packageReceiverRegistry.emplace(pkgType, std::vector{ receiver }); + } + } + + 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 + + * \brief Broadcast event to all receivers that are subscribed to this + * listener. + ****************************************************************************/ + void SHEventManager::Broadcast(SHEvent const& event) + { + ResponseVec& receivers{ packageReceiverRegistry[event.GetType()] }; + for (auto& receiver : receivers) + { + receiver->Receive(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 new file mode 100644 index 00000000..e8d499dc --- /dev/null +++ b/SHADE_Engine/src/Events/SHEventManager.h @@ -0,0 +1,112 @@ +/****************************************************************************** + * \file SHEventManager.h + * \author Loh Xiao Qi + * \brief Class declaration for event manager. + * + * \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. +******************************************************************************/ +#ifndef SH_EVENT_MANAGER_H +#define SH_EVENT_MANAGER_H + +#include "SHEvent.h" +#include "SHEventReceiver.h" +#include +#include +#include + +namespace SHADE +{ + using ResponseFunction = std::function; + using ReceiverPtr = std::shared_ptr; + using ResponseVec = std::vector; + using StaticResponseVec = std::vector; + + using EventManagerListener = std::function; + + class SHEventManager + { + public: + /**************************************************************************** + * \brief Ctor and Dtor. Dtor will delete instance of self stored at + * data member instance. + ****************************************************************************/ + SHEventManager(); + + /**************************************************************************** + * \brief Returns singleton instance of event manager in heap memory. If + * instance does not exist, one is created immediately. + ****************************************************************************/ + static SHEventManager& GetEventManagerInstance(); + + /**************************************************************************** + * \brief Returns function pointer to entry point for events. + ****************************************************************************/ + static EventManagerListener GetListenerFunction(); + + /**************************************************************************** + * \brief Exit function to terminate the manager properly and deallocate + * memory. + ****************************************************************************/ + static void Exit(); + + /**************************************************************************** + * \param ListenerConstPtr - Const pointer to listener that sent event. + * \param EventType - Templated type for every type of event + + * \brief Receives event from the listeners. + ****************************************************************************/ + static void CatchEvent(SHEvent); + + /**************************************************************************** + * \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 + * subscribing to. + + * \brief Links a function pointer from a subscriber to a particular + * package type + ****************************************************************************/ + void SubscribeTo(SHPackageType, ReceiverPtr); + + void StaticSubscribeTo(SHPackageType, ResponseFunction); + private: + // Singleton instance + static SHEventManager* instance; + + // Registry for broadcasters and subscribers + std::unordered_map< + SHPackageType, + ResponseVec> packageReceiverRegistry; + + // Registry for static broadcasters and subscribers + std::unordered_map< + SHPackageType, + StaticResponseVec> staticPackageReceiverRegistry; + + /**************************************************************************** + * \param ListenerConstPtr - Const pointer to listener that sent event. + * \param EventType - Event data + + * \brief Broadcast event to all receivers that are subscribed to this + * listener. + ****************************************************************************/ + void Broadcast(SHEvent const&); + + /**************************************************************************** + * \param ReceiverPtr - Pointer to receiver + * \param ListenerConstPtr - Const pointer to listener that receiver is + * subscribing to. + + * \brief Registers receiver as a subscriber to listener in the registry. + ****************************************************************************/ + void RegisterReceiverToType(SHPackageType, ReceiverPtr); + + void RegisterStaticReceiverToType(SHPackageType, ResponseFunction); + + }; + +} + +#endif // !SH_EVENTS_MANAGER_H diff --git a/SHADE_Engine/src/Events/SHEventReceiver.h b/SHADE_Engine/src/Events/SHEventReceiver.h new file mode 100644 index 00000000..387b8a97 --- /dev/null +++ b/SHADE_Engine/src/Events/SHEventReceiver.h @@ -0,0 +1,35 @@ +#ifndef SH_EVENT_RECEIVER_H +#define SH_EVENT_RECEIVER_H + +#include "SHEvent.h" + +namespace SHADE +{ + class SHEventReceiver + { + private: + public: + virtual void Receive(PackagePtr) = 0; + }; + + template + class SHEventReceiverSpec : public SHEventReceiver + { + private: + T* object; + void(T::*callback)(PackagePtr); + + public: + SHEventReceiverSpec(T* obj, void(T::*cb)(PackagePtr)) + :SHEventReceiver(), object{obj}, callback{ cb } + { + + } + + void Receive(PackagePtr evt) + { + (object->*callback)(evt); + } + }; +} +#endif // !SH_EVENT_RECEIVER_H diff --git a/SHADE_Engine/src/SHpch.h b/SHADE_Engine/src/SHpch.h index 0342eedb..43a832f3 100644 --- a/SHADE_Engine/src/SHpch.h +++ b/SHADE_Engine/src/SHpch.h @@ -30,3 +30,6 @@ #include #include #include +#include + +#include "Common/SHCommonTypes.h" From a3b166001342bb6c93ddf474ffcf5d2786b845aa Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Thu, 15 Sep 2022 01:46:11 +0800 Subject: [PATCH 2/6] 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); } From ddf6a7bee54c8adc4545ae78ef154d4a0145b74e Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Thu, 15 Sep 2022 12:06:09 +0800 Subject: [PATCH 3/6] SP3-108 Removed singleton instance from event manager and changed it to static class instead. WARNING: Might need to change back to singleton next time, I had to do it previously for some reason but I don't remember why --- SHADE_Engine/SHADE_Engine.vcxproj.filters | 4 +- SHADE_Engine/src/Events/SHEvent.h | 2 +- SHADE_Engine/src/Events/SHEventManager.cpp | 47 ++++--------------- SHADE_Engine/src/Events/SHEventManager.h | 52 ++++++++-------------- SHADE_Engine/src/Events/SHEventReceiver.h | 10 ++--- 5 files changed, 35 insertions(+), 80 deletions(-) diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 5d627ce6..6b8bde7f 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -171,6 +171,9 @@ Events + + Events + Events @@ -405,7 +408,6 @@ Tools - diff --git a/SHADE_Engine/src/Events/SHEvent.h b/SHADE_Engine/src/Events/SHEvent.h index c6d5c97b..2ca6648e 100644 --- a/SHADE_Engine/src/Events/SHEvent.h +++ b/SHADE_Engine/src/Events/SHEvent.h @@ -16,7 +16,7 @@ namespace SHADE struct SHEvent { SHEventIdentifier type; - SHEventDataPtr data; + SHEventDataPtr dataPtr; SHEventHandle handle; }; } diff --git a/SHADE_Engine/src/Events/SHEventManager.cpp b/SHADE_Engine/src/Events/SHEventManager.cpp index 7798c944..2b4d15f1 100644 --- a/SHADE_Engine/src/Events/SHEventManager.cpp +++ b/SHADE_Engine/src/Events/SHEventManager.cpp @@ -12,47 +12,18 @@ namespace SHADE { - SHEventManager* SHEventManager::instance; + std::unordered_map SHEventManager::packageReceiverRegistry; + std::unordered_map SHEventManager::dataEventMap; - /**************************************************************************** - * \brief Ctor and Dtor. Dtor will delete instance of self stored at - * data member instance. - ****************************************************************************/ - SHEventManager::SHEventManager() - { + SHEventHandle SHEventManager::handleCounter{ 0 }; - } - - /**************************************************************************** - * \brief Exit function to terminate the manager properly and deallocate - * memory. - ****************************************************************************/ - void SHEventManager::Exit() - { - delete instance; - } - - /**************************************************************************** - * \brief Returns singleton instance of event manager in heap memory. If - * instance does not exist, one is created immediately. - ****************************************************************************/ - SHEventManager& SHEventManager::GetEventManagerInstance() - { - // Create instance of event manager and return - if (!instance) - { - instance = new SHEventManager(); - } - - return *instance; - } /**************************************************************************** * \brief Returns function pointer to entry point for events. ****************************************************************************/ EventManagerListener SHEventManager::GetListenerFunction() { - return &(instance->CatchEvent); + return &CatchEvent; } /**************************************************************************** @@ -66,7 +37,7 @@ namespace SHADE // Do something with the event - instance->Broadcast(event); + Broadcast(event); } /**************************************************************************** @@ -80,11 +51,11 @@ namespace SHADE ****************************************************************************/ void SHEventManager::SubscribeTo(SHEventIdentifier pkgType, ReceiverPtr receiver) { - SHEventManager::GetEventManagerInstance().RegisterReceiverToType(pkgType, receiver); + RegisterReceiverToType(pkgType, receiver); } template - T* SHEventManager::BroadcastData(T data, SHEventIdentifier eventType) + T* SHEventManager::BroadcastEvent(T data, SHEventIdentifier eventType) { SHEventDataPtr ptr = new std::byte[sizeof(data)]; @@ -133,10 +104,10 @@ namespace SHADE ResponseVec& receivers{ packageReceiverRegistry[event.type] }; for (auto& receiver : receivers) { - receiver->Receive(event.data); + receiver->Receive(event); } - //auto& funcs{ staticPackageReceiverRegistry[event.GetType()] }; + //auto& funcs{ ackageReceiverRegistry[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 4f9b4350..35acd4de 100644 --- a/SHADE_Engine/src/Events/SHEventManager.h +++ b/SHADE_Engine/src/Events/SHEventManager.h @@ -3,21 +3,26 @@ * \author Loh Xiao Qi * \brief Class declaration for event manager. * - * \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_MANAGER_H -#define SH_EVENT_MANAGER_H +#pragma once #include "SHEvent.h" #include "SHEventReceiver.h" #include #include +/****************************************************************************** + INSTRUCTIONS FOR USE: + On broadcaster side: + 1. +******************************************************************************/ + namespace SHADE { - using ResponseFunction = std::function; + using ResponseFunction = std::function; using ReceiverPtr = std::shared_ptr; using ResponseVec = std::vector; using StaticResponseVec = std::vector; @@ -27,29 +32,11 @@ namespace SHADE class SHEventManager { public: - /**************************************************************************** - * \brief Ctor and Dtor. Dtor will delete instance of self stored at - * data member instance. - ****************************************************************************/ - SHEventManager(); - - /**************************************************************************** - * \brief Returns singleton instance of event manager in heap memory. If - * instance does not exist, one is created immediately. - ****************************************************************************/ - static SHEventManager& GetEventManagerInstance(); - - /**************************************************************************** + /**************************************************************************** * \brief Returns function pointer to entry point for events. ****************************************************************************/ static EventManagerListener GetListenerFunction(); - /**************************************************************************** - * \brief Exit function to terminate the manager properly and deallocate - * memory. - ****************************************************************************/ - static void Exit(); - /**************************************************************************** * \param ListenerConstPtr - Const pointer to listener that sent event. * \param EventType - Templated type for every type of event @@ -67,19 +54,18 @@ namespace SHADE * \brief Links a function pointer from a subscriber to a particular * package type ****************************************************************************/ - void SubscribeTo(SHEventIdentifier, ReceiverPtr); + static void SubscribeTo(SHEventIdentifier, ReceiverPtr); template - T* BroadcastData(T data, SHEventIdentifier eventType); + static T* BroadcastEvent(T data, SHEventIdentifier eventType); + private: - // Singleton instance - static SHEventManager* instance; // Registry for broadcasters and subscribers - std::unordered_map packageReceiverRegistry; - std::unordered_map dataEventMap; + static std::unordered_map packageReceiverRegistry; + static std::unordered_map dataEventMap; - SHEventHandle handleCounter; + static SHEventHandle handleCounter; /**************************************************************************** * \param ListenerConstPtr - Const pointer to listener that sent event. @@ -88,7 +74,7 @@ namespace SHADE * \brief Broadcast event to all receivers that are subscribed to this * listener. ****************************************************************************/ - void Broadcast(SHEvent const&); + static void Broadcast(SHEvent const&); /**************************************************************************** * \param ReceiverPtr - Pointer to receiver @@ -97,9 +83,7 @@ namespace SHADE * \brief Registers receiver as a subscriber to listener in the registry. ****************************************************************************/ - void RegisterReceiverToType(SHEventIdentifier, ReceiverPtr); + static 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 ab379901..f968e579 100644 --- a/SHADE_Engine/src/Events/SHEventReceiver.h +++ b/SHADE_Engine/src/Events/SHEventReceiver.h @@ -1,5 +1,4 @@ -#ifndef SH_EVENT_RECEIVER_H -#define SH_EVENT_RECEIVER_H +#pragma once #include "SHEvent.h" @@ -9,7 +8,7 @@ namespace SHADE { private: public: - virtual void Receive(SHEventDataPtr) = 0; + virtual void Receive(SHEvent) = 0; }; template @@ -17,7 +16,7 @@ namespace SHADE { private: T* object; - void(T::*callback)(SHEventDataPtr); + SHEventHandle(T::*callback)(SHEvent); public: SHEventReceiverSpec(T* obj, void(T::* cb)(SHEventDataPtr)) @@ -26,10 +25,9 @@ namespace SHADE } - void Receive(SHEventDataPtr evt) override + void Receive(SHEvent evt) override { (object->*callback)(evt); } }; } -#endif // !SH_EVENT_RECEIVER_H From 5af0bf7d25fa6d2d498cc4f42c2a35c20dd3d730 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Thu, 15 Sep 2022 18:33:36 +0800 Subject: [PATCH 4/6] Instructions on how to use events --- SHADE_Engine/src/Events/SHEventManager.cpp | 11 +------- SHADE_Engine/src/Events/SHEventManager.h | 33 ++++++++++++++++++---- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/SHADE_Engine/src/Events/SHEventManager.cpp b/SHADE_Engine/src/Events/SHEventManager.cpp index 2b4d15f1..1cede2a0 100644 --- a/SHADE_Engine/src/Events/SHEventManager.cpp +++ b/SHADE_Engine/src/Events/SHEventManager.cpp @@ -16,16 +16,7 @@ namespace SHADE std::unordered_map SHEventManager::dataEventMap; SHEventHandle SHEventManager::handleCounter{ 0 }; - - - /**************************************************************************** - * \brief Returns function pointer to entry point for events. - ****************************************************************************/ - EventManagerListener SHEventManager::GetListenerFunction() - { - return &CatchEvent; - } - + /**************************************************************************** * \param ListenerConstPtr - Const pointer to listener that sent event. * \param EventType - Templated type for every type of event diff --git a/SHADE_Engine/src/Events/SHEventManager.h b/SHADE_Engine/src/Events/SHEventManager.h index 35acd4de..fd09d323 100644 --- a/SHADE_Engine/src/Events/SHEventManager.h +++ b/SHADE_Engine/src/Events/SHEventManager.h @@ -17,7 +17,33 @@ /****************************************************************************** INSTRUCTIONS FOR USE: On broadcaster side: - 1. + 1. Create a struct/class to contain the data that you would need to send + in the event. + 2. Create unique event identifier in SHEventDefines.h, follow the example + provided. + 3. When ready to send the event, call + SHEventManager::BroadcastEvent(exampleClass, EVENT_IDENTIFIER); + + Headers required: SHEventManager.h + + On Receiver side: + 1. Create a function with the signature: + SHEventHandle FunctionName(SHEvent); + 2. In the init function of the class, copy the below in and replace the + necessary: + + std::shared_ptr> thisReceiver{ + std::make_shared>(this, &ReceiverClass::ReceiveFunction) + }; + ReceiverPtr receiver = std::dynamic_pointer_cast(thisReceiver); + SHEventManager::SubscribeTo(EVENT_IDENTIFIER, receiver); + + 3. Note: The EventIdentifier should match all that is defined in + SHEventDefines.h so check there. When the receiver catches the event, it + needs to know the struct that the broadcaster is using to cast the void* + properly. + + Headers required: SHEventManager.h, SHEventReceiver.h ******************************************************************************/ namespace SHADE @@ -32,10 +58,6 @@ namespace SHADE class SHEventManager { public: - /**************************************************************************** - * \brief Returns function pointer to entry point for events. - ****************************************************************************/ - static EventManagerListener GetListenerFunction(); /**************************************************************************** * \param ListenerConstPtr - Const pointer to listener that sent event. @@ -86,4 +108,5 @@ namespace SHADE static void RegisterReceiverToType(SHEventIdentifier, ReceiverPtr); }; + } From e1074887c8b78db2b75dbc0df59812875f2aafbe Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Thu, 15 Sep 2022 18:34:07 +0800 Subject: [PATCH 5/6] Added instructions --- SHADE_Engine/src/Events/SHEventManager.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SHADE_Engine/src/Events/SHEventManager.h b/SHADE_Engine/src/Events/SHEventManager.h index fd09d323..c831b63f 100644 --- a/SHADE_Engine/src/Events/SHEventManager.h +++ b/SHADE_Engine/src/Events/SHEventManager.h @@ -44,6 +44,8 @@ properly. Headers required: SHEventManager.h, SHEventReceiver.h + + If you have any questions/suggestions for improvement lmk. ******************************************************************************/ namespace SHADE From f9743f394d039ec193272bfc4eb310eac83e831e Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Thu, 15 Sep 2022 18:37:05 +0800 Subject: [PATCH 6/6] Added some lines in comments for clarity --- SHADE_Engine/src/Events/SHEventDefines.h | 1 - SHADE_Engine/src/Events/SHEventManager.h | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/SHADE_Engine/src/Events/SHEventDefines.h b/SHADE_Engine/src/Events/SHEventDefines.h index 4087caa0..76b403bd 100644 --- a/SHADE_Engine/src/Events/SHEventDefines.h +++ b/SHADE_Engine/src/Events/SHEventDefines.h @@ -6,5 +6,4 @@ 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.h b/SHADE_Engine/src/Events/SHEventManager.h index c831b63f..f2f19fef 100644 --- a/SHADE_Engine/src/Events/SHEventManager.h +++ b/SHADE_Engine/src/Events/SHEventManager.h @@ -38,6 +38,8 @@ ReceiverPtr receiver = std::dynamic_pointer_cast(thisReceiver); SHEventManager::SubscribeTo(EVENT_IDENTIFIER, receiver); + ReceiverClass is the class that the receiver is in. E.g., SHPhysicsSystem + 3. Note: The EventIdentifier should match all that is defined in SHEventDefines.h so check there. When the receiver catches the event, it needs to know the struct that the broadcaster is using to cast the void*