//-------------------------------------------------------------------------------------- // Create any D3D9 resources that won't live through a device reset (D3DPOOL_DEFAULT) // or that are tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9ResetDevice() ); V_RETURN( g_SettingsDlg.OnD3D9ResetDevice() ); if( g_pFont9 ) V_RETURN( g_pFont9->OnResetDevice() ); if( g_pEffect9 ) V_RETURN( g_pEffect9->OnResetDevice() ); V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pSprite9 ) ); g_pTxtHelper = new CDXUTTextHelper( g_pFont9, g_pSprite9, NULL, NULL, 15 ); // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f ); g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 ); g_HUD.SetSize( 170, 170 ); g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height - 350 ); g_SampleUI.SetSize( 170, 300 ); // load the mesh V_RETURN( g_Mesh.Create( pd3dDevice, L"misc\\ball.sdkmesh" ) ); // Load the texture WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"misc\\seafloor.dds" ) ); V_RETURN( CreateDDSTextureFromFile( pd3dDevice, str, &g_pTexture9 ) ); if( DXUTIsWindowed() ) g_SampleUI.GetButton( IDC_LOAD_TEXTURE )->SetEnabled( true ); else g_SampleUI.GetButton( IDC_LOAD_TEXTURE )->SetEnabled( false ); return S_OK; }
void CALLBACK MouseProc(bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos, void* pUserContext) { if(bLeftButtonDown) { NsCell cell = g_Board.GetCellAtPoint(DXUTGetD3D9Device(), xPos, yPos); g_Board.PerformMove(g_Board.GetActiveColor(), cell.iRow, cell.iColumn); if(g_Board.pRenjuGame->bGameIsFinished) { g_HUD.GetButton(IDC_RESETGAME)->SetText(L"Start"); g_SampleUI.GetRadioButton(IDC_GAMEMODE_HUMAN_HUMAN_RADIO)->SetVisible(true); g_SampleUI.GetRadioButton(IDC_GAMEMODE_AI_IS_BLACK_RADIO)->SetVisible(true); g_SampleUI.GetRadioButton(IDC_GAMEMODE_AI_IS_WHITE_RADIO)->SetVisible(true); } } }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; auto pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_SettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); // Create other render resources here g_States = std::make_unique<CommonStates>( pd3dDevice ); g_Batch = std::make_unique<PrimitiveBatch<VertexPositionColor>>( pd3dImmediateContext ); g_BatchEffect = std::make_unique<BasicEffect>( pd3dDevice ); g_BatchEffect->SetVertexColorEnabled(true); { void const* shaderByteCode; size_t byteCodeLength; g_BatchEffect->GetVertexShaderBytecode( &shaderByteCode, &byteCodeLength ); hr = pd3dDevice->CreateInputLayout( VertexPositionColor::InputElements, VertexPositionColor::InputElementCount, shaderByteCode, byteCodeLength, &g_pBatchInputLayout ); if( FAILED( hr ) ) return hr; } // Setup the camera's view parameters auto pComboBox = g_SampleUI.GetComboBox( IDC_GROUP ); SetViewForGroup( (pComboBox) ? (int)PtrToInt( pComboBox->GetSelectedData() ) : 0 ); g_HUD.GetButton( IDC_TOGGLEWARP )->SetEnabled( true ); return S_OK; }
//-------------------------------------------------------------------------------------- // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED) // and aren't tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_D3DSettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); // Initialize the font V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont9 ) ); // Load the mesh V_RETURN( LoadMesh( pd3dDevice, L"tiny\\tiny.x", &g_pMesh9 ) ); g_SampleUI.GetStatic( IDC_NUM_LIGHTS_STATIC )->SetVisible( true ); g_SampleUI.GetSlider( IDC_NUM_LIGHTS )->SetVisible( true ); g_SampleUI.GetButton( IDC_ACTIVE_LIGHT )->SetVisible( true ); D3DXVECTOR3* pData; D3DXVECTOR3 vCenter; FLOAT fObjectRadius; V( g_pMesh9->LockVertexBuffer( 0, ( LPVOID* )&pData ) ); V( D3DXComputeBoundingSphere( pData, g_pMesh9->GetNumVertices(), D3DXGetFVFVertexSize( g_pMesh9->GetFVF() ), &vCenter, &fObjectRadius ) ); V( g_pMesh9->UnlockVertexBuffer() ); D3DXMatrixTranslation( &g_mCenterMesh, -vCenter.x, -vCenter.y, -vCenter.z ); D3DXMATRIXA16 m; D3DXMatrixRotationY( &m, D3DX_PI ); g_mCenterMesh *= m; D3DXMatrixRotationX( &m, D3DX_PI / 2.0f ); g_mCenterMesh *= m; V_RETURN( CDXUTDirectionWidget::StaticOnD3D9CreateDevice( pd3dDevice ) ); for( int i = 0; i < MAX_LIGHTS; i++ ) g_LightControl[i].SetRadius( fObjectRadius ); // Read the D3DX effect file WCHAR str[MAX_PATH]; DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE | D3DXSHADER_NO_PRESHADER | D3DXFX_LARGEADDRESSAWARE; #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"BasicHLSL.fx" ) ); V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect9, NULL ) ); // Create the mesh texture from a file V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"tiny\\tiny_skin.dds" ) ); V_RETURN( D3DXCreateTextureFromFileEx( pd3dDevice, str, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &g_pMeshTexture9 ) ); // Set effect variables as needed D3DXCOLOR colorMtrlDiffuse( 1.0f, 1.0f, 1.0f, 1.0f ); D3DXCOLOR colorMtrlAmbient( 0.35f, 0.35f, 0.35f, 0 ); D3DXHANDLE hMaterialAmbientColor = g_pEffect9->GetParameterByName( NULL, "g_MaterialAmbientColor" ); D3DXHANDLE hMaterialDiffuseColor = g_pEffect9->GetParameterByName( NULL, "g_MaterialDiffuseColor" ); D3DXHANDLE hMeshTexture = g_pEffect9->GetParameterByName( NULL, "g_MeshTexture" ); V_RETURN( g_pEffect9->SetValue( hMaterialAmbientColor, &colorMtrlAmbient, sizeof( D3DXCOLOR ) ) ); V_RETURN( g_pEffect9->SetValue( hMaterialDiffuseColor, &colorMtrlDiffuse, sizeof( D3DXCOLOR ) ) ); V_RETURN( g_pEffect9->SetTexture( hMeshTexture, g_pMeshTexture9 ) ); g_hLightDir = g_pEffect9->GetParameterByName( NULL, "g_LightDir" ); g_hLightDiffuse = g_pEffect9->GetParameterByName( NULL, "g_LightDiffuse" ); g_hmWorldViewProjection = g_pEffect9->GetParameterByName( NULL, "g_mWorldViewProjection" ); g_hmWorld = g_pEffect9->GetParameterByName( NULL, "g_mWorld" ); g_hMaterialDiffuseColor = g_pEffect9->GetParameterByName( NULL, "g_MaterialDiffuseColor" ); g_hfTime = g_pEffect9->GetParameterByName( NULL, "g_fTime" ); g_hnNumLights = g_pEffect9->GetParameterByName( NULL, "g_nNumLights" ); g_hRenderSceneWithTexture1Light = g_pEffect9->GetTechniqueByName( "RenderSceneWithTexture1Light" ); g_hRenderSceneWithTexture2Light = g_pEffect9->GetTechniqueByName( "RenderSceneWithTexture2Light" ); g_hRenderSceneWithTexture3Light = g_pEffect9->GetTechniqueByName( "RenderSceneWithTexture3Light" ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -15.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); g_Camera.SetRadius( fObjectRadius * 3.0f, fObjectRadius * 0.5f, fObjectRadius * 10.0f ); return S_OK; }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; auto pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_SettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); // Read the HLSL file // You should use the lowest possible shader profile for your shader to enable various feature levels. These // shaders are simple enough to work well within the lowest possible profile, and will run on all feature levels DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _DEBUG // Disable optimizations to further improve shader debugging dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif ID3DBlob* pVertexShaderBuffer = nullptr; V_RETURN( DXUTCompileFromFile( L"SimpleSample.hlsl", nullptr, "RenderSceneVS", "vs_4_0_level_9_1", dwShaderFlags, 0, &pVertexShaderBuffer ) ); ID3DBlob* pPixelShaderBuffer = nullptr; V_RETURN( DXUTCompileFromFile( L"SimpleSample.hlsl", nullptr, "RenderScenePS", "ps_4_0_level_9_1", dwShaderFlags, 0, &pPixelShaderBuffer ) ); // Create the shaders V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), nullptr, &g_pVertexShader11 ) ); DXUT_SetDebugName( g_pVertexShader11, "RenderSceneVS" ); V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), nullptr, &g_pPixelShader11 ) ); DXUT_SetDebugName( g_pPixelShader11, "RenderScenePS" ); // Create a layout for the object data const D3D11_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; V_RETURN( pd3dDevice->CreateInputLayout( layout, ARRAYSIZE( layout ), pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), &g_pLayout11 ) ); DXUT_SetDebugName( g_pLayout11, "Primary" ); // No longer need the shader blobs SAFE_RELEASE( pVertexShaderBuffer ); SAFE_RELEASE( pPixelShaderBuffer ); // Create state objects D3D11_SAMPLER_DESC samDesc = {}; samDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samDesc.AddressU = samDesc.AddressV = samDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; samDesc.MaxAnisotropy = 1; samDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; samDesc.MaxLOD = D3D11_FLOAT32_MAX; V_RETURN( pd3dDevice->CreateSamplerState( &samDesc, &g_pSamLinear ) ); DXUT_SetDebugName( g_pSamLinear, "Linear" ); // Create constant buffers D3D11_BUFFER_DESC cbDesc = {}; cbDesc.Usage = D3D11_USAGE_DYNAMIC; cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; cbDesc.ByteWidth = sizeof( CB_VS_PER_OBJECT ); V_RETURN( pd3dDevice->CreateBuffer( &cbDesc, nullptr, &g_pcbVSPerObject11 ) ); DXUT_SetDebugName( g_pcbVSPerObject11, "CB_VS_PER_OBJECT" ); cbDesc.ByteWidth = sizeof( CB_VS_PER_FRAME ); V_RETURN( pd3dDevice->CreateBuffer( &cbDesc, nullptr, &g_pcbVSPerFrame11 ) ); DXUT_SetDebugName( g_pcbVSPerFrame11, "CB_VS_PER_FRAME" ); // Create other render resources here // Setup the camera's view parameters static const XMVECTORF32 s_vecEye = { 0.0f, 0.0f, -5.0f, 0.f }; g_Camera.SetViewParams( s_vecEye, g_XMZero ); g_HUD.GetButton( IDC_TOGGLEWARP )->SetEnabled( true ); return S_OK; }
void CALLBACK OnGUIEvent(UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext) { switch(nControlID) { case IDC_TOGGLEFULLSCREEN: DXUTToggleFullScreen(); break; case IDC_TOGGLEREF: DXUTToggleREF(); break; case IDC_HIDECONSOLE: NsConsole::Instance()->Toogle(); break; case IDC_RESETGAME: if(!g_Board.pRenjuGame->bGameIsFinished) { g_Board.Finish(); NsConsole::Instance()->AddMessage("The game was finished mannualy"); g_HUD.GetButton(IDC_RESETGAME)->SetText(L"Start"); g_SampleUI.GetRadioButton(IDC_GAMEMODE_HUMAN_HUMAN_RADIO)->SetVisible(true); g_SampleUI.GetRadioButton(IDC_GAMEMODE_AI_IS_BLACK_RADIO)->SetVisible(true); g_SampleUI.GetRadioButton(IDC_GAMEMODE_AI_IS_WHITE_RADIO)->SetVisible(true); } else { g_Board.Start(); g_HUD.GetButton(IDC_RESETGAME)->SetText(L"Finish"); g_SampleUI.GetRadioButton(IDC_GAMEMODE_HUMAN_HUMAN_RADIO)->SetVisible(false); g_SampleUI.GetRadioButton(IDC_GAMEMODE_AI_IS_BLACK_RADIO)->SetVisible(false); g_SampleUI.GetRadioButton(IDC_GAMEMODE_AI_IS_WHITE_RADIO)->SetVisible(false); } break; case IDC_GAMEMODE_HUMAN_HUMAN_RADIO: g_Board.pRenjuGame->SetNsGameMode(NsGameMode::Human_Human); break; case IDC_GAMEMODE_AI_IS_BLACK_RADIO: g_Board.pRenjuGame->SetNsGameMode(NsGameMode::AI_Black); break; case IDC_GAMEMODE_AI_IS_WHITE_RADIO: g_Board.pRenjuGame->SetNsGameMode(NsGameMode::AI_White); break; case IDC_LAUNCH_RENJU_MANUAL: HINSTANCE hiResult = ShellExecute( NULL, L"open", (NsD3DResourceManager::Instance()->GetAbsoluteContentFolderPath() + L"/Other/renju_manual.pdf").c_str(), NULL, NULL, SW_SHOWNORMAL); if((int)hiResult > 32) { NsConsole::Instance()->AddMessage("The manual was shown"); } else { NsConsole::Instance()->AddMessage("Warning! Fail to show manual"); } break; } }