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

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);
};


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.
  • 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.
    • 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.
    • 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.

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);
};


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:
  • Feature:
    • 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.