Merge pull request #38 from SHADE-DP/SP3-12-SceneGraph

This commit is contained in:
XiaoQiDigipen 2022-09-21 20:01:44 +08:00 committed by GitHub
commit 67d673ff80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 3 deletions

View File

@ -507,6 +507,11 @@ namespace SHADE
ReleaseNode(node);
}
void SHSceneGraph::Traverse (const UnaryPredicate& predicate) const
{
TraverseAndInvokePredicate(root, predicate);
}
/*-----------------------------------------------------------------------------------*/
/* Private Function Member Definitions */
/*-----------------------------------------------------------------------------------*/
@ -541,4 +546,13 @@ namespace SHADE
delete node;
}
void SHSceneGraph::TraverseAndInvokePredicate(const SHSceneNode* node, const UnaryPredicate& predicate)
{
for (auto* child : node->children)
{
predicate(child);
TraverseAndInvokePredicate(child, predicate);
}
}
} // namespace SHADE

View File

@ -18,12 +18,25 @@
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Forward Declarations */
/*-----------------------------------------------------------------------------------*/
class SHSceneGraph;
/*-----------------------------------------------------------------------------------*/
/* Type Definitions */
/*-----------------------------------------------------------------------------------*/
class SH_API SHSceneNode
{
private:
/*---------------------------------------------------------------------------------*/
/* Friends */
/*---------------------------------------------------------------------------------*/
friend class SHSceneGraph;
public:
/*---------------------------------------------------------------------------------*/
/* Data Members */
@ -71,6 +84,10 @@ namespace SHADE
void RemoveAllChildren () noexcept;
private:
/*---------------------------------------------------------------------------------*/
/* Data Members */
/*---------------------------------------------------------------------------------*/
EntityID entityID;
SHSceneNode* parent;
std::vector<SHSceneNode*> children;
@ -83,7 +100,10 @@ namespace SHADE
/* Type Definitions */
/*---------------------------------------------------------------------------------*/
using EntityNodeMap = std::unordered_map<EntityID, SHSceneNode*>;
using EntityNodeMap = std::unordered_map<EntityID, SHSceneNode*>;
using UnaryPredicate = void (*)(SHSceneNode*);
/*---------------------------------------------------------------------------------*/
/* Constructors & Destructor */
@ -124,6 +144,9 @@ namespace SHADE
bool RemoveNode (SHSceneNode* nodeToRemove) noexcept;
void Reset () noexcept;
void Traverse (const UnaryPredicate& predicate) const;
private:
/*---------------------------------------------------------------------------------*/
/* Data Members */
@ -136,8 +159,9 @@ namespace SHADE
/* Function Members */
/*---------------------------------------------------------------------------------*/
SHSceneNode* AllocateNode (EntityID entityID);
void ReleaseNode (SHSceneNode* node) noexcept;
SHSceneNode* AllocateNode (EntityID entityID);
void ReleaseNode (SHSceneNode* node) noexcept;
static void TraverseAndInvokePredicate (const SHSceneNode* node, const UnaryPredicate& predicate);
};