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

Revision 1378, 9.6 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - integrate into GTP SVN structure

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