-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.h
257 lines (207 loc) · 5.41 KB
/
scene.h
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#ifndef HLAM_SCENE_H
#define HLAM_SCENE_H
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
namespace hlam {
class Scene {
public:
virtual void Activate() = 0;
virtual void Update(float dt) = 0;
virtual void Draw() = 0;
virtual void Exit() = 0;
virtual ~Scene();
};
class SceneManager {
std::unordered_map<std::string, std::unique_ptr<Scene>> scenes;
std::string current;
std::string next;
public:
SceneManager();
template <class TScene, class... TParam>
TScene* Register(const std::string& name, TParam&&... params) {
scenes.emplace(name, std::make_unique<TScene>(std::forward<TParam>(params)...));
return dynamic_cast<TScene*>(scenes[name].get());
}
void Change(const std::string& name);
bool Update(float dt);
bool Draw();
std::string Current() const;
};
class ComboScene : public Scene {
std::vector<std::unique_ptr<Scene>> scenes;
public:
ComboScene();
void Activate();
void Update(float dt);
void Draw();
void Exit();
template <class TScene, class... TParam>
ComboScene* With(TParam&&... params) {
scenes.emplace_back(std::make_unique<TScene>(std::forward<TParam>(params)...));
return this;
}
};
class TimerScene : public Scene {
SceneManager* sm;
const float time;
std::string next;
float elapsed;
public:
TimerScene(SceneManager* sm, float time, std::string next);
void Activate();
void Update(float dt);
void Draw();
void Exit();
};
class LambdaScene : public Scene {
std::function<void()> callback;
public:
LambdaScene(std::function<void()> callback);
void Activate();
void Update(float dt);
void Draw();
void Exit();
};
#ifdef RAYLIB_H
class TextureScene : public Scene {
Texture2D tex;
int width;
int height;
public:
TextureScene(Texture2D tex, int width, int height);
void Activate();
void Update(float dt);
void Draw();
void Exit();
};
#endif // RAYLIB_H
class KeyAwaitScene : public Scene {
SceneManager* sm;
int key;
std::string next;
public:
KeyAwaitScene(SceneManager* sm, int key, std::string next);
void Activate();
void Update(float dt);
void Draw();
void Exit();
};
class DelayedKeyAwaitScene : public Scene {
SceneManager* sm;
int key;
float delay;
std::string next;
float time;
public:
DelayedKeyAwaitScene(SceneManager* sm, float delay, int key, std::string next);
void Activate();
void Update(float dt);
void Draw();
void Exit();
};
} // namespace hlam
#endif /* HLAM_SCENE_H */
#ifdef HLAM_SCENE_IMPLEMENTATION
#include <iostream>
namespace hlam {
Scene::~Scene() = default;
SceneManager::SceneManager() {}
void SceneManager::Change(const std::string& name) { next = name; }
bool SceneManager::Update(float dt) {
if (current != next) {
std::cout << "[INFO] change scene from '" << current << "' to '" << next << "'\n";
if (scenes.count(current) != 0) {
scenes[current]->Exit();
}
if (scenes.count(next) != 0) {
scenes[next]->Activate();
}
current = next;
}
if (scenes.count(current) != 0) {
scenes.at(current)->Update(dt);
return true;
}
return false;
}
bool SceneManager::Draw() {
if (scenes.count(current) != 0) {
scenes.at(current)->Draw();
return true;
}
return false;
}
std::string SceneManager::Current() const { return current; }
ComboScene::ComboScene() {}
void ComboScene::Activate() {
for (auto& scene : scenes) {
scene->Activate();
}
}
void ComboScene::Update(float dt) {
for (auto& scene : scenes) {
scene->Update(dt);
}
}
void ComboScene::Draw() {
for (auto& scene : scenes) {
scene->Draw();
}
}
void ComboScene::Exit() {
for (auto& scene : scenes) {
scene->Exit();
}
}
#ifdef RAYLIB_H
KeyAwaitScene::KeyAwaitScene(SceneManager* sm, int key, std::string next) : sm(sm), key(key), next(next) {}
void KeyAwaitScene::Activate() {}
void KeyAwaitScene::Update(float dt) {
if (GetKeyPressed() == key) {
sm->Change(next);
}
}
void KeyAwaitScene::Draw() {}
void KeyAwaitScene::Exit() {}
DelayedKeyAwaitScene::DelayedKeyAwaitScene(SceneManager* sm, float delay, int key, std::string next)
: sm(sm), key(key), delay(delay), next(next), time(0) {}
inline void DelayedKeyAwaitScene::Activate() { time = 0; }
inline void DelayedKeyAwaitScene::Update(float dt) {
time += dt;
if (time > delay) {
if (GetKeyPressed() == key) {
sm->Change(next);
}
}
}
void DelayedKeyAwaitScene::Draw() {}
void DelayedKeyAwaitScene::Exit() {}
TextureScene::TextureScene(Texture2D tex, int width, int height) : tex(tex), width(width), height(height) {}
void TextureScene::Activate() {}
void TextureScene::Update(float dt) {}
void TextureScene::Draw() {
Rectangle src{0, 0, float(tex.width), float(tex.height)};
Rectangle dst{0, 0, float(width), float(height)};
DrawTexturePro(tex, src, dst, {0, 0}, 0.0f, WHITE);
}
void TextureScene::Exit() {}
#endif // RAYLIB_H
TimerScene::TimerScene(SceneManager* sm, float time, std::string next) : sm{sm}, time{time}, next{next} {}
void TimerScene::Activate() { elapsed = 0; }
void TimerScene::Update(float dt) {
elapsed += dt;
if (elapsed > time) {
sm->Change(next);
}
}
void TimerScene::Draw() {}
void TimerScene::Exit() {}
LambdaScene::LambdaScene(std::function<void()> callback) : callback(callback) {}
void LambdaScene::Activate() {}
void LambdaScene::Update(float dt) { callback(); }
void LambdaScene::Draw() {}
void LambdaScene::Exit() {}
} // namespace hlam
#endif /* HLAM_SCENE_IMPLEMENTATION */