source: GTP/trunk/App/Games/Jungle_Rumble/src/GameManager.cpp @ 1403

Revision 1403, 12.3 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - GLOBAL_player_freezeQ WIP

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