From b1a799cf052471c6e125123a5ec9e09ec76372d3 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Thu, 29 Sep 2022 07:59:04 +0800 Subject: [PATCH 1/3] Attempt using dllimport/dllexport --- .../src/Application/SBApplication.cpp | 4 ++++ SHADE_Engine/src/ECS_Base/General/SHFamily.cpp | 15 +++++++++++++++ SHADE_Engine/src/ECS_Base/General/SHFamily.h | 16 +++++++++++----- SHADE_Engine/src/ECS_Base/SHECSMacros.h | 10 ++++++++++ SHADE_Engine/src/Editor/SHEditor.cpp | 5 ++++- 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 SHADE_Engine/src/ECS_Base/General/SHFamily.cpp diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 64095e03..0847de65 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -32,6 +32,8 @@ #include "Assets/SHAssetManager.h" +#include "Tools/SHLogger.h" + using namespace SHADE; namespace Sandbox @@ -110,6 +112,8 @@ namespace Sandbox //TODO: Change true to window is open while (!window.WindowShouldClose()) { + auto id = SystemFamily::GetID(); + SHLOG_INFO("Transform system id in application : {} {}", id, SystemFamily::currentID); SHFrameRateController::UpdateFRC(); SHSceneManager::UpdateSceneManager(); SHSceneManager::SceneUpdate(1/60.0f); diff --git a/SHADE_Engine/src/ECS_Base/General/SHFamily.cpp b/SHADE_Engine/src/ECS_Base/General/SHFamily.cpp new file mode 100644 index 00000000..d51a8b35 --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/General/SHFamily.cpp @@ -0,0 +1,15 @@ +#pragma once +#define COMPULING_THE_DLL +#include "SHFamily.h" +#include "SHpch.h" + +namespace SHADE +{ + //initialize currentID as 0 + + + + + + +} diff --git a/SHADE_Engine/src/ECS_Base/General/SHFamily.h b/SHADE_Engine/src/ECS_Base/General/SHFamily.h index 5815703f..7ef01f54 100644 --- a/SHADE_Engine/src/ECS_Base/General/SHFamily.h +++ b/SHADE_Engine/src/ECS_Base/General/SHFamily.h @@ -18,12 +18,12 @@ namespace SHADE { + template class SHFamilyID { private: - //this is used to keep track of the new current ID to be assign to a new Derived class type. - static ComponentTypeID currentID; + /*!************************************************************************* * \brief Construct a new SHFamilyID object @@ -46,6 +46,9 @@ namespace SHADE } public: + //this is used to keep track of the new current ID to be assign to a new Derived class type. + static MY_DLL_EXPORT ComponentTypeID currentID; + /*!************************************************************************* * \brief * Checks if this identifier is cuurrently in use / valid. @@ -59,7 +62,6 @@ namespace SHADE { return(id < currentID); } - /*!************************************************************************* * \brief * Get the ID of a derived class type. @@ -75,9 +77,13 @@ namespace SHADE static ComponentTypeID id = currentID++; return id; } + + + }; - //initialize currentID as 0 + +#ifdef COMPILING_THE_DLL template ComponentTypeID SHFamilyID::currentID = 0; - +#endif } \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/SHECSMacros.h b/SHADE_Engine/src/ECS_Base/SHECSMacros.h index 02615ca4..8de356f8 100644 --- a/SHADE_Engine/src/ECS_Base/SHECSMacros.h +++ b/SHADE_Engine/src/ECS_Base/SHECSMacros.h @@ -26,4 +26,14 @@ const EntityIndex MAX_EID = 51000; #define ENABLE_IF_UINT(_TYPE, _RETURN)\ typename std::enable_if<(std::is_integral<_TYPE>::value && !std::is_signed<_TYPE>::value),_RETURN>::type +#ifdef COMPILING_THE_DLL +#ifndef MY_DLL_EXPORT + #define MY_DLL_EXPORT __declspec(dllexport) +#endif +#else +#ifndef MY_DLL_EXPORT + #define MY_DLL_EXPORT __declspec(dllimport) +#endif +#endif + #endif \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp index b36518fd..f1a363e3 100644 --- a/SHADE_Engine/src/Editor/SHEditor.cpp +++ b/SHADE_Engine/src/Editor/SHEditor.cpp @@ -23,6 +23,8 @@ #include "SHEditor.hpp" #include "SHEditorWidgets.hpp" +#include "Math/Transform/SHTransformSystem.h" + //#==============================================================# //|| Editor Window Includes || //#==============================================================# @@ -104,7 +106,8 @@ namespace SHADE { (void)dt; NewFrame(); - + auto id = SystemFamily::GetID(); + SHLOG_INFO("Transform system id in Editor Update : {} {}", id, SystemFamily::currentID); for (const auto& window : editorWindows | std::views::values) { if(window->isOpen) From 114ae86a9f57940274c36fc8704eeabfe907225f Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Fri, 30 Sep 2022 20:13:18 +0800 Subject: [PATCH 2/3] Fixed SHFamilyID for SHSystem and SHComponent but still have issues with SHScene --- .../src/Application/SBApplication.cpp | 9 +++++-- .../src/ECS_Base/Components/SHComponent.h | 4 ++++ .../src/ECS_Base/General/SHFamily.cpp | 1 - SHADE_Engine/src/ECS_Base/General/SHFamily.h | 24 ++++++++++++------- .../src/ECS_Base/Managers/SHSystemManager.h | 3 +++ SHADE_Engine/src/ECS_Base/SHECSMacros.h | 10 +------- SHADE_Engine/src/ECS_Base/System/SHSystem.h | 5 ++++ SHADE_Engine/src/Editor/SHEditor.cpp | 7 ++++-- SHADE_Engine/src/Scene/SHScene.h | 2 ++ SHADE_Engine/src/Scene/SHSceneManager.h | 7 +++--- 10 files changed, 47 insertions(+), 25 deletions(-) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 0847de65..3e9b0dce 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -87,6 +87,11 @@ namespace Sandbox SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonBag_Color_Ver4.dds"); SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonPreTexturedVer1_Base9.dds"); SHADE::SHAssetManager::LoadDataTemp("../../Assets/TD_Checker_Base_Color.dds"); + + + auto id = SHFamilyID::GetID(); + auto id2 = SHFamilyID::GetID(); + auto id3 = SHFamilyID::GetID(); //TODO: REMOVE AFTER PRESENTATION //SHADE::SHSystemManager::RegisterRoutine(); @@ -102,7 +107,9 @@ namespace Sandbox #else #endif + SHSceneManager::InitSceneManager("TestScene"); + SHFrameRateController::UpdateFRC(); } @@ -112,8 +119,6 @@ namespace Sandbox //TODO: Change true to window is open while (!window.WindowShouldClose()) { - auto id = SystemFamily::GetID(); - SHLOG_INFO("Transform system id in application : {} {}", id, SystemFamily::currentID); SHFrameRateController::UpdateFRC(); SHSceneManager::UpdateSceneManager(); SHSceneManager::SceneUpdate(1/60.0f); diff --git a/SHADE_Engine/src/ECS_Base/Components/SHComponent.h b/SHADE_Engine/src/ECS_Base/Components/SHComponent.h index aba3ba51..cfcd724c 100644 --- a/SHADE_Engine/src/ECS_Base/Components/SHComponent.h +++ b/SHADE_Engine/src/ECS_Base/Components/SHComponent.h @@ -14,6 +14,7 @@ #include "SHpch.h" #include "../SHECSMacros.h" #include "SH_API.h" +#include "ECS_Base/General/SHFamily.h" namespace SHADE { @@ -117,4 +118,7 @@ namespace SHADE }; + + + template class SH_API SHFamilyID; } diff --git a/SHADE_Engine/src/ECS_Base/General/SHFamily.cpp b/SHADE_Engine/src/ECS_Base/General/SHFamily.cpp index d51a8b35..bd5c0378 100644 --- a/SHADE_Engine/src/ECS_Base/General/SHFamily.cpp +++ b/SHADE_Engine/src/ECS_Base/General/SHFamily.cpp @@ -1,5 +1,4 @@ #pragma once -#define COMPULING_THE_DLL #include "SHFamily.h" #include "SHpch.h" diff --git a/SHADE_Engine/src/ECS_Base/General/SHFamily.h b/SHADE_Engine/src/ECS_Base/General/SHFamily.h index 7ef01f54..51bd6a25 100644 --- a/SHADE_Engine/src/ECS_Base/General/SHFamily.h +++ b/SHADE_Engine/src/ECS_Base/General/SHFamily.h @@ -14,13 +14,14 @@ #pragma once #include "../SHECSMacros.h" +#include "SH_API.h" namespace SHADE { template - class SHFamilyID + class SH_API SHFamilyID { private: @@ -47,7 +48,7 @@ namespace SHADE public: //this is used to keep track of the new current ID to be assign to a new Derived class type. - static MY_DLL_EXPORT ComponentTypeID currentID; + static inline ComponentTypeID currentID = 0; /*!************************************************************************* * \brief @@ -70,20 +71,27 @@ namespace SHADE * @tparam DerivedClass * The derived class type that we are trying to get the ID of. ***************************************************************************/ +#ifdef SH_API_EXPORT template - static ENABLE_IF_DERIVED(ComponentTypeID, BaseClass, DerivedClass) GetID() noexcept + static SH_API ENABLE_IF_DERIVED(ComponentTypeID, BaseClass, DerivedClass) GetID() noexcept { //The first time a new derived class type call this get id, it will initialize id using the currentID from familyID class. - static ComponentTypeID id = currentID++; + static ComponentTypeID id = SHFamilyID::currentID++; return id; + //return 0; } +#else + template + static SH_API ENABLE_IF_DERIVED(ComponentTypeID, BaseClass, DerivedClass) GetID() noexcept; +#endif // SH_API_EXPORT + + }; -#ifdef COMPILING_THE_DLL - template - ComponentTypeID SHFamilyID::currentID = 0; -#endif + + + } \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h index f92f6635..995a1cf5 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h @@ -68,6 +68,9 @@ namespace SHADE id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID; } systemContainer.emplace(id, std::make_unique()); + + auto size = systemContainer.size(); + systemContainer[id].get()->systemID = id; return id; diff --git a/SHADE_Engine/src/ECS_Base/SHECSMacros.h b/SHADE_Engine/src/ECS_Base/SHECSMacros.h index 8de356f8..4690099f 100644 --- a/SHADE_Engine/src/ECS_Base/SHECSMacros.h +++ b/SHADE_Engine/src/ECS_Base/SHECSMacros.h @@ -26,14 +26,6 @@ const EntityIndex MAX_EID = 51000; #define ENABLE_IF_UINT(_TYPE, _RETURN)\ typename std::enable_if<(std::is_integral<_TYPE>::value && !std::is_signed<_TYPE>::value),_RETURN>::type -#ifdef COMPILING_THE_DLL -#ifndef MY_DLL_EXPORT - #define MY_DLL_EXPORT __declspec(dllexport) -#endif -#else -#ifndef MY_DLL_EXPORT - #define MY_DLL_EXPORT __declspec(dllimport) -#endif -#endif + #endif \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystem.h b/SHADE_Engine/src/ECS_Base/System/SHSystem.h index 93ea6a59..fe852b9a 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystem.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystem.h @@ -12,6 +12,7 @@ #include "../SHECSMacros.h" #include "SH_API.h" +#include "ECS_Base/General/SHFamily.h" namespace SHADE { @@ -69,5 +70,9 @@ namespace SHADE }; + template class SH_API SHFamilyID; + + + } \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp index f1a363e3..e94f7398 100644 --- a/SHADE_Engine/src/Editor/SHEditor.cpp +++ b/SHADE_Engine/src/Editor/SHEditor.cpp @@ -89,6 +89,11 @@ namespace SHADE io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; //Enable docking InitFonts(); + + + auto id = SHFamilyID::GetID(); + auto id2 = SHFamilyID::GetID(); + auto id3 = SHFamilyID::GetID(); InitBackend(sdlWindow); SetStyle(Style::SHADE); @@ -106,8 +111,6 @@ namespace SHADE { (void)dt; NewFrame(); - auto id = SystemFamily::GetID(); - SHLOG_INFO("Transform system id in Editor Update : {} {}", id, SystemFamily::currentID); for (const auto& window : editorWindows | std::views::values) { if(window->isOpen) diff --git a/SHADE_Engine/src/Scene/SHScene.h b/SHADE_Engine/src/Scene/SHScene.h index 372981a6..a81c70ef 100644 --- a/SHADE_Engine/src/Scene/SHScene.h +++ b/SHADE_Engine/src/Scene/SHScene.h @@ -13,6 +13,7 @@ #include #include "SHSceneGraph.h" +#include "ECS_Base/General/SHFamily.h" namespace SHADE { @@ -42,6 +43,7 @@ namespace SHADE virtual void Unload() = 0; }; + template class SH_API SHFamilyID; } diff --git a/SHADE_Engine/src/Scene/SHSceneManager.h b/SHADE_Engine/src/Scene/SHSceneManager.h index 83ebab0f..2e4b1717 100644 --- a/SHADE_Engine/src/Scene/SHSceneManager.h +++ b/SHADE_Engine/src/Scene/SHSceneManager.h @@ -84,7 +84,8 @@ namespace SHADE { //prevSceneCreate = newScene; newScene = [sceneName]() { currentScene = new T(); currentScene->sceneName = sceneName; }; - nextSceneID = SHFamilyID::template GetID(); + //nextSceneID = SHFamilyID::GetID(); + nextSceneID = 0; sceneChanged = true; } @@ -101,13 +102,13 @@ namespace SHADE static std::enable_if_t, void> ChangeScene(std::string const& sceneName) noexcept { //check if this new Scene is current Scene (Use RestartScene instead) - if (currentSceneID == SHFamilyID::template GetID()) + if (currentSceneID == SHFamilyID::GetID()) { return; } //prevSceneCreate = newScene; newScene = [sceneName]() { currentScene = new T(); currentScene->sceneName; }; - nextSceneID = SHFamilyID::template GetID(); + nextSceneID = SHFamilyID::GetID(); sceneChanged = true; } From 2d07fb0afcae86ac9a3bcfc1653aae07919951e6 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Fri, 30 Sep 2022 20:25:26 +0800 Subject: [PATCH 3/3] Added component added and removed events --- .../ECS_Base/Events/SHComponentAddedEvent.h | 12 +++++++++ .../ECS_Base/Events/SHComponentRemovedEvent.h | 12 +++++++++ .../ECS_Base/Managers/SHComponentManager.h | 25 +++++++++++++++---- SHADE_Engine/src/Events/SHEventDefines.h | 2 ++ 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 SHADE_Engine/src/ECS_Base/Events/SHComponentAddedEvent.h create mode 100644 SHADE_Engine/src/ECS_Base/Events/SHComponentRemovedEvent.h diff --git a/SHADE_Engine/src/ECS_Base/Events/SHComponentAddedEvent.h b/SHADE_Engine/src/ECS_Base/Events/SHComponentAddedEvent.h new file mode 100644 index 00000000..1f2b62d5 --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/Events/SHComponentAddedEvent.h @@ -0,0 +1,12 @@ +#pragma once + +#include "ECS_Base/Components/SHComponent.h" + +namespace SHADE +{ + struct SHComponentAddedEvent + { + EntityID eid; + ComponentTypeID addedComponentType; + }; +} diff --git a/SHADE_Engine/src/ECS_Base/Events/SHComponentRemovedEvent.h b/SHADE_Engine/src/ECS_Base/Events/SHComponentRemovedEvent.h new file mode 100644 index 00000000..34d6f9c8 --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/Events/SHComponentRemovedEvent.h @@ -0,0 +1,12 @@ +#pragma once + +#include "ECS_Base/Components/SHComponent.h" + +namespace SHADE +{ + struct SHComponentRemovedEvent + { + EntityID eid; + ComponentTypeID removedComponentType; + }; +} diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h index 60625d6a..12b5b001 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h @@ -17,6 +17,8 @@ #include "../General/SHSparseSetContainer.h" #include "../Components/SHComponent.h" #include "../Components/SHComponentGroup.h" +#include "../Events/SHComponentAddedEvent.h" +#include "../Events/SHComponentRemovedEvent.h" //#include "Scene/SHSceneNode.h" #include "SH_API.h" @@ -216,6 +218,11 @@ namespace SHADE comp->OnCreate(); } + SHComponentAddedEvent eventData; + eventData.eid = entityID; + eventData.addedComponentType = ComponentFamily::GetID(); + + SHEventManager::BroadcastEvent(eventData, SH_COMPONENT_ADDED_EVENT); } /************************************************************************** @@ -247,6 +254,13 @@ namespace SHADE { comp->OnCreate(); } + + SHComponentAddedEvent eventData; + eventData.eid = entityID; + eventData.addedComponentType = componentTypeID; + + SHEventManager::BroadcastEvent(eventData, SH_COMPONENT_ADDED_EVENT); + } @@ -313,6 +327,12 @@ namespace SHADE componentSet.GetSparseSet()->Remove(EntityHandleGenerator::GetIndex(entityID)); + + SHComponentRemovedEvent eventData; + eventData.eid = entityID; + eventData.addedComponentType = ComponentFamily::GetID(); + + SHEventManager::BroadcastEvent(eventData, SH_COMPONENT_REMOVED_EVENT); } /*!************************************************************************* @@ -464,11 +484,6 @@ namespace SHADE return componentGroups[index]; } - static void AddScriptComponent(EntityID eid, std::string const& scriptClassName) noexcept; - - static void RemoveScriptComponent(EntityID eid, std::string const& scriptClassName) noexcept; - - };// end SHComponentManager diff --git a/SHADE_Engine/src/Events/SHEventDefines.h b/SHADE_Engine/src/Events/SHEventDefines.h index f1e92b42..317b67c1 100644 --- a/SHADE_Engine/src/Events/SHEventDefines.h +++ b/SHADE_Engine/src/Events/SHEventDefines.h @@ -8,3 +8,5 @@ typedef uint32_t SHEventHandle; constexpr SHEventIdentifier SH_EXAMPLE_EVENT{0}; constexpr SHEventIdentifier SH_ENTITY_DESTROYED_EVENT{ 1 }; constexpr SHEventIdentifier SH_ENTITY_CREATION_EVENT{ 2 }; +constexpr SHEventIdentifier SH_COMPONENT_ADDED_EVENT{ 3 }; +constexpr SHEventIdentifier SH_COMPONENT_REMOVED_EVENT{ 4 };