Scene-Layer-Object-Component Hierarchy
Overview
Self-made framework is structured as Scene → Layer → Object → Component hierarchy. Based on object-oriented design principles, it clearly defines the roles at each level while providing efficient management and flexible extensibility.
Hierarchy Diagram
/Block-References/01_Game-Projcets/Self-Made-Game-Framework/Source/Scene-Layer-Object-Component-Hierarchy_Eng.png)
This diagram visually illustrates the hierarchy of each class.
Class Descriptions
⚠️ Note: The code has been simplified for explanatory purposes regarding the Scene-Layer-Object-Component hierarchy.
1. CScene Class
class CScene abstract { friend class CSceneManager; protected: CScene(); virtual ~CScene(); protected: std::vector<CLayer*> mLayers; protected: virtual bool Enter(void* payload = nullptr) = 0; virtual bool Exit() = 0; virtual void Update(float deltaTime); virtual void LateUpdate(float deltaTime); virtual void Render(SDL_Renderer* renderer); public: template <typename T, int initialCapacity = 50> T* InstantiateObject(const std::string& name, ELayer::Type type); };
- Role:
- An abstract class inherited by all scene classes.
- Holds multiple layers, which separate elements that need to be drawn sequentially, such as backgrounds and objects.
- Key Methods:
- CScene():
- Scene constructor. Creates and initializes layers from the memory pool.
- virtual ~CScene():
- Scene destructor. Releases layer memory while keeping the memory pool maintained.
- virtual void Update(float deltaTime):
- Updates scene elements (layers, collision system, camera, UI).
- virtual void LateUpdate(float deltaTime):
- LateUpdates scene elements (layers, collision system, UI).
- virtual void Render(SDL_Renderer* renderer):
- Renders scene elements (layers, UI).
- T* InstantiateObject<T, int>(const std::string& name, ELayer::Type type):
- Creates an object using the memory pool.
- If the initialization succeeds, it is registered to the specified layer type.
- If the initialization fails, the memory is returned to the pool.
- Creates an object using the memory pool.
- CScene():
2. CLayer Class
class CLayer { friend class CScene; public: CLayer(); ~CLayer(); private: std::vector<class CObject*> mObjects; ESort::Type mSort = ESort::Y; protected: void Update(float deltaTime); void LateUpdate(float deltaTime); void Render(SDL_Renderer* renderer); public: void AddObject(CObject* obj) { mObjects.emplace_back(obj); } private: static bool SortY(CObject* objA, CObject* objB); };
- Role:
- A class that manages all objects within a layer.
- Sequentially update, late-update, and render objects, performing Y-axis sorting when necessary.
- A class that manages all objects within a layer.
- Key Methods:
- ~CLayer():
- Layer destructor. Releases all objects within the layer through the memory pool.
- void Update(float deltaTime):
- Update objects sequentially.
- Objects with !Active state are marked for removal.
- Objects with !Enable state skip the update.
- Update objects sequentially.
- void LateUpdate(float deltaTime):
- Late-update objects in reverse order.
- Objects with !Active state are removed and returned to the memory pool.
- Objects with !Enable state skip the late-update.
- Late-update objects in reverse order.
- void Render(SDL_Renderer* renderer):
- If Y-axis sorting is enabled, sort all objects by Y-coordinate 1 time.
- Render objects sequentially.
- Objects with !Active or !Enable state skip the render.
- ~CLayer():
3. CObject Class
class CObject abstract : public CEntityBase { friend class CScene; friend class CLayer; protected: CObject(); virtual ~CObject(); protected: CScene* mScene; CLayer* mLayer; CComponent* mRootComponent; protected: virtual bool Init(); virtual void Update(float deltaTime); virtual void LateUpdate(float deltaTime); virtual void Render(SDL_Renderer* renderer); virtual void Release() = 0; public: CTransform* GetTransform() const { return mRootComponent->GetTransform(); } CComponent* GetComponent(const std::string& name = "") { if (name.empty()) return mRootComponent; size_t hashID = std::hash<std::string>()(name); return mRootComponent->FindComponent(hashID); } template <typename T> T* GetComponent() const { return mRootComponent->FindComponent<T>(); } template <typename T, int initialCapacity = 10> T* AllocateComponent(const std::string& name); };
- Role:
- An abstract class inherited by all objects within a scene and layer.
- Objects organize their component hierarchy around
mRootComponent.- Sequentially update, late-update, and render
mRootComponent.
- Sequentially update, late-update, and render
- Key Variable:
CComponent* mRootComponent:- The object’s root component.
- The entry point of the object’s component hierarchy, which also includes
CTransform.
- Key Methods:
- CObject():
- Object constructor. Creates and initializes the root component.
- virtual ~CObject():
- Object destructor. Removes the root component.
- virtual void Init():
- Calls the initialization function of the root component.
- virtual void Update(float deltaTime):
- Calls the update function of the root component.
- virtual void LateUpdate(float deltaTime):
- Calls the late-update function of the root component.
- virtual void Render(SDL_Renderer* renderer):
- Calls the render function of the root component.
- T* AllocateComponent<T>(const std::string& name):
- Creates a component using the memory pool.
- CObject():
4. CComponent Class
class CComponent : public CEntityBase { friend class CObject; protected: CComponent(); virtual ~CComponent(); protected: size_t mTypeID = -1; CObject* mObject = nullptr; CTransform* mTransform = nullptr; CComponent* mParent = nullptr; std::vector<CComponent*> mChilds; protected: virtual bool Init(); virtual void Update(float deltaTime); virtual void LateUpdate(float deltaTime); virtual void Render(SDL_Renderer* renderer); virtual void Release(); public: CObject* GetObject() const { return mObject; } CTransform* GetTransform() const { return mTransform; } void AddChild(CComponent* child); bool DeleteChild(CComponent* child); private: CComponent* FindRootComponent(); CComponent* FindComponent(size_t id); template <typename T> T* FindComponent(); };
- Role:
- Base class for all components that provide actual functionality to objects.
- Manages the component hierarchy and can contain other components as children.
- Key Methods:
- CComponent():
- Component constructor. Creates and initializes a transform from the memory pool.
- virtual ~CComponent():
- Component destructor. Returns all child components and transforms to the memory pool.
- virtual bool Init():
- Calls the initialization function of all child components, but returns false if any fail.
- virtual void Update(float deltaTime):
- Update components sequentially.
- Components with !Active state are marked for removal.
- Components with !Enable state skip the update.
- Update components sequentially.
- virtual void LateUpdate(float deltaTime):
- Late-update components in reverse order.
- Components with !Active state are removed and returned to the memory pool.
- This process also cleans up the transform hierarchy.
- Components with !Enable state skip the late-updates.
- Components with !Active state are removed and returned to the memory pool.
- Late-update components in reverse order.
- virtual void Render(SDL_Renderer* renderer):
- Render component sequentially.
- Components with !Active or !Enable state skip the render.
- Render component sequentially.
- CComponent():
- Feature:
- The component class allows flexible hierarchy structuring, as illustrated in the image.
/Block-References/01_Game-Projcets/Self-Made-Game-Framework/Source/Component-Hierarchy_Eng.png)
- The component class allows flexible hierarchy structuring, as illustrated in the image.
Conclusion
Designing the Scene–Layer–Object–Component hierarchy provided practical experience in applying object-oriented design principles. It allowed me to systematically organize and manage complex game elements, while making maintenance and future extensions significantly easier.