⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dxutsettingsdlg.cpp

📁 DX例子中等难度的。全是新例子。VC2003
💻 CPP
📖 第 1 页 / 共 4 页
字号:
                if( DXUTIsWindowed() )
                    GetClientRect( DXUTGetHWND(), &rcClient );
                else
                    rcClient = DXUTGetWindowClientRectAtModeChange();
                DWORD dwWindowWidth  = rcClient.right - rcClient.left;
                DWORD dwWindowHeight = rcClient.bottom - rcClient.top;

                g_DeviceSettings.pp.BackBufferWidth = dwWindowWidth;
                g_DeviceSettings.pp.BackBufferHeight = dwWindowHeight;
            }

            if( g_DeviceSettings.pp.MultiSampleType != D3DMULTISAMPLE_NONE )
            {
                g_DeviceSettings.pp.Flags &= ~D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
            }

            DXUTCreateDeviceFromSettings( &g_DeviceSettings );

            SetActive( false );
            break;
        }

        case DXUTSETTINGSDLG_CANCEL:                
        {
            SetActive( false );
            break;
        }

    }
}


//-------------------------------------------------------------------------------------
HRESULT CD3DSettingsDlg::SetDeviceSettingsFromUI()
{
    CDXUTComboBox* pComboBox;
    CDXUTRadioButton* pRadioButton;

    // DXUTSETTINGSDLG_DEVICE_TYPE
    pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_DEVICE_TYPE );
    g_DeviceSettings.DeviceType = (D3DDEVTYPE) PtrToUlong( pComboBox->GetSelectedData() );
    
    // DXUTSETTINGSDLG_WINDOWED
    pRadioButton = m_Dialog.GetRadioButton( DXUTSETTINGSDLG_WINDOWED );
    g_DeviceSettings.pp.Windowed = pRadioButton->GetChecked();

    // DXUTSETTINGSDLG_ADAPTER_FORMAT
    pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_ADAPTER_FORMAT );
    g_DeviceSettings.AdapterFormat = (D3DFORMAT) PtrToUlong( pComboBox->GetSelectedData() );
    

    if( g_DeviceSettings.pp.Windowed )
    {
        g_DeviceSettings.pp.BackBufferFormat = D3DFMT_UNKNOWN;
        g_DeviceSettings.pp.FullScreen_RefreshRateInHz = 0;
    }
    else
    {
        // DXUTSETTINGSDLG_BACK_BUFFER_FORMAT
        pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_BACK_BUFFER_FORMAT );
        g_DeviceSettings.pp.BackBufferFormat = (D3DFORMAT) PtrToUlong( pComboBox->GetSelectedData() );
    
        // DXUTSETTINGSDLG_RESOLUTION
        pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_RESOLUTION );
        DWORD dwResolution = PtrToUlong( pComboBox->GetSelectedData() );
        g_DeviceSettings.pp.BackBufferWidth = HIWORD( dwResolution );
        g_DeviceSettings.pp.BackBufferHeight = LOWORD( dwResolution );
        
        // DXUTSETTINGSDLG_REFRESH_RATE
        pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_REFRESH_RATE );
        g_DeviceSettings.pp.FullScreen_RefreshRateInHz = PtrToUlong( pComboBox->GetSelectedData() );
    }

    // DXUTSETTINGSDLG_DEPTH_STENCIL
    pComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_DEPTH_STENCIL );
    g_DeviceSettings.pp.AutoDepthStencilFormat = (D3DFORMAT) PtrToUlong( pComboBox->GetSelectedData() );
    
    return S_OK;
}


//-------------------------------------------------------------------------------------
CD3DEnumAdapterInfo* CD3DSettingsDlg::GetCurrentAdapterInfo()
{
    CD3DEnumeration* pD3DEnum = DXUTGetEnumeration();
    return pD3DEnum->GetAdapterInfo( g_DeviceSettings.AdapterOrdinal );
}


//-------------------------------------------------------------------------------------
CD3DEnumDeviceInfo* CD3DSettingsDlg::GetCurrentDeviceInfo()
{
    CD3DEnumeration* pD3DEnum = DXUTGetEnumeration();
    return pD3DEnum->GetDeviceInfo( g_DeviceSettings.AdapterOrdinal,
                                      g_DeviceSettings.DeviceType );
}


//-------------------------------------------------------------------------------------
CD3DEnumDeviceSettingsCombo* CD3DSettingsDlg::GetCurrentDeviceSettingsCombo()
{
    CD3DEnumeration* pD3DEnum = DXUTGetEnumeration();
    return pD3DEnum->GetDeviceSettingsCombo( g_DeviceSettings.AdapterOrdinal,
                                             g_DeviceSettings.DeviceType,
                                             g_DeviceSettings.AdapterFormat,
                                             g_DeviceSettings.pp.BackBufferFormat,
                                             (g_DeviceSettings.pp.Windowed == TRUE) );
}


//-------------------------------------------------------------------------------------
HRESULT CD3DSettingsDlg::OnAdapterChanged()
{
    HRESULT hr = S_OK;

    // Store the adapter index
    g_DeviceSettings.AdapterOrdinal = GetSelectedAdapter();
    
    // DXUTSETTINGSDLG_DEVICE_TYPE
    CDXUTComboBox* pDeviceTypeComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_DEVICE_TYPE );
    pDeviceTypeComboBox->RemoveAllItems();
 
    CD3DEnumAdapterInfo* pAdapterInfo = GetCurrentAdapterInfo();
    if( pAdapterInfo == NULL )
        return E_FAIL;

    for( int iDeviceInfo=0; iDeviceInfo < pAdapterInfo->deviceInfoList.GetSize(); iDeviceInfo++ )
    {
        CD3DEnumDeviceInfo* pDeviceInfo = pAdapterInfo->deviceInfoList.GetAt(iDeviceInfo);
        AddDeviceType( pDeviceInfo->DeviceType );
    }

    pDeviceTypeComboBox->SetSelectedByData( ULongToPtr(g_DeviceSettings.DeviceType) );

    hr = OnDeviceTypeChanged();
    if( FAILED(hr) )
        return hr;

    return S_OK; 
}



//-------------------------------------------------------------------------------------
HRESULT CD3DSettingsDlg::OnDeviceTypeChanged()
{
    HRESULT hr = S_OK;
    
    g_DeviceSettings.DeviceType = GetSelectedDeviceType();
   
    // Update windowed/full screen radio buttons
    bool bHasWindowedDeviceCombo = false;
    bool bHasFullScreenDeviceCombo = false;

    CD3DEnumDeviceInfo* pDeviceInfo = GetCurrentDeviceInfo();
    if( pDeviceInfo == NULL )
        return E_FAIL;
            
    for( int idc = 0; idc < pDeviceInfo->deviceSettingsComboList.GetSize(); idc++ )
    {
        CD3DEnumDeviceSettingsCombo* pDeviceSettingsCombo = pDeviceInfo->deviceSettingsComboList.GetAt( idc );

        if( pDeviceSettingsCombo->Windowed )
            bHasWindowedDeviceCombo = true;
        else
            bHasFullScreenDeviceCombo = true;
    }

    // DXUTSETTINGSDLG_WINDOWED, DXUTSETTINGSDLG_FULLSCREEN
    m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_WINDOWED, bHasWindowedDeviceCombo );
    m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_FULLSCREEN, bHasFullScreenDeviceCombo );

    SetWindowed( g_DeviceSettings.pp.Windowed && bHasWindowedDeviceCombo );

    hr = OnWindowedFullScreenChanged();
    if( FAILED(hr) )
        return hr;

    return S_OK;
}



//-------------------------------------------------------------------------------------
HRESULT CD3DSettingsDlg::OnWindowedFullScreenChanged()
{
    HRESULT hr = S_OK;

    bool bWindowed = IsWindowed();
    g_DeviceSettings.pp.Windowed = bWindowed;

    m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_ADAPTER_FORMAT_LABEL, !bWindowed );
    m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_RESOLUTION_LABEL, !bWindowed );
    m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_REFRESH_RATE_LABEL, !bWindowed );

    m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_ADAPTER_FORMAT, !bWindowed );
    m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_RESOLUTION, !bWindowed );
    m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_RESOLUTION_SHOW_ALL, !bWindowed );
    m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_REFRESH_RATE, !bWindowed );
    m_Dialog.SetControlEnabled( DXUTSETTINGSDLG_DEVICECLIP, bWindowed );

    bool bDeviceClip = ( 0x0 != (g_DeviceSettings.pp.Flags & D3DPRESENTFLAG_DEVICECLIP) );

    // If windowed, get the appropriate adapter format from Direct3D
    if( g_DeviceSettings.pp.Windowed )
    {
        IDirect3D9* pD3D = DXUTGetD3DObject();
        if( pD3D == NULL )
            return DXTRACE_ERR( L"DXUTGetD3DObject", E_FAIL );

        D3DDISPLAYMODE mode;
        hr = pD3D->GetAdapterDisplayMode( g_DeviceSettings.AdapterOrdinal, &mode );
        if( FAILED(hr) )
            return DXTRACE_ERR( L"GetAdapterDisplayMode", hr );

        // Default resolution to the fullscreen res that was last used
        RECT rc = DXUTGetFullsceenClientRectAtModeChange();
        if( rc.right == 0 || rc.bottom == 0 )
        {
            // If nothing last used, then default to the adapter desktop res
            g_DeviceSettings.pp.BackBufferWidth = mode.Width;
            g_DeviceSettings.pp.BackBufferHeight = mode.Height;
        }
        else
        {
            g_DeviceSettings.pp.BackBufferWidth = rc.right;
            g_DeviceSettings.pp.BackBufferHeight = rc.bottom;
        }

        g_DeviceSettings.AdapterFormat = mode.Format;
        g_DeviceSettings.pp.FullScreen_RefreshRateInHz = mode.RefreshRate;
    }

    const D3DFORMAT adapterFormat = g_DeviceSettings.AdapterFormat;
    const DWORD dwWidth = g_DeviceSettings.pp.BackBufferWidth;
    const DWORD dwHeight = g_DeviceSettings.pp.BackBufferHeight;
    const DWORD dwRefreshRate = g_DeviceSettings.pp.FullScreen_RefreshRateInHz;

    // DXUTSETTINGSDLG_DEVICECLIP
    SetDeviceClip( bDeviceClip );
    
    // DXUTSETTINGSDLG_ADAPTER_FORMAT
    CDXUTComboBox* pAdapterFormatComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_ADAPTER_FORMAT );
    if( pAdapterFormatComboBox == NULL )
        return E_FAIL;
    pAdapterFormatComboBox->RemoveAllItems();

    CD3DEnumDeviceInfo* pDeviceInfo = GetCurrentDeviceInfo();
    if( pDeviceInfo == NULL )
        return E_FAIL;

    if( bWindowed )
    {
        AddAdapterFormat( adapterFormat );
    }
    else
    {
        for( int iSettingsCombo=0; iSettingsCombo < pDeviceInfo->deviceSettingsComboList.GetSize(); iSettingsCombo++ )
        {
            CD3DEnumDeviceSettingsCombo* pSettingsCombo = pDeviceInfo->deviceSettingsComboList.GetAt(iSettingsCombo);
            AddAdapterFormat( pSettingsCombo->AdapterFormat );
        }    
    }

    pAdapterFormatComboBox->SetSelectedByData( ULongToPtr(adapterFormat) );

    hr = OnAdapterFormatChanged();
    if( FAILED(hr) )
        return hr;

    // DXUTSETTINGSDLG_RESOLUTION
    CDXUTComboBox* pResolutionComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_RESOLUTION );
    
    if( bWindowed )
    {
        pResolutionComboBox->RemoveAllItems();
        AddResolution( dwWidth, dwHeight );
    }

    pResolutionComboBox->SetSelectedByData( ULongToPtr( MAKELONG(dwWidth, dwHeight) ) );
    
    hr = OnResolutionChanged();
    if( FAILED(hr) )
        return hr;

    // DXUTSETTINGSDLG_REFRESH_RATE
    CDXUTComboBox* pRefreshRateComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_REFRESH_RATE );
    
    if( bWindowed )
    {
        pRefreshRateComboBox->RemoveAllItems();
        AddRefreshRate( dwRefreshRate );
    }
    
    pRefreshRateComboBox->SetSelectedByData( ULongToPtr(dwRefreshRate) ); 

    hr = OnRefreshRateChanged();
    if( FAILED(hr) )
        return hr;

    return S_OK;
}


//-------------------------------------------------------------------------------------
HRESULT CD3DSettingsDlg::OnAdapterFormatChanged()
{ 
    HRESULT hr = S_OK;

    // DXUTSETTINGSDLG_ADAPTER_FORMAT
    D3DFORMAT adapterFormat = GetSelectedAdapterFormat();
    g_DeviceSettings.AdapterFormat = adapterFormat;

    // DXUTSETTINGSDLG_RESOLUTION
    CDXUTComboBox* pResolutionComboBox = m_Dialog.GetComboBox( DXUTSETTINGSDLG_RESOLUTION );
    pResolutionComboBox->RemoveAllItems();

    CD3DEnumAdapterInfo* pAdapterInfo = GetCurrentAdapterInfo();
    if( pAdapterInfo == NULL )
        return E_FAIL;

    bool bShowAll = m_Dialog.GetCheckBox( DXUTSETTINGSDLG_RESOLUTION_SHOW_ALL )->GetChecked();

    // Get the desktop aspect ratio
    D3DDISPLAYMODE dmDesktop;
    DXUTGetDesktopResolution( g_DeviceSettings.AdapterOrdinal, &dmDesktop.Width, &dmDesktop.Height );
    float fDesktopAspectRatio = dmDesktop.Width / (float)dmDesktop.Height;

    for( int idm = 0; idm < pAdapterInfo->displayModeList.GetSize(); idm++ )
    {
        D3DDISPLAYMODE DisplayMode = pAdapterInfo->displayModeList.GetAt( idm );
        float fAspect = (float)DisplayMode.Width / (float)DisplayMode.Height;

        if( DisplayMode.Format == adapterFormat )
        {
            // If "Show All" is not checked, then hide all resolutions
            // that don't match the aspect ratio of the desktop resolution
            if( bShowAll || (!bShowAll && fabsf(fDesktopAspectRatio - fAspect) < 0.05f) )
            {
                AddResolution( DisplayMode.Width, DisplayMode.Height );    
            }
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -