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