-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnowGame.cpp
360 lines (249 loc) · 10.3 KB
/
SnowGame.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "SnowGame.h"
#include <Windows.h>
#include "Vector3D.h"
#include "Vector2D.h"
#include "Rotator3D.h"
#include "Matrix4x4.h"
#include "InputSystem.h"
#include "Mesh.h"
#include "MathUtilities.h"
#include "DebuggingTools.h"
#include <iostream>
struct vertex
{
Vector3D position;
Vector2D texcoord;
};
SnowGame::SnowGame()
{
}
void SnowGame::OnCreate()
{
Window::OnCreate();
InputSystem::GetInputSystem()->addListener(this);////ADD WINDOW AS LISTENER TO THE INPUT SYSTEM
RECT rect = this->GetClientWindowRect();
m_swap_chain = GraphicsEngine::GetGraphicsEngine()->getRenderSystem()->createSwapChain(this->m_hwnd, rect.right - rect.left, rect.bottom - rect.top);
HUD::GetHUDSystem()->setScreenRect(rect);
//--------------------------------------SETUP QUAD FOR FRAME BUFFER---------------------------------------------------------------------
//SETUP THE MATERIAL FOR THE SCREEN QUAD
m_screen_quad_mat = GraphicsEngine::GetGraphicsEngine()->createMaterial(L"PostProcessVS.hlsl", L"PostProcessDefaultPS.hlsl");
m_screen_quad_mat->setCullMode(CULL_MODE::CULL_MODE_BACK);
//-------------
VertexMesh quad_vertex_list[] =
{
VertexMesh(Vector3D(-1,-1,0),Vector2D(0,1),Vector3D(),Vector3D(),Vector3D()),
VertexMesh(Vector3D(-1,1,0),Vector2D(0,0),Vector3D(),Vector3D(),Vector3D()),
VertexMesh(Vector3D(1,1,0),Vector2D(1,0),Vector3D(),Vector3D(),Vector3D()),
VertexMesh(Vector3D(1,-1,0),Vector2D(1,1),Vector3D(),Vector3D(),Vector3D()),
};
unsigned int quad_index_list[] =
{
0,1,2,
2,3,0
};
MaterialSlot quad_mat_slots[] =
{
{0,6,0}
};
m_quad_mesh = GraphicsEngine::GetGraphicsEngine()->getMeshManager()->createMesh(quad_vertex_list, 4, quad_index_list, 6, quad_mat_slots, 1);
m_list_materials.reserve(32);
//------------------------------------------------------------------------------------------------------------------------------
/////////CREATE SCENE RENDER TARGET/DEPTH STENCIL ///////////////////////////////////////////////////////
m_render_target = GraphicsEngine::GetGraphicsEngine()->getTextureManager()
->createTexture(Rect(rect.right - rect.left, rect.bottom - rect.top), Texture::Type::RenderTarget);
m_depth_stencil = GraphicsEngine::GetGraphicsEngine()->getTextureManager()
->createTexture(Rect(rect.right - rect.left, rect.bottom - rect.top), Texture::Type::DepthStencil);
/////////CREATE HUD RENDER TARGET/DEPTH STENCIL ///////////////////////////////////////////////////////
m_HUD_render_target = GraphicsEngine::GetGraphicsEngine()->getTextureManager()
->createTexture(Rect(rect.right - rect.left, rect.bottom - rect.top), Texture::Type::RenderTarget);
m_HUD_depth_stencil = GraphicsEngine::GetGraphicsEngine()->getTextureManager()
->createTexture(Rect(rect.right - rect.left, rect.bottom - rect.top), Texture::Type::DepthStencil);
m_HUD_render_target->m_texture->QueryInterface(&m_HUD_rt_surface);
HUD::GetHUDSystem()->BindSurfaceToHUDRT(m_HUD_rt_surface);
/////////////ADD THE RENDER TARGETS AS TEXTURES TO THE SCREEN QUAD
m_screen_quad_mat->addTexture(m_render_target);
m_screen_quad_mat->addTexture(m_HUD_render_target);
CreateMenuLevel();
}
void SnowGame::OnUpdate()
{
if (m_menu_level == m_active_level)
if (m_menu_level->GetExit())
{
OnDestroy();
}
Window::OnUpdate();
InputSystem::GetInputSystem()->update();
m_active_level->SetWindowRect(this->GetClientWindowRect());
this->update();
this->render();
}
void SnowGame::update()
{
if (m_game_level->GetRestart())
{
delete m_game_level;
m_game_level = nullptr;
CreateGameLevel();
}
if (m_active_level->GetActiveLevel() == Levels::EGameLevel && m_active_level->GetChangeLevel())
{
CreateGameLevel();
delete m_menu_level;
m_menu_level = nullptr;
}
else if (m_active_level->GetActiveLevel() == Levels::EMainMenu && m_active_level->GetChangeLevel())
{
CreateMenuLevel();
delete m_game_level;
m_game_level = nullptr;
}
m_active_level->Tick(m_delta_time);
}
void SnowGame::render()
{
//SET VIEWPORT
Rect viewport_size = m_render_target->getSize();
GraphicsEngine::GetGraphicsEngine()->getRenderSystem()->getImmediateDeviceContext()
->setViewportSize(viewport_size.width, viewport_size.height);
//SET SCENE RENDER TARGET----------------------------------------------------------
GraphicsEngine::GetGraphicsEngine()->getRenderSystem()->getImmediateDeviceContext()
->clearRenderTargetColor(this->m_render_target, 0.5, 0.3f, 0.4f, 1);
GraphicsEngine::GetGraphicsEngine()->getRenderSystem()->getImmediateDeviceContext()
->clearDepthStencil(this->m_depth_stencil);
GraphicsEngine::GetGraphicsEngine()->getRenderSystem()->getImmediateDeviceContext()
->setRenderTarget(this->m_render_target, this->m_depth_stencil);
//UPDATE ACTOR DATA ON ACTIVE LEVEL
m_active_level->updateActors(m_delta_time);
//SET HUD RENDER TARGET-----------------------------------------------------------
GraphicsEngine::GetGraphicsEngine()->getRenderSystem()->getImmediateDeviceContext()
->clearRenderTargetColor(this->m_HUD_render_target, 0, 0, 0, 0);
GraphicsEngine::GetGraphicsEngine()->getRenderSystem()->getImmediateDeviceContext()
->clearDepthStencil(this->m_HUD_depth_stencil);
GraphicsEngine::GetGraphicsEngine()->getRenderSystem()->getImmediateDeviceContext()
->setRenderTarget(this->m_HUD_render_target, m_HUD_depth_stencil);
//UPDATE HUD DATA ON ACTIVE LEVEL
m_active_level->updateWidgets(m_delta_time);
//CLEAR THE RENDER TARGET
GraphicsEngine::GetGraphicsEngine()->getRenderSystem()->getImmediateDeviceContext()->clearRenderTargetColor(this->m_swap_chain, 0, 0, 0, 1);
//SET VIEWPORT OF RENDER TARGET
RECT rc = this->GetClientWindowRect();
GraphicsEngine::GetGraphicsEngine()->getRenderSystem()->getImmediateDeviceContext()->setViewportSize(rc.right - rc.left, rc.bottom - rc.top);
//DRAW SCREEN QUAD
m_list_materials.clear();
m_list_materials.push_back(m_screen_quad_mat);
GraphicsEngine::GetGraphicsEngine()->drawMesh(m_quad_mesh, m_list_materials);
//PRESENT IMAGE
m_swap_chain->present(true);
//CALCULATE DELTA TIME
m_current_time = float(GetTickCount64());
m_delta_time = (m_current_time - m_old_time)/ 1000.0f;
if (m_delta_time > m_low_delta_limit)m_delta_time = m_low_delta_limit;
else if (m_delta_time < m_high_delta_limit)m_delta_time = m_high_delta_limit;
m_old_time = m_current_time;
}
void SnowGame::CreateGameLevel()
{
m_game_level = new GameLevel;
m_active_level = m_game_level;
m_active_level->SetWindowRect(this->GetClientWindowRect());
m_active_level->BeginPlay();
m_active_level->SetActiveLevel(Levels::EGameLevel);
}
void SnowGame::CreateMenuLevel()
{
m_menu_level = new MainMenuLevel;
m_active_level = m_menu_level;
m_active_level->SetWindowRect(this->GetClientWindowRect());
m_active_level->BeginPlay();
m_active_level->SetActiveLevel(Levels::EMainMenu);
}
void SnowGame::OnKillFocus()
{
//REMOVE THE WINDOW FROM THE LISTENER LIST ON INPUT SYSTEM -- ON KILL FOCUS EVENT
InputSystem::GetInputSystem()->removeListener(this);
}
void SnowGame::onKeyDown(int key)
{
//CALL KEY DOWN FUNCTION ON ACTIVE LEVEL
m_active_level->onKeyDown(key);
}
void SnowGame::onKeyUp(int key)
{
m_active_level->onKeyUp(key);
}
void SnowGame::onMouseMove(const Point& mouse_pos)
{
//SET MOUSE POSITION ON THE ACTIVE LEVEL
m_active_level->onMouseMove(mouse_pos);
}
void SnowGame::onLeftMouseDown(const Point& mouse_pos)
{
if(!m_lm_pressed)
m_active_level->onLeftMouseDown(mouse_pos);
m_lm_pressed = true;
}
void SnowGame::onLeftMouseUp(const Point& mouse_pos)
{
m_active_level->onLeftMouseUp(mouse_pos);
m_lm_pressed = false;
}
void SnowGame::onRightMouseDown(const Point& mouse_pos)
{
if (!m_rm_pressed)
m_active_level->onRightMouseDown(mouse_pos);
m_rm_pressed = true;
}
void SnowGame::onRightMouseUp(const Point& mouse_pos)
{
m_active_level->onRightMouseUp(mouse_pos);
m_rm_pressed = false;
}
void SnowGame::OnFocus()
{
//ADD THE WINDOW TO THE LISTENER LIST ON INPUT SYSTEM -- ON FOCUS EVENT
InputSystem::GetInputSystem()->addListener(this);
}
void SnowGame::OnSize()
{
//RESIZE SWAP CHAIN----------------------------------------------------
RECT rect = this->GetClientWindowRect();
m_swap_chain->resize(rect.right - rect.left, rect.bottom - rect.top);
m_active_level->SetWindowRect(this->GetClientWindowRect());
/*RECT size_screen = this->GetScreenSize();
m_swap_chain->setFullScreen(true, size_screen.right, size_screen.bottom);
if(size_screen.right== rect.right && size_screen.bottom == rect.bottom)
{
}
else m_swap_chain->setFullScreen(false, rect.right, rect.bottom);*/
//SET-UP THE RENDER TRAGETS/DEPTH STENCILS WITH THE NEW SIZES---------------------------------------
//3D SCENE
m_render_target = GraphicsEngine::GetGraphicsEngine()->getTextureManager()
->createTexture(Rect(rect.right - rect.left, rect.bottom - rect.top), Texture::Type::RenderTarget);
m_depth_stencil = GraphicsEngine::GetGraphicsEngine()->getTextureManager()
->createTexture(Rect(rect.right - rect.left, rect.bottom - rect.top), Texture::Type::DepthStencil);
//HUD
m_HUD_render_target = GraphicsEngine::GetGraphicsEngine()->getTextureManager()
->createTexture(Rect(rect.right - rect.left, rect.bottom - rect.top), Texture::Type::RenderTarget);
m_HUD_depth_stencil = GraphicsEngine::GetGraphicsEngine()->getTextureManager()
->createTexture(Rect(rect.right - rect.left, rect.bottom - rect.top), Texture::Type::DepthStencil);
m_HUD_render_target->m_texture->QueryInterface(&m_HUD_rt_surface);
HUD::GetHUDSystem()->BindSurfaceToHUDRT(m_HUD_rt_surface);
HUD::GetHUDSystem()->setScreenRect(rect);
//REMOVE THE RENDER TARGET TEXTURES FROM THE QUAD MATERIAL
m_screen_quad_mat->removeTexture(0);
m_screen_quad_mat->removeTexture(0);
//ADD THE NEW, RESIZED RENDER TARGET TEXTURES TO THE QUAD MATERIAL
m_screen_quad_mat->addTexture(m_render_target);
m_screen_quad_mat->addTexture(m_HUD_render_target);
this->update();
this->render();
}
void SnowGame::OnDestroy()
{
Window::OnDestroy();
m_swap_chain->setFullScreen(false, 1, 1);
}
SnowGame::~SnowGame()
{
}