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