📄 dxut.cpp
字号:
hr = DXUTInit();
if( FAILED(hr) )
return hr;
}
WCHAR* strCachedWindowTitle = GetDXUTState().GetWindowTitle();
GetWindowText( hWndFocus, strCachedWindowTitle, 255 );
strCachedWindowTitle[255] = 0;
// Get the window's initial style
DWORD dwWindowStyle = GetWindowLong( hWndDeviceWindowed, GWL_STYLE );
GetDXUTState().SetWinStyle( dwWindowStyle );
GetDXUTState().SetWindowCreatedWithDefaultPositions( false );
// Store the client and window rects of the windowed-mode device window
RECT rcClient;
GetClientRect( hWndDeviceWindowed, &rcClient );
GetDXUTState().SetWindowClientRect( rcClient );
RECT rcWindow;
GetWindowRect( hWndDeviceWindowed, &rcWindow );
GetDXUTState().SetWindowBoundsRect( rcWindow );
GetDXUTState().SetWindowCreated( true );
GetDXUTState().SetHWNDFocus( hWndFocus );
GetDXUTState().SetHWNDDeviceFullScreen( hWndDeviceFullScreen );
GetDXUTState().SetHWNDDeviceWindowed( hWndDeviceWindowed );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Creates a Direct3D device. If DXUTCreateWindow() or DXUTSetWindow() has not already
// been called, it will call DXUTCreateWindow() with the default parameters.
// Instead of calling this, you can call DXUTSetDevice() or DXUTCreateDeviceFromSettings()
//--------------------------------------------------------------------------------------
HRESULT DXUTCreateDevice( UINT AdapterOrdinal, bool bWindowed,
int nSuggestedWidth, int nSuggestedHeight,
LPDXUTCALLBACKISDEVICEACCEPTABLE pCallbackIsDeviceAcceptable,
LPDXUTCALLBACKMODIFYDEVICESETTINGS pCallbackModifyDeviceSettings )
{
HRESULT hr;
// Not allowed to call this from inside the device callbacks
if( GetDXUTState().GetInsideDeviceCallback() )
return DXUT_ERR_MSGBOX( L"DXUTCreateWindow", E_FAIL );
// Record the function arguments in the global state
GetDXUTState().SetIsDeviceAcceptableFunc( pCallbackIsDeviceAcceptable );
GetDXUTState().SetModifyDeviceSettingsFunc( pCallbackModifyDeviceSettings );
GetDXUTState().SetDeviceCreateCalled( true );
// If DXUTCreateWindow() or DXUTSetWindow() has not already been called,
// then call DXUTCreateWindow() with the default parameters.
if( !GetDXUTState().GetWindowCreated() )
{
// If DXUTCreateWindow() or DXUTSetWindow() was already called and failed, then fail.
// DXUTCreateWindow() or DXUTSetWindow() must first succeed for this function to succeed
if( GetDXUTState().GetWindowCreateCalled() )
return E_FAIL;
// If DXUTCreateWindow() or DXUTSetWindow() hasn't been called, then
// automatically call DXUTCreateWindow() with default params
hr = DXUTCreateWindow();
if( FAILED(hr) )
return hr;
}
// Force an enumeration with the new IsDeviceAcceptable callback
DXUTPrepareEnumerationObject( true );
DXUTMatchOptions matchOptions;
matchOptions.eAdapterOrdinal = DXUTMT_PRESERVE_INPUT;
matchOptions.eDeviceType = DXUTMT_IGNORE_INPUT;
matchOptions.eWindowed = DXUTMT_PRESERVE_INPUT;
matchOptions.eAdapterFormat = DXUTMT_IGNORE_INPUT;
matchOptions.eVertexProcessing = DXUTMT_IGNORE_INPUT;
matchOptions.eResolution = DXUTMT_CLOSEST_TO_INPUT;
matchOptions.eBackBufferFormat = DXUTMT_IGNORE_INPUT;
matchOptions.eBackBufferCount = DXUTMT_IGNORE_INPUT;
matchOptions.eMultiSample = DXUTMT_IGNORE_INPUT;
matchOptions.eSwapEffect = DXUTMT_IGNORE_INPUT;
matchOptions.eDepthFormat = DXUTMT_IGNORE_INPUT;
matchOptions.eStencilFormat = DXUTMT_IGNORE_INPUT;
matchOptions.ePresentFlags = DXUTMT_IGNORE_INPUT;
matchOptions.eRefreshRate = DXUTMT_IGNORE_INPUT;
matchOptions.ePresentInterval = DXUTMT_IGNORE_INPUT;
DXUTDeviceSettings deviceSettings;
ZeroMemory( &deviceSettings, sizeof(DXUTDeviceSettings) );
deviceSettings.AdapterOrdinal = AdapterOrdinal;
deviceSettings.pp.Windowed = bWindowed;
deviceSettings.pp.BackBufferWidth = nSuggestedWidth;
deviceSettings.pp.BackBufferHeight = nSuggestedHeight;
// Override with settings from the command line
if( GetDXUTState().GetOverrideWidth() != 0 )
deviceSettings.pp.BackBufferWidth = GetDXUTState().GetOverrideWidth();
if( GetDXUTState().GetOverrideHeight() != 0 )
deviceSettings.pp.BackBufferHeight = GetDXUTState().GetOverrideHeight();
if( GetDXUTState().GetOverrideAdapterOrdinal() != -1 )
deviceSettings.AdapterOrdinal = GetDXUTState().GetOverrideAdapterOrdinal();
if( GetDXUTState().GetOverrideFullScreen() )
{
deviceSettings.pp.Windowed = FALSE;
if( GetDXUTState().GetOverrideWidth() == 0 && GetDXUTState().GetOverrideHeight() == 0 )
matchOptions.eResolution = DXUTMT_IGNORE_INPUT;
}
if( GetDXUTState().GetOverrideWindowed() )
deviceSettings.pp.Windowed = TRUE;
if( GetDXUTState().GetOverrideForceHAL() )
{
deviceSettings.DeviceType = D3DDEVTYPE_HAL;
matchOptions.eDeviceType = DXUTMT_PRESERVE_INPUT;
}
if( GetDXUTState().GetOverrideForceREF() )
{
deviceSettings.DeviceType = D3DDEVTYPE_REF;
matchOptions.eDeviceType = DXUTMT_PRESERVE_INPUT;
}
if( GetDXUTState().GetOverrideForcePureHWVP() )
{
deviceSettings.BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE;
matchOptions.eVertexProcessing = DXUTMT_PRESERVE_INPUT;
}
else if( GetDXUTState().GetOverrideForceHWVP() )
{
deviceSettings.BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
matchOptions.eVertexProcessing = DXUTMT_PRESERVE_INPUT;
}
else if( GetDXUTState().GetOverrideForceSWVP() )
{
deviceSettings.BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
matchOptions.eVertexProcessing = DXUTMT_PRESERVE_INPUT;
}
hr = DXUTFindValidDeviceSettings( &deviceSettings, &deviceSettings, &matchOptions );
if( FAILED(hr) ) // the call will fail if no valid devices were found
{
DXUTDisplayErrorMessage( hr );
return DXUT_ERR( L"DXUTFindValidDeviceSettings", hr );
}
// If the ModifyDeviceSettings callback is non-NULL, then call it to
// let the app change the settings
if( pCallbackModifyDeviceSettings )
{
D3DCAPS9 caps;
IDirect3D9* pD3D = DXUTGetD3DObject();
pD3D->GetDeviceCaps( deviceSettings.AdapterOrdinal, deviceSettings.DeviceType, &caps );
pCallbackModifyDeviceSettings( &deviceSettings, &caps );
}
// Change to a Direct3D device created from the new device settings.
// If there is an existing device, then either reset or recreated the scene
hr = DXUTChangeDevice( &deviceSettings, NULL, false );
if( FAILED(hr) )
return hr;
return S_OK;
}
//--------------------------------------------------------------------------------------
// Passes a previously created Direct3D device for use by the framework.
// If DXUTCreateWindow() has not already been called, it will call it with the
// default parameters. Instead of calling this, you can call DXUTCreateDevice() or
// DXUTCreateDeviceFromSettings()
//--------------------------------------------------------------------------------------
HRESULT DXUTSetDevice( IDirect3DDevice9* pd3dDevice )
{
HRESULT hr;
if( pd3dDevice == NULL )
return DXUT_ERR_MSGBOX( L"DXUTSetDevice", E_INVALIDARG );
// Not allowed to call this from inside the device callbacks
if( GetDXUTState().GetInsideDeviceCallback() )
return DXUT_ERR_MSGBOX( L"DXUTCreateWindow", E_FAIL );
GetDXUTState().SetDeviceCreateCalled( true );
// If DXUTCreateWindow() or DXUTSetWindow() has not already been called,
// then call DXUTCreateWindow() with the default parameters.
if( !GetDXUTState().GetWindowCreated() )
{
// If DXUTCreateWindow() or DXUTSetWindow() was already called and failed, then fail.
// DXUTCreateWindow() or DXUTSetWindow() must first succeed for this function to succeed
if( GetDXUTState().GetWindowCreateCalled() )
return E_FAIL;
// If DXUTCreateWindow() or DXUTSetWindow() hasn't been called, then
// automatically call DXUTCreateWindow() with default params
hr = DXUTCreateWindow();
if( FAILED(hr) )
return hr;
}
DXUTDeviceSettings* pDeviceSettings = new DXUTDeviceSettings;
if( pDeviceSettings == NULL )
return E_OUTOFMEMORY;
ZeroMemory( pDeviceSettings, sizeof(DXUTDeviceSettings) );
// Get the present params from the swap chain
IDirect3DSurface9* pBackBuffer = NULL;
hr = pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
if( SUCCEEDED(hr) )
{
IDirect3DSwapChain9* pSwapChain = NULL;
hr = pBackBuffer->GetContainer( IID_IDirect3DSwapChain9, (void**) &pSwapChain );
if( SUCCEEDED(hr) )
{
pSwapChain->GetPresentParameters( &pDeviceSettings->pp );
SAFE_RELEASE( pSwapChain );
}
SAFE_RELEASE( pBackBuffer );
}
D3DDEVICE_CREATION_PARAMETERS d3dCreationParams;
pd3dDevice->GetCreationParameters( &d3dCreationParams );
// Fill out the rest of the device settings struct
pDeviceSettings->AdapterOrdinal = d3dCreationParams.AdapterOrdinal;
pDeviceSettings->DeviceType = d3dCreationParams.DeviceType;
DXUTFindAdapterFormat( pDeviceSettings->AdapterOrdinal, pDeviceSettings->DeviceType,
pDeviceSettings->pp.BackBufferFormat, pDeviceSettings->pp.Windowed,
&pDeviceSettings->AdapterFormat );
pDeviceSettings->BehaviorFlags = d3dCreationParams.BehaviorFlags;
// Change to the Direct3D device passed in
hr = DXUTChangeDevice( pDeviceSettings, pd3dDevice, false );
if( FAILED(hr) )
return hr;
return S_OK;
}
//--------------------------------------------------------------------------------------
// Tells the framework to change to a device created from the passed in device settings
// If DXUTCreateWindow() has not already been called, it will call it with the
// default parameters. Instead of calling this, you can call DXUTCreateDevice()
// or DXUTSetDevice()
//--------------------------------------------------------------------------------------
HRESULT DXUTCreateDeviceFromSettings( DXUTDeviceSettings* pDeviceSettings, bool bPreserveInput )
{
HRESULT hr;
GetDXUTState().SetDeviceCreateCalled( true );
// If DXUTCreateWindow() or DXUTSetWindow() has not already been called,
// then call DXUTCreateWindow() with the default parameters.
if( !GetDXUTState().GetWindowCreated() )
{
// If DXUTCreateWindow() or DXUTSetWindow() was already called and failed, then fail.
// DXUTCreateWindow() or DXUTSetWindow() must first succeed for this function to succeed
if( GetDXUTState().GetWindowCreateCalled() )
return E_FAIL;
// If DXUTCreateWindow() or DXUTSetWindow() hasn't been called, then
// automatically call DXUTCreateWindow() with default params
hr = DXUTCreateWindow();
if( FAILED(hr) )
return hr;
}
if( !bPreserveInput )
{
// If not preserving the input, then find the closest valid to it
DXUTMatchOptions matchOptions;
matchOptions.eAdapterOrdinal = DXUTMT_CLOSEST_TO_INPUT;
matchOptions.eDeviceType = DXUTMT_CLOSEST_TO_INPUT;
matchOptions.eWindowed = DXUTMT_CLOSEST_TO_INPUT;
matchOptions.eAdapterFormat = DXUTMT_CLOSEST_TO_INPUT;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -