-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneManager.cpp
166 lines (140 loc) · 3 KB
/
SceneManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "stdafx.h"
#include "SceneManager.h"
#include "GameScene.h"
#include "D3D11.h"
#include "DebugRenderer.h"
SceneManager::SceneManager()
: m_pActiveScene{}
, m_pNewActiveScene{}
, m_InputEnabled{true}
, m_IsInitialized{false}
, m_pDebugRenderer{ std::make_shared<DebugRenderer>()}
{
}
SceneManager::~SceneManager()
{
Destroy();
}
void SceneManager::Destroy()
{
for (GameScene* pScene : m_Scenes)
{
SafeDelete(pScene);
}
m_Scenes.clear();
}
void SceneManager::AddGameScene(GameScene* pScene)
{
auto it = find(m_Scenes.begin(), m_Scenes.end(), pScene);
if (it == m_Scenes.end())
{
m_Scenes.push_back(pScene);
if (m_IsInitialized)
pScene->RootInitialize();
if (m_pActiveScene == nullptr && m_pNewActiveScene == nullptr)
m_pNewActiveScene = pScene;
pScene->SetDebugRenderer(m_pDebugRenderer);
}
}
void SceneManager::RemoveGameScene(GameScene* scene)
{
auto it = find(m_Scenes.begin(), m_Scenes.end(), scene);
if (it != m_Scenes.end())
{
// Delete scene before removing its pointer from the container
SafeDelete(*it);
m_Scenes.erase(it);
}
}
void SceneManager::NextScene()
{
for (size_t i = 0; i < m_Scenes.size(); ++i)
{
if (m_Scenes[i] == m_pActiveScene)
{
auto nextScene = (++i) % m_Scenes.size();
m_pNewActiveScene = m_Scenes[nextScene];
return;
}
}
}
void SceneManager::PreviousScene()
{
for (size_t i = 0; i < m_Scenes.size(); ++i)
{
if (m_Scenes[i] == m_pActiveScene)
{
if (i == 0) i = m_Scenes.size();
--i;
m_pNewActiveScene = m_Scenes[i];
return;
}
}
}
void SceneManager::SetActiveGameScene(const std::wstring& sceneName)
{
auto it = find_if(m_Scenes.begin(), m_Scenes.end(), [sceneName](GameScene* pScene)
{
return (pScene->GetName() == sceneName);
});
if (it != m_Scenes.end())
{
m_pNewActiveScene = *it;
}
}
void SceneManager::Initialize()
{
m_pDebugRenderer->InitRenderer(Locator::GetD3D11()->pDevice);
for (GameScene* pScene : m_Scenes)
{
pScene->RootInitialize();
}
m_IsInitialized = true;
}
void SceneManager::WindowStateChanged(bool active)
{
if (m_pActiveScene)
{
m_pActiveScene->RootWindowStateChanged(active);
}
}
void SceneManager::ProcessMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYUP:
{
// process scene manager related messages
if (wParam == 'A')
{
// do stuff
break;
}
}
}
if(m_pActiveScene) m_pActiveScene->ProcessMessage(message, wParam, lParam);
}
void SceneManager::ToggleDebugRenderer()
{
m_pDebugRenderer->ToggleDebugRenderer();
}
void SceneManager::Update()
{
if (m_pNewActiveScene != nullptr)
{
//Deactivate the current active scene
if (m_pActiveScene != nullptr)
m_pActiveScene->RootSceneDeactivated();
//Set New Scene
m_pActiveScene = m_pNewActiveScene;
m_pNewActiveScene = nullptr;
//Active the new scene and reset SceneTimer
m_pActiveScene->RootSceneActivated();
}
if (m_pActiveScene != nullptr)
m_pActiveScene->RootUpdate();
}
void SceneManager::Draw()
{
if (m_pActiveScene ) m_pActiveScene->RootDraw();
}