1 | #include "dxstdafx.h"
|
---|
2 | #include ".\GameManager.h"
|
---|
3 | #include "fmod.h"
|
---|
4 | #include "RaytraceRenderer.h"
|
---|
5 | #include "Object3d.h"
|
---|
6 | #include "SoundNode.h"
|
---|
7 | #include "Goodie.h"
|
---|
8 | #include "Box.h"
|
---|
9 | #include "HUD.h"
|
---|
10 | #include "ParticleGroup.h"
|
---|
11 | #include "ParticleEmitter.h"
|
---|
12 | #include "Sprite.h"
|
---|
13 |
|
---|
14 |
|
---|
15 | bool GLOBAL_display_help_screenQ = false;
|
---|
16 | bool GLOBAL_player_freezeQ = false;
|
---|
17 |
|
---|
18 |
|
---|
19 | GameManager::GameManager(void)
|
---|
20 | {
|
---|
21 | this->consoleStarted = false;
|
---|
22 | this->activeScene=0;
|
---|
23 |
|
---|
24 | // Create the physics SDK
|
---|
25 | this->pOutputStream.manager = this;
|
---|
26 | this->pAllocator = new UserAllocator();
|
---|
27 | this->pPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION, this->pAllocator, &this->pOutputStream);
|
---|
28 | if (!this->pPhysicsSDK){
|
---|
29 | this->pPhysicsSDK = NULL;
|
---|
30 | this->printToConsole("PhysicsSDK building failed!");
|
---|
31 | MessageBox(NULL, L"Could not create PhysX instance! Maybe the drivers are not installed properly!", L"Error with PhysX Engine!", MB_ICONERROR);
|
---|
32 | exit(0);
|
---|
33 | }
|
---|
34 |
|
---|
35 | //create ResourceManager
|
---|
36 | this->resManager.setGameManager(this);
|
---|
37 | this->firstFrame = true;
|
---|
38 | this->fadeEffect = NULL;
|
---|
39 | this->gs.setSceneAlpha(0);
|
---|
40 |
|
---|
41 | for(int i=0;i<this->NB_EFFECTS;i++) {
|
---|
42 | this->effectList[i] = NULL;
|
---|
43 | this->effectReleaseCount[i] = 0;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | GameManager::~GameManager(void)
|
---|
48 | {
|
---|
49 | this->pPhysicsSDK->release();
|
---|
50 | this->pPhysicsSDK=NULL;
|
---|
51 | if(this->pAllocator)
|
---|
52 | delete this->pAllocator;
|
---|
53 | this->pAllocator = NULL;
|
---|
54 | SAFE_RELEASE(this->finalRenderTarget);
|
---|
55 |
|
---|
56 | for(int i=0;i<this->NB_EFFECTS;i++) {
|
---|
57 | if(this->effectList[i]) {
|
---|
58 | SAFE_RELEASE(this->effectList [i]);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | void GameManager::setScreenDimension(int width, int height)
|
---|
64 | {
|
---|
65 | this->screenWidth = width;
|
---|
66 | this->screenHeight = height;
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | std::wstring GameManager::HelpScreenString_Get()
|
---|
71 | {
|
---|
72 | std::wstring s;
|
---|
73 | //s += L"HELP:\n drive: w-a-s-d\n shoot: left mouse\n F2: show FPS\n F3: PhysX Debugger\n F4: Music On/Off";
|
---|
74 | s += L"HELP:\n drive: w-a-s-d\n shoot: left mouse";
|
---|
75 | s += L"\n Pause: Pause Game";
|
---|
76 | s += L"\n";
|
---|
77 |
|
---|
78 | s += L"\n 1 Big Guns\n 2 Bombs - explode late, but do cause lots of damage!\n 3 Alien weapon - just shoot!\n 4 Flamethrower - Toast your opponents\n 5 Icethrower - Slows down opponents (then toast them ;-) )";
|
---|
79 | s += L"\n";
|
---|
80 |
|
---|
81 | s += L"\n F3: PhysX Debugger\n F4: Music On/Off";
|
---|
82 | s += L"\n F5 Opponent Fire On/Off";
|
---|
83 | s += L"\n F6 Wrath of God";
|
---|
84 |
|
---|
85 | s += L"\n F9: DEPTH IMPOSTERS On/Off ";
|
---|
86 | if(this->gs.useDepthImposter) { s+= L"[is ON]"; } else { s+= L"[is OFF]"; }
|
---|
87 | s += L" <<< ";
|
---|
88 |
|
---|
89 | s += L"\n F10: RAYTRACE EFFECTS On/Off ";
|
---|
90 | if(this->gs.useRaytracer) { s+= L"[is ON]"; } else { s+= L"[is OFF]"; }
|
---|
91 | s += L" <<< ";
|
---|
92 |
|
---|
93 | return s;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | void GameManager::keyPressed(UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext)
|
---|
98 | {
|
---|
99 | if( bKeyDown )
|
---|
100 | {
|
---|
101 | //if(nChar == VK_PAUSE || nChar == VK_F9 || nChar == VK_F10 || !this->gs.preparepaused) {
|
---|
102 | switch( nChar )
|
---|
103 | {
|
---|
104 | case VK_ESCAPE:
|
---|
105 | break;
|
---|
106 | case VK_F1:
|
---|
107 | if(activeScene == &this->gs) {
|
---|
108 | #if(0)
|
---|
109 | this->gs.getHUD()->message("HELP:\n drive: w-a-s-d\n shoot: left mouse\n F2: show FPS\n F3: PhysX Debugger\n F4: Music On/Off\n F9: Depth Imposters On/Off\n F10: Raytracer On/Off\n F11 Opponent Fire On/Off\n Pause: Pause Game\n 1 Standard weapon\n 2 Bombs - explode late, but do cause lots of damage!\n 3 Alien weapon - just shoot!\n 4 Firethrower - Toast your opponent\n 5 Icethrower - Slow down your opponent", 2, 0.01f, true);
|
---|
110 | #elif(1) // MG
|
---|
111 | // Toggle help with F1
|
---|
112 | GLOBAL_display_help_screenQ = !GLOBAL_display_help_screenQ;
|
---|
113 | if(GLOBAL_display_help_screenQ) {
|
---|
114 | this->gs.preparepaused = true;
|
---|
115 | }
|
---|
116 | else {
|
---|
117 | this->gs.preparepaused= false;
|
---|
118 | this->gs.currentlypaused = false;
|
---|
119 | this->gs.getHUD()->message("", 1, 0.01f);
|
---|
120 | }
|
---|
121 | #endif
|
---|
122 | }
|
---|
123 | break;
|
---|
124 | case VK_F2:
|
---|
125 | //this->gs.showFPS = !this->gs.showFPS;
|
---|
126 | break;
|
---|
127 | case VK_F3:
|
---|
128 | if(!this->activeScene->usePhysXDebugger)
|
---|
129 | this->activeScene->usePhysXDebugger = true;
|
---|
130 | else
|
---|
131 | this->activeScene->usePhysXDebugger = false;
|
---|
132 | break;
|
---|
133 | case VK_F4:
|
---|
134 | this->gs.setBackgroundSoundMute(!this->gs.getBackgroundSoundMute());
|
---|
135 | this->ms.setBackgroundSoundMute(!this->ms.getBackgroundSoundMute());
|
---|
136 | break;
|
---|
137 | case VK_F5:
|
---|
138 | this->gs.aiPlayerFireEnable = !this->gs.aiPlayerFireEnable;
|
---|
139 | break;
|
---|
140 | case VK_F6:
|
---|
141 | //this->gs.getHUD()->message("hallo martin", 3, 0.01f);
|
---|
142 | if(this->activeScene == &this->gs) { gs.hasWon(); } // MG
|
---|
143 | break;
|
---|
144 | case VK_F7:
|
---|
145 | //this->gs.getHUD()->displayLose(3, 0.01f);
|
---|
146 | //GLOBAL_player_freezeQ = !GLOBAL_player_freezeQ;
|
---|
147 | break;
|
---|
148 | case VK_F8:
|
---|
149 | this->gs.drawBBoxes = !this->gs.drawBBoxes;
|
---|
150 | break;
|
---|
151 | case VK_F9:
|
---|
152 | this->gs.useDepthImposter = !this->gs.useDepthImposter;
|
---|
153 | break;
|
---|
154 | case VK_F10:
|
---|
155 | this->gs.useRaytracer = !this->gs.useRaytracer;
|
---|
156 | break;
|
---|
157 | case VK_PAUSE:
|
---|
158 | if(activeScene == &this->gs) {
|
---|
159 | if(!this->gs.preparepaused) {
|
---|
160 | this->gs.getHUD()->message("Game paused. Press Pause to Continue...", 1, 0.01f);
|
---|
161 | } else {
|
---|
162 | this->gs.currentlypaused = false;
|
---|
163 | this->gs.getHUD()->message("", 1, 0.01f);
|
---|
164 | }
|
---|
165 | this->gs.preparepaused= !this->gs.preparepaused;
|
---|
166 | }
|
---|
167 | break;
|
---|
168 | }
|
---|
169 | //}
|
---|
170 |
|
---|
171 | }
|
---|
172 | this->activeScene->setKeyPressed(nChar, bKeyDown);
|
---|
173 | }
|
---|
174 |
|
---|
175 | void GameManager::setMouseStatus(bool bLeftButtonDown, bool bRightButtonDown,bool bMiddleButtonDown,
|
---|
176 | bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta,
|
---|
177 | int xPos, int yPos)
|
---|
178 | {
|
---|
179 | this->activeScene->setMouseStatus(bLeftButtonDown, bRightButtonDown, bMiddleButtonDown, bSideButton1Down, bSideButton2Down, nMouseWheelDelta, xPos, yPos);
|
---|
180 | }
|
---|
181 |
|
---|
182 | void GameManager::addScene(Scene &newScene)
|
---|
183 | {
|
---|
184 | if(this->activeScene==0) {
|
---|
185 | this->activeScene = &newScene;
|
---|
186 | }
|
---|
187 | this->sceneVector.push_back(&newScene);
|
---|
188 | }
|
---|
189 |
|
---|
190 | void GameManager::removeScene(Scene &oldScene)
|
---|
191 | {
|
---|
192 | std::vector<Scene*>::iterator it;
|
---|
193 |
|
---|
194 | bool found = false;
|
---|
195 | int index = 0;
|
---|
196 | for(it=this->sceneVector.begin();it!=this->sceneVector.end();it++) {
|
---|
197 | if(&oldScene == *it) {
|
---|
198 | found = true;
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | index++;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | void GameManager::setActiveScene(Scene &aScene)
|
---|
206 | {
|
---|
207 | this->activeScene = &aScene;
|
---|
208 | }
|
---|
209 |
|
---|
210 | void GameManager::printToConsole(std::string output)
|
---|
211 | {
|
---|
212 | #if defined(DEBUG) | defined(_DEBUG)
|
---|
213 | if (!consoleStarted) {
|
---|
214 | AllocConsole();
|
---|
215 | outputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
|
---|
216 | consoleStarted = true;
|
---|
217 | }
|
---|
218 | output += "\n";
|
---|
219 | DWORD dwCharsWritten;
|
---|
220 | WriteConsoleA(outputHandle, output.c_str(), output.length(), &dwCharsWritten, NULL);
|
---|
221 | #endif
|
---|
222 | }
|
---|
223 |
|
---|
224 | void GameManager::initGame() {
|
---|
225 | ms.initScene(*this);
|
---|
226 | //add menue scene to scene vector:
|
---|
227 | ms.setVisible(true);
|
---|
228 | ms.setSceneAlpha(0);
|
---|
229 | this->addScene(ms);
|
---|
230 | gs.setVisible(false);
|
---|
231 | this->addScene(gs);
|
---|
232 | this->setActiveScene(ms);
|
---|
233 | }
|
---|
234 |
|
---|
235 | void GameManager::updateGame(float fElapsedTime)
|
---|
236 | {
|
---|
237 | FSOUND_Update();
|
---|
238 |
|
---|
239 | if(this->firstFrame) {
|
---|
240 | this->firstFrame = false;
|
---|
241 | this->ms.setSceneAlpha(0);
|
---|
242 | this->ms.fadeIn(1);
|
---|
243 | }
|
---|
244 | this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
|
---|
245 | this->device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
|
---|
246 |
|
---|
247 | UINT numberOfScenes = (UINT)this->sceneVector.size();
|
---|
248 | for(UINT i = 0; i < numberOfScenes; i++) {
|
---|
249 | this->sceneVector.at(i)->renderScene(fElapsedTime);
|
---|
250 | }
|
---|
251 |
|
---|
252 | this->device->SetRenderTarget(0, this->finalRenderTarget);
|
---|
253 | this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
|
---|
254 | this->device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
|
---|
255 | this->device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
|
---|
256 | this->device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_DESTALPHA);
|
---|
257 | this->device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
|
---|
258 |
|
---|
259 | for(UINT i = 0; i < numberOfScenes; i++) {
|
---|
260 | if(this->sceneVector.at(i)->isVisible()) {
|
---|
261 | this->sceneVector.at(i)->renderFinalImage(this->fadeEffect);
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | this->device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
|
---|
266 | this->device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
|
---|
267 | this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | void GameManager::OnLostDevice( void* pUserContext )
|
---|
272 | {
|
---|
273 | SAFE_RELEASE(this->finalRenderTarget);
|
---|
274 | this->resManager.OnLostDevice();
|
---|
275 | std::vector<Scene *>::iterator it;
|
---|
276 | for(it=this->sceneVector.begin();it!=this->sceneVector.end();it++) {
|
---|
277 | (*it)->OnLostDevice(pUserContext);
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | void GameManager::OnDestroyDevice( void* pUserContext )
|
---|
282 | {
|
---|
283 | this->resManager.OnDestroyDevice();
|
---|
284 | this->releaseEffect(this->fadeEffect);
|
---|
285 | this->fadeEffect = NULL;
|
---|
286 | std::vector<Scene *>::iterator it;
|
---|
287 | for(it=this->sceneVector.begin();it!=this->sceneVector.end();it++) {
|
---|
288 | (*it)->OnDestroyDevice(pUserContext);
|
---|
289 | }
|
---|
290 |
|
---|
291 | for(int i=0;i<this->NB_EFFECTS;i++) {
|
---|
292 | if(this->effectList[i] != NULL) {
|
---|
293 | this->printToConsole("WARNING! Not all Effects released!!");
|
---|
294 | }
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | HRESULT GameManager::OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
|
---|
299 | {
|
---|
300 | this->device = pd3dDevice;
|
---|
301 | return S_OK;
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | HRESULT GameManager::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
|
---|
306 | {
|
---|
307 | this->screenWidth = pBackBufferSurfaceDesc->Width;
|
---|
308 | this->screenHeight = pBackBufferSurfaceDesc->Height;
|
---|
309 |
|
---|
310 | this->device = pd3dDevice;
|
---|
311 | this->device->GetRenderTarget(0, &this->finalRenderTarget);
|
---|
312 | this->fadeEffect = this->loadEffect(GameManager::EFFECT_FADE, L"shaders/sceneFader.obj");
|
---|
313 |
|
---|
314 | std::vector<Scene *>::iterator it;
|
---|
315 | for(it=this->sceneVector.begin();it!=this->sceneVector.end();it++) {
|
---|
316 | (*it)->OnResetDevice(pd3dDevice, pBackBufferSurfaceDesc, pUserContext );
|
---|
317 | }
|
---|
318 |
|
---|
319 | return S_OK;
|
---|
320 | }
|
---|
321 |
|
---|
322 | ID3DXEffect* GameManager::getEffect(UINT id)
|
---|
323 | {
|
---|
324 | if(id<0 || id>=this->NB_EFFECTS)
|
---|
325 | return NULL;
|
---|
326 | ID3DXEffect* effect = this->effectList[id];
|
---|
327 | if(effect) {
|
---|
328 | this->effectReleaseCount[id]++;
|
---|
329 | }
|
---|
330 | return effect;
|
---|
331 | }
|
---|
332 |
|
---|
333 | void GameManager::releaseEffect(ID3DXEffect* effect)
|
---|
334 | {
|
---|
335 | }
|
---|
336 |
|
---|
337 | ID3DXEffect* GameManager::loadEffect(UINT id, LPCWSTR filename) {
|
---|
338 | ID3DXEffect* effect;
|
---|
339 |
|
---|
340 | if(!this->effectList[id]) {
|
---|
341 | DWORD dwShaderFlags = D3DXSHADER_PREFER_FLOW_CONTROL | D3DXSHADER_SKIPOPTIMIZATION;
|
---|
342 | #ifdef DEBUG_VS
|
---|
343 | dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT;
|
---|
344 | #endif
|
---|
345 | #ifdef DEBUG_PS
|
---|
346 | dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT;
|
---|
347 | #endif
|
---|
348 |
|
---|
349 | // Read the D3DX effect file
|
---|
350 | ID3DXBuffer* errBuff = NULL;
|
---|
351 | if (FAILED(D3DXCreateEffectFromFile( DXUTGetD3DDevice(),filename, NULL, NULL, dwShaderFlags,
|
---|
352 | NULL, &effect, &errBuff )))
|
---|
353 | {
|
---|
354 | int BufSize = errBuff->GetBufferSize();
|
---|
355 |
|
---|
356 | // displaying error message of arbitrary length
|
---|
357 | wchar_t* wbuf = new wchar_t[BufSize];
|
---|
358 | mbstowcs( wbuf, (const char*)errBuff->GetBufferPointer(), BufSize );
|
---|
359 | MessageBox(NULL, wbuf, L".fx Compilation Error", MB_ICONERROR); // show error message
|
---|
360 |
|
---|
361 | delete wbuf;
|
---|
362 | exit(-1);
|
---|
363 | } else {
|
---|
364 | this->effectList[id] = effect;
|
---|
365 | this->effectReleaseCount[id]++;
|
---|
366 | }
|
---|
367 | return effect;
|
---|
368 | } else {
|
---|
369 | this->effectReleaseCount[id]++;
|
---|
370 | return this->effectList[id];
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 | void GameManager::switchToMenueScene() {
|
---|
376 | ms.activateStandardBackground();
|
---|
377 | ms.getGUI()->SetVisible(true);
|
---|
378 | gs.fadeOut(1);
|
---|
379 | ms.fadeIn(1);
|
---|
380 | setActiveScene(this->ms);
|
---|
381 | //show mouse cursor:
|
---|
382 | ShowCursor(true);
|
---|
383 | this->gs.currentlypaused = false;
|
---|
384 | this->gs.preparepaused = false;
|
---|
385 | }
|
---|
386 |
|
---|
387 | void GameManager::switchToGameScene() {
|
---|
388 | if (gs.alreadyUsed) {
|
---|
389 |
|
---|
390 | //make menue scene invisible:
|
---|
391 | ms.fadeOut(1);
|
---|
392 | gs.setSceneAlpha(0);
|
---|
393 | gs.fadeIn(1);
|
---|
394 |
|
---|
395 | //make the gamescene active:
|
---|
396 | setActiveScene(this->gs);
|
---|
397 |
|
---|
398 | //hide mouse cursor:
|
---|
399 | ShowCursor(false);
|
---|
400 |
|
---|
401 | this->gs.currentlypaused = false;
|
---|
402 | this->gs.preparepaused = false;
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | void GameManager::OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
|
---|
407 | {
|
---|
408 | this->activeScene->OnGUIEvent(nEvent, nControlID, pControl, pUserContext);
|
---|
409 | }
|
---|