Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Node.hpp
1/*
2Copyright (C) 2014-2019,2021,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#ifndef AEONGAMES_NODE_H
17#define AEONGAMES_NODE_H
18#include <cstdint>
19#include <string>
20#include <vector>
21#include <bitset>
22#include <functional>
23#include <limits>
24#include <unordered_map>
25#include <tuple>
28#include "aeongames/AABB.hpp"
29#include "aeongames/CRC.hpp"
30#include "aeongames/Component.hpp"
31#include "aeongames/DependencyMap.hpp"
32
33namespace AeonGames
34{
35 class Window;
36 class NodeMsg;
37 class Scene;
39 using NodeParent = std::variant<Node*, Scene*>;
41 inline Node* GetNodePtr ( const NodeParent& aNodeParent )
42 {
43 return std::holds_alternative<Node*> ( aNodeParent ) ? std::get<Node*> ( aNodeParent ) : nullptr;
44 }
45
46 inline Scene* GetScenePtr ( const NodeParent& aNodeParent )
47 {
48 return std::holds_alternative<Scene*> ( aNodeParent ) ? std::get<Scene*> ( aNodeParent ) : nullptr;
49 }
50
53 class Node
54 {
55 public:
58 {
59 EnabledBit = 1,
60 VisibleBit = 2,
61 AllBits = 3
62 };
63
64 enum Flags
65 {
66 Enabled = 0,
67 Visible,
68 FlagCount
69 };
70
72 DLL Node ( uint32_t aFlags = AllBits );
75 DLL void SetName ( const std::string& aName );
78 DLL const std::string& GetName() const;
81 DLL void Serialize ( NodeMsg& aNodeMsg ) const;
84 DLL void Deserialize ( const NodeMsg& aNodeMsg );
89 DLL void SetFlags ( uint32_t aFlagBits, bool aEnabled = true );
94 DLL void SetFlag ( enum Flags aFlag, bool aEnabled = true );
98 DLL bool IsFlagEnabled ( enum Flags aFlag ) const;
102 DLL Node* Add ( std::unique_ptr<Node> aNode );
107 DLL Node* Insert ( size_t aIndex, std::unique_ptr<Node> aNode );
111 DLL std::unique_ptr<Node> Remove ( Node* );
115 DLL std::unique_ptr<Node> RemoveByIndex ( size_t aIndex );
120 DLL Scene* GetScene() const;
127 DLL void LoopTraverseDFSPreOrder ( const std::function<void ( Node& ) >& aAction );
138 DLL void LoopTraverseDFSPreOrder (
139 const std::function<void ( Node& ) >& aPreamble,
140 const std::function<void ( Node& ) >& aPostamble );
142 DLL void LoopTraverseDFSPreOrder ( const std::function<void ( const Node& ) >& aAction ) const;
149 DLL void LoopTraverseDFSPostOrder ( const std::function<void ( Node& ) >& aAction );
151 DLL void LoopTraverseDFSPostOrder ( const std::function<void ( const Node& ) >& aAction ) const;
158 DLL void RecursiveTraverseDFSPreOrder ( const std::function<void ( Node& ) >& aAction );
165 DLL void RecursiveTraverseDFSPostOrder ( const std::function<void ( Node& ) >& aAction );
167 DLL void LoopTraverseAncestors ( const std::function<void ( Node& ) >& aAction );
169 DLL void LoopTraverseAncestors ( const std::function<void ( const Node& ) >& aAction ) const;
171 DLL void RecursiveTraverseAncestors ( const std::function<void ( Node& ) >& aAction );
173 DLL Node* Find ( const std::function<bool ( const Node& ) >& aUnaryPredicate ) const;
176 DLL const Transform& GetLocalTransform() const;
179 DLL const Transform& GetGlobalTransform() const;
182 DLL const AABB& GetAABB() const;
185 DLL void SetLocalTransform ( const Transform& aTransform );
188 DLL void SetGlobalTransform ( const Transform& aTransform );
191 DLL void SetAABB ( const AABB& aAABB );
194 DLL size_t GetChildrenCount() const;
198 DLL Node* GetChild ( size_t aIndex ) const;
202 DLL const Node& operator[] ( const std::size_t index ) const;
206 DLL Node& operator[] ( const std::size_t index );
209 DLL NodeParent GetParent() const;
212 DLL size_t GetIndex() const;
215 DLL uint32_t GetId() const;
221 DLL Component* AddComponent ( std::unique_ptr<Component> aComponent );
224 DLL size_t GetComponentCount() const;
228 DLL Component* GetComponentByIndex ( size_t aIndex ) const;
232 DLL Component* GetComponent ( uint32_t aId ) const;
236 DLL std::unique_ptr<Component> RemoveComponent ( uint32_t aId );
240 DLL void Update ( const double delta );
244 DLL void Render ( Renderer& aRenderer, void* aWindowId ) const;
248 DLL void ProcessMessage ( uint32_t aMessageType, const void* aMessageData );
249 private:
250 friend class Scene;
251 std::string mName{};
252 NodeParent mParent{};
253 Transform mLocalTransform{};
254 Transform mGlobalTransform{};
255 AABB mAABB{};
256 std::vector<std::unique_ptr<Node >> mNodes{};
257 DependencyMap<uint32_t> mComponentDependencyMap{};
261 std::vector<std::unique_ptr<Component >> mComponents{};
264 mutable std::vector<Node*>::size_type mIterator{ 0 };
265 uint32_t mId{};
266 std::bitset<8> mFlags{};
267 };
268}
269#endif
Header for the axis aligned bounding box class.
#define LoopTraverseDFSPreOrder(...)
Macro generating LoopTraverseDFSPreOrder implementations for const and non-const variants.
Definition Node.cpp:425
Platform-specific macros, includes, and DLL export/import definitions.
Header for Transform component class.
Axis Aligned Bounding Box class.
Definition AABB.hpp:34
Abstract base class for node components.
Definition Component.hpp:39
Scene graph node representing an entity in the game world.
Definition Node.hpp:54
DLL void SetLocalTransform(const Transform &aTransform)
Set the local transform relative to the parent.
Definition Node.cpp:137
DLL Component * GetComponent(uint32_t aId) const
Get a component by its type identifier.
Definition Node.cpp:616
DLL const Node & operator[](const std::size_t index) const
Access a child node by index (const).
Definition Node.cpp:60
DLL Component * GetComponentByIndex(size_t aIndex) const
Get a component by its index.
Definition Node.cpp:594
DLL std::unique_ptr< Node > Remove(Node *)
Remove a child node by pointer.
Definition Node.cpp:355
DLL Scene * GetScene() const
Retrieve a pointer to the scene containing the node instance.
Definition Node.cpp:95
DLL const Transform & GetLocalTransform() const
Get the local transform relative to the parent.
Definition Node.cpp:120
DLL const AABB & GetAABB() const
Get the axis-aligned bounding box.
Definition Node.cpp:130
DLL uint32_t GetId() const
Get the unique identifier of this node.
Definition Node.cpp:210
DLL void SetAABB(const AABB &aAABB)
Set the axis-aligned bounding box.
Definition Node.cpp:194
DLL size_t GetIndex() const
Get this node's index within its parent's child list.
Definition Node.cpp:77
DLL Node * GetChild(size_t aIndex) const
Get a child node by index.
Definition Node.cpp:69
DLL Node * Find(const std::function< bool(const Node &) > &aUnaryPredicate) const
Find a descendant node matching a predicate.
DLL Node(uint32_t aFlags=AllBits)
Construct a node with the given initial flags.
Definition Node.cpp:45
Flags
Individual flag indices for use with SetFlag/IsFlagEnabled.
Definition Node.hpp:65
DLL void Serialize(NodeMsg &aNodeMsg) const
Serialize this node's state into a protobuf message.
Definition Node.cpp:218
DLL void RecursiveTraverseDFSPostOrder(const std::function< void(Node &) > &aAction)
Recursive depth first search iteration.
DLL void SetName(const std::string &aName)
Set the name of this node.
Definition Node.cpp:199
DLL void RecursiveTraverseDFSPreOrder(const std::function< void(Node &) > &aAction)
Recursive depth first search iteration.
Definition Node.cpp:527
FlagBits
Bitmask values for node flags.
Definition Node.hpp:58
DLL const std::string & GetName() const
Get the name of this node.
Definition Node.cpp:205
DLL void SetGlobalTransform(const Transform &aTransform)
Set the global (world-space) transform.
Definition Node.cpp:158
DLL void Render(Renderer &aRenderer, void *aWindowId) const
Render this node and its children.
Definition Node.cpp:581
DLL void ProcessMessage(uint32_t aMessageType, const void *aMessageData)
Deliver a message to this node and its components.
Definition Node.cpp:573
DLL const Transform & GetGlobalTransform() const
Get the global (world-space) transform.
Definition Node.cpp:125
DLL std::unique_ptr< Component > RemoveComponent(uint32_t aId)
Remove a component by its type identifier.
Definition Node.cpp:629
DLL Node * Add(std::unique_ptr< Node > aNode)
Append a child node.
Definition Node.cpp:340
DLL NodeParent GetParent() const
Get this node's parent.
Definition Node.cpp:73
DLL void RecursiveTraverseAncestors(const std::function< void(Node &) > &aAction)
Traverse ancestors recursively, calling an action on each.
Definition Node.cpp:556
DLL void LoopTraverseAncestors(const std::function< void(Node &) > &aAction)
Traverse ancestors iteratively, calling an action on each.
Definition Node.cpp:536
DLL void LoopTraverseDFSPreOrder(const std::function< void(const Node &) > &aAction) const
Constant version of LoopTraverseDFSPreOrder.
DLL void LoopTraverseDFSPostOrder(const std::function< void(Node &) > &aAction)
Iterative depth first search iteration.
DLL void Update(const double delta)
Update this node and its children.
Definition Node.cpp:565
DLL void Deserialize(const NodeMsg &aNodeMsg)
Deserialize this node's state from a protobuf message.
Definition Node.cpp:296
DLL void LoopTraverseDFSPreOrder(const std::function< void(Node &) > &aAction)
Iterative depth first search iteration.
DLL Node * Insert(size_t aIndex, std::unique_ptr< Node > aNode)
Insert a child node at a specific index.
Definition Node.cpp:317
DLL size_t GetComponentCount() const
Get the number of components attached to this node.
Definition Node.cpp:589
DLL bool IsFlagEnabled(enum Flags aFlag) const
Check whether a specific flag is enabled.
Definition Node.cpp:115
DLL Component * AddComponent(std::unique_ptr< Component > aComponent)
Add a component to this node.
Definition Node.cpp:599
DLL void SetFlag(enum Flags aFlag, bool aEnabled=true)
Enables or disables a single node flag.
Definition Node.cpp:110
DLL std::unique_ptr< Node > RemoveByIndex(size_t aIndex)
Remove a child node by index.
Definition Node.cpp:379
DLL void LoopTraverseDFSPostOrder(const std::function< void(const Node &) > &aAction) const
Constant version of LoopTraverseDFSPostOrder.
DLL void SetFlags(uint32_t aFlagBits, bool aEnabled=true)
Enables or disables a set of node flags.
Definition Node.cpp:105
DLL size_t GetChildrenCount() const
Get the number of direct child nodes.
Definition Node.cpp:56
Abstract base class for rendering backends.
Definition Renderer.hpp:44
Scene class. Scene is the container for all elements in a game level, takes care of collision,...
Definition Scene.hpp:40
Component class for any object that requires space transformations.
Definition Transform.hpp:34
<- This is here just for the literals
Definition AABB.hpp:31
Scene * GetScenePtr(const NodeParent &aNodeParent)
Extract the Scene pointer from a NodeParent variant.
Definition Node.hpp:46
Node * GetNodePtr(const NodeParent &aNodeParent)
Extract the Node pointer from a NodeParent variant.
Definition Node.hpp:41
std::variant< Node *, Scene * > NodeParent
Variant type representing a node's parent (either a Node or a Scene).
Definition Node.hpp:39