#include "dxstdafx.h" #include "resource.h" #include "GameManager.h" #include "fmod.h" #import named_guids // Global Variables CDXUTDialogResourceManager DialogResourceManager; CDXUTDialog GUI; GameManager manager; bool firstFrame; bool secondFrame; bool usePhysXDebug; MSXML::IXMLDOMDocumentPtr configDomDocument; MSXML::IXMLDOMElementPtr configDocRoot; int width; int height; //-------------------------------------------------------------------------------------- // Rejects any devices that aren't acceptable by returning false //-------------------------------------------------------------------------------------- bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext ) { // Skip backbuffer formats that don't support alpha blending IDirect3D9* pD3D = DXUTGetD3DObject(); if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, BackBufferFormat ) ) ) return false; // Must support cube textures if( !( pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP ) ) return false; // Must support pixel shader 3.0 if( pCaps->PixelShaderVersion < D3DPS_VERSION( 3, 0 ) ) return false; // need to support D3DFMT_A32B32G32R32F render target if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, D3DUSAGE_RENDERTARGET, D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F ) ) ) return false; // Verify that the depth format exists HRESULT hr = pD3D->CheckDeviceFormat(pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16); if(FAILED(hr)) return FALSE; // Verify that the depth format is compatible hr = pD3D->CheckDepthStencilMatch(pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, BackBufferFormat, D3DFMT_D16); if(FAILED(hr)) return false; return true; } //-------------------------------------------------------------------------------------- // Before a device is created, modify the device settings as needed //-------------------------------------------------------------------------------------- bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext ) { // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW then switch to SWVP. if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 || pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) ) { pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING; } else { pDeviceSettings->BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING; } // This application is designed to work on a pure device by not using // IDirect3D9::Get*() methods, so create a pure device if supported and using HWVP. if ((pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE) != 0 && (pDeviceSettings->BehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING) != 0 ) pDeviceSettings->BehaviorFlags |= D3DCREATE_PUREDEVICE; // Debugging vertex shaders requires either REF or software vertex processing // and debugging pixel shaders requires REF. #ifdef DEBUG_VS if( pDeviceSettings->DeviceType != D3DDEVTYPE_REF ) { pDeviceSettings->BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING; pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE; pDeviceSettings->BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING; } #endif #ifdef DEBUG_PS pDeviceSettings->DeviceType = D3DDEVTYPE_REF; #endif return true; } //-------------------------------------------------------------------------------------- // Create any D3DPOOL_MANAGED resources here //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { //GUI Stuff: manager.OnCreateDevice( pd3dDevice, pBackBufferSurfaceDesc, pUserContext ); DialogResourceManager.OnCreateDevice(pd3dDevice); return S_OK; } //-------------------------------------------------------------------------------------- // Create any D3DPOOL_DEFAULT resources here //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { manager.printToConsole("On Reset Device in WuermerDX9 Called!"); HRESULT hr = DialogResourceManager.OnResetDevice(); if(hr!=S_OK) manager.printToConsole("Reset failed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); manager.OnResetDevice(pd3dDevice, pBackBufferSurfaceDesc, pUserContext); return S_OK; } //-------------------------------------------------------------------------------------- // Handle updates to the scene //-------------------------------------------------------------------------------------- void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { } //-------------------------------------------------------------------------------------- // Render the scene //-------------------------------------------------------------------------------------- void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { manager.updateGame(fElapsedTime); } //-------------------------------------------------------------------------------------- // Handle messages to the application //-------------------------------------------------------------------------------------- LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext ) { // Always allow dialog resource manager calls to handle global messages // so GUI state is updated correctly *pbNoFurtherProcessing = DialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam ); if( *pbNoFurtherProcessing ) return 0; // Give the dialogs a chance to handle the message first *pbNoFurtherProcessing = GUI.MsgProc( hWnd, uMsg, wParam, lParam ); if( *pbNoFurtherProcessing ) return 0; return 0; } //-------------------------------------------------------------------------------------- // Release resources created in the OnResetDevice callback here //-------------------------------------------------------------------------------------- void CALLBACK OnLostDevice( void* pUserContext ) { DialogResourceManager.OnLostDevice(); manager.OnLostDevice(pUserContext); } //-------------------------------------------------------------------------------------- // Release resources created in the OnCreateDevice callback here //-------------------------------------------------------------------------------------- void CALLBACK OnDestroyDevice( void* pUserContext ) { DialogResourceManager.OnDestroyDevice(); manager.OnDestroyDevice(pUserContext); } //-------------------------------------------------------------------------------------- // As a convenience, DXUT inspects the incoming windows messages for // keystroke messages and decodes the message parameters to pass relevant keyboard // messages to the application. The framework does not remove the underlying keystroke // messages, which are still passed to the application's MsgProc callback. //-------------------------------------------------------------------------------------- void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext ) { manager.keyPressed(nChar, bKeyDown, bAltDown, pUserContext); } void CALLBACK MouseCallback(bool bLeftButtonDown, bool bRightButtonDown,bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos, void* pUserContext) { manager.setMouseStatus(bLeftButtonDown, bRightButtonDown, bMiddleButtonDown, bSideButton1Down, bSideButton2Down, nMouseWheelDelta, xPos, yPos); } void testfunktion() { } //-------------------------------------------------------------------------------------- // Handles the GUI events //-------------------------------------------------------------------------------------- void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext ) { manager.OnGUIEvent(nEvent, nControlID, pControl, pUserContext); } //-------------------------------------------------------------------------------------- // Initialize everything and go into a render loop //-------------------------------------------------------------------------------------- extern int CUBEMAP_SIZE; INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int ) { // Enable run-time memory check for debug builds. #if defined(DEBUG) | defined(_DEBUG) _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); #endif // Set the callback functions DXUTSetCallbackDeviceCreated( OnCreateDevice ); DXUTSetCallbackDeviceReset( OnResetDevice ); DXUTSetCallbackDeviceLost( OnLostDevice ); DXUTSetCallbackDeviceDestroyed( OnDestroyDevice ); DXUTSetCallbackKeyboard( KeyboardProc ); DXUTSetCallbackMouse(MouseCallback,true); DXUTSetCallbackMsgProc( MsgProc ); DXUTSetCallbackFrameRender( OnFrameRender ); DXUTSetCallbackFrameMove( OnFrameMove ); // TODO: Perform any application-level initialization here manager.printToConsole("initialising game"); //Sound init manager.printToConsole("initialising FMOD"); FSOUND_Init(44100, 32, 0); FSOUND_3D_SetDistanceFactor(1.0f); //Load Config XML //XML Stuff ::CoInitialize(NULL); HRESULT hr = configDomDocument.CreateInstance(MSXML::CLSID_DOMDocument); if (FAILED(hr)) { manager.printToConsole("Unable to create XML instance!"); exit(0); } variant_t vResult; vResult = configDomDocument->load("./config.xml"); if (((bool)vResult) == TRUE) { configDocRoot = configDomDocument->documentElement; } else { manager.printToConsole("Loading Configuration failed!"); exit(0); } _bstr_t tmpWidth("width"); _bstr_t tmpHeight("height"); _bstr_t tmpFullscreen("fullscreen"); _bstr_t tmpUsePhysX("physxdebug"); _bstr_t tmpChallenge("challenge"); _bstr_t tmpChallengeName("name"); _bstr_t tmpChallengeLocation("location"); _bstr_t tmpChallengeScreen("loadingScreen"); _bstr_t tmpCubemapSize("cubemapsize"); // MG int fullscreen; for (MSXML::IXMLDOMNodePtr pChild = configDocRoot->firstChild;NULL != pChild;pChild = pChild->nextSibling) { if(pChild->nodeType == MSXML::NODE_ELEMENT) { if(pChild->nodeName == tmpWidth) { width = atoi(pChild->firstChild->text); //Width } else if(pChild->nodeName == tmpHeight) { height = atoi(pChild->firstChild->text); //Height } else if(pChild->nodeName == tmpFullscreen) { fullscreen = atoi(pChild->firstChild->text); //Use Fullscreen? } else if(pChild->nodeName == tmpCubemapSize) { CUBEMAP_SIZE = atoi(pChild->firstChild->text); //Raytrace Effects Cubemap Size } else if(pChild->nodeName == tmpUsePhysX) { usePhysXDebug = (atoi(pChild->firstChild->text)==1);//Use Shader? } else if(pChild->nodeName == tmpChallenge) { //A Challenge for(MSXML::IXMLDOMNodePtr pChallengeChild = pChild->firstChild; NULL != pChallengeChild; pChallengeChild = pChallengeChild->nextSibling) { if(pChallengeChild->nodeName == tmpChallengeName) { std::string temp = pChallengeChild->firstChild->text; manager.ms.getChallengeNameList()->push_back(temp); //Challenge Name } else if(pChallengeChild->nodeName == tmpChallengeLocation) { std::string temp = pChallengeChild->firstChild->text; manager.ms.getChallengeList()->push_back(temp); //Challenge xml file location } else if(pChallengeChild->nodeName == tmpChallengeScreen) { std::string temp = pChallengeChild->firstChild->text; manager.ms.getChallengeScreenList()->push_back(temp); //Challenge loading screen file location } } } } } manager.gs.usePhysXDebugger = usePhysXDebug; // Initialize DXUT and create the desired Win32 window and Direct3D device for the application DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen // init GUI: GUI.Init(&DialogResourceManager); GUI.SetCallback(OnGUIEvent); manager.ms.setDialogResourceManager(&DialogResourceManager); manager.ms.setGUI(&GUI); manager.ms.initGUI(); //Setup Window DXUTInit( true, false, true ); // Parse the command line, handle the default hotkeys, and show msgboxes DXUTCreateWindow( L"WuermerDX9", hInstance, NULL, NULL, 0, 0 ); DXUTCreateDevice( D3DADAPTER_DEFAULT, (fullscreen==0), // windowed mode - set to false for full screen! width, // width height, // height IsDeviceAcceptable, ModifyDeviceSettings ); // initialising game: manager.setScreenDimension(width, height); manager.initGame(); firstFrame = true; secondFrame = false; // Start the render loop DXUTMainLoop(); // TODO: Perform any application-level cleanup here return DXUTGetExitCode(); }