#include "dxstdafx.h" #include ".\GameManager.h" #include "fmod.h" #include "RaytraceRenderer.h" #include "Object3d.h" #include "SoundNode.h" #include "Goodie.h" #include "Box.h" #include "HUD.h" #include "ParticleGroup.h" #include "ParticleEmitter.h" #include "Sprite.h" bool GLOBAL_display_help_screenQ = false; bool GLOBAL_player_freezeQ = false; GameManager::GameManager(void) { this->consoleStarted = false; this->activeScene=0; // Create the physics SDK this->pOutputStream.manager = this; this->pAllocator = new UserAllocator(); this->pPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION, this->pAllocator, &this->pOutputStream); if (!this->pPhysicsSDK){ this->pPhysicsSDK = NULL; this->printToConsole("PhysicsSDK building failed!"); MessageBox(NULL, L"Could not create PhysX instance! Maybe the drivers are not installed properly!", L"Error with PhysX Engine!", MB_ICONERROR); exit(0); } //create ResourceManager this->resManager.setGameManager(this); this->firstFrame = true; this->fadeEffect = NULL; this->gs.setSceneAlpha(0); for(int i=0;iNB_EFFECTS;i++) { this->effectList[i] = NULL; this->effectReleaseCount[i] = 0; } } GameManager::~GameManager(void) { this->pPhysicsSDK->release(); this->pPhysicsSDK=NULL; if(this->pAllocator) delete this->pAllocator; this->pAllocator = NULL; SAFE_RELEASE(this->finalRenderTarget); for(int i=0;iNB_EFFECTS;i++) { if(this->effectList[i]) { SAFE_RELEASE(this->effectList [i]); } } } void GameManager::setScreenDimension(int width, int height) { this->screenWidth = width; this->screenHeight = height; } std::wstring GameManager::HelpScreenString_Get() { std::wstring s; //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"; s += L"HELP:\n drive: w-a-s-d\n shoot: left mouse"; s += L"\n Pause: Pause Game"; s += L"\n"; 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 ;-) )"; s += L"\n"; s += L"\n F3: PhysX Debugger\n F4: Music On/Off"; s += L"\n F5 Opponent Fire On/Off"; s += L"\n F9: DEPTH IMPOSTERS On/Off "; if(this->gs.useDepthImposter) { s+= L"[is ON]"; } else { s+= L"[is OFF]"; } s += L"\n F10: RAYTRACE EFFECTS On/Off "; if(this->gs.useRaytracer) { s+= L"[is ON]"; } else { s+= L"[is OFF]"; } return s; } void GameManager::keyPressed(UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext) { if( bKeyDown ) { //if(nChar == VK_PAUSE || nChar == VK_F9 || nChar == VK_F10 || !this->gs.preparepaused) { switch( nChar ) { case VK_ESCAPE: break; case VK_F1: if(activeScene == &this->gs) { #if(0) 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); #elif(0) // MG std::string s(""); 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"; s += "\n F9: Depth Imposters On/Off "; if(this->gs.useDepthImposter) { s+= "[ON]"; } else { s+= "[OFF]"; } s += "\n F10: Raytracer On/Off "; if(this->gs.useRaytracer) { s+= "[ON]"; } else { s+= "[OFF]"; } 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"; this->gs.getHUD()->message(s, 2, 0.01f, true); #elif(1) // MG // Toggle help with F1 GLOBAL_display_help_screenQ = !GLOBAL_display_help_screenQ; if(GLOBAL_display_help_screenQ) { this->gs.preparepaused = true; } else { this->gs.preparepaused= false; this->gs.currentlypaused = false; this->gs.getHUD()->message("", 1, 0.01f); } #endif } break; case VK_F2: //this->gs.showFPS = !this->gs.showFPS; break; case VK_F3: if(!this->activeScene->usePhysXDebugger) this->activeScene->usePhysXDebugger = true; else this->activeScene->usePhysXDebugger = false; break; case VK_F4: this->gs.setBackgroundSoundMute(!this->gs.getBackgroundSoundMute()); this->ms.setBackgroundSoundMute(!this->ms.getBackgroundSoundMute()); break; case VK_F6: //this->gs.getHUD()->message("hallo martin", 3, 0.01f); break; case VK_F7: //this->gs.getHUD()->displayLose(3, 0.01f); GLOBAL_player_freezeQ = !GLOBAL_player_freezeQ; break; case VK_F8: this->gs.drawBBoxes = !this->gs.drawBBoxes; break; case VK_F9: this->gs.useDepthImposter = !this->gs.useDepthImposter; break; case VK_F10: this->gs.useRaytracer = !this->gs.useRaytracer; break; case VK_F5: this->gs.aiPlayerFireEnable = !this->gs.aiPlayerFireEnable; break; case VK_PAUSE: if(activeScene == &this->gs) { if(!this->gs.preparepaused) { this->gs.getHUD()->message("Game paused. Press Pause to Continue...", 1, 0.01f); } else { this->gs.currentlypaused = false; this->gs.getHUD()->message("", 1, 0.01f); } this->gs.preparepaused= !this->gs.preparepaused; } break; } //} } this->activeScene->setKeyPressed(nChar, bKeyDown); } void GameManager::setMouseStatus(bool bLeftButtonDown, bool bRightButtonDown,bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos) { this->activeScene->setMouseStatus(bLeftButtonDown, bRightButtonDown, bMiddleButtonDown, bSideButton1Down, bSideButton2Down, nMouseWheelDelta, xPos, yPos); } void GameManager::addScene(Scene &newScene) { if(this->activeScene==0) { this->activeScene = &newScene; } this->sceneVector.push_back(&newScene); } void GameManager::removeScene(Scene &oldScene) { std::vector::iterator it; bool found = false; int index = 0; for(it=this->sceneVector.begin();it!=this->sceneVector.end();it++) { if(&oldScene == *it) { found = true; break; } index++; } } void GameManager::setActiveScene(Scene &aScene) { this->activeScene = &aScene; } void GameManager::printToConsole(std::string output) { #if defined(DEBUG) | defined(_DEBUG) if (!consoleStarted) { AllocConsole(); outputHandle = GetStdHandle(STD_OUTPUT_HANDLE); consoleStarted = true; } output += "\n"; DWORD dwCharsWritten; WriteConsoleA(outputHandle, output.c_str(), output.length(), &dwCharsWritten, NULL); #endif } void GameManager::initGame() { ms.initScene(*this); //add menue scene to scene vector: ms.setVisible(true); ms.setSceneAlpha(0); this->addScene(ms); gs.setVisible(false); this->addScene(gs); this->setActiveScene(ms); } void GameManager::updateGame(float fElapsedTime) { FSOUND_Update(); if(this->firstFrame) { this->firstFrame = false; this->ms.setSceneAlpha(0); this->ms.fadeIn(1); } this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); this->device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0); UINT numberOfScenes = (UINT)this->sceneVector.size(); for(UINT i = 0; i < numberOfScenes; i++) { this->sceneVector.at(i)->renderScene(fElapsedTime); } this->device->SetRenderTarget(0, this->finalRenderTarget); this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); this->device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); this->device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); this->device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_DESTALPHA); this->device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); for(UINT i = 0; i < numberOfScenes; i++) { if(this->sceneVector.at(i)->isVisible()) { this->sceneVector.at(i)->renderFinalImage(this->fadeEffect); } } this->device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); this->device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); } void GameManager::OnLostDevice( void* pUserContext ) { SAFE_RELEASE(this->finalRenderTarget); this->resManager.OnLostDevice(); std::vector::iterator it; for(it=this->sceneVector.begin();it!=this->sceneVector.end();it++) { (*it)->OnLostDevice(pUserContext); } } void GameManager::OnDestroyDevice( void* pUserContext ) { this->resManager.OnDestroyDevice(); this->releaseEffect(this->fadeEffect); this->fadeEffect = NULL; std::vector::iterator it; for(it=this->sceneVector.begin();it!=this->sceneVector.end();it++) { (*it)->OnDestroyDevice(pUserContext); } for(int i=0;iNB_EFFECTS;i++) { if(this->effectList[i] != NULL) { this->printToConsole("WARNING! Not all Effects released!!"); } } } HRESULT GameManager::OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { this->device = pd3dDevice; return S_OK; } HRESULT GameManager::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { this->screenWidth = pBackBufferSurfaceDesc->Width; this->screenHeight = pBackBufferSurfaceDesc->Height; this->device = pd3dDevice; this->device->GetRenderTarget(0, &this->finalRenderTarget); this->fadeEffect = this->loadEffect(GameManager::EFFECT_FADE, L"shaders/sceneFader.obj"); std::vector::iterator it; for(it=this->sceneVector.begin();it!=this->sceneVector.end();it++) { (*it)->OnResetDevice(pd3dDevice, pBackBufferSurfaceDesc, pUserContext ); } return S_OK; } ID3DXEffect* GameManager::getEffect(UINT id) { if(id<0 || id>=this->NB_EFFECTS) return NULL; ID3DXEffect* effect = this->effectList[id]; if(effect) { this->effectReleaseCount[id]++; } return effect; } void GameManager::releaseEffect(ID3DXEffect* effect) { } ID3DXEffect* GameManager::loadEffect(UINT id, LPCWSTR filename) { ID3DXEffect* effect; if(!this->effectList[id]) { DWORD dwShaderFlags = D3DXSHADER_PREFER_FLOW_CONTROL | D3DXSHADER_SKIPOPTIMIZATION; #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif // Read the D3DX effect file ID3DXBuffer* errBuff = NULL; if (FAILED(D3DXCreateEffectFromFile( DXUTGetD3DDevice(),filename, NULL, NULL, dwShaderFlags, NULL, &effect, &errBuff ))) { int BufSize = errBuff->GetBufferSize(); // displaying error message of arbitrary length wchar_t* wbuf = new wchar_t[BufSize]; mbstowcs( wbuf, (const char*)errBuff->GetBufferPointer(), BufSize ); MessageBox(NULL, wbuf, L".fx Compilation Error", MB_ICONERROR); // show error message delete wbuf; exit(-1); } else { this->effectList[id] = effect; this->effectReleaseCount[id]++; } return effect; } else { this->effectReleaseCount[id]++; return this->effectList[id]; } } void GameManager::switchToMenueScene() { ms.activateStandardBackground(); ms.getGUI()->SetVisible(true); gs.fadeOut(1); ms.fadeIn(1); setActiveScene(this->ms); //show mouse cursor: ShowCursor(true); this->gs.currentlypaused = false; this->gs.preparepaused = false; } void GameManager::switchToGameScene() { if (gs.alreadyUsed) { //make menue scene invisible: ms.fadeOut(1); gs.setSceneAlpha(0); gs.fadeIn(1); //make the gamescene active: setActiveScene(this->gs); //hide mouse cursor: ShowCursor(false); this->gs.currentlypaused = false; this->gs.preparepaused = false; } } void GameManager::OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext ) { this->activeScene->OnGUIEvent(nEvent, nControlID, pControl, pUserContext); }