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