From 9a223098f658cebd2a7abc173e2392d6d3a4a991 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Thu, 8 Sep 2022 11:17:25 +0800 Subject: [PATCH 1/4] Initial Commit --- SHADE_Engine/SHADE_Engine.vcxproj | 2 + SHADE_Engine/SHADE_Engine.vcxproj.filters | 2 + SHADE_Engine/src/Filesystem/SHFileSystem.cpp | 75 ++++++++++++++++++++ SHADE_Engine/src/Filesystem/SHFileSystem.h | 44 ++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 SHADE_Engine/src/Filesystem/SHFileSystem.cpp create mode 100644 SHADE_Engine/src/Filesystem/SHFileSystem.h diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index d89ec12a..ba1be338 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -103,10 +103,12 @@ + + Create diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index b16c713b..6ca01248 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -10,11 +10,13 @@ Engine + Engine + \ No newline at end of file diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp new file mode 100644 index 00000000..81ff6a8b --- /dev/null +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp @@ -0,0 +1,75 @@ +#include "SHFileSystem.h" +#include "fileapi.h" +#include + +namespace SHADE +{ + char const FOLDER_MAX_COUNT {15}; + + std::unordered_map> SHFileSystem::folders; + + SHFolder::SHFolder(FolderHandle id, FolderName name) + :id{id}, name{name}, subFolders(0) + { + } + + FolderLocation SHFileSystem::CreateNewFolderHere(FolderName name, FolderLocation here) noexcept + { + if (here == 0) + { + if (!folders.contains(0)) + { + folders[0] = std::make_unique(0, "root"); + } + + FolderCounter const count = static_cast(folders[here]->subFolders.size()); + + assert(count < FOLDER_MAX_COUNT, "Max subfolders reached\n"); + + FolderHandle const location = static_cast(count); + + CreateFolderHelper(folders[0]->path, here, location, name); + + return location; + } + + assert(folders.contains(here), "Folder creation location does not exist/invalid\n"); + + FolderCounter const count = static_cast(folders[here]->subFolders.size()); + + FolderHandle location = here; + location <<= 4; + location |= count; + + assert(count < FOLDER_MAX_COUNT, "Max subfolders reached\n"); + CreateFolderHelper(folders[0]->path, here, location, name); + + return location; + } + + bool SHFileSystem::DeleteFolder(FolderLocation location) noexcept + { + assert(folders.contains(location), "Delete target does not exist/invalid.\n"); + + for (auto const& subFolder : folders[location]->subFolders) + { + DeleteFolder(subFolder); + } + + RemoveDirectoryA(folders[location]->path.string().c_str()); + return true; + } + + bool SHFileSystem::CreateFolderHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept + { + assert( + CreateDirectoryA(path.string().c_str(), nullptr), + "Failed to create folder\n" + ); + + folders[location] = std::make_unique(location, name); + folders[parent]->subFolders.push_back(location); + + return true; + } +} diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.h b/SHADE_Engine/src/Filesystem/SHFileSystem.h new file mode 100644 index 00000000..c58eb81e --- /dev/null +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace SHADE +{ + typedef unsigned char FolderCounter; + typedef unsigned char FileCounter; + typedef uint32_t FolderLocation; + typedef uint32_t FolderHandle; + typedef std::string FolderName; + typedef std::filesystem::path FolderPath; + + class SHFolder + { + public: + SHFolder(FolderHandle id, FolderName name); + + FolderHandle id; + FolderName name; + std::vector subFolders; + + private: + FolderPath path; + friend class SHFileSystem; + }; + + class SHFileSystem + { + public: + static FolderLocation CreateNewFolderHere(FolderName name, FolderLocation here = 0) noexcept; + + static bool DeleteFolder(FolderLocation location) noexcept; + + private: + static std::unordered_map> folders; + + static bool CreateFolderHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept; + }; +} \ No newline at end of file -- 2.40.1 From d4944ef9209526b87ab9e09400622f72c4c6144d Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sun, 11 Sep 2022 22:47:30 +0800 Subject: [PATCH 2/4] Added a scene graph --- SHADE_Engine/SHADE_Engine.vcxproj | 4 +- SHADE_Engine/SHADE_Engine.vcxproj.filters | 2 + .../ECS_Base/System/SHComponentManager.h | 2 +- SHADE_Engine/src/SHpch.h | 1 + SHADE_Engine/src/Scene/SHSceneGraph.cpp | 475 ++++++++++++++++++ SHADE_Engine/src/Scene/SHSceneGraph.h | 143 ++++++ 6 files changed, 625 insertions(+), 2 deletions(-) create mode 100644 SHADE_Engine/src/Scene/SHSceneGraph.cpp create mode 100644 SHADE_Engine/src/Scene/SHSceneGraph.h diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 07b7eb4a..5e7342d0 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -183,6 +183,7 @@ + @@ -253,6 +254,7 @@ + Create @@ -284,4 +286,4 @@ - + \ No newline at end of file diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 8e6cedf2..974e2fe0 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -353,6 +353,7 @@ + @@ -536,5 +537,6 @@ + \ No newline at end of file diff --git a/SHADE_Engine/src/Engine/ECS_Base/System/SHComponentManager.h b/SHADE_Engine/src/Engine/ECS_Base/System/SHComponentManager.h index 05a3d1ee..2f6ff504 100644 --- a/SHADE_Engine/src/Engine/ECS_Base/System/SHComponentManager.h +++ b/SHADE_Engine/src/Engine/ECS_Base/System/SHComponentManager.h @@ -34,7 +34,7 @@ namespace SHADE //The Container of all Componentgroups static std::vector componentGroups; - friend class SHSceneNode; + friend struct SHSceneNode; diff --git a/SHADE_Engine/src/SHpch.h b/SHADE_Engine/src/SHpch.h index b54a8a5b..0342eedb 100644 --- a/SHADE_Engine/src/SHpch.h +++ b/SHADE_Engine/src/SHpch.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include diff --git a/SHADE_Engine/src/Scene/SHSceneGraph.cpp b/SHADE_Engine/src/Scene/SHSceneGraph.cpp new file mode 100644 index 00000000..3c27604d --- /dev/null +++ b/SHADE_Engine/src/Scene/SHSceneGraph.cpp @@ -0,0 +1,475 @@ +/**************************************************************************************** + * \file SHSceneGraph.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for a Scene Graph & Scene Nodes. + * + * \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. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHSceneGraph.h" + +// Project Headers +#include "Engine/ECS_Base/System/SHEntityManager.h" +#include "Tools/SHLogger.h" +#include "Tools/SHException.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Constructors & Destructor Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHSceneNode::SHSceneNode(EntityID eid, SHSceneNode* parent) noexcept + : isActive { true } + , entityID { eid } + , parent { parent } + {} + + + SHSceneNode::SHSceneNode(const SHSceneNode& rhs) noexcept + : isActive { rhs.isActive } + , entityID { rhs.entityID } + , parent { rhs.parent } + { + std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children)); + } + + SHSceneNode::SHSceneNode(SHSceneNode&& rhs) noexcept + : isActive { rhs.isActive } + , entityID { rhs.entityID } + , parent { rhs.parent } + { + std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children)); + } + + SHSceneNode& SHSceneNode::operator=(const SHSceneNode& rhs) noexcept + { + if (this == &rhs) + return *this; + + isActive = rhs.isActive; + entityID = rhs.entityID; + parent = rhs.parent; + + children.clear(); + std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children)); + + return *this; + } + + SHSceneNode& SHSceneNode::operator=(SHSceneNode&& rhs) noexcept + { + isActive = rhs.isActive; + entityID = rhs.entityID; + parent = rhs.parent; + + children.clear(); + std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children)); + + return *this; + } + + SHSceneGraph::SHSceneGraph() noexcept + : root { nullptr } + {} + + SHSceneGraph::~SHSceneGraph() noexcept + { + SHASSERT(root != nullptr, "Unable to destroy a Scene without a root node!") + + #ifdef _DEBUG + SHLOG_INFO("Destroying Scene Graph...") + #endif + + // Go through the map and release all the nodes + for (auto* node : entityNodeMap | std::views::values) + ReleaseNode(node); + + #ifdef _DEBUG + SHLOG_INFO("Scene Graph Destroyed Successfully!") + #endif + } + + /*-----------------------------------------------------------------------------------*/ + /* Getter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHSceneNode* SHSceneNode::GetChild(EntityID childID) const noexcept + { + // Error handling + { + if (!SHEntityManager::IsValidEID(childID)) + { + SHLOG_ERROR("Child Entity {} is invalid! Unable to get child from Entity {}", childID, entityID) + return nullptr; + } + + if (children.empty()) + { + SHLOG_WARNING("Entity {} has no children!", entityID) + return nullptr; + } + } + + // Find child + const auto ENTITY_MATCH = [&](const SHSceneNode* node) { return node->GetEntityID() == childID; }; + + const auto CHILD_ITER = std::ranges::find_if(children.begin(), children.end(),ENTITY_MATCH); + if (CHILD_ITER == children.end()) + { + SHLOG_WARNING("Entity {} is not a child of Entity {}! Unable to retrieve child node!", childID, entityID) + return nullptr; + } + + return *CHILD_ITER; + } + + SHSceneNode* SHSceneGraph::GetRoot() const noexcept + { + if (root != nullptr) + return root; + + SHLOG_WARNING("Scene has no root object!") + return nullptr; + } + + SHSceneNode* SHSceneGraph::GetNode(EntityID entityID) const noexcept + { + if (!SHEntityManager::IsValidEID(entityID)) + { + SHLOG_ERROR("Entity {} is invalid! Unable to Get Scene node!", entityID) + return nullptr; + } + + const auto NODE_ITER = entityNodeMap.find(entityID); + if (NODE_ITER == entityNodeMap.end()) + { + SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to Get Scene node!", entityID) + return nullptr; + } + + return NODE_ITER->second; + } + + SHSceneNode* SHSceneGraph::GetParent(EntityID entityID) const noexcept + { + if (!SHEntityManager::IsValidEID(entityID)) + { + SHLOG_ERROR("Entity {} is invalid! Unable to get Parent node!", entityID) + return nullptr; + } + + const auto NODE_ITER = entityNodeMap.find(entityID); + if (NODE_ITER == entityNodeMap.end()) + { + SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to get Parent node!", entityID) + return nullptr; + } + + return NODE_ITER->second->GetParent(); + } + + SHSceneNode* SHSceneGraph::GetChild(EntityID entityID, SHSceneNode* childNode) const noexcept + { + // Error Handling + if (!SHEntityManager::IsValidEID(entityID)) + { + SHLOG_ERROR("Entity {} is invalid!", entityID) + return nullptr; + } + + const auto NODE_ITER = entityNodeMap.find(entityID); + if (NODE_ITER == entityNodeMap.end()) + { + SHLOG_WARNING("Entity {} cannot be found in the scene!", entityID) + return nullptr; + } + + const auto& children = NODE_ITER->second->GetChildren(); + if (children.empty()) + { + SHLOG_WARNING("Entity {} has no children!", entityID) + return nullptr; + } + + const auto CHILD_ITER = std::ranges::find(children.begin(), children.end(), childNode); + if (CHILD_ITER == children.end()) + { + SHLOG_WARNING("Entity {} is not a child of Entity {}!", childNode->GetEntityID(), entityID) + return nullptr; + } + + return *CHILD_ITER; + } + + SHSceneNode* SHSceneGraph::GetChild(EntityID entityID, EntityID childEntityID) const noexcept + { + if (!SHEntityManager::IsValidEID(entityID)) + { + SHLOG_ERROR("Entity {} is invalid!", entityID) + return nullptr; + } + + const auto NODE_ITER = entityNodeMap.find(entityID); + if (NODE_ITER == entityNodeMap.end()) + { + SHLOG_WARNING("Entity {} cannot be found in the scene!", entityID) + return nullptr; + } + + return NODE_ITER->second->GetChild(childEntityID); + } + + const std::vector& SHSceneGraph::GetChildren(EntityID entityID) const noexcept + { + // TODO(Diren): Discuss with team best way to handle this + + if (!SHEntityManager::IsValidEID(entityID)) + { + SHLOG_ERROR("Entity {} is invalid!", entityID) + return root->GetChildren(); + } + + const auto NODE_ITER = entityNodeMap.find(entityID); + if (NODE_ITER == entityNodeMap.end()) + { + SHLOG_WARNING("Entity {} cannot be found in the scene!", entityID) + return root->GetChildren(); + } + + return NODE_ITER->second->GetChildren(); + } + + /*-----------------------------------------------------------------------------------*/ + /* Setter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + void SHSceneNode::SetParent(SHSceneNode* parentNode) noexcept + { + if (parentNode == nullptr) + SHLOG_WARNING("Removing Entity {}'s parent", entityID) + + if (parentNode == parent) + return; + + parent = parentNode; + // Update parent's children + parent->AddChild(this); + } + + void SHSceneGraph::SetParent(EntityID entityID, SHSceneNode* parent) const noexcept + { + if (!SHEntityManager::IsValidEID(entityID)) + { + SHLOG_ERROR("Entity {} is invalid!", entityID) + return; + } + + const auto NODE_ITER = entityNodeMap.find(entityID); + if (NODE_ITER == entityNodeMap.end()) + { + SHLOG_WARNING("Entity {} cannot be found in the scene!", entityID) + return; + } + + NODE_ITER->second->SetParent(parent); + } + + void SHSceneGraph::SetParent(EntityID entityID, EntityID parent) const noexcept + { + if (!SHEntityManager::IsValidEID(entityID)) + { + SHLOG_ERROR("Entity {} is invalid! Unable to set parent of an invalid entity!", entityID) + return; + } + + if (!SHEntityManager::IsValidEID(parent)) + { + SHLOG_ERROR("Parent Entity {} is invalid! Unable to set Entity {}'s parent!", parent, entityID) + return; + } + + auto NODE_ITER = entityNodeMap.find(entityID); + if (NODE_ITER == entityNodeMap.end()) + { + SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to set parent!", entityID) + return; + } + + auto PARENT_ITER = entityNodeMap.find(entityID); + if (PARENT_ITER == entityNodeMap.end()) + { + SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to parent to Entity {}", parent, entityID) + return; + } + + SHSceneNode* currentNode = NODE_ITER->second; + currentNode->SetParent(PARENT_ITER->second); + } + + /*-----------------------------------------------------------------------------------*/ + /* Public Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + void SHSceneNode::AddChild(SHSceneNode* newChild) noexcept + { + if (newChild == nullptr) + { + SHLOG_WARNING("Attempting to add a non-existent child to an entity!") + return; + } + + children.emplace_back(newChild); + } + + bool SHSceneNode::RemoveChild(EntityID childID) noexcept + { + if (!SHEntityManager::IsValidEID(childID)) + { + SHLOG_ERROR("Entity {} is invalid!", childID) + return false; + } + + SHSceneNode* removedChild = nullptr; + const auto ENTITY_MATCH = [&](SHSceneNode* node) + { + if (node->GetEntityID() == childID) + { + removedChild = node; + return true; + } + + return false; + }; + + children.end() = std::remove_if(children.begin(), children.end(), ENTITY_MATCH); + removedChild->parent = nullptr; + + return removedChild == nullptr; + } + + bool SHSceneNode::RemoveChild(SHSceneNode* childToRemove) noexcept + { + if (childToRemove == nullptr) + { + SHLOG_WARNING("Attempting to remove non-existent child from Entity {}", entityID) + return false; + } + + children.end() = std::remove(children.begin(), children.end(), childToRemove); + childToRemove->parent = nullptr; + + return true; + } + + void SHSceneNode::RemoveAllChildren() noexcept + { + for (const auto child : children) + child->parent = nullptr; + + children.clear(); + } + + + SHSceneNode* SHSceneGraph::AddNode(EntityID entityID, SHSceneNode* parent) + { + if (!SHEntityManager::IsValidEID(entityID)) + { + SHLOG_ERROR("Entity {} is invalid!", entityID) + return nullptr; + } + + if (auto NODE_ITER = entityNodeMap.find(entityID); NODE_ITER != entityNodeMap.end()) + { + SHLOG_WARNING("Entity {} already exists in the scene!", entityID) + return NODE_ITER->second; + } + + SHSceneNode* newNode = AllocateNode(entityID); + newNode->SetParent(parent); + + return newNode; + } + + bool SHSceneGraph::RemoveNode(EntityID entityID) noexcept + { + if (!SHEntityManager::IsValidEID(entityID)) + { + SHLOG_ERROR("Entity {} is invalid!", entityID) + return false; + } + + auto NODE_ITER = entityNodeMap.find(entityID); + if (NODE_ITER == entityNodeMap.end()) + { + SHLOG_WARNING("Entity {} does not exist in the scene!", entityID) + return false; + } + + // Remove reference of current node from parent + SHSceneNode* currentNode = NODE_ITER->second; + SHSceneNode* parent = currentNode->GetParent(); + if (parent != nullptr) + parent->RemoveChild(currentNode); + + ReleaseNode(currentNode); + return true; + } + + bool SHSceneGraph::RemoveNode(SHSceneNode* nodeToRemove) noexcept + { + // Remove reference of current node from parent + SHSceneNode* parent = nodeToRemove->GetParent(); + if (parent != nullptr) + parent->RemoveChild(nodeToRemove); + + ReleaseNode(nodeToRemove); + return true; + } + + void SHSceneGraph::Reset() noexcept + { + for (auto* node : entityNodeMap | std::views::values) + ReleaseNode(node); + } + + /*-----------------------------------------------------------------------------------*/ + /* Private Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHSceneNode* SHSceneGraph::AllocateNode(EntityID entityID) + { + SHSceneNode* newNode = new SHSceneNode{entityID}; + + #ifdef _DEBUG + SHLOG_INFO("Allocated a new Scene Node for Entity {}!", entityID) + #endif + + entityNodeMap.emplace(entityID, newNode); + return newNode; + } + + void SHSceneGraph::ReleaseNode(SHSceneNode* node) noexcept + { + SHASSERT(node != nullptr, "Attempting to release Invalid Node!") + + // Remove parent's reference to this node if there is a parent + if (node->GetParent() != nullptr) + node->GetParent()->RemoveChild(node); + + // Remove child's references to this node. Children end up as floating nodes. + for (auto* child : node->GetChildren()) + { + child->SetParent(nullptr); + } + + entityNodeMap.erase(node->GetEntityID()); + delete node; + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Scene/SHSceneGraph.h b/SHADE_Engine/src/Scene/SHSceneGraph.h new file mode 100644 index 00000000..7f19cf3b --- /dev/null +++ b/SHADE_Engine/src/Scene/SHSceneGraph.h @@ -0,0 +1,143 @@ +/**************************************************************************************** + * \file SHSceneGraph.h + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for a Scene Graph & Scene Nodes. + * + * \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. +****************************************************************************************/ + +#pragma once + +#include + +// Project Headers +#include "Engine/ECS_Base/Entity/SHEntity.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + class SHSceneNode + { + public: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + bool isActive; + + /*---------------------------------------------------------------------------------*/ + /* Constructors & Destructor */ + /*---------------------------------------------------------------------------------*/ + + ~SHSceneNode () = default; + + SHSceneNode (EntityID eid, SHSceneNode* parent = nullptr) noexcept; + SHSceneNode (const SHSceneNode& rhs) noexcept; + SHSceneNode (SHSceneNode&& rhs) noexcept; + SHSceneNode& operator= (const SHSceneNode& rhs) noexcept; + SHSceneNode& operator= (SHSceneNode&& rhs) noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] EntityID GetEntityID () const noexcept { return entityID ;} + [[nodiscard]] SHSceneNode* GetParent () const noexcept { return parent; } + [[nodiscard]] std::vector& GetChildren () noexcept { return children; } + + [[nodiscard]] SHSceneNode* GetChild (EntityID childID) const noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Setter Functions */ + /*---------------------------------------------------------------------------------*/ + + void SetParent (SHSceneNode* parentNode) noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + void AddChild (SHSceneNode* newChild) noexcept; + + bool RemoveChild (EntityID childID) noexcept; + bool RemoveChild (SHSceneNode* childToRemove) noexcept; + + void RemoveAllChildren () noexcept; + + private: + EntityID entityID; + SHSceneNode* parent; + std::vector children; + }; + + class SHSceneGraph + { + public: + /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*---------------------------------------------------------------------------------*/ + + using EntityNodeMap = std::unordered_map; + + /*---------------------------------------------------------------------------------*/ + /* Constructors & Destructor */ + /*---------------------------------------------------------------------------------*/ + + SHSceneGraph () noexcept; + ~SHSceneGraph () noexcept; + + SHSceneGraph (const SHSceneGraph&) = delete; + SHSceneGraph (SHSceneGraph&&) = delete; + SHSceneGraph& operator= (const SHSceneGraph&) = delete; + SHSceneGraph& operator= (SHSceneGraph&&) = delete; + + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] SHSceneNode* GetRoot () const noexcept; + [[nodiscard]] SHSceneNode* GetNode (EntityID entityID) const noexcept; + [[nodiscard]] SHSceneNode* GetParent (EntityID entityID) const noexcept; + [[nodiscard]] SHSceneNode* GetChild (EntityID entityID, SHSceneNode* childNode) const noexcept; + [[nodiscard]] SHSceneNode* GetChild (EntityID entityID, EntityID childEntityID) const noexcept; + [[nodiscard]] const std::vector& GetChildren (EntityID entityID) const noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Setter Functions */ + /*---------------------------------------------------------------------------------*/ + + void SetParent (EntityID entityID, SHSceneNode* parent) const noexcept; + void SetParent (EntityID entityID, EntityID parent) const noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + SHSceneNode* AddNode (EntityID entityID, SHSceneNode* parent = nullptr); + bool RemoveNode (EntityID entityID) noexcept; + bool RemoveNode (SHSceneNode* nodeToRemove) noexcept; + void Reset () noexcept; + + private: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + SHSceneNode* root; + EntityNodeMap entityNodeMap; + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + SHSceneNode* AllocateNode (EntityID entityID); + void ReleaseNode (SHSceneNode* node) noexcept; + }; + + +} // namespace SHADE \ No newline at end of file -- 2.40.1 From 47f716f72b16a85c7c42e817016739de470ce644 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Tue, 13 Sep 2022 11:27:51 +0800 Subject: [PATCH 3/4] SP3-105 SP3-106 Folder tree building done Location bit shift done --- SHADE_Engine/src/Filesystem/SHFileSystem.cpp | 90 ++++++++++++++++---- SHADE_Engine/src/Filesystem/SHFileSystem.h | 26 ++++-- 2 files changed, 94 insertions(+), 22 deletions(-) diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp index 81ff6a8b..5663dadd 100644 --- a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp @@ -1,15 +1,19 @@ +#include "SHpch.h" #include "SHFileSystem.h" #include "fileapi.h" +#include #include +#include namespace SHADE { char const FOLDER_MAX_COUNT {15}; std::unordered_map> SHFileSystem::folders; + FolderPointer SHFileSystem::root {nullptr}; SHFolder::SHFolder(FolderHandle id, FolderName name) - :id{id}, name{name}, subFolders(0) + :id{ id }, name{ name }, subFolders(0), folded{ false }, path{""} { } @@ -22,54 +26,110 @@ namespace SHADE folders[0] = std::make_unique(0, "root"); } - FolderCounter const count = static_cast(folders[here]->subFolders.size()); + auto const count = static_cast(folders[here]->subFolders.size()); assert(count < FOLDER_MAX_COUNT, "Max subfolders reached\n"); - FolderHandle const location = static_cast(count); + auto const location = static_cast(count); - CreateFolderHelper(folders[0]->path, here, location, name); + CreateFolder(folders[0]->path, here, location, name); return location; } assert(folders.contains(here), "Folder creation location does not exist/invalid\n"); - FolderCounter const count = static_cast(folders[here]->subFolders.size()); + auto const count = static_cast(folders[here]->subFolders.size()); FolderHandle location = here; - location <<= 4; + location <<= FOLDER_BIT_ALLOCATE; location |= count; assert(count < FOLDER_MAX_COUNT, "Max subfolders reached\n"); - CreateFolderHelper(folders[0]->path, here, location, name); + CreateFolder(folders[0]->path, here, location, name); return location; } - bool SHFileSystem::DeleteFolder(FolderLocation location) noexcept + bool SHFileSystem::DeleteFolder(FolderPointer location) noexcept { - assert(folders.contains(location), "Delete target does not exist/invalid.\n"); + assert(folders.contains(location->id), "Delete target does not exist/invalid.\n"); - for (auto const& subFolder : folders[location]->subFolders) + for (auto const& subFolder : folders[location->id]->subFolders) { DeleteFolder(subFolder); } - RemoveDirectoryA(folders[location]->path.string().c_str()); + RemoveDirectoryA(folders[location->id]->path.c_str()); return true; } - bool SHFileSystem::CreateFolderHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept + void SHFileSystem::StartupFillDirectories(FolderPath path) noexcept + { + std::queue folderQueue; + + folderQueue.push(RegisterFolder(path, 0, 0, "Root")); + + while (!folderQueue.empty()) + { + auto folder = folderQueue.front(); + folderQueue.pop(); + FolderCounter count = 0; + + for (auto const& dirEntry : std::filesystem::directory_iterator(folder->path)) + { + if (!dirEntry.is_directory()) + { + continue; + } + + FolderLocation location = folder->id; + location <<= FOLDER_BIT_ALLOCATE; + location |= ++count; + + std::string name = dirEntry.path().string(); + name = name.substr(name.find_last_of('/') + 1, name.length() - name.find_last_of('/')); + + FolderPointer newFolder{ RegisterFolder( + dirEntry.path().string(), + folder->id, + location, + name) + }; + + folderQueue.push(newFolder); + folder->subFolders.push_back(newFolder); + } + } + } + + FolderPointer SHFileSystem::CreateFolder(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept { assert( - CreateDirectoryA(path.string().c_str(), nullptr), + CreateDirectoryA(path.c_str(), nullptr), "Failed to create folder\n" ); folders[location] = std::make_unique(location, name); - folders[parent]->subFolders.push_back(location); + folders[location]->path = path; + folders[parent]->subFolders.push_back(folders[location].get()); - return true; + return FolderMakeHelper(path, parent, location, name); + } + + FolderPointer SHFileSystem::RegisterFolder(FolderPath path, FolderLocation parent, FolderHandle location, + FolderName name) noexcept + { + return FolderMakeHelper(path, parent, location, name); + } + + FolderPointer SHFileSystem::FolderMakeHelper(FolderPath path, FolderLocation parent, FolderHandle location, + FolderName name) noexcept + { + folders[location] = std::make_unique(location, name); + folders[location]->path = path; + folders[parent]->subFolders.push_back(folders[location].get()); + + return folders[location].get(); } } diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.h b/SHADE_Engine/src/Filesystem/SHFileSystem.h index c58eb81e..2eff32bd 100644 --- a/SHADE_Engine/src/Filesystem/SHFileSystem.h +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.h @@ -3,17 +3,21 @@ #include #include #include -#include #include namespace SHADE { + class SHFolder; typedef unsigned char FolderCounter; typedef unsigned char FileCounter; - typedef uint32_t FolderLocation; - typedef uint32_t FolderHandle; + typedef uint64_t FolderLocation; + typedef uint64_t FolderHandle; typedef std::string FolderName; - typedef std::filesystem::path FolderPath; + typedef std::string FolderPath; + typedef SHFolder* FolderPointer; + + constexpr char FOLDER_BIT_ALLOCATE{ 4 }; + constexpr char FOLDER_MAX_DEPTH{ 26 }; class SHFolder { @@ -22,7 +26,9 @@ namespace SHADE FolderHandle id; FolderName name; - std::vector subFolders; + std::vector subFolders; + + bool folded; private: FolderPath path; @@ -34,11 +40,17 @@ namespace SHADE public: static FolderLocation CreateNewFolderHere(FolderName name, FolderLocation here = 0) noexcept; - static bool DeleteFolder(FolderLocation location) noexcept; + static bool DeleteFolder(FolderPointer location) noexcept; + + static void StartupFillDirectories(FolderPath path) noexcept; private: + static FolderPointer root; + static std::unordered_map> folders; - static bool CreateFolderHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept; + static FolderPointer CreateFolder(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept; + static FolderPointer RegisterFolder(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept; + static FolderPointer FolderMakeHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept; }; } \ No newline at end of file -- 2.40.1 From b6a6e9b3e5ca4d802f9d45e5b99ff1b81cb94ba1 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Tue, 13 Sep 2022 11:28:16 +0800 Subject: [PATCH 4/4] Updated some const --- SHADE_Engine/src/Filesystem/SHFileSystem.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.h b/SHADE_Engine/src/Filesystem/SHFileSystem.h index 2eff32bd..9b8b94a2 100644 --- a/SHADE_Engine/src/Filesystem/SHFileSystem.h +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.h @@ -8,6 +8,7 @@ namespace SHADE { class SHFolder; + typedef unsigned char FolderCounter; typedef unsigned char FileCounter; typedef uint64_t FolderLocation; @@ -17,7 +18,7 @@ namespace SHADE typedef SHFolder* FolderPointer; constexpr char FOLDER_BIT_ALLOCATE{ 4 }; - constexpr char FOLDER_MAX_DEPTH{ 26 }; + constexpr char FOLDER_MAX_DEPTH{ 16 }; class SHFolder { -- 2.40.1