Print Friendly and PDF

C++/유니티 엔진 모작(일시중지)

[유니티 엔진 제작] Layer

나는야 개발자 2025. 2. 18. 23:34
반응형

Layer을 이용해 그리는 순서를 정할 예정

 

1. Enum 클래스 추가

//DirectX_Enums.h

#pragma once

namespace dir
{
	enum class eLayerType
	{
		None,
		BackGround,
		Player,
		Max = 16,
	};
}

 

2. Layer 클래스 추가

#pragma once
//DirectX_Layer.h

#include "DirectX_Entity.h"
#include "CommonInclude.h"
#include "DirectX_GameObject.h"

namespace dir
{
	class Layer : public Entity
	{
	public:
		Layer();
		~Layer();

		virtual void Initialize(); //실행
		virtual void Update(); //우선 업데이트
		virtual void LateUpdate(); //나중 업데이트
		virtual void Render(HDC hdc); //렌더링

		//씬이 들어왔을때 처리
		virtual void OnEnter();
		//씬을 나갔을때 처리
		virtual void OnExit();

		void AddGameObject(GameObject* gameobj);

	private:
		//eLayerType mType;
		std::vector<GameObject*> mGameObjects;
	};
}
//DirectX_Layer.cpp

#include "DirectX_Layer.h"

namespace dir
{
	Layer::Layer() : mGameObjects{}
	{
	}

	Layer::~Layer()
	{
	}

	void Layer::Initialize()
	{
		for (auto& gameObject : mGameObjects)
		{
			if (gameObject == nullptr)
			{
				continue;
			}
			gameObject->Initialize();
		}
	}
	void Layer::Update()
	{
		for (auto& gameObject : mGameObjects)
		{
			if (gameObject == nullptr)
			{
				continue;
			}
			gameObject->Update();
		}
	}
	void Layer::LateUpdate()
	{
		for (auto& gameObject : mGameObjects)
		{
			if (gameObject == nullptr)
			{
				continue;
			}
			gameObject->LateUpdate();
		}
	}
	void Layer::Render(HDC hdc)
	{
		for (auto& gameObject : mGameObjects)
		{
			if (gameObject == nullptr)
			{
				continue;
			}
			gameObject->Render(hdc);
		}
	}

	void Layer::AddGameObject(GameObject* gameobj)
	{
		if (gameobj == nullptr)
		{
			return;
		}	

		mGameObjects.push_back(gameobj);
	}


	void Layer::OnEnter()
	{
	}
	void Layer::OnExit()
	{
	}
}

- Scene클래스에서 하던 GameObject 관리를 Layer클래스에서 관리하도록 수정

 

3. Scene 클래스 수정

//DirectX_Scene.h

public:
	//나머지 생략
	void AddGameobject(GameObject* gameobj, const eLayerType layerType);

private:
	std::vector<Layer*> mLayers;
//DirectX_Scene.cpp

#include "DirectX_Scene.h"

namespace dir
{
	Scene::Scene() : mLayers{(unsigned int)eLayerType::Max}
	{
	}

	Scene::~Scene()
	{
	}
	void Scene::Initialize()
	{
		for (Layer* layer : mLayers)
		{
			if (layer == nullptr)
			{
				continue;
			}
			layer->Initialize();
		}
	}
	void Scene::Update()
	{
		//for문(옛날 방식)
		/*for (size_t i = 0; i < mGameObjects.size(); i++)
		{
			mGameObjects[i]->Update();
		}*/

		//범위 기반 for문
		for (Layer* layer : mLayers)
		{
			if (layer == nullptr)
			{
				continue;
			}
			layer->Update();
		}
	}
	void Scene::LateUpdate()
	{
		for (Layer* layer : mLayers)
		{
			if (layer == nullptr)
			{
				continue;
			}
			layer->LateUpdate();
		}
	}
	void Scene::Render(HDC hdc)
	{
		for (Layer* layer : mLayers)
		{
			if (layer == nullptr)
			{
				continue;
			}
			layer->Render(hdc);
		}
	}

	void Scene::AddGameobject(GameObject* gameobj,const eLayerType layerType)
	{
		if (gameobj == nullptr)
		{
			return;
		}
		if (mLayers[(unsigned int)layerType] == nullptr)
		{
			mLayers[(unsigned int)layerType] = new Layer();
		}
		mLayers[(unsigned int)layerType]->AddGameObject(gameobj);
	}

	//씬이 들어왔을때 처리
	void Scene::OnEnter()
	{

	}
	//씬을 나갔을때 처리
	void Scene::OnExit()
	{

	}
}

 

-  AddGameObject에 LayerType을 추가

- vector에 담아두었던 GameObject를 Layer로 변경

 

4. Scene클래스 초기화

//DirectX_Scene.cpp

Scene::Scene() :mLayers{}
{
	mLayers.resize((unsigned int)eLayerType::Max);
	for (size_t i = 0; i < (unsigned int)eLayerType::Max; i++)
	{
		mLayers[i] = new Layer();
	}
}

Scene::~Scene()
{
}

- 실행 시 각 mLayers에 할당해주기

 

결과

- PlayScene에서 배경을 추가하게 되면 첫번째 배열에 1개가 할당됨

 

################################

- 2.19 : Enum, Layer 클래스 추가 및 Scene 클래스 수정

- 2.20 : 클래스 초기화 처리 및 결과

반응형