반응형
1. PlayScene, TitleScene 코드 추가
//DirectX_PlayScene.cpp, DirectX_TitleScene.cpp
void TitleScene::LateUpdate()
{
Scene::LateUpdate();
//키 입력 시 씬 이동하는 거 추가
if (Input::GetKeyDown(eKeyCode::N))
{
SceneManager::LoadScene(L"PlayScene");
}
}
- N을 누르면 씬 이동 되는거 확인 가능
2. 씬 초기화
//DirectX_Scene.h
#pragma once
#include "DirectX_Entity.h"
#include "DirectX_GameObject.h"
namespace dir
{
//각 이름 등 사용할 것이기 때문에 상속 받아서 사용
class Scene : public Entity
{
public:
Scene();
~Scene();
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:
std::vector<GameObject*> mGameObjects;
};
}
- OnEnter, OnExit 함수 추가
3. PlayScene 코드 추가
//DirectX_PlayScene.h
#pragma once
#include "..//DirectX_SOURECE//DirectX_Scene.h"
namespace dir
{
class PlayScene : public Scene
{
public:
PlayScene();
~PlayScene();
void Initialize() override; //실행
void Update() override; //우선 업데이트
void LateUpdate() override; //나중 업데이트
void Render(HDC hdc) override; //렌더링
void OnEnter() override;
void OnExit() override;
private:
class Player* bg;
};
}
//DirectX_PlayerScene.cpp
#include "DirectX_PlayScene.h"
#include "DirectX_GameObject.h"
#include "DirectX_Transform.h"
#include "DirectX_SpriteRenderer.h"
#include "DirectX_Player.h"
#include "DirectX_Input.h"
#include "DirectX_SceneManager.h"
namespace dir
{
PlayScene::PlayScene()
{
}
PlayScene::~PlayScene()
{
}
void PlayScene::Initialize()
{
{
bg = new Player();
Transform* tr = bg->AddComponent<Transform>();
tr->SetPos(Vector2(0, 0));
tr->SetName(L"TR");
SpriteRenderer* sr = bg->AddComponent<SpriteRenderer>();
sr->SetName(L"SR");
sr->ImageLoad(L"D:\\Git\\Win_DirectX_Engine\\Resources\\CloudOcean.png");
AddGameObject(bg);
}
}
void PlayScene::Update()
{
//부모꺼의 함수 호출
Scene::Update();
}
void PlayScene::LateUpdate()
{
Scene::LateUpdate();
if (Input::GetKeyDown(eKeyCode::N))
{
SceneManager::LoadScene(L"TitleScene");
}
}
void PlayScene::Render(HDC hdc)
{
Scene::Render(hdc);
//2바이트 문자
wchar_t str[50] = L"Play Scene";
TextOut(hdc, 0, 0, str, 10);
}
void PlayScene::OnEnter()
{
}
void PlayScene::OnExit()
{
//위치 초기화
Transform* tr = bg->GetComponent<Transform>();
tr->SetPos(Vector2(0, 0));
}
}
- Player 전방선언 후 변수 추가
- OnExit를 이용해 위치 초기화 가능
4. SceneManager코드 수정
//DirectX_SceneManager.h
#pragma once
#include "DirectX_Scene.h"
namespace dir
{
class SceneManager
{
public:
//나머지 생략
static Scene* LoadScene(const std::wstring& name)
{
//내 현재씬의 OnExit() 호출
if (mActiveScene != nullptr)
{
mActiveScene->OnExit();
}
//wstring 퍼스트, iter 세컨드
std::map<std::wstring, Scene*>::iterator iter = mScenes.find(name);
if (iter == mScenes.end())
{
return nullptr;
}
//다음 씬 가져오기
mActiveScene = iter->second;
//다음씬을 가져왔을때 OnEnter() 호출
mActiveScene->OnEnter();
return iter->second;
}
//나머지 생략
}
- 현재 씬이 있다면 그 씬의 OnExit() 호출
- 다음 씬을 불렀을때 OnEnter() 호출
반응형
'C++ > 유니티 엔진 모작(일시중지)' 카테고리의 다른 글
[유니티 엔진 제작] Instantiate 함수 (0) | 2025.02.20 |
---|---|
[유니티 엔진 제작] Layer (0) | 2025.02.18 |
[유니티 엔진 제작] Vector2, GdiPlus (0) | 2025.02.18 |
[유니티 엔진 제작] 컴퍼넌트 구조 (0) | 2025.02.17 |
[유니티 엔진 제작] Scene 제작 (0) | 2025.02.13 |