DXUTSettingsDlg.cpp
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:52k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. //--------------------------------------------------------------------------------------
  2. // File: DXUTSettingsDlg.cpp
  3. //
  4. // Dialog for selection of device settings 
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved
  7. //--------------------------------------------------------------------------------------
  8. #include "dxstdafx.h"
  9. #include "DXUTgui.h"
  10. #include "DXUTsettingsDlg.h"
  11. #undef min // use __min instead
  12. #undef max // use __max instead
  13. //--------------------------------------------------------------------------------------
  14. // Internal functions forward declarations
  15. //--------------------------------------------------------------------------------------
  16. WCHAR* DXUTPresentIntervalToString( UINT pi );
  17. WCHAR* DXUTMultisampleTypeToString(D3DMULTISAMPLE_TYPE MultiSampleType);
  18. WCHAR* DXUTD3DDeviceTypeToString(D3DDEVTYPE devType);
  19. WCHAR* DXUTVertexProcessingTypeToString(DWORD vpt);
  20. //--------------------------------------------------------------------------------------
  21. // Global state
  22. //--------------------------------------------------------------------------------------
  23. DXUTDeviceSettings g_DeviceSettings;
  24. CD3DSettingsDlg* DXUTGetSettingsDialog()
  25. {
  26.     // Using an accessor function gives control of the construction order
  27.     static CD3DSettingsDlg dlg;
  28.     return &dlg;
  29. }
  30. //--------------------------------------------------------------------------------------
  31. CD3DSettingsDlg::CD3DSettingsDlg()
  32. {
  33.     m_pStateBlock = NULL;
  34.     m_bActive = false;
  35. }
  36. //--------------------------------------------------------------------------------------
  37. CD3DSettingsDlg::~CD3DSettingsDlg()
  38. {
  39.     OnDestroyDevice();
  40. }
  41. //--------------------------------------------------------------------------------------
  42. void CD3DSettingsDlg::Init( CDXUTDialogResourceManager* pManager )
  43. {
  44.     assert( pManager );
  45.     m_Dialog.Init( pManager, false );  // Don't register this dialog.
  46.     CreateControls();
  47. }
  48. //--------------------------------------------------------------------------------------
  49. void CD3DSettingsDlg::Init( CDXUTDialogResourceManager* pManager, LPCWSTR szControlTextureFileName )
  50. {
  51.     assert( pManager );
  52.     m_Dialog.Init( pManager, false, szControlTextureFileName );  // Don't register this dialog.
  53.     CreateControls();
  54. }
  55. //--------------------------------------------------------------------------------------
  56. void CD3DSettingsDlg::Init( CDXUTDialogResourceManager* pManager, LPCWSTR pszControlTextureResourcename, HMODULE hModule )
  57. {
  58.     assert( pManager );
  59.     m_Dialog.Init( pManager, false, pszControlTextureResourcename, hModule );  // Don't register this dialog.
  60.     CreateControls();
  61. }
  62. //--------------------------------------------------------------------------------------
  63. void CD3DSettingsDlg::CreateControls()
  64. {
  65.     m_Dialog.EnableKeyboardInput( true );
  66.     m_Dialog.SetFont( 0, L"Arial", 15, FW_NORMAL );
  67.     m_Dialog.SetFont( 1, L"Arial", 28, FW_BOLD );
  68.     // Right-justify static controls
  69.     CDXUTElement* pElement = m_Dialog.GetDefaultElement( DXUT_CONTROL_STATIC, 0 );
  70.     if( pElement )
  71.     {
  72.         pElement->dwTextFormat = DT_VCENTER | DT_RIGHT;
  73.         
  74.         // Title
  75.         CDXUTStatic* pStatic = NULL;
  76.         m_Dialog.AddStatic( DXUTSETTINGSDLG_STATIC, L"Direct3D Settings", 10, 5, 400, 50, false, &pStatic );
  77.         pElement = pStatic->GetElement( 0 );
  78.         pElement->iFont = 1;
  79.         pElement->dwTextFormat = DT_TOP | DT_LEFT;
  80.     }
  81.     // DXUTSETTINGSDLG_ADAPTER
  82.     m_Dialog.AddStatic( DXUTSETTINGSDLG_STATIC, L"Display Adapter", 10, 50, 180, 23 );
  83.     m_Dialog.AddComboBox( DXUTSETTINGSDLG_ADAPTER, 200, 50, 300, 23 );
  84.     // DXUTSETTINGSDLG_DEVICE_TYPE
  85.     m_Dialog.AddStatic( DXUTSETTINGSDLG_STATIC, L"Render Device", 10, 75, 180, 23 );
  86.     m_Dialog.AddComboBox( DXUTSETTINGSDLG_DEVICE_TYPE, 200, 75, 300, 23 );
  87.     // DXUTSETTINGSDLG_WINDOWED, DXUTSETTINGSDLG_FULLSCREEN
  88.     m_Dialog.AddRadioButton( DXUTSETTINGSDLG_WINDOWED, DXUTSETTINGSDLG_WINDOWED_GROUP, L"Windowed", 240, 105, 300, 16 );
  89.     m_Dialog.AddCheckBox( DXUTSETTINGSDLG_DEVICECLIP, L"Clip to device when window spans across multiple monitors", 250, 126, 400, 16 );
  90.     m_Dialog.AddRadioButton( DXUTSETTINGSDLG_FULLSCREEN, DXUTSETTINGSDLG_WINDOWED_GROUP, L"Full Screen", 240, 147, 300, 16 );
  91.     // DXUTSETTINGSDLG_ADAPTER_FORMAT
  92.     m_Dialog.AddStatic( DXUTSETTINGSDLG_ADAPTER_FORMAT_LABEL, L"Adapter Format", 10, 180, 180, 23 );
  93.     m_Dialog.AddComboBox( DXUTSETTINGSDLG_ADAPTER_FORMAT, 200, 180, 300, 23 );
  94.     // DXUTSETTINGSDLG_RESOLUTION
  95.     m_Dialog.AddStatic( DXUTSETTINGSDLG_RESOLUTION_LABEL, L"Resolution", 10, 205, 180, 23 );
  96.     m_Dialog.AddComboBox( DXUTSETTINGSDLG_RESOLUTION, 200, 205, 200, 23 );
  97.     m_Dialog.GetComboBox( DXUTSETTINGSDLG_RESOLUTION )->SetDropHeight( 106 );
  98.     // DXUTSETTINGSDLG_RES_SHOW_ALL
  99.     m_Dialog.AddCheckBox( DXUTSETTINGSDLG_RESOLUTION_SHOW_ALL, L"Show All Aspect Ratios", 420, 205, 200, 23, false );
  100.     // DXUTSETTINGSDLG_REFRESH_RATE
  101.     m_Dialog.AddStatic( DXUTSETTINGSDLG_REFRESH_RATE_LABEL, L"Refresh Rate", 10, 230, 180, 23 );
  102.     m_Dialog.AddComboBox( DXUTSETTINGSDLG_REFRESH_RATE, 200, 230, 300, 23 );
  103.     // DXUTSETTINGSDLG_BACK_BUFFER_FORMAT
  104.     m_Dialog.AddStatic( DXUTSETTINGSDLG_STATIC, L"Back Buffer Format", 10, 265, 180, 23 );
  105.     m_Dialog.AddComboBox( DXUTSETTINGSDLG_BACK_BUFFER_FORMAT, 200, 265, 300, 23 );
  106.     // DXUTSETTINGSDLG_DEPTH_STENCIL
  107.     m_Dialog.AddStatic( DXUTSETTINGSDLG_STATIC, L"Depth/Stencil Format", 10, 290, 180, 23 );
  108.     m_Dialog.AddComboBox( DXUTSETTINGSDLG_DEPTH_STENCIL, 200, 290, 300, 23 );
  109.     // DXUTSETTINGSDLG_MULTISAMPLE_TYPE
  110.     m_Dialog.AddStatic( DXUTSETTINGSDLG_STATIC, L"Multisample Type", 10, 315, 180, 23 );
  111.     m_Dialog.AddComboBox( DXUTSETTINGSDLG_MULTISAMPLE_TYPE, 200, 315, 300, 23 );
  112.     // DXUTSETTINGSDLG_MULTISAMPLE_QUALITY
  113.     m_Dialog.AddStatic( DXUTSETTINGSDLG_STATIC, L"Multisample Quality", 10, 340, 180, 23 );
  114.     m_Dialog.AddComboBox( DXUTSETTINGSDLG_MULTISAMPLE_QUALITY, 200, 340, 300, 23 );
  115.      // DXUTSETTINGSDLG_VERTEX_PROCESSING
  116.     m_Dialog.AddStatic( DXUTSETTINGSDLG_STATIC, L"Vertex Processing", 10, 365, 180, 23 );
  117.     m_Dialog.AddComboBox( DXUTSETTINGSDLG_VERTEX_PROCESSING, 200, 365, 300, 23 );
  118.      // DXUTSETTINGSDLG_PRESENT_INTERVAL
  119.     m_Dialog.AddStatic( DXUTSETTINGSDLG_STATIC, L"Vertical Sync", 10, 390, 180, 23 );
  120.     m_Dialog.AddComboBox( DXUTSETTINGSDLG_PRESENT_INTERVAL, 200, 390, 300, 23 );
  121.     
  122.     // DXUTSETTINGSDLG_OK, DXUTSETTINGSDLG_CANCEL
  123.     m_Dialog.AddButton( DXUTSETTINGSDLG_OK, L"OK", 230, 435, 73, 31 );
  124.     m_Dialog.AddButton( DXUTSETTINGSDLG_CANCEL, L"Cancel", 315, 435, 73, 31, 0, true );
  125. }
  126. //--------------------------------------------------------------------------------------
  127. HRESULT CD3DSettingsDlg::OnCreateDevice( IDirect3DDevice9* pd3dDevice )
  128. {
  129.     if( pd3dDevice == NULL )
  130.         return DXUT_ERR_MSGBOX( L"CD3DSettingsDlg::OnCreatedDevice", E_INVALIDARG );
  131.     // Create the fonts/textures 
  132.     m_Dialog.SetCallback( StaticOnEvent, (void*) this );
  133.   
  134.     return S_OK;
  135. }
  136. //--------------------------------------------------------------------------------------
  137. // Changes the UI defaults to the current device settings
  138. //--------------------------------------------------------------------------------------
  139. HRESULT CD3DSettingsDlg::Refresh()
  140. {
  141.     HRESULT hr = S_OK;
  142.     CD3DEnumeration* pD3DEnum = DXUTGetEnumeration();
  143.     g_DeviceSettings = DXUTGetDeviceSettings();
  144.     // Fill the UI with the current settings
  145.     AddDeviceType( g_DeviceSettings.DeviceType );
  146.     SetWindowed( FALSE != g_DeviceSettings.pp.Windowed );
  147.     SetDeviceClip( 0 != (g_DeviceSettings.pp.Flags & D3DPRESENTFLAG_DEVICECLIP) );
  148.     AddAdapterFormat( g_DeviceSettings.AdapterFormat );
  149.     AddResolution( g_DeviceSettings.pp.BackBufferWidth, g_DeviceSettings.pp.BackBufferHeight );
  150.     AddRefreshRate( g_DeviceSettings.pp.FullScreen_RefreshRateInHz );
  151.     AddBackBufferFormat( g_DeviceSettings.pp.BackBufferFormat );
  152.     AddDepthStencilBufferFormat( g_DeviceSettings.pp.AutoDepthStencilFormat );
  153.     AddMultisampleType( g_DeviceSettings.pp.MultiSampleType );
  154.     AddMultisampleQuality( g_DeviceSettings.pp.MultiSampleQuality );
  155.     
  156.     if( g_DeviceSettings.BehaviorFlags & D3DCREATE_PUREDEVICE )
  157.         AddVertexProcessingType( D3DCREATE_PUREDEVICE );
  158.     else if( g_DeviceSettings.BehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING )
  159.         AddVertexProcessingType( D3DCREATE_HARDWARE_VERTEXPROCESSING );
  160.     else if( g_DeviceSettings.BehaviorFlags & D3DCREATE_SOFTWARE_VERTEXPROCESSING )
  161.         AddVertexProcessingType( D3DCREATE_SOFTWARE_VERTEXPROCESSING );
  162.     else if( g_DeviceSettings.BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING )
  163.         AddVertexProcessingType( D3DCREATE_MIXED_VERTEXPROCESSING );
  164.     CD3DEnumDeviceSettingsCombo* pBestDeviceSettingsCombo = pD3DEnum->GetDeviceSettingsCombo( g_DeviceSettings.AdapterOrdinal, g_DeviceSettings.DeviceType, g_DeviceSettings.AdapterFormat, g_DeviceSettings.pp.BackBufferFormat, (g_DeviceSettings.pp.Windowed != 0) );
  165.     if( NULL == pBestDeviceSettingsCombo )
  166.         return DXUT_ERR_MSGBOX( L"GetDeviceSettingsCombo", E_INVALIDARG );    
  167.     // Get the adapters list from CD3DEnumeration object
  168.     CGrowableArray<CD3DEnumAdapterInfo*>* pAdapterInfoList = pD3DEnum->GetAdapterInfoList();
  169.     if( pAdapterInfoList->GetSize() == 0 )
  170.         return DXUT_ERR_MSGBOX( L"CD3DSettingsDlg::OnCreatedDevice", DXUTERR_NOCOMPATIBLEDEVICES );
  171.     
  172.     CDXUTComboBox* pAdapterCombo = m_Dialog.GetComboBox( DXUTSETTINGSDLG_ADAPTER );
  173.     pAdapterCombo->RemoveAllItems();
  174.     // Add adapters
  175.     for( int iAdapter=0; iAdapter<pAdapterInfoList->GetSize(); iAdapter++ )
  176.     {          
  177.         CD3DEnumAdapterInfo* pAdapterInfo = pAdapterInfoList->GetAt(iAdapter);
  178.         AddAdapter( pAdapterInfo->szUniqueDescription, pAdapterInfo->AdapterOrdinal );
  179.     }
  180.     
  181.     pAdapterCombo->SetSelectedByData( ULongToPtr( g_DeviceSettings.AdapterOrdinal ) );
  182.     hr = OnAdapterChanged();
  183.     if( FAILED(hr) )
  184.         return hr;
  185.     //m_Dialog.Refresh();
  186.     CDXUTDialog::SetRefreshTime( (float) DXUTGetTime() );
  187.     return S_OK;
  188. }
  189. //--------------------------------------------------------------------------------------
  190. HRESULT CD3DSettingsDlg::OnResetDevice()
  191. {
  192.     const D3DSURFACE_DESC* pDesc = DXUTGetBackBufferSurfaceDesc();
  193.     m_Dialog.SetLocation( 0, 0 );
  194.     m_Dialog.SetSize( pDesc->Width, pDesc->Height );
  195.     m_Dialog.SetBackgroundColors( D3DCOLOR_ARGB(255, 98, 138, 206), 
  196.                                          D3DCOLOR_ARGB(255, 54, 105, 192),
  197.                                          D3DCOLOR_ARGB(255, 54, 105, 192),
  198.                                          D3DCOLOR_ARGB(255, 10,  73, 179) );
  199.     
  200.     
  201.     IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
  202.     pd3dDevice->BeginStateBlock();
  203.     pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID ); 
  204.     pd3dDevice->EndStateBlock( &m_pStateBlock );
  205.     return S_OK;
  206. }
  207. //--------------------------------------------------------------------------------------
  208. HRESULT CD3DSettingsDlg::OnRender( float fElapsedTime )
  209. {
  210.     IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
  211.     // Clear the render target and the zbuffer 
  212.     pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0x00003F3F, 1.0f, 0);
  213.     // Render the scene
  214.     if( SUCCEEDED( pd3dDevice->BeginScene() ) )
  215.     {
  216.         m_pStateBlock->Capture();
  217.         pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID ); 
  218.         m_Dialog.OnRender( fElapsedTime );    
  219.         m_pStateBlock->Apply();
  220.         pd3dDevice->EndScene();
  221.     }
  222.     
  223.     return S_OK;
  224. }
  225. //--------------------------------------------------------------------------------------
  226. LRESULT CD3DSettingsDlg::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  227. {
  228.     m_Dialog.MsgProc( hWnd, uMsg, wParam, lParam );
  229.     if( uMsg == WM_KEYDOWN && wParam == VK_F2 )
  230.         SetActive( false );
  231.     return 0;
  232. }
  233. //--------------------------------------------------------------------------------------
  234. HRESULT CD3DSettingsDlg::OnLostDevice()
  235. {
  236.     SAFE_RELEASE( m_pStateBlock );
  237.     return S_OK;
  238. }
  239. //--------------------------------------------------------------------------------------
  240. HRESULT CD3DSettingsDlg::OnDestroyDevice()
  241. {
  242.     return S_OK;
  243. }
  244. //--------------------------------------------------------------------------------------
  245. void WINAPI CD3DSettingsDlg::StaticOnEvent( UINT nEvent, int nControlID, 
  246.                                             CDXUTControl* pControl, void* pUserData )
  247. {
  248.     CD3DSettingsDlg* pD3DSettings = (CD3DSettingsDlg*) pUserData;
  249.     if( pD3DSettings )
  250.         pD3DSettings->OnEvent( nEvent, nControlID, pControl );
  251. }
  252. //--------------------------------------------------------------------------------------
  253. void CD3DSettingsDlg::OnEvent( UINT nEvent, int nControlID, 
  254.                                CDXUTControl* pControl )
  255. {
  256.     switch( nControlID )
  257.     {
  258.         case DXUTSETTINGSDLG_ADAPTER:               OnAdapterChanged(); break;
  259.         case DXUTSETTINGSDLG_DEVICE_TYPE:           OnDeviceTypeChanged(); break;
  260.         case DXUTSETTINGSDLG_WINDOWED:              OnWindowedFullScreenChanged(); break;
  261.         case DXUTSETTINGSDLG_FULLSCREEN:            OnWindowedFullScreenChanged(); break;
  262.         case DXUTSETTINGSDLG_ADAPTER_FORMAT:        OnAdapterFormatChanged(); break;
  263.         case DXUTSETTINGSDLG_RESOLUTION_SHOW_ALL:   OnAdapterFormatChanged(); break;
  264.         case DXUTSETTINGSDLG_RESOLUTION:            OnResolutionChanged(); break;
  265.         case DXUTSETTINGSDLG_REFRESH_RATE:          OnRefreshRateChanged(); break;
  266.         case DXUTSETTINGSDLG_BACK_BUFFER_FORMAT:    OnBackBufferFormatChanged(); break;
  267.         case DXUTSETTINGSDLG_DEPTH_STENCIL:         OnDepthStencilBufferFormatChanged(); break;
  268.         case DXUTSETTINGSDLG_MULTISAMPLE_TYPE:      OnMultisampleTypeChanged(); break;
  269.         case DXUTSETTINGSDLG_MULTISAMPLE_QUALITY:   OnMultisampleQualityChanged(); break;
  270.         case DXUTSETTINGSDLG_VERTEX_PROCESSING:     OnVertexProcessingChanged(); break;
  271.         case DXUTSETTINGSDLG_PRESENT_INTERVAL:      OnPresentIntervalChanged(); break;
  272.         case DXUTSETTINGSDLG_DEVICECLIP:            OnDeviceClipChanged(); break;
  273.         case DXUTSETTINGSDLG_OK:
  274.         {
  275.             if( g_DeviceSettings.pp.Windowed )
  276.             {
  277.                 g_DeviceSettings.pp.FullScreen_RefreshRateInHz = 0;
  278.                 RECT rcClient;
  279.                 if( DXUTIsWindowed() )
  280.                     GetClientRect( DXUTGetHWND(), &rcClient );
  281.                 else
  282.                     rcClient = DXUTGetWindowClientRectAtModeChange();
  283.                 DWORD dwWindowWidth  = rcClient.right - rcClient.left;
  284.                 DWORD dwWindowHeight = rcClient.bottom - rcClient.top;
  285.                 g_DeviceSettings.pp.BackBufferWidth = dwWindowWidth;
  286.                 g_DeviceSettings.pp.BackBufferHeight = dwWindowHeight;
  287.             }
  288.             if( g_DeviceSettings.pp.MultiSampleType != D3DMULTISAMPLE_NONE )
  289.             {
  290.                 g_DeviceSettings.pp.Flags &= ~D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  291.             }
  292.             DXUTCreateDeviceFromSettings( &g_DeviceSettings );
  293.             SetActive( false );
  294.             break;
  295.         }
  296.         case DXUTSETTINGSDLG_CANCEL:                
  297.         {
  298.             SetActive( false );
  299.             break;
  300.         }
  301.     }
  302. }
  303. //-------------------------------------------------------------------------------------
  304. HRESULT CD3DSettingsDlg::SetDeviceSettingsFromUI()
  305. {
  306.     CDXUTComboBox* pComboBox;
  307.     CDXUTRadioButton* pRadioButton;
  308.     // DXUTSETTINGSDLG_DEVICE_TYPE
  309.     pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_DEVICE_TYPE );
  310.     g_DeviceSettings.DeviceType = (D3DDEVTYPE) PtrToUlong( pComboBox->GetSelectedData() );
  311.     
  312.     // DXUTSETTINGSDLG_WINDOWED
  313.     pRadioButton = m_Dialog.GetRadioButton( DXUTSETTINGSDLG_WINDOWED );
  314.     g_DeviceSettings.pp.Windowed = pRadioButton->GetChecked();
  315.     // DXUTSETTINGSDLG_ADAPTER_FORMAT
  316.     pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_ADAPTER_FORMAT );
  317.     g_DeviceSettings.AdapterFormat = (D3DFORMAT) PtrToUlong( pComboBox->GetSelectedData() );
  318.     
  319.     if( g_DeviceSettings.pp.Windowed )
  320.     {
  321.         g_DeviceSettings.pp.BackBufferFormat = D3DFMT_UNKNOWN;
  322.         g_DeviceSettings.pp.FullScreen_RefreshRateInHz = 0;
  323.     }
  324.     else
  325.     {
  326.         // DXUTSETTINGSDLG_BACK_BUFFER_FORMAT
  327.         pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_BACK_BUFFER_FORMAT );
  328.         g_DeviceSettings.pp.BackBufferFormat = (D3DFORMAT) PtrToUlong( pComboBox->GetSelectedData() );
  329.     
  330.         // DXUTSETTINGSDLG_RESOLUTION
  331.         pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_RESOLUTION );
  332.         DWORD dwResolution = PtrToUlong( pComboBox->GetSelectedData() );
  333.         g_DeviceSettings.pp.BackBufferWidth = HIWORD( dwResolution );
  334.         g_DeviceSettings.pp.BackBufferHeight = LOWORD( dwResolution );
  335.         
  336.         // DXUTSETTINGSDLG_REFRESH_RATE
  337.         pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_REFRESH_RATE );
  338.         g_DeviceSettings.pp.FullScreen_RefreshRateInHz = PtrToUlong( pComboBox->GetSelectedData() );
  339.     }
  340.     // DXUTSETTINGSDLG_DEPTH_STENCIL
  341.     pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_DEPTH_STENCIL );
  342.     g_DeviceSettings.pp.AutoDepthStencilFormat = (D3DFORMAT) PtrToUlong( pComboBox->GetSelectedData() );
  343.     
  344.     return S_OK;
  345. }
  346. //-------------------------------------------------------------------------------------
  347. CD3DEnumAdapterInfo* CD3DSettingsDlg::GetCurrentAdapterInfo()
  348. {
  349.     CD3DEnumeration* pD3DEnum = DXUTGetEnumeration();
  350.     return pD3DEnum->GetAdapterInfo( g_DeviceSettings.AdapterOrdinal );
  351. }
  352. //-------------------------------------------------------------------------------------
  353. CD3DEnumDeviceInfo* CD3DSettingsDlg::GetCurrentDeviceInfo()
  354. {
  355.     CD3DEnumeration* pD3DEnum = DXUTGetEnumeration();
  356.     return pD3DEnum->GetDeviceInfo( g_DeviceSettings.AdapterOrdinal,
  357.                                       g_DeviceSettings.DeviceType );
  358. }
  359. //-------------------------------------------------------------------------------------
  360. CD3DEnumDeviceSettingsCombo* CD3DSettingsDlg::GetCurrentDeviceSettingsCombo()
  361. {
  362.     CD3DEnumeration* pD3DEnum = DXUTGetEnumeration();
  363.     return pD3DEnum->GetDeviceSettingsCombo( g_DeviceSettings.AdapterOrdinal,
  364.                                              g_DeviceSettings.DeviceType,
  365.                                              g_DeviceSettings.AdapterFormat,
  366.                                              g_DeviceSettings.pp.BackBufferFormat,
  367.                                              (g_DeviceSettings.pp.Windowed == TRUE) );
  368. }
  369. //-------------------------------------------------------------------------------------
  370. HRESULT CD3DSettingsDlg::OnAdapterChanged()
  371. {
  372.     HRESULT hr = S_OK;
  373.     // Store the adapter index
  374.     g_DeviceSettings.AdapterOrdinal = GetSelectedAdapter();
  375.     
  376.     // DXUTSETTINGSDLG_DEVICE_TYPE
  377.     CDXUTComboBox* pDeviceTypeComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_DEVICE_TYPE );
  378.     pDeviceTypeComboBox->RemoveAllItems();
  379.  
  380.     CD3DEnumAdapterInfo* pAdapterInfo = GetCurrentAdapterInfo();
  381.     if( pAdapterInfo == NULL )
  382.         return E_FAIL;
  383.     for( int iDeviceInfo=0; iDeviceInfo < pAdapterInfo->deviceInfoList.GetSize(); iDeviceInfo++ )
  384.     {
  385.         CD3DEnumDeviceInfo* pDeviceInfo = pAdapterInfo->deviceInfoList.GetAt(iDeviceInfo);
  386.         AddDeviceType( pDeviceInfo->DeviceType );
  387.     }
  388.     pDeviceTypeComboBox->SetSelectedByData( ULongToPtr(g_DeviceSettings.DeviceType) );
  389.     hr = OnDeviceTypeChanged();
  390.     if( FAILED(hr) )
  391.         return hr;
  392.     return S_OK; 
  393. }
  394. //-------------------------------------------------------------------------------------
  395. HRESULT CD3DSettingsDlg::OnDeviceTypeChanged()
  396. {
  397.     HRESULT hr = S_OK;
  398.     
  399.     g_DeviceSettings.DeviceType = GetSelectedDeviceType();
  400.    
  401.     // Update windowed/full screen radio buttons
  402.     bool bHasWindowedDeviceCombo = false;
  403.     bool bHasFullScreenDeviceCombo = false;
  404.     CD3DEnumDeviceInfo* pDeviceInfo = GetCurrentDeviceInfo();
  405.     if( pDeviceInfo == NULL )
  406.         return E_FAIL;
  407.             
  408.     for( int idc = 0; idc < pDeviceInfo->deviceSettingsComboList.GetSize(); idc++ )
  409.     {
  410.         CD3DEnumDeviceSettingsCombo* pDeviceSettingsCombo = pDeviceInfo->deviceSettingsComboList.GetAt( idc );
  411.         if( pDeviceSettingsCombo->Windowed )
  412.             bHasWindowedDeviceCombo = true;
  413.         else
  414.             bHasFullScreenDeviceCombo = true;
  415.     }
  416.     // DXUTSETTINGSDLG_WINDOWED, DXUTSETTINGSDLG_FULLSCREEN
  417.     m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_WINDOWED, bHasWindowedDeviceCombo );
  418.     m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_FULLSCREEN, bHasFullScreenDeviceCombo );
  419.     SetWindowed( g_DeviceSettings.pp.Windowed && bHasWindowedDeviceCombo );
  420.     hr = OnWindowedFullScreenChanged();
  421.     if( FAILED(hr) )
  422.         return hr;
  423.     return S_OK;
  424. }
  425. //-------------------------------------------------------------------------------------
  426. HRESULT CD3DSettingsDlg::OnWindowedFullScreenChanged()
  427. {
  428.     HRESULT hr = S_OK;
  429.     bool bWindowed = IsWindowed();
  430.     g_DeviceSettings.pp.Windowed = bWindowed;
  431.     m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_ADAPTER_FORMAT_LABEL, !bWindowed );
  432.     m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_RESOLUTION_LABEL, !bWindowed );
  433.     m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_REFRESH_RATE_LABEL, !bWindowed );
  434.     m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_ADAPTER_FORMAT, !bWindowed );
  435.     m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_RESOLUTION, !bWindowed );
  436.     m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_RESOLUTION_SHOW_ALL, !bWindowed );
  437.     m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_REFRESH_RATE, !bWindowed );
  438.     m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_DEVICECLIP, bWindowed );
  439.     bool bDeviceClip = ( 0x0 != (g_DeviceSettings.pp.Flags & D3DPRESENTFLAG_DEVICECLIP) );
  440.     // If windowed, get the appropriate adapter format from Direct3D
  441.     if( g_DeviceSettings.pp.Windowed )
  442.     {
  443.         IDirect3D9* pD3D = DXUTGetD3DObject();
  444.         if( pD3D == NULL )
  445.             return DXTRACE_ERR( L"DXUTGetD3DObject", E_FAIL );
  446.         D3DDISPLAYMODE mode;
  447.         hr = pD3D->GetAdapterDisplayMode( g_DeviceSettings.AdapterOrdinal, &mode );
  448.         if( FAILED(hr) )
  449.             return DXTRACE_ERR( L"GetAdapterDisplayMode", hr );
  450.         // Default resolution to the fullscreen res that was last used
  451.         RECT rc = DXUTGetFullsceenClientRectAtModeChange();
  452.         if( rc.right == 0 || rc.bottom == 0 )
  453.         {
  454.             // If nothing last used, then default to the adapter desktop res
  455.             g_DeviceSettings.pp.BackBufferWidth = mode.Width;
  456.             g_DeviceSettings.pp.BackBufferHeight = mode.Height;
  457.         }
  458.         else
  459.         {
  460.             g_DeviceSettings.pp.BackBufferWidth = rc.right;
  461.             g_DeviceSettings.pp.BackBufferHeight = rc.bottom;
  462.         }
  463.         g_DeviceSettings.AdapterFormat = mode.Format;
  464.         g_DeviceSettings.pp.FullScreen_RefreshRateInHz = mode.RefreshRate;
  465.     }
  466.     const D3DFORMAT adapterFormat = g_DeviceSettings.AdapterFormat;
  467.     const DWORD dwWidth = g_DeviceSettings.pp.BackBufferWidth;
  468.     const DWORD dwHeight = g_DeviceSettings.pp.BackBufferHeight;
  469.     const DWORD dwRefreshRate = g_DeviceSettings.pp.FullScreen_RefreshRateInHz;
  470.     // DXUTSETTINGSDLG_DEVICECLIP
  471.     SetDeviceClip( bDeviceClip );
  472.     
  473.     // DXUTSETTINGSDLG_ADAPTER_FORMAT
  474.     CDXUTComboBox* pAdapterFormatComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_ADAPTER_FORMAT );
  475.     if( pAdapterFormatComboBox == NULL )
  476.         return E_FAIL;
  477.     pAdapterFormatComboBox->RemoveAllItems();
  478.     CD3DEnumDeviceInfo* pDeviceInfo = GetCurrentDeviceInfo();
  479.     if( pDeviceInfo == NULL )
  480.         return E_FAIL;
  481.     if( bWindowed )
  482.     {
  483.         AddAdapterFormat( adapterFormat );
  484.     }
  485.     else
  486.     {
  487.         for( int iSettingsCombo=0; iSettingsCombo < pDeviceInfo->deviceSettingsComboList.GetSize(); iSettingsCombo++ )
  488.         {
  489.             CD3DEnumDeviceSettingsCombo* pSettingsCombo = pDeviceInfo->deviceSettingsComboList.GetAt(iSettingsCombo);
  490.             AddAdapterFormat( pSettingsCombo->AdapterFormat );
  491.         }    
  492.     }
  493.     pAdapterFormatComboBox->SetSelectedByData( ULongToPtr(adapterFormat) );
  494.     hr = OnAdapterFormatChanged();
  495.     if( FAILED(hr) )
  496.         return hr;
  497.     // DXUTSETTINGSDLG_RESOLUTION
  498.     CDXUTComboBox* pResolutionComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_RESOLUTION );
  499.     
  500.     if( bWindowed )
  501.     {
  502.         pResolutionComboBox->RemoveAllItems();
  503.         AddResolution( dwWidth, dwHeight );
  504.     }
  505.     pResolutionComboBox->SetSelectedByData( ULongToPtr( MAKELONG(dwWidth, dwHeight) ) );
  506.     
  507.     hr = OnResolutionChanged();
  508.     if( FAILED(hr) )
  509.         return hr;
  510.     // DXUTSETTINGSDLG_REFRESH_RATE
  511.     CDXUTComboBox* pRefreshRateComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_REFRESH_RATE );
  512.     
  513.     if( bWindowed )
  514.     {
  515.         pRefreshRateComboBox->RemoveAllItems();
  516.         AddRefreshRate( dwRefreshRate );
  517.     }
  518.     
  519.     pRefreshRateComboBox->SetSelectedByData( ULongToPtr(dwRefreshRate) ); 
  520.     hr = OnRefreshRateChanged();
  521.     if( FAILED(hr) )
  522.         return hr;
  523.     return S_OK;
  524. }
  525. //-------------------------------------------------------------------------------------
  526. HRESULT CD3DSettingsDlg::OnAdapterFormatChanged()
  527.     HRESULT hr = S_OK;
  528.     // DXUTSETTINGSDLG_ADAPTER_FORMAT
  529.     D3DFORMAT adapterFormat = GetSelectedAdapterFormat();
  530.     g_DeviceSettings.AdapterFormat = adapterFormat;
  531.     // DXUTSETTINGSDLG_RESOLUTION
  532.     CDXUTComboBox* pResolutionComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_RESOLUTION );
  533.     pResolutionComboBox->RemoveAllItems();
  534.     CD3DEnumAdapterInfo* pAdapterInfo = GetCurrentAdapterInfo();
  535.     if( pAdapterInfo == NULL )
  536.         return E_FAIL;
  537.     bool bShowAll = m_Dialog.GetCheckBox( DXUTSETTINGSDLG_RESOLUTION_SHOW_ALL )->GetChecked();
  538.     // Get the desktop aspect ratio
  539.     D3DDISPLAYMODE dmDesktop;
  540.     DXUTGetDesktopResolution( g_DeviceSettings.AdapterOrdinal, &dmDesktop.Width, &dmDesktop.Height );
  541.     float fDesktopAspectRatio = dmDesktop.Width / (float)dmDesktop.Height;
  542.     for( int idm = 0; idm < pAdapterInfo->displayModeList.GetSize(); idm++ )
  543.     {
  544.         D3DDISPLAYMODE DisplayMode = pAdapterInfo->displayModeList.GetAt( idm );
  545.         float fAspect = (float)DisplayMode.Width / (float)DisplayMode.Height;
  546.         if( DisplayMode.Format == adapterFormat )
  547.         {
  548.             // If "Show All" is not checked, then hide all resolutions
  549.             // that don't match the aspect ratio of the desktop resolution
  550.             if( bShowAll || (!bShowAll && fabsf(fDesktopAspectRatio - fAspect) < 0.05f) )
  551.             {
  552.                 AddResolution( DisplayMode.Width, DisplayMode.Height );    
  553.             }
  554.         }
  555.     }
  556.     const DWORD dwCurResolution = MAKELONG( g_DeviceSettings.pp.BackBufferWidth, 
  557.                                             g_DeviceSettings.pp.BackBufferHeight );
  558.     pResolutionComboBox->SetSelectedByData( ULongToPtr(dwCurResolution) );
  559.     hr = OnResolutionChanged();
  560.     if( FAILED(hr) )
  561.         return hr;
  562.     // DXUTSETTINGSDLG_BACK_BUFFER_FORMAT
  563.     CDXUTComboBox* pBackBufferFormatComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_BACK_BUFFER_FORMAT );
  564.     pBackBufferFormatComboBox->RemoveAllItems();
  565.     CD3DEnumDeviceInfo* pDeviceInfo = GetCurrentDeviceInfo();
  566.     if( pDeviceInfo == NULL )
  567.         return E_FAIL;
  568.     const BOOL bWindowed = IsWindowed();
  569.     bool bHasWindowedBackBuffer = false;
  570.     for( int idc = 0; idc < pDeviceInfo->deviceSettingsComboList.GetSize(); idc++ )
  571.     {
  572.         CD3DEnumDeviceSettingsCombo* pDeviceCombo = pDeviceInfo->deviceSettingsComboList.GetAt( idc );
  573.         if( pDeviceCombo->Windowed == bWindowed &&
  574.             pDeviceCombo->AdapterFormat == g_DeviceSettings.AdapterFormat )
  575.         {
  576.             AddBackBufferFormat( pDeviceCombo->BackBufferFormat );
  577.             bHasWindowedBackBuffer = true;
  578.         }
  579.     }
  580.     pBackBufferFormatComboBox->SetSelectedByData( ULongToPtr(g_DeviceSettings.pp.BackBufferFormat) );
  581.     hr = OnBackBufferFormatChanged();
  582.     if( FAILED(hr) )
  583.         return hr;
  584.     if( !bHasWindowedBackBuffer )
  585.     {
  586.         m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_WINDOWED, false );
  587.         if( g_DeviceSettings.pp.Windowed )
  588.         {
  589.             SetWindowed( false );
  590.             hr = OnWindowedFullScreenChanged();
  591.             if( FAILED(hr) )
  592.                 return hr;
  593.         }
  594.     }
  595.     return S_OK;
  596. }
  597. //-------------------------------------------------------------------------------------
  598. HRESULT CD3DSettingsDlg::OnResolutionChanged()
  599. {
  600.     HRESULT hr = S_OK;
  601.     CD3DEnumAdapterInfo* pAdapterInfo = GetCurrentAdapterInfo();
  602.     if( pAdapterInfo == NULL )
  603.         return E_FAIL;
  604.     // Set resolution
  605.     DWORD dwWidth, dwHeight;
  606.     GetSelectedResolution( &dwWidth, &dwHeight );
  607.     g_DeviceSettings.pp.BackBufferWidth = dwWidth;
  608.     g_DeviceSettings.pp.BackBufferHeight = dwHeight;
  609.     DWORD dwRefreshRate = g_DeviceSettings.pp.FullScreen_RefreshRateInHz;
  610.     // Update the refresh rate list
  611.     CDXUTComboBox* pRefreshRateComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_REFRESH_RATE );
  612.     pRefreshRateComboBox->RemoveAllItems();
  613.     D3DFORMAT adapterFormat = g_DeviceSettings.AdapterFormat;
  614.     for( int idm = 0; idm < pAdapterInfo->displayModeList.GetSize(); idm++ )
  615.     {
  616.         D3DDISPLAYMODE displayMode = pAdapterInfo->displayModeList.GetAt( idm );
  617.         if( displayMode.Format == adapterFormat &&
  618.             displayMode.Width == dwWidth &&
  619.             displayMode.Height == dwHeight )
  620.         {
  621.             AddRefreshRate( displayMode.RefreshRate );
  622.         }
  623.     }
  624.     pRefreshRateComboBox->SetSelectedByData( ULongToPtr(dwRefreshRate) );
  625.     hr = OnRefreshRateChanged();
  626.     if( FAILED(hr) )
  627.         return hr;
  628.     return S_OK;
  629. }
  630. //-------------------------------------------------------------------------------------
  631. HRESULT CD3DSettingsDlg::OnRefreshRateChanged()
  632. {
  633.     // Set refresh rate
  634.     g_DeviceSettings.pp.FullScreen_RefreshRateInHz = GetSelectedRefreshRate();
  635.     return S_OK;
  636. }
  637. //-------------------------------------------------------------------------------------
  638. HRESULT CD3DSettingsDlg::OnBackBufferFormatChanged()
  639. {
  640.     HRESULT hr = S_OK;
  641.     g_DeviceSettings.pp.BackBufferFormat = GetSelectedBackBufferFormat();
  642.     
  643.     D3DFORMAT adapterFormat = g_DeviceSettings.AdapterFormat;
  644.     D3DFORMAT backBufferFormat = g_DeviceSettings.pp.BackBufferFormat;
  645.     CD3DEnumDeviceInfo* pDeviceInfo = GetCurrentDeviceInfo();
  646.     if( pDeviceInfo == NULL )
  647.         return E_FAIL;
  648.     bool bAllowSoftwareVP, bAllowHardwareVP, bAllowPureHardwareVP, bAllowMixedVP;
  649.     DXUTGetEnumeration()->GetPossibleVertexProcessingList( &bAllowSoftwareVP, &bAllowHardwareVP, 
  650.                                                            &bAllowPureHardwareVP, &bAllowMixedVP );
  651.     
  652.     for( int idc=0; idc < pDeviceInfo->deviceSettingsComboList.GetSize(); idc++ )
  653.     {
  654.         CD3DEnumDeviceSettingsCombo* pDeviceCombo = pDeviceInfo->deviceSettingsComboList.GetAt( idc );
  655.         if( pDeviceCombo->Windowed == (g_DeviceSettings.pp.Windowed == TRUE) &&
  656.             pDeviceCombo->AdapterFormat == adapterFormat &&
  657.             pDeviceCombo->BackBufferFormat == backBufferFormat )
  658.         {
  659.             CDXUTComboBox* pDepthStencilComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_DEPTH_STENCIL );
  660.             pDepthStencilComboBox->RemoveAllItems();
  661.             pDepthStencilComboBox->SetEnabled( (g_DeviceSettings.pp.EnableAutoDepthStencil == TRUE) ); 
  662.             if( g_DeviceSettings.pp.EnableAutoDepthStencil )
  663.             {
  664.                 for( int ifmt=0; ifmt < pDeviceCombo->depthStencilFormatList.GetSize(); ifmt++ )
  665.                 {
  666.                     D3DFORMAT fmt = pDeviceCombo->depthStencilFormatList.GetAt( ifmt );
  667.                     AddDepthStencilBufferFormat( fmt );
  668.                 }
  669.                 pDepthStencilComboBox->SetSelectedByData( ULongToPtr(g_DeviceSettings.pp.AutoDepthStencilFormat) );
  670.             }
  671.             else
  672.             {
  673.                 if( !pDepthStencilComboBox->ContainsItem( L"(not used)" ) )
  674.                     pDepthStencilComboBox->AddItem( L"(not used)", NULL );
  675.             }
  676.             hr = OnDepthStencilBufferFormatChanged();
  677.             if( FAILED(hr) )
  678.                 return hr;
  679.             CDXUTComboBox* pVertexProcessingComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_VERTEX_PROCESSING );
  680.             pVertexProcessingComboBox->RemoveAllItems();
  681.             // Add valid vertex processing types
  682.             if( bAllowSoftwareVP )
  683.                 AddVertexProcessingType( D3DCREATE_SOFTWARE_VERTEXPROCESSING );
  684.             if( bAllowHardwareVP && pDeviceInfo->Caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
  685.                 AddVertexProcessingType( D3DCREATE_HARDWARE_VERTEXPROCESSING );
  686.             if( bAllowPureHardwareVP && pDeviceInfo->Caps.DevCaps & D3DDEVCAPS_PUREDEVICE )
  687.                 AddVertexProcessingType( D3DCREATE_PUREDEVICE );
  688.             if( bAllowMixedVP && pDeviceInfo->Caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
  689.                 AddVertexProcessingType( D3DCREATE_MIXED_VERTEXPROCESSING );
  690.             if( g_DeviceSettings.BehaviorFlags & D3DCREATE_PUREDEVICE )
  691.                 pVertexProcessingComboBox->SetSelectedByData( ULongToPtr(D3DCREATE_PUREDEVICE) );
  692.             else if( g_DeviceSettings.BehaviorFlags & D3DCREATE_SOFTWARE_VERTEXPROCESSING )
  693.                 pVertexProcessingComboBox->SetSelectedByData( ULongToPtr(D3DCREATE_SOFTWARE_VERTEXPROCESSING) );
  694.             else if( g_DeviceSettings.BehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING )
  695.                 pVertexProcessingComboBox->SetSelectedByData( ULongToPtr(D3DCREATE_HARDWARE_VERTEXPROCESSING) );
  696.             else if( g_DeviceSettings.BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING )
  697.                 pVertexProcessingComboBox->SetSelectedByData( ULongToPtr(D3DCREATE_MIXED_VERTEXPROCESSING) );
  698.             hr = OnVertexProcessingChanged();
  699.             if( FAILED(hr) )
  700.                 return hr;
  701.             CDXUTComboBox* pPresentIntervalComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_PRESENT_INTERVAL );
  702.             pPresentIntervalComboBox->RemoveAllItems();
  703.             pPresentIntervalComboBox->AddItem( L"On", ULongToPtr(D3DPRESENT_INTERVAL_DEFAULT) );
  704.             pPresentIntervalComboBox->AddItem( L"Off", ULongToPtr(D3DPRESENT_INTERVAL_IMMEDIATE) );
  705.             pPresentIntervalComboBox->SetSelectedByData( ULongToPtr( g_DeviceSettings.pp.PresentationInterval ) );
  706.         
  707.             hr = OnPresentIntervalChanged();
  708.             if( FAILED(hr) )
  709.                 return hr;
  710.         }
  711.     }
  712.     return S_OK;
  713. }
  714. //-------------------------------------------------------------------------------------
  715. HRESULT CD3DSettingsDlg::OnDepthStencilBufferFormatChanged()
  716. {
  717.     HRESULT hr = S_OK;
  718.     D3DFORMAT depthStencilBufferFormat = GetSelectedDepthStencilBufferFormat();
  719.     if( g_DeviceSettings.pp.EnableAutoDepthStencil )
  720.         g_DeviceSettings.pp.AutoDepthStencilFormat = depthStencilBufferFormat;
  721.     CD3DEnumDeviceSettingsCombo* pDeviceSettingsCombo = GetCurrentDeviceSettingsCombo();
  722.     if( pDeviceSettingsCombo == NULL )
  723.         return E_FAIL;
  724.     
  725.     CDXUTComboBox* pMultisampleTypeCombo = m_Dialog.GetComboBox( DXUTSETTINGSDLG_MULTISAMPLE_TYPE );
  726.     pMultisampleTypeCombo->RemoveAllItems();
  727.     for( int ims=0; ims < pDeviceSettingsCombo->multiSampleTypeList.GetSize(); ims++ )
  728.     {
  729.         D3DMULTISAMPLE_TYPE msType = pDeviceSettingsCombo->multiSampleTypeList.GetAt( ims );
  730.         bool bConflictFound = false;
  731.         for( int iConf = 0; iConf < pDeviceSettingsCombo->DSMSConflictList.GetSize(); iConf++ )
  732.         {
  733.             CD3DEnumDSMSConflict DSMSConf = pDeviceSettingsCombo->DSMSConflictList.GetAt( iConf );
  734.             if( DSMSConf.DSFormat == depthStencilBufferFormat &&
  735.                 DSMSConf.MSType == msType )
  736.             {
  737.                 bConflictFound = true;
  738.                 break;
  739.             }
  740.         }
  741.         if( !bConflictFound )
  742.             AddMultisampleType( msType );
  743.     }
  744.     CDXUTComboBox* pMultisampleQualityCombo = m_Dialog.GetComboBox( DXUTSETTINGSDLG_MULTISAMPLE_TYPE );
  745.     pMultisampleQualityCombo->SetSelectedByData( ULongToPtr(g_DeviceSettings.pp.MultiSampleType) );
  746.     hr = OnMultisampleTypeChanged();
  747.     if( FAILED(hr) )
  748.         return hr;
  749.     return S_OK;
  750. }
  751. //-------------------------------------------------------------------------------------
  752. HRESULT CD3DSettingsDlg::OnMultisampleTypeChanged()
  753. {
  754.     HRESULT hr = S_OK;
  755.     D3DMULTISAMPLE_TYPE multisampleType = GetSelectedMultisampleType();
  756.     g_DeviceSettings.pp.MultiSampleType = multisampleType;
  757.     CD3DEnumDeviceSettingsCombo* pDeviceSettingsCombo = GetCurrentDeviceSettingsCombo();
  758.     if( pDeviceSettingsCombo == NULL )
  759.         return E_FAIL;
  760.     DWORD dwMaxQuality = 0;
  761.     for( int iType = 0; iType < pDeviceSettingsCombo->multiSampleTypeList.GetSize(); iType++ )
  762.     {
  763.         D3DMULTISAMPLE_TYPE msType = pDeviceSettingsCombo->multiSampleTypeList.GetAt( iType );
  764.         if( msType == multisampleType )
  765.         {
  766.             dwMaxQuality = pDeviceSettingsCombo->multiSampleQualityList.GetAt( iType );
  767.             break;
  768.         }
  769.     }
  770.    
  771.     // DXUTSETTINGSDLG_MULTISAMPLE_QUALITY
  772.     CDXUTComboBox* pMultisampleQualityCombo = m_Dialog.GetComboBox( DXUTSETTINGSDLG_MULTISAMPLE_QUALITY );
  773.     pMultisampleQualityCombo->RemoveAllItems();
  774.     for( UINT iQuality = 0; iQuality < dwMaxQuality; iQuality++ )
  775.     {
  776.         AddMultisampleQuality( iQuality );
  777.     }
  778.     pMultisampleQualityCombo->SetSelectedByData( ULongToPtr(g_DeviceSettings.pp.MultiSampleQuality) );
  779.     hr = OnMultisampleQualityChanged();
  780.     if( FAILED(hr) )
  781.         return hr;
  782.     return S_OK;
  783. }
  784. //-------------------------------------------------------------------------------------
  785. HRESULT CD3DSettingsDlg::OnMultisampleQualityChanged()
  786. {
  787.     g_DeviceSettings.pp.MultiSampleQuality = GetSelectedMultisampleQuality();
  788.     return S_OK;
  789. }
  790. //-------------------------------------------------------------------------------------
  791. HRESULT CD3DSettingsDlg::OnVertexProcessingChanged()
  792. {
  793.     DWORD dwBehavior = g_DeviceSettings.BehaviorFlags;
  794.     // Clear vertex processing flags
  795.     dwBehavior &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
  796.     dwBehavior &= ~D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  797.     dwBehavior &= ~D3DCREATE_MIXED_VERTEXPROCESSING;
  798.     dwBehavior &= ~D3DCREATE_PUREDEVICE;
  799.     // Determine new flags
  800.     DWORD dwNewFlags = GetSelectedVertexProcessingType();
  801.     if( dwNewFlags & D3DCREATE_PUREDEVICE )
  802.         dwNewFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
  803.     // Make changes
  804.     g_DeviceSettings.BehaviorFlags = dwBehavior | dwNewFlags;
  805.     return S_OK;
  806. }
  807. //-------------------------------------------------------------------------------------
  808. HRESULT CD3DSettingsDlg::OnPresentIntervalChanged()
  809. {
  810.     g_DeviceSettings.pp.PresentationInterval = GetSelectedPresentInterval();
  811.     return S_OK;
  812. }
  813. //-------------------------------------------------------------------------------------
  814. HRESULT CD3DSettingsDlg::OnDeviceClipChanged()
  815. {
  816.     if( IsDeviceClip() )
  817.         g_DeviceSettings.pp.Flags |= D3DPRESENTFLAG_DEVICECLIP;
  818.     else
  819.         g_DeviceSettings.pp.Flags &= ~D3DPRESENTFLAG_DEVICECLIP;
  820.     return S_OK;
  821. }
  822. //-------------------------------------------------------------------------------------
  823. void CD3DSettingsDlg::AddAdapter( const WCHAR* strDescription, UINT iAdapter )
  824. {
  825.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_ADAPTER );
  826.  
  827.     if( !pComboBox->ContainsItem( strDescription ) )
  828.         pComboBox->AddItem( strDescription, ULongToPtr(iAdapter) );
  829. }
  830. //-------------------------------------------------------------------------------------
  831. UINT CD3DSettingsDlg::GetSelectedAdapter()
  832. {
  833.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_ADAPTER );
  834.     return PtrToUlong( pComboBox->GetSelectedData() );  
  835. }
  836. //-------------------------------------------------------------------------------------
  837. void CD3DSettingsDlg::AddDeviceType( D3DDEVTYPE devType )
  838. {
  839.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_DEVICE_TYPE );
  840.     if( !pComboBox->ContainsItem( DXUTD3DDeviceTypeToString(devType) ) )
  841.         pComboBox->AddItem( DXUTD3DDeviceTypeToString(devType), ULongToPtr(devType) );
  842. }
  843. //-------------------------------------------------------------------------------------
  844. D3DDEVTYPE CD3DSettingsDlg::GetSelectedDeviceType()
  845. {
  846.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_DEVICE_TYPE );
  847.     return (D3DDEVTYPE) PtrToUlong( pComboBox->GetSelectedData() );
  848. }
  849. //-------------------------------------------------------------------------------------
  850. void CD3DSettingsDlg::SetWindowed( bool bWindowed )
  851. {
  852.     CDXUTRadioButton* pRadioButton = m_Dialog.GetRadioButton( DXUTSETTINGSDLG_WINDOWED );
  853.     pRadioButton->SetChecked( bWindowed );
  854.     pRadioButton = m_Dialog.GetRadioButton( DXUTSETTINGSDLG_FULLSCREEN );
  855.     pRadioButton->SetChecked( !bWindowed );
  856. }
  857. //-------------------------------------------------------------------------------------
  858. bool CD3DSettingsDlg::IsWindowed()
  859. {
  860.     CDXUTRadioButton* pRadioButton = m_Dialog.GetRadioButton( DXUTSETTINGSDLG_WINDOWED );
  861.     return pRadioButton->GetChecked();
  862. }
  863. //-------------------------------------------------------------------------------------
  864. void CD3DSettingsDlg::AddAdapterFormat( D3DFORMAT format )
  865. {
  866.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_ADAPTER_FORMAT );
  867.     
  868.     if( !pComboBox->ContainsItem( DXUTD3DFormatToString(format, TRUE) ) )
  869.         pComboBox->AddItem( DXUTD3DFormatToString(format, TRUE), ULongToPtr( format ) );
  870. }
  871. //-------------------------------------------------------------------------------------
  872. D3DFORMAT CD3DSettingsDlg::GetSelectedAdapterFormat()
  873. {
  874.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_ADAPTER_FORMAT );
  875.   
  876.     return (D3DFORMAT) PtrToUlong( pComboBox->GetSelectedData() ); 
  877. }
  878. //-------------------------------------------------------------------------------------
  879. void CD3DSettingsDlg::AddResolution( DWORD dwWidth, DWORD dwHeight )
  880. {
  881.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_RESOLUTION );
  882.   
  883.     DWORD dwResolutionData;
  884.     WCHAR strResolution[50];
  885.     dwResolutionData = MAKELONG( dwWidth, dwHeight );
  886.     StringCchPrintf( strResolution, 50, L"%d by %d", dwWidth, dwHeight );
  887.     if( !pComboBox->ContainsItem( strResolution ) )
  888.         pComboBox->AddItem( strResolution, ULongToPtr( dwResolutionData ) );
  889. }
  890. //-------------------------------------------------------------------------------------
  891. void CD3DSettingsDlg::GetSelectedResolution( DWORD* pdwWidth, DWORD* pdwHeight )
  892. {
  893.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_RESOLUTION );
  894.     DWORD dwResolution = PtrToUlong( pComboBox->GetSelectedData() );
  895.     *pdwWidth = LOWORD( dwResolution );
  896.     *pdwHeight = HIWORD( dwResolution );
  897. }
  898. //-------------------------------------------------------------------------------------
  899. void CD3DSettingsDlg::AddRefreshRate( DWORD dwRate )
  900. {
  901.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_REFRESH_RATE );
  902.         
  903.     WCHAR strRefreshRate[50];
  904.     if( dwRate == 0 )
  905.         StringCchCopy( strRefreshRate, 50, L"Default Rate" );
  906.     else
  907.         StringCchPrintf( strRefreshRate, 50, L"%d Hz", dwRate );
  908.     if( !pComboBox->ContainsItem( strRefreshRate ) )
  909.         pComboBox->AddItem( strRefreshRate, ULongToPtr(dwRate) );
  910. }
  911. //-------------------------------------------------------------------------------------
  912. DWORD CD3DSettingsDlg::GetSelectedRefreshRate()
  913. {
  914.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_REFRESH_RATE );
  915.     
  916.     return PtrToUlong( pComboBox->GetSelectedData() );
  917. }
  918. //-------------------------------------------------------------------------------------
  919. void CD3DSettingsDlg::AddBackBufferFormat( D3DFORMAT format )
  920. {
  921.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_BACK_BUFFER_FORMAT );
  922.     
  923.     if( !pComboBox->ContainsItem( DXUTD3DFormatToString(format, TRUE) ) )
  924.         pComboBox->AddItem( DXUTD3DFormatToString(format, TRUE), ULongToPtr( format ) );
  925. }
  926. //-------------------------------------------------------------------------------------
  927. D3DFORMAT CD3DSettingsDlg::GetSelectedBackBufferFormat()
  928. {
  929.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_BACK_BUFFER_FORMAT );
  930.     
  931.     return (D3DFORMAT) PtrToUlong( pComboBox->GetSelectedData() ); 
  932. }
  933. //-------------------------------------------------------------------------------------
  934. void CD3DSettingsDlg::AddDepthStencilBufferFormat( D3DFORMAT format )
  935. {
  936.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_DEPTH_STENCIL );
  937.     
  938.     if( !pComboBox->ContainsItem( DXUTD3DFormatToString(format, TRUE) ) )
  939.         pComboBox->AddItem( DXUTD3DFormatToString(format, TRUE), ULongToPtr(format) );
  940. }
  941. //-------------------------------------------------------------------------------------
  942. D3DFORMAT CD3DSettingsDlg::GetSelectedDepthStencilBufferFormat()
  943. {
  944.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_DEPTH_STENCIL );
  945.     
  946.     return (D3DFORMAT) PtrToUlong( pComboBox->GetSelectedData() ); 
  947. }
  948. //-------------------------------------------------------------------------------------
  949. void CD3DSettingsDlg::AddMultisampleType( D3DMULTISAMPLE_TYPE type )
  950. {
  951.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_MULTISAMPLE_TYPE );
  952.     
  953.     if( !pComboBox->ContainsItem( DXUTMultisampleTypeToString(type) ) )
  954.         pComboBox->AddItem( DXUTMultisampleTypeToString(type), ULongToPtr(type) );
  955. }
  956. //-------------------------------------------------------------------------------------
  957. D3DMULTISAMPLE_TYPE CD3DSettingsDlg::GetSelectedMultisampleType()
  958. {
  959.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_MULTISAMPLE_TYPE );
  960.     
  961.     return (D3DMULTISAMPLE_TYPE) PtrToUlong( pComboBox->GetSelectedData() ); 
  962. }
  963. //-------------------------------------------------------------------------------------
  964. void CD3DSettingsDlg::AddMultisampleQuality( DWORD dwQuality )
  965. {
  966.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_MULTISAMPLE_QUALITY );
  967.         
  968.     WCHAR strQuality[50];
  969.     StringCchPrintf( strQuality, 50, L"%d", dwQuality );
  970.     if( !pComboBox->ContainsItem( strQuality ) )
  971.         pComboBox->AddItem( strQuality, ULongToPtr(dwQuality) );
  972. }
  973. //-------------------------------------------------------------------------------------
  974. DWORD CD3DSettingsDlg::GetSelectedMultisampleQuality()
  975. {
  976.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_MULTISAMPLE_QUALITY );
  977.     
  978.     return PtrToUlong( pComboBox->GetSelectedData() ); 
  979. }
  980. //-------------------------------------------------------------------------------------
  981. void CD3DSettingsDlg::AddVertexProcessingType( DWORD dwType )
  982. {
  983.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_VERTEX_PROCESSING );
  984.     
  985.     if( !pComboBox->ContainsItem( DXUTVertexProcessingTypeToString(dwType) ) )
  986.         pComboBox->AddItem( DXUTVertexProcessingTypeToString(dwType), ULongToPtr(dwType) );
  987. }
  988. //-------------------------------------------------------------------------------------
  989. DWORD CD3DSettingsDlg::GetSelectedVertexProcessingType()
  990. {
  991.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_VERTEX_PROCESSING );
  992.     
  993.     return PtrToUlong( pComboBox->GetSelectedData() ); 
  994. }
  995. //-------------------------------------------------------------------------------------
  996. DWORD CD3DSettingsDlg::GetSelectedPresentInterval()
  997. {
  998.     CDXUTComboBox* pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_PRESENT_INTERVAL );
  999.     
  1000.     return PtrToUlong( pComboBox->GetSelectedData() ); 
  1001. }
  1002. //-------------------------------------------------------------------------------------
  1003. void CD3DSettingsDlg::SetDeviceClip( bool bDeviceClip )
  1004. {
  1005.     CDXUTCheckBox* pCheckBox = m_Dialog.GetCheckBox( DXUTSETTINGSDLG_DEVICECLIP );
  1006.     pCheckBox->SetChecked( bDeviceClip );
  1007. }
  1008. //-------------------------------------------------------------------------------------
  1009. bool CD3DSettingsDlg::IsDeviceClip()
  1010. {
  1011.     CDXUTCheckBox* pCheckBox = m_Dialog.GetCheckBox( DXUTSETTINGSDLG_DEVICECLIP );
  1012.     return pCheckBox->GetChecked();
  1013. }
  1014. //--------------------------------------------------------------------------------------
  1015. // Returns the string for the given D3DDEVTYPE.
  1016. //--------------------------------------------------------------------------------------
  1017. WCHAR* DXUTD3DDeviceTypeToString(D3DDEVTYPE devType)
  1018. {
  1019.     switch (devType)
  1020.     {
  1021.         case D3DDEVTYPE_HAL:        return L"D3DDEVTYPE_HAL";
  1022.         case D3DDEVTYPE_SW:         return L"D3DDEVTYPE_SW";
  1023.         case D3DDEVTYPE_REF:        return L"D3DDEVTYPE_REF";
  1024.         default:                    return L"Unknown devType";
  1025.     }
  1026. }
  1027. //--------------------------------------------------------------------------------------
  1028. // Returns the string for the given D3DMULTISAMPLE_TYPE.
  1029. //--------------------------------------------------------------------------------------
  1030. WCHAR* DXUTMultisampleTypeToString(D3DMULTISAMPLE_TYPE MultiSampleType)
  1031. {
  1032.     switch (MultiSampleType)
  1033.     {
  1034.     case D3DMULTISAMPLE_NONE:       return L"D3DMULTISAMPLE_NONE";
  1035.     case D3DMULTISAMPLE_NONMASKABLE: return L"D3DMULTISAMPLE_NONMASKABLE";
  1036.     case D3DMULTISAMPLE_2_SAMPLES:  return L"D3DMULTISAMPLE_2_SAMPLES";
  1037.     case D3DMULTISAMPLE_3_SAMPLES:  return L"D3DMULTISAMPLE_3_SAMPLES";
  1038.     case D3DMULTISAMPLE_4_SAMPLES:  return L"D3DMULTISAMPLE_4_SAMPLES";
  1039.     case D3DMULTISAMPLE_5_SAMPLES:  return L"D3DMULTISAMPLE_5_SAMPLES";
  1040.     case D3DMULTISAMPLE_6_SAMPLES:  return L"D3DMULTISAMPLE_6_SAMPLES";
  1041.     case D3DMULTISAMPLE_7_SAMPLES:  return L"D3DMULTISAMPLE_7_SAMPLES";
  1042.     case D3DMULTISAMPLE_8_SAMPLES:  return L"D3DMULTISAMPLE_8_SAMPLES";
  1043.     case D3DMULTISAMPLE_9_SAMPLES:  return L"D3DMULTISAMPLE_9_SAMPLES";
  1044.     case D3DMULTISAMPLE_10_SAMPLES: return L"D3DMULTISAMPLE_10_SAMPLES";
  1045.     case D3DMULTISAMPLE_11_SAMPLES: return L"D3DMULTISAMPLE_11_SAMPLES";
  1046.     case D3DMULTISAMPLE_12_SAMPLES: return L"D3DMULTISAMPLE_12_SAMPLES";
  1047.     case D3DMULTISAMPLE_13_SAMPLES: return L"D3DMULTISAMPLE_13_SAMPLES";
  1048.     case D3DMULTISAMPLE_14_SAMPLES: return L"D3DMULTISAMPLE_14_SAMPLES";
  1049.     case D3DMULTISAMPLE_15_SAMPLES: return L"D3DMULTISAMPLE_15_SAMPLES";
  1050.     case D3DMULTISAMPLE_16_SAMPLES: return L"D3DMULTISAMPLE_16_SAMPLES";
  1051.     default:                        return L"Unknown Multisample Type";
  1052.     }
  1053. }
  1054. //--------------------------------------------------------------------------------------
  1055. // Returns the string for the given vertex processing type
  1056. //--------------------------------------------------------------------------------------
  1057. WCHAR* DXUTVertexProcessingTypeToString(DWORD vpt)
  1058. {
  1059.     switch (vpt)
  1060.     {
  1061.     case D3DCREATE_SOFTWARE_VERTEXPROCESSING: return L"Software vertex processing";
  1062.     case D3DCREATE_MIXED_VERTEXPROCESSING:    return L"Mixed vertex processing";
  1063.     case D3DCREATE_HARDWARE_VERTEXPROCESSING: return L"Hardware vertex processing";
  1064.     case D3DCREATE_PUREDEVICE:                return L"Pure hardware vertex processing";
  1065.     default:                                  return L"Unknown vertex processing type";
  1066.     }
  1067. }
  1068. //--------------------------------------------------------------------------------------
  1069. // Returns the string for the given present interval.
  1070. //--------------------------------------------------------------------------------------
  1071. WCHAR* DXUTPresentIntervalToString( UINT pi )
  1072. {
  1073.     switch( pi )
  1074.     {
  1075.     case D3DPRESENT_INTERVAL_IMMEDIATE: return L"D3DPRESENT_INTERVAL_IMMEDIATE";
  1076.     case D3DPRESENT_INTERVAL_DEFAULT:   return L"D3DPRESENT_INTERVAL_DEFAULT";
  1077.     case D3DPRESENT_INTERVAL_ONE:       return L"D3DPRESENT_INTERVAL_ONE";
  1078.     case D3DPRESENT_INTERVAL_TWO:       return L"D3DPRESENT_INTERVAL_TWO";
  1079.     case D3DPRESENT_INTERVAL_THREE:     return L"D3DPRESENT_INTERVAL_THREE";
  1080.     case D3DPRESENT_INTERVAL_FOUR:      return L"D3DPRESENT_INTERVAL_FOUR";
  1081.     default:                            return L"Unknown PresentInterval";
  1082.     }
  1083. }