📄 dxut.cpp
字号:
GET_SET_ACCESSOR( CGrowableArray<DXUT_TIMER>*, TimerList );
GET_ACCESSOR( bool*, Keys );
GET_ACCESSOR( bool*, MouseButtons );
GET_ACCESSOR( WCHAR*, StaticFrameStats );
GET_ACCESSOR( WCHAR*, FrameStats );
GET_ACCESSOR( WCHAR*, DeviceStats );
GET_ACCESSOR( WCHAR*, WindowTitle );
};
//--------------------------------------------------------------------------------------
// Global state class
//--------------------------------------------------------------------------------------
DXUTState& GetDXUTState()
{
// Using an accessor function gives control of the construction order
static DXUTState state;
return state;
}
//--------------------------------------------------------------------------------------
// Internal functions forward declarations
//--------------------------------------------------------------------------------------
typedef IDirect3D9* (WINAPI* LPDIRECT3DCREATE9)(UINT SDKVersion);
typedef DECLSPEC_IMPORT UINT (WINAPI* LPTIMEBEGINPERIOD)( UINT uPeriod );
int DXUTMapButtonToArrayIndex( BYTE vButton );
void DXUTParseCommandLine();
CD3DEnumeration* DXUTPrepareEnumerationObject( bool bEnumerate = false );
CD3DSettingsDlg* DXUTPrepareSettingsDialog();
void DXUTBuildOptimalDeviceSettings( DXUTDeviceSettings* pOptimalDeviceSettings, DXUTDeviceSettings* pDeviceSettingsIn, DXUTMatchOptions* pMatchOptions );
bool DXUTDoesDeviceComboMatchPreserveOptions( CD3DEnumDeviceSettingsCombo* pDeviceSettingsCombo, DXUTDeviceSettings* pDeviceSettingsIn, DXUTMatchOptions* pMatchOptions );
float DXUTRankDeviceCombo( CD3DEnumDeviceSettingsCombo* pDeviceSettingsCombo, DXUTDeviceSettings* pDeviceSettingsIn, D3DDISPLAYMODE* pAdapterDesktopDisplayMode );
void DXUTBuildValidDeviceSettings( DXUTDeviceSettings* pDeviceSettings, CD3DEnumDeviceSettingsCombo* pBestDeviceSettingsCombo, DXUTDeviceSettings* pDeviceSettingsIn, DXUTMatchOptions* pMatchOptions );
HRESULT DXUTFindValidResolution( CD3DEnumDeviceSettingsCombo* pBestDeviceSettingsCombo, D3DDISPLAYMODE displayModeIn, D3DDISPLAYMODE* pBestDisplayMode );
HRESULT DXUTFindAdapterFormat( UINT AdapterOrdinal, D3DDEVTYPE DeviceType, D3DFORMAT BackBufferFormat, BOOL Windowed, D3DFORMAT* pAdapterFormat );
HRESULT DXUTChangeDevice( DXUTDeviceSettings* pNewDeviceSettings, IDirect3DDevice9* pd3dDeviceFromApp, bool bForceRecreate );
void DXUTUpdateDeviceSettingsWithOverrides( DXUTDeviceSettings* pNewDeviceSettings );
HRESULT DXUTInitialize3DEnvironment();
void DXUTPrepareDevice( IDirect3DDevice9* pd3dDevice );
HRESULT DXUTReset3DEnvironment();
void DXUTRender3DEnvironment();
void DXUTCleanup3DEnvironment( bool bReleaseSettings = true );
void DXUTHandlePossibleSizeChange();
void DXUTAdjustWindowStyle( HWND hWnd, bool bWindowed );
void DXUTUpdateFrameStats();
void DXUTUpdateDeviceStats( D3DDEVTYPE DeviceType, DWORD BehaviorFlags, D3DADAPTER_IDENTIFIER9* pAdapterIdentifier );
void DXUTUpdateStaticFrameStats();
void DXUTHandleTimers();
bool DXUTGetCmdParam( WCHAR*& strCmdLine, WCHAR* strFlag, int nFlagLen );
void DXUTDisplayErrorMessage( HRESULT hr );
LRESULT CALLBACK DXUTStaticWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
UINT DXUTColorChannelBits( D3DFORMAT fmt );
UINT DXUTStencilBits( D3DFORMAT fmt );
UINT DXUTDepthBits( D3DFORMAT fmt );
void DXUTCheckForWindowMonitorChange();
HRESULT DXUTGetAdapterOrdinalFromMonitor( HMONITOR hMonitor, UINT* pAdapterOrdinal );
//--------------------------------------------------------------------------------------
// External callback setup functions
//--------------------------------------------------------------------------------------
void DXUTSetCallbackDeviceCreated( LPDXUTCALLBACKDEVICECREATED pCallbackDeviceCreated ) { GetDXUTState().SetDeviceCreatedFunc( pCallbackDeviceCreated ); }
void DXUTSetCallbackDeviceReset( LPDXUTCALLBACKDEVICERESET pCallbackDeviceReset ) { GetDXUTState().SetDeviceResetFunc( pCallbackDeviceReset ); }
void DXUTSetCallbackDeviceLost( LPDXUTCALLBACKDEVICELOST pCallbackDeviceLost ) { GetDXUTState().SetDeviceLostFunc( pCallbackDeviceLost ); }
void DXUTSetCallbackDeviceDestroyed( LPDXUTCALLBACKDEVICEDESTROYED pCallbackDeviceDestroyed ) { GetDXUTState().SetDeviceDestroyedFunc( pCallbackDeviceDestroyed ); }
void DXUTSetCallbackFrameMove( LPDXUTCALLBACKFRAMEMOVE pCallbackFrameMove ) { GetDXUTState().SetFrameMoveFunc( pCallbackFrameMove ); }
void DXUTSetCallbackFrameRender( LPDXUTCALLBACKFRAMERENDER pCallbackFrameRender ) { GetDXUTState().SetFrameRenderFunc( pCallbackFrameRender ); }
void DXUTSetCallbackKeyboard( LPDXUTCALLBACKKEYBOARD pCallbackKeyboard ) { GetDXUTState().SetKeyboardFunc( pCallbackKeyboard ); }
void DXUTSetCallbackMouse( LPDXUTCALLBACKMOUSE pCallbackMouse, bool bIncludeMouseMove ) { GetDXUTState().SetMouseFunc( pCallbackMouse ); GetDXUTState().SetNotifyOnMouseMove( bIncludeMouseMove ); }
void DXUTSetCallbackMsgProc( LPDXUTCALLBACKMSGPROC pCallbackMsgProc ) { GetDXUTState().SetWindowMsgFunc( pCallbackMsgProc ); }
//--------------------------------------------------------------------------------------
// Optionally parses the command line and sets if default hotkeys are handled
//
// Possible command line parameters are:
// -adapter:# forces app to use this adapter # (fails if the adapter doesn't exist)
// -windowed forces app to start windowed
// -fullscreen forces app to start full screen
// -forcehal forces app to use HAL (fails if HAL doesn't exist)
// -forceref forces app to use REF (fails if REF doesn't exist)
// -forcepurehwvp forces app to use pure HWVP (fails if device doesn't support it)
// -forcehwvp forces app to use HWVP (fails if device doesn't support it)
// -forceswvp forces app to use SWVP
// -width:# forces app to use # for width. for full screen, it will pick the closest possible supported mode
// -height:# forces app to use # for height. for full screen, it will pick the closest possible supported mode
// -startx:# forces app to use # for the x coord of the window position for windowed mode
// -starty:# forces app to use # for the y coord of the window position for windowed mode
// -constantframetime:# forces app to use constant frame time, where # is the time/frame in seconds
// -quitafterframe:x forces app to quit after # frames
// -noerrormsgboxes prevents the display of message boxes generated by the framework so the application can be run without user interaction
//
// Hotkeys handled by default are:
// ESC app exits
// Alt-Enter toggle between full screen & windowed
// F2 device selection dialog
// F3 toggle HAL/REF
// F8 toggle wire-frame mode
// Pause pauses time
//--------------------------------------------------------------------------------------
HRESULT DXUTInit( bool bParseCommandLine, bool bHandleDefaultHotkeys, bool bShowMsgBoxOnError )
{
GetDXUTState().SetDXUTInitCalled( true );
// Not always needed, but lets the app create GDI dialogs
InitCommonControls();
// Increase the accuracy of Sleep() without needing to link to winmm.lib
WCHAR wszPath[MAX_PATH+1];
if( !::GetSystemDirectory( wszPath, MAX_PATH+1 ) )
return E_FAIL;
lstrcatW( wszPath, L"\\winmm.dll" );
HINSTANCE hInstWinMM = LoadLibrary( wszPath );
if( hInstWinMM != NULL )
{
LPTIMEBEGINPERIOD pTimeBeginPeriod = (LPTIMEBEGINPERIOD)GetProcAddress( hInstWinMM, "timeBeginPeriod" );
if( NULL != pTimeBeginPeriod )
pTimeBeginPeriod(1);
}
FreeLibrary(hInstWinMM);
GetDXUTState().SetShowMsgBoxOnError( bShowMsgBoxOnError );
GetDXUTState().SetHandleDefaultHotkeys( bHandleDefaultHotkeys );
if( bParseCommandLine )
DXUTParseCommandLine();
// Verify D3DX version
if( !D3DXCheckVersion( D3D_SDK_VERSION, D3DX_SDK_VERSION ) )
{
DXUTDisplayErrorMessage( DXUTERR_INCORRECTVERSION );
return DXUT_ERR( L"D3DXCheckVersion", DXUTERR_INCORRECTVERSION );
}
// Create a Direct3D object if one has not already been created
IDirect3D9* pD3D = DXUTGetD3DObject();
if( pD3D == NULL )
{
// This may fail if DirectX 9 isn't installed
// This may fail if the DirectX headers are out of sync with the installed DirectX DLLs
pD3D = DXUT_Dynamic_Direct3DCreate9( D3D_SDK_VERSION );
GetDXUTState().SetD3D( pD3D );
}
if( pD3D == NULL )
{
// If still NULL, then something went wrong
DXUTDisplayErrorMessage( DXUTERR_NODIRECT3D );
return DXUT_ERR( L"Direct3DCreate9", DXUTERR_NODIRECT3D );
}
// Reset the timer
DXUTGetGlobalTimer()->Reset();
GetDXUTState().SetDXUTInited( true );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Parses the command line for parameters. See DXUTInit() for list
//--------------------------------------------------------------------------------------
void DXUTParseCommandLine()
{
WCHAR* strCmdLine = GetCommandLine();
// Skip past program name (first token in command line).
if (*strCmdLine == L'"') // Check for and handle quoted program name
{
strCmdLine++;
// Skip over until another double-quote or a null
while (*strCmdLine && (*strCmdLine != L'"'))
strCmdLine++;
// Skip over double-quote
if (*strCmdLine == L'"')
strCmdLine++;
}
else
{
// First token wasn't a quote
while (*strCmdLine > L' ')
strCmdLine++;
}
for(;;)
{
// Skip past any white space preceding the next token
while (*strCmdLine && (*strCmdLine <= L' '))
strCmdLine++;
if( *strCmdLine == 0 )
break;
WCHAR strFlag[256];
int nFlagLen = 0;
// Skip past the flag marker
if( *strCmdLine == L'/' ||
*strCmdLine == L'-' )
strCmdLine++;
// Compare the first N letters w/o regard to case
wcscpy( strFlag, L"adapter" ); nFlagLen = (int) wcslen(strFlag);
if( _wcsnicmp( strCmdLine, strFlag, nFlagLen ) == 0 )
{
if( DXUTGetCmdParam( strCmdLine, strFlag, nFlagLen ) )
{
int nAdapter = _wtoi(strFlag);
GetDXUTState().SetOverrideAdapterOrdinal( nAdapter );
}
continue;
}
wcscpy( strFlag, L"windowed" ); nFlagLen = (int) wcslen(strFlag);
if( _wcsnicmp( strCmdLine, strFlag, nFlagLen ) == 0 )
{
GetDXUTState().SetOverrideWindowed( true );
strCmdLine += nFlagLen;
continue;
}
wcscpy( strFlag, L"fullscreen" ); nFlagLen = (int) wcslen(strFlag);
if( _wcsnicmp( strCmdLine, strFlag, nFlagLen ) == 0 )
{
GetDXUTState().SetOverrideFullScreen( true );
strCmdLine += nFlagLen;
continue;
}
wcscpy( strFlag, L"forcehal" ); nFlagLen = (int) wcslen(strFlag);
if( _wcsnicmp( strCmdLine, strFlag, nFlagLen ) == 0 )
{
GetDXUTState().SetOverrideForceHAL( true );
strCmdLine += nFlagLen;
continue;
}
wcscpy( strFlag, L"forceref" ); nFlagLen = (int) wcslen(strFlag);
if( _wcsnicmp( strCmdLine, strFlag, nFlagLen ) == 0 )
{
GetDXUTState().SetOverrideForceREF( true );
strCmdLine += nFlagLen;
continue;
}
wcscpy( strFlag, L"forcepurehwvp" ); nFlagLen = (int) wcslen(strFlag);
if( _wcsnicmp( strCmdLine, strFlag, nFlagLen ) == 0 )
{
GetDXUTState().SetOverrideForcePureHWVP( true );
strCmdLine += nFlagLen;
continue;
}
wcscpy( strFlag, L"forcehwvp" ); nFlagLen = (int) wcslen(strFlag);
if( _wcsnicmp( strCmdLine, strFlag, nFlagLen ) == 0 )
{
GetDXUTState().SetOverrideForceHWVP( true );
strCmdLine += nFlagLen;
continue;
}
wcscpy( strFlag, L"forceswvp" ); nFlagLen = (int) wcslen(strFlag);
if( _wcsnicmp( strCmdLine, strFlag, nFlagLen ) == 0 )
{
GetDXUTState().SetOverrideForceSWVP( true );
strCmdLine += nFlagLen;
continue;
}
wcscpy( strFlag, L"width" ); nFlagLen = (int) wcslen(strFlag);
if( _wcsnicmp( strCmdLine, strFlag, nFlagLen ) == 0 )
{
if( DXUTGetCmdParam( strCmdLine, strFlag, nFlagLen ) )
{
int nWidth = _wtoi(strFlag);
GetDXUTState().SetOverrideWidth( nWidth );
}
continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -