DXUT.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:227k
源码类别:
DirextX编程
开发平台:
Visual C++
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Internal helper function to return the adapter format from the first device settings
- // combo that matches the passed adapter ordinal, device type, backbuffer format, and windowed.
- //--------------------------------------------------------------------------------------
- HRESULT DXUTFindAdapterFormat( UINT AdapterOrdinal, D3DDEVTYPE DeviceType, D3DFORMAT BackBufferFormat,
- BOOL Windowed, D3DFORMAT* pAdapterFormat )
- {
- CD3DEnumeration* pd3dEnum = DXUTPrepareEnumerationObject();
- CD3DEnumDeviceInfo* pDeviceInfo = pd3dEnum->GetDeviceInfo( AdapterOrdinal, DeviceType );
- if( pDeviceInfo )
- {
- for( int iDeviceCombo=0; iDeviceCombo<pDeviceInfo->deviceSettingsComboList.GetSize(); iDeviceCombo++ )
- {
- CD3DEnumDeviceSettingsCombo* pDeviceSettingsCombo = pDeviceInfo->deviceSettingsComboList.GetAt(iDeviceCombo);
- if( pDeviceSettingsCombo->BackBufferFormat == BackBufferFormat &&
- pDeviceSettingsCombo->Windowed == Windowed )
- {
- // Return the adapter format from the first match
- *pAdapterFormat = pDeviceSettingsCombo->AdapterFormat;
- return S_OK;
- }
- }
- }
- *pAdapterFormat = BackBufferFormat;
- return E_FAIL;
- }
- //--------------------------------------------------------------------------------------
- // Change to a Direct3D device created from the device settings or passed in.
- // The framework will only reset if the device is similar to the previous device
- // otherwise it will cleanup the previous device (if there is one) and recreate the
- // scene using the app's device callbacks.
- //--------------------------------------------------------------------------------------
- HRESULT DXUTChangeDevice( DXUTDeviceSettings* pNewDeviceSettings, IDirect3DDevice9* pd3dDeviceFromApp, bool bForceRecreate, bool bClipWindowToSingleAdapter )
- {
- HRESULT hr;
- DXUTDeviceSettings* pOldDeviceSettings = GetDXUTState().GetCurrentDeviceSettings();
- if( DXUTGetD3DObject() == NULL )
- return S_FALSE;
- // Make a copy of the pNewDeviceSettings on the heap
- DXUTDeviceSettings* pNewDeviceSettingsOnHeap = new DXUTDeviceSettings;
- if( pNewDeviceSettingsOnHeap == NULL )
- return E_OUTOFMEMORY;
- memcpy( pNewDeviceSettingsOnHeap, pNewDeviceSettings, sizeof(DXUTDeviceSettings) );
- pNewDeviceSettings = pNewDeviceSettingsOnHeap;
- // If the ModifyDeviceSettings callback is non-NULL, then call it to let the app
- // change the settings or reject the device change by returning false.
- LPDXUTCALLBACKMODIFYDEVICESETTINGS pCallbackModifyDeviceSettings = GetDXUTState().GetModifyDeviceSettingsFunc();
- if( pCallbackModifyDeviceSettings )
- {
- D3DCAPS9 caps;
- IDirect3D9* pD3D = DXUTGetD3DObject();
- pD3D->GetDeviceCaps( pNewDeviceSettings->AdapterOrdinal, pNewDeviceSettings->DeviceType, &caps );
- bool bContinue = pCallbackModifyDeviceSettings( pNewDeviceSettings, &caps, GetDXUTState().GetModifyDeviceSettingsFuncUserContext() );
- if( !bContinue )
- {
- // The app rejected the device change by returning false, so just use the current device if there is one.
- if( pOldDeviceSettings == NULL )
- DXUTDisplayErrorMessage( DXUTERR_NOCOMPATIBLEDEVICES );
- SAFE_DELETE( pNewDeviceSettings );
- return E_ABORT;
- }
- if( GetDXUTState().GetD3D() == NULL )
- {
- SAFE_DELETE( pNewDeviceSettings );
- return S_FALSE;
- }
- }
- GetDXUTState().SetCurrentDeviceSettings( pNewDeviceSettings );
- DXUTPause( true, true );
- // When a WM_SIZE message is received, it calls DXUTCheckForWindowSizeChange().
- // A WM_SIZE message might be sent when adjusting the window, so tell
- // DXUTCheckForWindowSizeChange() to ignore size changes temporarily
- GetDXUTState().SetIgnoreSizeChange( true );
- // Update thread safety on/off depending on Direct3D device's thread safety
- g_bThreadSafe = ((pNewDeviceSettings->BehaviorFlags & D3DCREATE_MULTITHREADED) != 0 );
- // Only apply the cmd line overrides if this is the first device created
- // and DXUTSetDevice() isn't used
- if( NULL == pd3dDeviceFromApp && NULL == pOldDeviceSettings )
- {
- // Updates the device settings struct based on the cmd line args.
- // Warning: if the device doesn't support these new settings then CreateDevice() will fail.
- DXUTUpdateDeviceSettingsWithOverrides( pNewDeviceSettings );
- }
- // Take note if the backbuffer width & height are 0 now as they will change after pd3dDevice->Reset()
- bool bKeepCurrentWindowSize = false;
- if( pNewDeviceSettings->pp.BackBufferWidth == 0 && pNewDeviceSettings->pp.BackBufferHeight == 0 )
- bKeepCurrentWindowSize = true;
- //////////////////////////
- // Before reset
- /////////////////////////
- if( pNewDeviceSettings->pp.Windowed )
- {
- // Going to windowed mode
- if( pOldDeviceSettings && !pOldDeviceSettings->pp.Windowed )
- {
- // Going from fullscreen -> windowed
- GetDXUTState().SetFullScreenBackBufferWidthAtModeChange( pOldDeviceSettings->pp.BackBufferWidth );
- GetDXUTState().SetFullScreenBackBufferHeightAtModeChange( pOldDeviceSettings->pp.BackBufferHeight );
- // Restore windowed mode style
- SetWindowLong( DXUTGetHWNDDeviceWindowed(), GWL_STYLE, GetDXUTState().GetWindowedStyleAtModeChange() );
- }
- // If different device windows are used for windowed mode and fullscreen mode,
- // hide the fullscreen window so that it doesn't obscure the screen.
- if( DXUTGetHWNDDeviceFullScreen() != DXUTGetHWNDDeviceWindowed() )
- ShowWindow( DXUTGetHWNDDeviceFullScreen(), SW_HIDE );
- // If using the same window for windowed and fullscreen mode, reattach menu if one exists
- if( DXUTGetHWNDDeviceFullScreen() == DXUTGetHWNDDeviceWindowed() )
- {
- if( GetDXUTState().GetMenu() != NULL )
- SetMenu( DXUTGetHWNDDeviceWindowed(), GetDXUTState().GetMenu() );
- }
- }
- else
- {
- // Going to fullscreen mode
- if( pOldDeviceSettings == NULL || (pOldDeviceSettings && pOldDeviceSettings->pp.Windowed) )
- {
- // Transistioning to full screen mode from a standard window so
- // save current window position/size/style now in case the user toggles to windowed mode later
- WINDOWPLACEMENT* pwp = GetDXUTState().GetWindowedPlacement();
- ZeroMemory( pwp, sizeof(WINDOWPLACEMENT) );
- pwp->length = sizeof(WINDOWPLACEMENT);
- GetWindowPlacement( DXUTGetHWNDDeviceWindowed(), pwp );
- bool bIsTopmost = ( (GetWindowLong(DXUTGetHWNDDeviceWindowed(),GWL_EXSTYLE) & WS_EX_TOPMOST) != 0);
- GetDXUTState().SetTopmostWhileWindowed( bIsTopmost );
- DWORD dwStyle = GetWindowLong( DXUTGetHWNDDeviceWindowed(), GWL_STYLE );
- dwStyle &= ~WS_MAXIMIZE & ~WS_MINIMIZE; // remove minimize/maximize style
- GetDXUTState().SetWindowedStyleAtModeChange( dwStyle );
- if( pOldDeviceSettings )
- {
- GetDXUTState().SetWindowBackBufferWidthAtModeChange( pOldDeviceSettings->pp.BackBufferWidth );
- GetDXUTState().SetWindowBackBufferHeightAtModeChange( pOldDeviceSettings->pp.BackBufferHeight );
- }
- }
- // Hide the window to avoid animation of blank windows
- ShowWindow( DXUTGetHWNDDeviceFullScreen(), SW_HIDE );
- // Set FS window style
- SetWindowLong( DXUTGetHWNDDeviceFullScreen(), GWL_STYLE, WS_POPUP|WS_SYSMENU );
- // If using the same window for windowed and fullscreen mode, save and remove menu
- if( DXUTGetHWNDDeviceFullScreen() == DXUTGetHWNDDeviceWindowed() )
- {
- HMENU hMenu = GetMenu( DXUTGetHWNDDeviceFullScreen() );
- GetDXUTState().SetMenu( hMenu );
- SetMenu( DXUTGetHWNDDeviceFullScreen(), NULL );
- }
- WINDOWPLACEMENT wpFullscreen;
- ZeroMemory( &wpFullscreen, sizeof(WINDOWPLACEMENT) );
- wpFullscreen.length = sizeof(WINDOWPLACEMENT);
- GetWindowPlacement( DXUTGetHWNDDeviceFullScreen(), &wpFullscreen );
- if( (wpFullscreen.flags & WPF_RESTORETOMAXIMIZED) != 0 )
- {
- // Restore the window to normal if the window was maximized then minimized. This causes the
- // WPF_RESTORETOMAXIMIZED flag to be set which will cause SW_RESTORE to restore the
- // window from minimized to maxmized which isn't what we want
- wpFullscreen.flags &= ~WPF_RESTORETOMAXIMIZED;
- wpFullscreen.showCmd = SW_RESTORE;
- SetWindowPlacement( DXUTGetHWNDDeviceFullScreen(), &wpFullscreen );
- }
- }
- // If AdapterOrdinal and DeviceType are the same, we can just do a Reset().
- // If they've changed, we need to do a complete device tear down/rebuild.
- // Also only allow a reset if pd3dDevice is the same as the current device
- if( !bForceRecreate &&
- (pd3dDeviceFromApp == NULL || pd3dDeviceFromApp == DXUTGetD3DDevice()) &&
- DXUTGetD3DDevice() &&
- pOldDeviceSettings &&
- pOldDeviceSettings->AdapterOrdinal == pNewDeviceSettings->AdapterOrdinal &&
- pOldDeviceSettings->DeviceType == pNewDeviceSettings->DeviceType &&
- pOldDeviceSettings->BehaviorFlags == pNewDeviceSettings->BehaviorFlags )
- {
- // Reset the Direct3D device and call the app's device callbacks
- hr = DXUTReset3DEnvironment();
- if( FAILED(hr) )
- {
- if( D3DERR_DEVICELOST == hr )
- {
- // The device is lost, just mark it as so and continue on with
- // capturing the state and resizing the window/etc.
- GetDXUTState().SetDeviceLost( true );
- }
- else if( DXUTERR_RESETTINGDEVICEOBJECTS == hr ||
- DXUTERR_MEDIANOTFOUND == hr )
- {
- // Something bad happened in the app callbacks
- SAFE_DELETE( pOldDeviceSettings );
- DXUTDisplayErrorMessage( hr );
- DXUTShutdown();
- return hr;
- }
- else // DXUTERR_RESETTINGDEVICE
- {
- // Reset failed and the device wasn't lost and it wasn't the apps fault,
- // so recreate the device to try to recover
- GetDXUTState().SetCurrentDeviceSettings( pOldDeviceSettings );
- if( FAILED( DXUTChangeDevice( pNewDeviceSettings, pd3dDeviceFromApp, true, bClipWindowToSingleAdapter ) ) )
- {
- // If that fails, then shutdown
- SAFE_DELETE( pOldDeviceSettings );
- DXUTShutdown();
- return DXUTERR_CREATINGDEVICE;
- }
- else
- {
- SAFE_DELETE( pOldDeviceSettings );
- return S_OK;
- }
- }
- }
- }
- else
- {
- // Cleanup if not first device created
- if( pOldDeviceSettings )
- DXUTCleanup3DEnvironment( false );
- // Create the D3D device and call the app's device callbacks
- hr = DXUTCreate3DEnvironment( pd3dDeviceFromApp );
- if( FAILED(hr) )
- {
- SAFE_DELETE( pOldDeviceSettings );
- DXUTCleanup3DEnvironment();
- DXUTDisplayErrorMessage( hr );
- DXUTPause( false, false );
- GetDXUTState().SetIgnoreSizeChange( false );
- return hr;
- }
- }
- // Enable/disable StickKeys shortcut, ToggleKeys shortcut, FilterKeys shortcut, and Windows key
- // to prevent accidental task switching
- DXUTAllowShortcutKeys( ( pNewDeviceSettings->pp.Windowed ) ? GetDXUTState().GetAllowShortcutKeysWhenWindowed() : GetDXUTState().GetAllowShortcutKeysWhenFullscreen() );
- IDirect3D9* pD3D = DXUTGetD3DObject();
- HMONITOR hAdapterMonitor = pD3D->GetAdapterMonitor( pNewDeviceSettings->AdapterOrdinal );
- GetDXUTState().SetAdapterMonitor( hAdapterMonitor );
- // Update the device stats text
- DXUTUpdateStaticFrameStats();
- if( pOldDeviceSettings && !pOldDeviceSettings->pp.Windowed && pNewDeviceSettings->pp.Windowed )
- {
- // Going from fullscreen -> windowed
- // Restore the show state, and positions/size of the window to what it was
- // It is important to adjust the window size
- // after resetting the device rather than beforehand to ensure
- // that the monitor resolution is correct and does not limit the size of the new window.
- WINDOWPLACEMENT* pwp = GetDXUTState().GetWindowedPlacement();
- SetWindowPlacement( DXUTGetHWNDDeviceWindowed(), pwp );
- // Also restore the z-order of window to previous state
- HWND hWndInsertAfter = GetDXUTState().GetTopmostWhileWindowed() ? HWND_TOPMOST : HWND_NOTOPMOST;
- SetWindowPos( DXUTGetHWNDDeviceWindowed(), hWndInsertAfter, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOREDRAW|SWP_NOSIZE );
- }
- // Check to see if the window needs to be resized.
- // Handle cases where the window is minimized and maxmimized as well.
- bool bNeedToResize = false;
- if( pNewDeviceSettings->pp.Windowed && // only resize if in windowed mode
- !bKeepCurrentWindowSize ) // only resize if pp.BackbufferWidth/Height were not 0
- {
- UINT nClientWidth;
- UINT nClientHeight;
- if( IsIconic(DXUTGetHWNDDeviceWindowed()) )
- {
- // Window is currently minimized. To tell if it needs to resize,
- // get the client rect of window when its restored the
- // hard way using GetWindowPlacement()
- WINDOWPLACEMENT wp;
- ZeroMemory( &wp, sizeof(WINDOWPLACEMENT) );
- wp.length = sizeof(WINDOWPLACEMENT);
- GetWindowPlacement( DXUTGetHWNDDeviceWindowed(), &wp );
- if( (wp.flags & WPF_RESTORETOMAXIMIZED) != 0 && wp.showCmd == SW_SHOWMINIMIZED )
- {
- // WPF_RESTORETOMAXIMIZED means that when the window is restored it will
- // be maximized. So maximize the window temporarily to get the client rect
- // when the window is maximized. GetSystemMetrics( SM_CXMAXIMIZED ) will give this
- // information if the window is on the primary but this will work on multimon.
- ShowWindow( DXUTGetHWNDDeviceWindowed(), SW_RESTORE );
- RECT rcClient;
- GetClientRect( DXUTGetHWNDDeviceWindowed(), &rcClient );
- nClientWidth = (UINT)(rcClient.right - rcClient.left);
- nClientHeight = (UINT)(rcClient.bottom - rcClient.top);
- ShowWindow( DXUTGetHWNDDeviceWindowed(), SW_MINIMIZE );
- }
- else
- {
- // Use wp.rcNormalPosition to get the client rect, but wp.rcNormalPosition
- // includes the window frame so subtract it
- RECT rcFrame = {0};
- AdjustWindowRect( &rcFrame, GetDXUTState().GetWindowedStyleAtModeChange(), GetDXUTState().GetMenu() != NULL );
- LONG nFrameWidth = rcFrame.right - rcFrame.left;
- LONG nFrameHeight = rcFrame.bottom - rcFrame.top;
- nClientWidth = (UINT)(wp.rcNormalPosition.right - wp.rcNormalPosition.left - nFrameWidth);
- nClientHeight = (UINT)(wp.rcNormalPosition.bottom - wp.rcNormalPosition.top - nFrameHeight);
- }
- }
- else
- {
- // Window is restored or maximized so just get its client rect
- RECT rcClient;
- GetClientRect( DXUTGetHWNDDeviceWindowed(), &rcClient );
- nClientWidth = (UINT)(rcClient.right - rcClient.left);
- nClientHeight = (UINT)(rcClient.bottom - rcClient.top);
- }
- // Now that we know the client rect, compare it against the back buffer size
- // to see if the client rect is already the right size
- if( nClientWidth != pNewDeviceSettings->pp.BackBufferWidth ||
- nClientHeight != pNewDeviceSettings->pp.BackBufferHeight )
- {
- bNeedToResize = true;
- }
- if( bClipWindowToSingleAdapter && !IsIconic(DXUTGetHWNDDeviceWindowed()) )
- {
- // Get the rect of the monitor attached to the adapter
- MONITORINFO miAdapter;
- miAdapter.cbSize = sizeof(MONITORINFO);
- HMONITOR hAdapterMonitor = DXUTGetD3DObject()->GetAdapterMonitor( pNewDeviceSettings->AdapterOrdinal );
- DXUTGetMonitorInfo( hAdapterMonitor, &miAdapter );
- HMONITOR hWindowMonitor = DXUTMonitorFromWindow( DXUTGetHWND(), MONITOR_DEFAULTTOPRIMARY );
- // Get the rect of the window
- RECT rcWindow;
- GetWindowRect( DXUTGetHWNDDeviceWindowed(), &rcWindow );
- // Check if the window rect is fully inside the adapter's vitural screen rect
- if( (rcWindow.left < miAdapter.rcWork.left ||
- rcWindow.right > miAdapter.rcWork.right ||
- rcWindow.top < miAdapter.rcWork.top ||
- rcWindow.bottom > miAdapter.rcWork.bottom) )
- {
- if( hWindowMonitor == hAdapterMonitor && IsZoomed(DXUTGetHWNDDeviceWindowed()) )
- {
- // If the window is maximized and on the same monitor as the adapter, then
- // no need to clip to single adapter as the window is already clipped
- // even though the rcWindow rect is outside of the miAdapter.rcWork
- }
- else
- {
- bNeedToResize = true;
- }
- }
- }
- }
- // Only resize window if needed
- if( bNeedToResize )
- {
- // Need to resize, so if window is maximized or minimized then restore the window
- if( IsIconic(DXUTGetHWNDDeviceWindowed()) )
- ShowWindow( DXUTGetHWNDDeviceWindowed(), SW_RESTORE );
- if( IsZoomed(DXUTGetHWNDDeviceWindowed()) ) // doing the IsIconic() check first also handles the WPF_RESTORETOMAXIMIZED case
- ShowWindow( DXUTGetHWNDDeviceWindowed(), SW_RESTORE );
- if( bClipWindowToSingleAdapter )
- {
- // Get the rect of the monitor attached to the adapter
- MONITORINFO miAdapter;
- miAdapter.cbSize = sizeof(MONITORINFO);
- DXUTGetMonitorInfo( DXUTGetD3DObject()->GetAdapterMonitor( pNewDeviceSettings->AdapterOrdinal ), &miAdapter );
- // Get the rect of the monitor attached to the window
- MONITORINFO miWindow;
- miWindow.cbSize = sizeof(MONITORINFO);
- DXUTGetMonitorInfo( DXUTMonitorFromWindow( DXUTGetHWND(), MONITOR_DEFAULTTOPRIMARY ), &miWindow );
- // Do something reasonable if the BackBuffer size is greater than the monitor size
- int nAdapterMonitorWidth = miAdapter.rcWork.right - miAdapter.rcWork.left;
- int nAdapterMonitorHeight = miAdapter.rcWork.bottom - miAdapter.rcWork.top;
- int nClientWidth = pNewDeviceSettings->pp.BackBufferWidth;
- int nClientHeight = pNewDeviceSettings->pp.BackBufferHeight;
- // Get the rect of the window
- RECT rcWindow;
- GetWindowRect( DXUTGetHWNDDeviceWindowed(), &rcWindow );
- // Make a window rect with a client rect that is the same size as the backbuffer
- RECT rcResizedWindow;
- rcResizedWindow.left = 0;
- rcResizedWindow.right = nClientWidth;
- rcResizedWindow.top = 0;
- rcResizedWindow.bottom = nClientHeight;
- AdjustWindowRect( &rcResizedWindow, GetWindowLong( DXUTGetHWNDDeviceWindowed(), GWL_STYLE ), GetDXUTState().GetMenu() != NULL );
- int nWindowWidth = rcResizedWindow.right - rcResizedWindow.left;
- int nWindowHeight = rcResizedWindow.bottom - rcResizedWindow.top;
- if( nWindowWidth > nAdapterMonitorWidth )
- nWindowWidth = (nAdapterMonitorWidth - 0);
- if( nWindowHeight > nAdapterMonitorHeight )
- nWindowHeight = (nAdapterMonitorHeight - 0);
- if( rcResizedWindow.left < miAdapter.rcWork.left ||
- rcResizedWindow.top < miAdapter.rcWork.top ||
- rcResizedWindow.right > miAdapter.rcWork.right ||
- rcResizedWindow.bottom > miAdapter.rcWork.bottom )
- {
- int nWindowOffsetX = (nAdapterMonitorWidth - nWindowWidth) / 2;
- int nWindowOffsetY = (nAdapterMonitorHeight - nWindowHeight) / 2;
- rcResizedWindow.left = miAdapter.rcWork.left + nWindowOffsetX;
- rcResizedWindow.top = miAdapter.rcWork.top + nWindowOffsetY;
- rcResizedWindow.right = miAdapter.rcWork.left + nWindowOffsetX + nWindowWidth;
- rcResizedWindow.bottom = miAdapter.rcWork.top + nWindowOffsetY + nWindowHeight;
- }
- // Resize the window. It is important to adjust the window size
- // after resetting the device rather than beforehand to ensure
- // that the monitor resolution is correct and does not limit the size of the new window.
- SetWindowPos( DXUTGetHWNDDeviceWindowed(), 0, rcResizedWindow.left, rcResizedWindow.top, nWindowWidth, nWindowHeight, SWP_NOZORDER );
- }
- else
- {
- // Make a window rect with a client rect that is the same size as the backbuffer
- RECT rcWindow = {0};
- rcWindow.right = (long)(pNewDeviceSettings->pp.BackBufferWidth);
- rcWindow.bottom = (long)(pNewDeviceSettings->pp.BackBufferHeight);
- AdjustWindowRect( &rcWindow, GetWindowLong( DXUTGetHWNDDeviceWindowed(), GWL_STYLE ), GetDXUTState().GetMenu() != NULL );
- // Resize the window. It is important to adjust the window size
- // after resetting the device rather than beforehand to ensure
- // that the monitor resolution is correct and does not limit the size of the new window.
- int cx = (int)(rcWindow.right - rcWindow.left);
- int cy = (int)(rcWindow.bottom - rcWindow.top);
- SetWindowPos( DXUTGetHWNDDeviceWindowed(), 0, 0, 0, cx, cy, SWP_NOZORDER|SWP_NOMOVE );
- }
- // Its possible that the new window size is not what we asked for.
- // No window can be sized larger than the desktop, so see see if the Windows OS resized the
- // window to something smaller to fit on the desktop. Also if WM_GETMINMAXINFO
- // will put a limit on the smallest/largest window size.
- RECT rcClient;
- GetClientRect( DXUTGetHWNDDeviceWindowed(), &rcClient );
- UINT nClientWidth = (UINT)(rcClient.right - rcClient.left);
- UINT nClientHeight = (UINT)(rcClient.bottom - rcClient.top);
- if( nClientWidth != pNewDeviceSettings->pp.BackBufferWidth ||
- nClientHeight != pNewDeviceSettings->pp.BackBufferHeight )
- {
- // If its different, then resize the backbuffer again. This time create a backbuffer that matches the
- // client rect of the current window w/o resizing the window.
- DXUTDeviceSettings deviceSettings = DXUTGetDeviceSettings();
- deviceSettings.pp.BackBufferWidth = 0;
- deviceSettings.pp.BackBufferHeight = 0;
- hr = DXUTChangeDevice( &deviceSettings, NULL, false, bClipWindowToSingleAdapter );
- if( FAILED( hr ) )
- {
- SAFE_DELETE( pOldDeviceSettings );
- DXUTCleanup3DEnvironment();
- DXUTPause( false, false );
- GetDXUTState().SetIgnoreSizeChange( false );
- return hr;
- }
- }
- }
- // Make the window visible
- if( !IsWindowVisible( DXUTGetHWND() ) )
- ShowWindow( DXUTGetHWND(), SW_SHOW );
- // Make the window visible
- if( !IsWindowVisible( DXUTGetHWND() ) )
- ShowWindow( DXUTGetHWND(), SW_SHOW );
- // Ensure that the display doesn't power down when fullscreen but does when windowed
- if( !DXUTIsWindowed() )
- SetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
- else
- SetThreadExecutionState( ES_CONTINUOUS );
- SAFE_DELETE( pOldDeviceSettings );
- GetDXUTState().SetIgnoreSizeChange( false );
- DXUTPause( false, false );
- GetDXUTState().SetDeviceCreated( true );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Low level keyboard hook to disable Windows key to prevent accidental task switching.
- //--------------------------------------------------------------------------------------
- LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )
- {
- if (nCode < 0 || nCode != HC_ACTION) // do not process message
- return CallNextHookEx( GetDXUTState().GetKeyboardHook(), nCode, wParam, lParam);
- bool bEatKeystroke = false;
- KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*)lParam;
- switch (wParam)
- {
- case WM_KEYDOWN:
- case WM_KEYUP:
- {
- bEatKeystroke = ( !GetDXUTState().GetAllowShortcutKeys() &&
- (p->vkCode == VK_LWIN || p->vkCode == VK_RWIN) );
- break;
- }
- }
- if( bEatKeystroke )
- return 1;
- else
- return CallNextHookEx( GetDXUTState().GetKeyboardHook(), nCode, wParam, lParam );
- }
- //--------------------------------------------------------------------------------------
- // Controls how DXUT behaves when fullscreen and windowed mode with regard to
- // shortcut keys (Windows keys, StickyKeys shortcut, ToggleKeys shortcut, FilterKeys shortcut)
- //--------------------------------------------------------------------------------------
- void DXUTSetShortcutKeySettings( bool bAllowWhenFullscreen, bool bAllowWhenWindowed )
- {
- GetDXUTState().SetAllowShortcutKeysWhenWindowed( bAllowWhenWindowed );
- GetDXUTState().SetAllowShortcutKeysWhenFullscreen( bAllowWhenFullscreen );
- // DXUTInit() records initial accessibility states so don't change them until then
- if( GetDXUTState().GetDXUTInited() )
- {
- if( DXUTIsWindowed() )
- DXUTAllowShortcutKeys( GetDXUTState().GetAllowShortcutKeysWhenWindowed() );
- else
- DXUTAllowShortcutKeys( GetDXUTState().GetAllowShortcutKeysWhenFullscreen() );
- }
- }
- //--------------------------------------------------------------------------------------
- // Enables/disables Windows keys, and disables or restores the StickyKeys/ToggleKeys/FilterKeys
- // shortcut to help prevent accidental task switching
- //--------------------------------------------------------------------------------------
- void DXUTAllowShortcutKeys( bool bAllowKeys )
- {
- GetDXUTState().SetAllowShortcutKeys( bAllowKeys );
- if( bAllowKeys )
- {
- // Restore StickyKeys/etc to original state and enable Windows key
- STICKYKEYS sk = GetDXUTState().GetStartupStickyKeys();
- TOGGLEKEYS tk = GetDXUTState().GetStartupToggleKeys();
- FILTERKEYS fk = GetDXUTState().GetStartupFilterKeys();
- SystemParametersInfo(SPI_SETSTICKYKEYS, sizeof(STICKYKEYS), &sk, 0);
- SystemParametersInfo(SPI_SETTOGGLEKEYS, sizeof(TOGGLEKEYS), &tk, 0);
- SystemParametersInfo(SPI_SETFILTERKEYS, sizeof(FILTERKEYS), &fk, 0);
- // Remove the keyboard hoook when it isn't needed to prevent any slow down of other apps
- if( GetDXUTState().GetKeyboardHook() )
- {
- UnhookWindowsHookEx( GetDXUTState().GetKeyboardHook() );
- GetDXUTState().SetKeyboardHook( NULL );
- }
- }
- else
- {
- // Set low level keyboard hook if haven't already
- if( GetDXUTState().GetKeyboardHook() == NULL )
- {
- // Set the low-level hook procedure. Only works on Windows 2000 and above
- OSVERSIONINFO OSVersionInfo;
- OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVersionInfo);
- GetVersionEx(&OSVersionInfo);
- if( OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT && OSVersionInfo.dwMajorVersion > 4 )
- {
- HHOOK hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0 );
- GetDXUTState().SetKeyboardHook( hKeyboardHook );
- }
- }
- // Disable StickyKeys/etc shortcuts but if the accessibility feature is on,
- // then leave the settings alone as its probably being usefully used
- STICKYKEYS skOff = GetDXUTState().GetStartupStickyKeys();
- if( (skOff.dwFlags & SKF_STICKYKEYSON) == 0 )
- {
- // Disable the hotkey and the confirmation
- skOff.dwFlags &= ~SKF_HOTKEYACTIVE;
- skOff.dwFlags &= ~SKF_CONFIRMHOTKEY;
- SystemParametersInfo(SPI_SETSTICKYKEYS, sizeof(STICKYKEYS), &skOff, 0);
- }
- TOGGLEKEYS tkOff = GetDXUTState().GetStartupToggleKeys();
- if( (tkOff.dwFlags & TKF_TOGGLEKEYSON) == 0 )
- {
- // Disable the hotkey and the confirmation
- tkOff.dwFlags &= ~TKF_HOTKEYACTIVE;
- tkOff.dwFlags &= ~TKF_CONFIRMHOTKEY;
- SystemParametersInfo(SPI_SETTOGGLEKEYS, sizeof(TOGGLEKEYS), &tkOff, 0);
- }
- FILTERKEYS fkOff = GetDXUTState().GetStartupFilterKeys();
- if( (fkOff.dwFlags & FKF_FILTERKEYSON) == 0 )
- {
- // Disable the hotkey and the confirmation
- fkOff.dwFlags &= ~FKF_HOTKEYACTIVE;
- fkOff.dwFlags &= ~FKF_CONFIRMHOTKEY;
- SystemParametersInfo(SPI_SETFILTERKEYS, sizeof(FILTERKEYS), &fkOff, 0);
- }
- }
- }
- //--------------------------------------------------------------------------------------
- // Updates the device settings struct based on the cmd line args.
- //--------------------------------------------------------------------------------------
- void DXUTUpdateDeviceSettingsWithOverrides( DXUTDeviceSettings* pDeviceSettings )
- {
- if( GetDXUTState().GetOverrideAdapterOrdinal() != -1 )
- pDeviceSettings->AdapterOrdinal = GetDXUTState().GetOverrideAdapterOrdinal();
- if( GetDXUTState().GetOverrideFullScreen() )
- pDeviceSettings->pp.Windowed = false;
- if( GetDXUTState().GetOverrideWindowed() )
- pDeviceSettings->pp.Windowed = true;
- if( GetDXUTState().GetOverrideForceREF() )
- pDeviceSettings->DeviceType = D3DDEVTYPE_REF;
- else if( GetDXUTState().GetOverrideForceHAL() )
- pDeviceSettings->DeviceType = D3DDEVTYPE_HAL;
- if( GetDXUTState().GetOverrideWidth() != 0 )
- pDeviceSettings->pp.BackBufferWidth = GetDXUTState().GetOverrideWidth();
- if( GetDXUTState().GetOverrideHeight() != 0 )
- pDeviceSettings->pp.BackBufferHeight = GetDXUTState().GetOverrideHeight();
- if( GetDXUTState().GetOverrideForcePureHWVP() )
- {
- pDeviceSettings->BehaviorFlags &= ~D3DCREATE_SOFTWARE_VERTEXPROCESSING;
- pDeviceSettings->BehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
- pDeviceSettings->BehaviorFlags |= D3DCREATE_PUREDEVICE;
- }
- else if( GetDXUTState().GetOverrideForceHWVP() )
- {
- pDeviceSettings->BehaviorFlags &= ~D3DCREATE_SOFTWARE_VERTEXPROCESSING;
- pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE;
- pDeviceSettings->BehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
- }
- else if( GetDXUTState().GetOverrideForceSWVP() )
- {
- pDeviceSettings->BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
- pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE;
- pDeviceSettings->BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
- }
- }
- //--------------------------------------------------------------------------------------
- // Creates the 3D environment
- //--------------------------------------------------------------------------------------
- HRESULT DXUTCreate3DEnvironment( IDirect3DDevice9* pd3dDeviceFromApp )
- {
- HRESULT hr = S_OK;
- IDirect3DDevice9* pd3dDevice = NULL;
- DXUTDeviceSettings* pNewDeviceSettings = GetDXUTState().GetCurrentDeviceSettings();
- // Only create a Direct3D device if one hasn't been supplied by the app
- if( pd3dDeviceFromApp == NULL )
- {
- // Try to create the device with the chosen settings
- IDirect3D9* pD3D = DXUTGetD3DObject();
- hr = pD3D->CreateDevice( pNewDeviceSettings->AdapterOrdinal, pNewDeviceSettings->DeviceType,
- DXUTGetHWNDFocus(), pNewDeviceSettings->BehaviorFlags,
- &pNewDeviceSettings->pp, &pd3dDevice );
- if( hr == D3DERR_DEVICELOST )
- {
- GetDXUTState().SetDeviceLost( true );
- return S_OK;
- }
- else if( FAILED(hr) )
- {
- DXUT_ERR( L"CreateDevice", hr );
- return DXUTERR_CREATINGDEVICE;
- }
- }
- else
- {
- pd3dDeviceFromApp->AddRef();
- pd3dDevice = pd3dDeviceFromApp;
- }
- GetDXUTState().SetD3DDevice( pd3dDevice );
- // If switching to REF, set the exit code to 11. If switching to HAL and exit code was 11, then set it back to 0.
- if( pNewDeviceSettings->DeviceType == D3DDEVTYPE_REF && GetDXUTState().GetExitCode() == 0 )
- GetDXUTState().SetExitCode(11);
- else if( pNewDeviceSettings->DeviceType == D3DDEVTYPE_HAL && GetDXUTState().GetExitCode() == 11 )
- GetDXUTState().SetExitCode(0);
- // Update back buffer desc before calling app's device callbacks
- DXUTUpdateBackBufferDesc();
- // Setup cursor based on current settings (window/fullscreen mode, show cursor state, clip cursor state)
- DXUTSetupCursor();
- // Update GetDXUTState()'s copy of D3D caps
- D3DCAPS9* pd3dCaps = GetDXUTState().GetCaps();
- DXUTGetD3DDevice()->GetDeviceCaps( pd3dCaps );
- // Update the device stats text
- CD3DEnumeration* pd3dEnum = DXUTPrepareEnumerationObject();
- CD3DEnumAdapterInfo* pAdapterInfo = pd3dEnum->GetAdapterInfo( pNewDeviceSettings->AdapterOrdinal );
- DXUTUpdateDeviceStats( pNewDeviceSettings->DeviceType,
- pNewDeviceSettings->BehaviorFlags,
- &pAdapterInfo->AdapterIdentifier );
- // Call the resource cache created function
- hr = DXUTGetGlobalResourceCache().OnCreateDevice( pd3dDevice );
- if( FAILED(hr) )
- return DXUT_ERR( L"OnCreateDevice", ( hr == DXUTERR_MEDIANOTFOUND ) ? DXUTERR_MEDIANOTFOUND : DXUTERR_CREATINGDEVICEOBJECTS );
- // Call the app's device created callback if non-NULL
- const D3DSURFACE_DESC* pbackBufferSurfaceDesc = DXUTGetBackBufferSurfaceDesc();
- GetDXUTState().SetInsideDeviceCallback( true );
- LPDXUTCALLBACKDEVICECREATED pCallbackDeviceCreated = GetDXUTState().GetDeviceCreatedFunc();
- hr = S_OK;
- if( pCallbackDeviceCreated != NULL )
- hr = pCallbackDeviceCreated( DXUTGetD3DDevice(), pbackBufferSurfaceDesc, GetDXUTState().GetDeviceCreatedFuncUserContext() );
- GetDXUTState().SetInsideDeviceCallback( false );
- if( DXUTGetD3DDevice() == NULL ) // Handle DXUTShutdown from inside callback
- return E_FAIL;
- if( FAILED(hr) )
- {
- DXUT_ERR( L"DeviceCreated callback", hr );
- return ( hr == DXUTERR_MEDIANOTFOUND ) ? DXUTERR_MEDIANOTFOUND : DXUTERR_CREATINGDEVICEOBJECTS;
- }
- GetDXUTState().SetDeviceObjectsCreated( true );
- // Call the resource cache device reset function
- hr = DXUTGetGlobalResourceCache().OnResetDevice( pd3dDevice );
- if( FAILED(hr) )
- return DXUT_ERR( L"OnResetDevice", DXUTERR_RESETTINGDEVICEOBJECTS );
- // Call the app's device reset callback if non-NULL
- GetDXUTState().SetInsideDeviceCallback( true );
- LPDXUTCALLBACKDEVICERESET pCallbackDeviceReset = GetDXUTState().GetDeviceResetFunc();
- hr = S_OK;
- if( pCallbackDeviceReset != NULL )
- hr = pCallbackDeviceReset( DXUTGetD3DDevice(), pbackBufferSurfaceDesc, GetDXUTState().GetDeviceResetFuncUserContext() );
- GetDXUTState().SetInsideDeviceCallback( false );
- if( DXUTGetD3DDevice() == NULL ) // Handle DXUTShutdown from inside callback
- return E_FAIL;
- if( FAILED(hr) )
- {
- DXUT_ERR( L"DeviceReset callback", hr );
- return ( hr == DXUTERR_MEDIANOTFOUND ) ? DXUTERR_MEDIANOTFOUND : DXUTERR_RESETTINGDEVICEOBJECTS;
- }
- GetDXUTState().SetDeviceObjectsReset( true );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Resets the 3D environment by:
- // - Calls the device lost callback
- // - Resets the device
- // - Stores the back buffer description
- // - Sets up the full screen Direct3D cursor if requested
- // - Calls the device reset callback
- //--------------------------------------------------------------------------------------
- HRESULT DXUTReset3DEnvironment()
- {
- HRESULT hr;
- IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
- assert( pd3dDevice != NULL );
- // Call the app's device lost callback
- if( GetDXUTState().GetDeviceObjectsReset() == true )
- {
- GetDXUTState().SetInsideDeviceCallback( true );
- LPDXUTCALLBACKDEVICELOST pCallbackDeviceLost = GetDXUTState().GetDeviceLostFunc();
- if( pCallbackDeviceLost != NULL )
- pCallbackDeviceLost( GetDXUTState().GetDeviceLostFuncUserContext() );
- GetDXUTState().SetDeviceObjectsReset( false );
- GetDXUTState().SetInsideDeviceCallback( false );
- // Call the resource cache device lost function
- DXUTGetGlobalResourceCache().OnLostDevice();
- }
- // Reset the device
- DXUTDeviceSettings* pDeviceSettings = GetDXUTState().GetCurrentDeviceSettings();
- hr = pd3dDevice->Reset( &pDeviceSettings->pp );
- if( FAILED(hr) )
- {
- if( hr == D3DERR_DEVICELOST )
- return D3DERR_DEVICELOST; // Reset could legitimately fail if the device is lost
- else
- return DXUT_ERR( L"Reset", DXUTERR_RESETTINGDEVICE );
- }
- // Update back buffer desc before calling app's device callbacks
- DXUTUpdateBackBufferDesc();
- // Setup cursor based on current settings (window/fullscreen mode, show cursor state, clip cursor state)
- DXUTSetupCursor();
- hr = DXUTGetGlobalResourceCache().OnResetDevice( pd3dDevice );
- if( FAILED(hr) )
- return DXUT_ERR( L"OnResetDevice", DXUTERR_RESETTINGDEVICEOBJECTS );
- // Call the app's OnDeviceReset callback
- GetDXUTState().SetInsideDeviceCallback( true );
- const D3DSURFACE_DESC* pbackBufferSurfaceDesc = DXUTGetBackBufferSurfaceDesc();
- LPDXUTCALLBACKDEVICERESET pCallbackDeviceReset = GetDXUTState().GetDeviceResetFunc();
- hr = S_OK;
- if( pCallbackDeviceReset != NULL )
- hr = pCallbackDeviceReset( pd3dDevice, pbackBufferSurfaceDesc, GetDXUTState().GetDeviceResetFuncUserContext() );
- GetDXUTState().SetInsideDeviceCallback( false );
- if( FAILED(hr) )
- {
- // If callback failed, cleanup
- DXUT_ERR( L"DeviceResetCallback", hr );
- if( hr != DXUTERR_MEDIANOTFOUND )
- hr = DXUTERR_RESETTINGDEVICEOBJECTS;
- GetDXUTState().SetInsideDeviceCallback( true );
- LPDXUTCALLBACKDEVICELOST pCallbackDeviceLost = GetDXUTState().GetDeviceLostFunc();
- if( pCallbackDeviceLost != NULL )
- pCallbackDeviceLost( GetDXUTState().GetDeviceLostFuncUserContext() );
- GetDXUTState().SetInsideDeviceCallback( false );
- DXUTGetGlobalResourceCache().OnLostDevice();
- return hr;
- }
- // Success
- GetDXUTState().SetDeviceObjectsReset( true );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Pauses time or rendering. Keeps a ref count so pausing can be layered
- //--------------------------------------------------------------------------------------
- void DXUTPause( bool bPauseTime, bool bPauseRendering )
- {
- int nPauseTimeCount = GetDXUTState().GetPauseTimeCount();
- nPauseTimeCount += ( bPauseTime ? +1 : -1 );
- if( nPauseTimeCount < 0 )
- nPauseTimeCount = 0;
- GetDXUTState().SetPauseTimeCount( nPauseTimeCount );
- int nPauseRenderingCount = GetDXUTState().GetPauseRenderingCount();
- nPauseRenderingCount += ( bPauseRendering ? +1 : -1 );
- if( nPauseRenderingCount < 0 )
- nPauseRenderingCount = 0;
- GetDXUTState().SetPauseRenderingCount( nPauseRenderingCount );
- if( nPauseTimeCount > 0 )
- {
- // Stop the scene from animating
- DXUTGetGlobalTimer()->Stop();
- }
- else
- {
- // Restart the timer
- DXUTGetGlobalTimer()->Start();
- }
- GetDXUTState().SetRenderingPaused( nPauseRenderingCount > 0 );
- GetDXUTState().SetTimePaused( nPauseTimeCount > 0 );
- }
- //--------------------------------------------------------------------------------------
- // Checks if the window client rect has changed and if it has, then reset the device
- //--------------------------------------------------------------------------------------
- void DXUTCheckForWindowSizeChange()
- {
- // Skip the check for various reasons
- if( GetDXUTState().GetIgnoreSizeChange() ||
- !GetDXUTState().GetDeviceCreated() ||
- !GetDXUTState().GetCurrentDeviceSettings()->pp.Windowed )
- return;
- RECT rcCurrentClient;
- GetClientRect( DXUTGetHWND(), &rcCurrentClient );
- if( (UINT)rcCurrentClient.right != GetDXUTState().GetCurrentDeviceSettings()->pp.BackBufferWidth ||
- (UINT)rcCurrentClient.bottom != GetDXUTState().GetCurrentDeviceSettings()->pp.BackBufferHeight )
- {
- // A new window size will require a new backbuffer size
- // size, so the device must be reset and the D3D structures updated accordingly.
- // Tell DXUTChangeDevice and D3D to size according to the HWND's client rect
- DXUTDeviceSettings deviceSettings = DXUTGetDeviceSettings();
- deviceSettings.pp.BackBufferWidth = 0;
- deviceSettings.pp.BackBufferHeight = 0;
- DXUTChangeDevice( &deviceSettings, NULL, false, false );
- }
- }
- //--------------------------------------------------------------------------------------
- // Handles app's message loop and rendering when idle. If DXUTCreateDevice*() or DXUTSetDevice()
- // has not already been called, it will call DXUTCreateWindow() with the default parameters.
- //--------------------------------------------------------------------------------------
- HRESULT DXUTMainLoop( HACCEL hAccel )
- {
- HRESULT hr;
- // Not allowed to call this from inside the device callbacks or reenter
- if( GetDXUTState().GetInsideDeviceCallback() || GetDXUTState().GetInsideMainloop() )
- {
- if( (GetDXUTState().GetExitCode() == 0) || (GetDXUTState().GetExitCode() == 11) )
- GetDXUTState().SetExitCode(1);
- return DXUT_ERR_MSGBOX( L"DXUTMainLoop", E_FAIL );
- }
- GetDXUTState().SetInsideMainloop( true );
- // If DXUTCreateDevice*() or DXUTSetDevice() has not already been called,
- // then call DXUTCreateDevice() with the default parameters.
- if( !GetDXUTState().GetDeviceCreated() )
- {
- if( GetDXUTState().GetDeviceCreateCalled() )
- {
- if( (GetDXUTState().GetExitCode() == 0) || (GetDXUTState().GetExitCode() == 11) )
- GetDXUTState().SetExitCode(1);
- return E_FAIL; // DXUTCreateDevice() must first succeed for this function to succeed
- }
- hr = DXUTCreateDevice();
- if( FAILED(hr) )
- {
- if( (GetDXUTState().GetExitCode() == 0) || (GetDXUTState().GetExitCode() == 11) )
- GetDXUTState().SetExitCode(1);
- return hr;
- }
- }
- HWND hWnd = DXUTGetHWND();
- // DXUTInit() must have been called and succeeded for this function to proceed
- // DXUTCreateWindow() or DXUTSetWindow() must have been called and succeeded for this function to proceed
- // DXUTCreateDevice() or DXUTCreateDeviceFromSettings() or DXUTSetDevice() must have been called and succeeded for this function to proceed
- if( !GetDXUTState().GetDXUTInited() || !GetDXUTState().GetWindowCreated() || !GetDXUTState().GetDeviceCreated() )
- {
- if( (GetDXUTState().GetExitCode() == 0) || (GetDXUTState().GetExitCode() == 11) )
- GetDXUTState().SetExitCode(1);
- return DXUT_ERR_MSGBOX( L"DXUTMainLoop", E_FAIL );
- }
- // Now we're ready to receive and process Windows messages.
- bool bGotMsg;
- MSG msg;
- msg.message = WM_NULL;
- PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );
- while( WM_QUIT != msg.message )
- {
- // Use PeekMessage() so we can use idle time to render the scene.
- bGotMsg = ( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) != 0 );
- if( bGotMsg )
- {
- // Translate and dispatch the message
- if( hAccel == NULL || hWnd == NULL ||
- 0 == TranslateAccelerator( hWnd, hAccel, &msg ) )
- {
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- }
- else
- {
- // Render a frame during idle time (no messages are waiting)
- DXUTRender3DEnvironment();
- }
- }
- // Cleanup the accelerator table
- if( hAccel != NULL )
- DestroyAcceleratorTable( hAccel );
- GetDXUTState().SetInsideMainloop( false );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Render the 3D environment by:
- // - Checking if the device is lost and trying to reset it if it is
- // - Get the elapsed time since the last frame
- // - Calling the app's framemove and render callback
- // - Calling Present()
- //--------------------------------------------------------------------------------------
- void DXUTRender3DEnvironment()
- {
- HRESULT hr;
- if( GetDXUTState().GetDeviceLost() || DXUTIsRenderingPaused() || !DXUTIsActive() )
- {
- // Window is minimized or paused so yield CPU time to other processes
- Sleep( 50 );
- }
- IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
- if( NULL == pd3dDevice )
- {
- if( GetDXUTState().GetDeviceLost() )
- {
- DXUTDeviceSettings deviceSettings = DXUTGetDeviceSettings();
- DXUTChangeDevice( &deviceSettings, NULL, false, true );
- }
- return;
- }
- if( GetDXUTState().GetDeviceLost() && !GetDXUTState().GetRenderingPaused() )
- {
- // Test the cooperative level to see if it's okay to render
- if( FAILED( hr = pd3dDevice->TestCooperativeLevel() ) )
- {
- if( D3DERR_DEVICELOST == hr )
- {
- // The device has been lost but cannot be reset at this time.
- // So wait until it can be reset.
- return;
- }
- // If we are windowed, read the desktop format and
- // ensure that the Direct3D device is using the same format
- // since the user could have changed the desktop bitdepth
- if( DXUTIsWindowed() )
- {
- D3DDISPLAYMODE adapterDesktopDisplayMode;
- IDirect3D9* pD3D = DXUTGetD3DObject();
- DXUTDeviceSettings* pDeviceSettings = GetDXUTState().GetCurrentDeviceSettings();
- pD3D->GetAdapterDisplayMode( pDeviceSettings->AdapterOrdinal, &adapterDesktopDisplayMode );
- if( pDeviceSettings->AdapterFormat != adapterDesktopDisplayMode.Format )
- {
- DXUTMatchOptions matchOptions;
- matchOptions.eAdapterOrdinal = DXUTMT_PRESERVE_INPUT;
- matchOptions.eDeviceType = DXUTMT_PRESERVE_INPUT;
- matchOptions.eWindowed = DXUTMT_PRESERVE_INPUT;
- matchOptions.eAdapterFormat = DXUTMT_PRESERVE_INPUT;
- matchOptions.eVertexProcessing = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eResolution = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eBackBufferFormat = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eBackBufferCount = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eMultiSample = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eSwapEffect = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eDepthFormat = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eStencilFormat = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.ePresentFlags = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eRefreshRate = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.ePresentInterval = DXUTMT_CLOSEST_TO_INPUT;
- DXUTDeviceSettings deviceSettings = DXUTGetDeviceSettings();
- deviceSettings.AdapterFormat = adapterDesktopDisplayMode.Format;
- hr = DXUTFindValidDeviceSettings( &deviceSettings, &deviceSettings, &matchOptions );
- if( FAILED(hr) ) // the call will fail if no valid devices were found
- {
- DXUTDisplayErrorMessage( DXUTERR_NOCOMPATIBLEDEVICES );
- DXUTShutdown();
- }
- // Change to a Direct3D device created from the new device settings.
- // If there is an existing device, then either reset or recreate the scene
- hr = DXUTChangeDevice( &deviceSettings, NULL, false, false );
- if( FAILED(hr) )
- {
- // If this fails, try to go fullscreen and if this fails also shutdown.
- if( FAILED(DXUTToggleFullScreen()) )
- DXUTShutdown();
- }
- return;
- }
- }
- // Try to reset the device
- if( FAILED( hr = DXUTReset3DEnvironment() ) )
- {
- if( D3DERR_DEVICELOST == hr )
- {
- // The device was lost again, so continue waiting until it can be reset.
- return;
- }
- else if( DXUTERR_RESETTINGDEVICEOBJECTS == hr ||
- DXUTERR_MEDIANOTFOUND == hr )
- {
- DXUTDisplayErrorMessage( hr );
- DXUTShutdown();
- return;
- }
- else
- {
- // Reset failed, but the device wasn't lost so something bad happened,
- // so recreate the device to try to recover
- DXUTDeviceSettings* pDeviceSettings = GetDXUTState().GetCurrentDeviceSettings();
- if( FAILED( DXUTChangeDevice( pDeviceSettings, NULL, true, false ) ) )
- {
- DXUTShutdown();
- return;
- }
- }
- }
- }
- GetDXUTState().SetDeviceLost( false );
- }
- // Get the app's time, in seconds. Skip rendering if no time elapsed
- double fTime, fAbsTime; float fElapsedTime;
- DXUTGetGlobalTimer()->GetTimeValues( &fTime, &fAbsTime, &fElapsedTime );
- // Store the time for the app
- if( GetDXUTState().GetConstantFrameTime() )
- {
- fElapsedTime = GetDXUTState().GetTimePerFrame();
- fTime = DXUTGetTime() + fElapsedTime;
- }
- GetDXUTState().SetTime( fTime );
- GetDXUTState().SetAbsoluteTime( fAbsTime );
- GetDXUTState().SetElapsedTime( fElapsedTime );
- // Update the FPS stats
- DXUTUpdateFrameStats();
- DXUTHandleTimers();
- // Animate the scene by calling the app's frame move callback
- LPDXUTCALLBACKFRAMEMOVE pCallbackFrameMove = GetDXUTState().GetFrameMoveFunc();
- if( pCallbackFrameMove != NULL )
- {
- pCallbackFrameMove( pd3dDevice, fTime, fElapsedTime, GetDXUTState().GetFrameMoveFuncUserContext() );
- pd3dDevice = DXUTGetD3DDevice();
- if( NULL == pd3dDevice ) // Handle DXUTShutdown from inside callback
- return;
- }
- if( !GetDXUTState().GetRenderingPaused() )
- {
- // Render the scene by calling the app's render callback
- LPDXUTCALLBACKFRAMERENDER pCallbackFrameRender = GetDXUTState().GetFrameRenderFunc();
- if( pCallbackFrameRender != NULL )
- {
- pCallbackFrameRender( pd3dDevice, fTime, fElapsedTime, GetDXUTState().GetFrameRenderFuncUserContext() );
- pd3dDevice = DXUTGetD3DDevice();
- if( NULL == pd3dDevice ) // Handle DXUTShutdown from inside callback
- return;
- }
- #if defined(DEBUG) || defined(_DEBUG)
- // The back buffer should always match the client rect
- // if the Direct3D backbuffer covers the entire window
- RECT rcClient;
- GetClientRect( DXUTGetHWND(), &rcClient );
- if( !IsIconic( DXUTGetHWND() ) )
- {
- GetClientRect( DXUTGetHWND(), &rcClient );
- assert( DXUTGetBackBufferSurfaceDesc()->Width == (UINT)rcClient.right );
- assert( DXUTGetBackBufferSurfaceDesc()->Height == (UINT)rcClient.bottom );
- }
- #endif
- // Show the frame on the primary surface.
- hr = pd3dDevice->Present( NULL, NULL, NULL, NULL );
- if( FAILED(hr) )
- {
- if( D3DERR_DEVICELOST == hr )
- {
- GetDXUTState().SetDeviceLost( true );
- }
- else if( D3DERR_DRIVERINTERNALERROR == hr )
- {
- // When D3DERR_DRIVERINTERNALERROR is returned from Present(),
- // the application can do one of the following:
- //
- // - End, with the pop-up window saying that the application cannot continue
- // because of problems in the display adapter and that the user should
- // contact the adapter manufacturer.
- //
- // - Attempt to restart by calling IDirect3DDevice9::Reset, which is essentially the same
- // path as recovering from a lost device. If IDirect3DDevice9::Reset fails with
- // D3DERR_DRIVERINTERNALERROR, the application should end immediately with the message
- // that the user should contact the adapter manufacturer.
- //
- // The framework attempts the path of resetting the device
- //
- GetDXUTState().SetDeviceLost( true );
- }
- }
- }
- // Update current frame #
- int nFrame = GetDXUTState().GetCurrentFrameNumber();
- nFrame++;
- GetDXUTState().SetCurrentFrameNumber( nFrame );
- // Check to see if the app should shutdown due to cmdline
- if( GetDXUTState().GetOverrideQuitAfterFrame() != 0 )
- {
- if( nFrame > GetDXUTState().GetOverrideQuitAfterFrame() )
- DXUTShutdown();
- }
- return;
- }
- //--------------------------------------------------------------------------------------
- // Updates the string which describes the device
- //--------------------------------------------------------------------------------------
- void DXUTUpdateDeviceStats( D3DDEVTYPE DeviceType, DWORD BehaviorFlags, D3DADAPTER_IDENTIFIER9* pAdapterIdentifier )
- {
- if( GetDXUTState().GetNoStats() )
- return;
- // Store device description
- WCHAR* pstrDeviceStats = GetDXUTState().GetDeviceStats();
- if( DeviceType == D3DDEVTYPE_REF )
- StringCchCopy( pstrDeviceStats, 256, L"REF" );
- else if( DeviceType == D3DDEVTYPE_HAL )
- StringCchCopy( pstrDeviceStats, 256, L"HAL" );
- else if( DeviceType == D3DDEVTYPE_SW )
- StringCchCopy( pstrDeviceStats, 256, L"SW" );
- if( BehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING &&
- BehaviorFlags & D3DCREATE_PUREDEVICE )
- {
- if( DeviceType == D3DDEVTYPE_HAL )
- StringCchCat( pstrDeviceStats, 256, L" (pure hw vp)" );
- else
- StringCchCat( pstrDeviceStats, 256, L" (simulated pure hw vp)" );
- }
- else if( BehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING )
- {
- if( DeviceType == D3DDEVTYPE_HAL )
- StringCchCat( pstrDeviceStats, 256, L" (hw vp)" );
- else
- StringCchCat( pstrDeviceStats, 256, L" (simulated hw vp)" );
- }
- else if( BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING )
- {
- if( DeviceType == D3DDEVTYPE_HAL )
- StringCchCat( pstrDeviceStats, 256, L" (mixed vp)" );
- else
- StringCchCat( pstrDeviceStats, 256, L" (simulated mixed vp)" );
- }
- else if( BehaviorFlags & D3DCREATE_SOFTWARE_VERTEXPROCESSING )
- {
- StringCchCat( pstrDeviceStats, 256, L" (sw vp)" );
- }
- if( DeviceType == D3DDEVTYPE_HAL )
- {
- // Be sure not to overflow m_strDeviceStats when appending the adapter
- // description, since it can be long.
- StringCchCat( pstrDeviceStats, 256, L": " );
- // Try to get a unique description from the CD3DEnumDeviceSettingsCombo
- DXUTDeviceSettings* pDeviceSettings = GetDXUTState().GetCurrentDeviceSettings();
- CD3DEnumeration* pd3dEnum = DXUTPrepareEnumerationObject();
- CD3DEnumDeviceSettingsCombo* pDeviceSettingsCombo = pd3dEnum->GetDeviceSettingsCombo( pDeviceSettings->AdapterOrdinal, pDeviceSettings->DeviceType, pDeviceSettings->AdapterFormat, pDeviceSettings->pp.BackBufferFormat, pDeviceSettings->pp.Windowed );
- if( pDeviceSettingsCombo )
- {
- StringCchCat( pstrDeviceStats, 256, pDeviceSettingsCombo->pAdapterInfo->szUniqueDescription );
- }
- else
- {
- const int cchDesc = sizeof(pAdapterIdentifier->Description);
- WCHAR szDescription[cchDesc];
- MultiByteToWideChar( CP_ACP, 0, pAdapterIdentifier->Description, -1, szDescription, cchDesc );
- szDescription[cchDesc-1] = 0;
- StringCchCat( pstrDeviceStats, 256, szDescription );
- }
- }
- }
- //--------------------------------------------------------------------------------------
- // Updates the frames/sec stat once per second
- //--------------------------------------------------------------------------------------
- void DXUTUpdateFrameStats()
- {
- if( GetDXUTState().GetNoStats() )
- return;
- // Keep track of the frame count
- double fLastTime = GetDXUTState().GetLastStatsUpdateTime();
- DWORD dwFrames = GetDXUTState().GetLastStatsUpdateFrames();
- double fAbsTime = GetDXUTState().GetAbsoluteTime();
- dwFrames++;
- GetDXUTState().SetLastStatsUpdateFrames( dwFrames );
- // Update the scene stats once per second
- if( fAbsTime - fLastTime > 1.0f )
- {
- float fFPS = (float) (dwFrames / (fAbsTime - fLastTime));
- GetDXUTState().SetFPS( fFPS );
- GetDXUTState().SetLastStatsUpdateTime( fAbsTime );
- GetDXUTState().SetLastStatsUpdateFrames( 0 );
- WCHAR* pstrFPS = GetDXUTState().GetFPSStats();
- StringCchPrintf( pstrFPS, 64, L"%0.2f fps ", fFPS );
- }
- }
- //--------------------------------------------------------------------------------------
- // Updates the static part of the frame stats so it doesn't have be generated every frame
- //--------------------------------------------------------------------------------------
- void DXUTUpdateStaticFrameStats()
- {
- if( GetDXUTState().GetNoStats() )
- return;
- DXUTDeviceSettings* pDeviceSettings = GetDXUTState().GetCurrentDeviceSettings();
- if( NULL == pDeviceSettings )
- return;
- CD3DEnumeration* pd3dEnum = DXUTPrepareEnumerationObject();
- if( NULL == pd3dEnum )
- return;
- CD3DEnumDeviceSettingsCombo* pDeviceSettingsCombo = pd3dEnum->GetDeviceSettingsCombo( pDeviceSettings->AdapterOrdinal, pDeviceSettings->DeviceType, pDeviceSettings->AdapterFormat, pDeviceSettings->pp.BackBufferFormat, pDeviceSettings->pp.Windowed );
- if( NULL == pDeviceSettingsCombo )
- return;
- WCHAR strFmt[100];
- D3DPRESENT_PARAMETERS* pPP = &pDeviceSettings->pp;
- if( pDeviceSettingsCombo->AdapterFormat == pDeviceSettingsCombo->BackBufferFormat )
- {
- StringCchCopy( strFmt, 100, DXUTD3DFormatToString( pDeviceSettingsCombo->AdapterFormat, false ) );
- }
- else
- {
- StringCchPrintf( strFmt, 100, L"backbuf %s, adapter %s",
- DXUTD3DFormatToString( pDeviceSettingsCombo->BackBufferFormat, false ),
- DXUTD3DFormatToString( pDeviceSettingsCombo->AdapterFormat, false ) );
- }
- WCHAR strDepthFmt[100];
- if( pPP->EnableAutoDepthStencil )
- {
- StringCchPrintf( strDepthFmt, 100, L" (%s)", DXUTD3DFormatToString( pPP->AutoDepthStencilFormat, false ) );
- }
- else
- {
- // No depth buffer
- strDepthFmt[0] = 0;
- }
- WCHAR strMultiSample[100];
- switch( pPP->MultiSampleType )
- {
- case D3DMULTISAMPLE_NONMASKABLE: StringCchCopy( strMultiSample, 100, L" (Nonmaskable Multisample)" ); break;
- case D3DMULTISAMPLE_NONE: StringCchCopy( strMultiSample, 100, L"" ); break;
- default: StringCchPrintf( strMultiSample, 100, L" (%dx Multisample)", pPP->MultiSampleType ); break;
- }
- WCHAR* pstrStaticFrameStats = GetDXUTState().GetStaticFrameStats();
- StringCchPrintf( pstrStaticFrameStats, 256, L"%%sVsync %s (%dx%d), %s%s%s",
- ( pPP->PresentationInterval == D3DPRESENT_INTERVAL_IMMEDIATE ) ? L"off" : L"on",
- pPP->BackBufferWidth, pPP->BackBufferHeight,
- strFmt, strDepthFmt, strMultiSample );
- }
- //--------------------------------------------------------------------------------------
- LPCWSTR DXUTGetFrameStats( bool bShowFPS )
- {
- WCHAR* pstrFrameStats = GetDXUTState().GetFrameStats();
- WCHAR* pstrFPS = ( bShowFPS ) ? GetDXUTState().GetFPSStats() : L"";
- StringCchPrintf( pstrFrameStats, 256, GetDXUTState().GetStaticFrameStats(), pstrFPS );
- return pstrFrameStats;
- }
- //--------------------------------------------------------------------------------------
- // Handles window messages
- //--------------------------------------------------------------------------------------
- LRESULT CALLBACK DXUTStaticWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- // Consolidate the keyboard messages and pass them to the app's keyboard callback
- if( uMsg == WM_KEYDOWN ||
- uMsg == WM_SYSKEYDOWN ||
- uMsg == WM_KEYUP ||
- uMsg == WM_SYSKEYUP )
- {
- bool bKeyDown = (uMsg == WM_KEYDOWN || uMsg == WM_SYSKEYDOWN);
- DWORD dwMask = (1 << 29);
- bool bAltDown = ( (lParam & dwMask) != 0 );
- bool* bKeys = GetDXUTState().GetKeys();
- bKeys[ (BYTE) (wParam & 0xFF) ] = bKeyDown;
- LPDXUTCALLBACKKEYBOARD pCallbackKeyboard = GetDXUTState().GetKeyboardFunc();
- if( pCallbackKeyboard )
- pCallbackKeyboard( (UINT)wParam, bKeyDown, bAltDown, GetDXUTState().GetKeyboardFuncUserContext() );
- }
- // Consolidate the mouse button messages and pass them to the app's mouse callback
- if( uMsg == WM_LBUTTONDOWN ||
- uMsg == WM_LBUTTONUP ||
- uMsg == WM_LBUTTONDBLCLK ||
- uMsg == WM_MBUTTONDOWN ||
- uMsg == WM_MBUTTONUP ||
- uMsg == WM_MBUTTONDBLCLK ||
- uMsg == WM_RBUTTONDOWN ||
- uMsg == WM_RBUTTONUP ||
- uMsg == WM_RBUTTONDBLCLK ||
- uMsg == WM_XBUTTONDOWN ||
- uMsg == WM_XBUTTONUP ||
- uMsg == WM_XBUTTONDBLCLK ||
- uMsg == WM_MOUSEWHEEL ||
- (GetDXUTState().GetNotifyOnMouseMove() && uMsg == WM_MOUSEMOVE) )
- {
- int xPos = (short)LOWORD(lParam);
- int yPos = (short)HIWORD(lParam);
- if( uMsg == WM_MOUSEWHEEL )
- {
- // WM_MOUSEWHEEL passes screen mouse coords
- // so convert them to client coords
- POINT pt;
- pt.x = xPos; pt.y = yPos;
- ScreenToClient( hWnd, &pt );
- xPos = pt.x; yPos = pt.y;
- }
- int nMouseWheelDelta = 0;
- if( uMsg == WM_MOUSEWHEEL )
- nMouseWheelDelta = (short) HIWORD(wParam);
- int nMouseButtonState = LOWORD(wParam);
- bool bLeftButton = ((nMouseButtonState & MK_LBUTTON) != 0);
- bool bRightButton = ((nMouseButtonState & MK_RBUTTON) != 0);
- bool bMiddleButton = ((nMouseButtonState & MK_MBUTTON) != 0);
- bool bSideButton1 = ((nMouseButtonState & MK_XBUTTON1) != 0);
- bool bSideButton2 = ((nMouseButtonState & MK_XBUTTON2) != 0);
- bool* bMouseButtons = GetDXUTState().GetMouseButtons();
- bMouseButtons[0] = bLeftButton;
- bMouseButtons[1] = bMiddleButton;
- bMouseButtons[2] = bRightButton;
- bMouseButtons[3] = bSideButton1;
- bMouseButtons[4] = bSideButton2;
- LPDXUTCALLBACKMOUSE pCallbackMouse = GetDXUTState().GetMouseFunc();
- if( pCallbackMouse )
- pCallbackMouse( bLeftButton, bRightButton, bMiddleButton, bSideButton1, bSideButton2, nMouseWheelDelta, xPos, yPos, GetDXUTState().GetMouseFuncUserContext() );
- }
- // Pass all messages to the app's MsgProc callback, and don't
- // process further messages if the apps says not to.
- LPDXUTCALLBACKMSGPROC pCallbackMsgProc = GetDXUTState().GetWindowMsgFunc();
- if( pCallbackMsgProc )
- {
- bool bNoFurtherProcessing = false;
- LRESULT nResult = pCallbackMsgProc( hWnd, uMsg, wParam, lParam, &bNoFurtherProcessing, GetDXUTState().GetWindowMsgFuncUserContext() );
- if( bNoFurtherProcessing )
- return nResult;
- }
- switch( uMsg )
- {
- case WM_PAINT:
- {
- IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
- // Handle paint messages when the app is paused
- if( pd3dDevice && DXUTIsRenderingPaused() &&
- GetDXUTState().GetDeviceObjectsCreated() && GetDXUTState().GetDeviceObjectsReset() )
- {
- HRESULT hr;
- double fTime = DXUTGetTime();
- float fElapsedTime = DXUTGetElapsedTime();
- LPDXUTCALLBACKFRAMERENDER pCallbackFrameRender = GetDXUTState().GetFrameRenderFunc();
- if( pCallbackFrameRender != NULL )
- pCallbackFrameRender( pd3dDevice, fTime, fElapsedTime, GetDXUTState().GetFrameRenderFuncUserContext() );
- hr = pd3dDevice->Present( NULL, NULL, NULL, NULL );
- if( D3DERR_DEVICELOST == hr )
- {
- GetDXUTState().SetDeviceLost( true );
- }
- else if( D3DERR_DRIVERINTERNALERROR == hr )
- {
- // When D3DERR_DRIVERINTERNALERROR is returned from Present(),
- // the application can do one of the following:
- //
- // - End, with the pop-up window saying that the application cannot continue
- // because of problems in the display adapter and that the user should
- // contact the adapter manufacturer.
- //
- // - Attempt to restart by calling IDirect3DDevice9::Reset, which is essentially the same
- // path as recovering from a lost device. If IDirect3DDevice9::Reset fails with
- // D3DERR_DRIVERINTERNALERROR, the application should end immediately with the message
- // that the user should contact the adapter manufacturer.
- //
- // The framework attempts the path of resetting the device
- //
- GetDXUTState().SetDeviceLost( true );
- }
- }
- break;
- }
- case WM_SIZE:
- if( SIZE_MINIMIZED == wParam )
- {
- DXUTPause( true, true ); // Pause while we're minimized
- GetDXUTState().SetMinimized( true );
- GetDXUTState().SetMaximized( false );
- }
- else
- {
- RECT rcCurrentClient;
- GetClientRect( DXUTGetHWND(), &rcCurrentClient );
- if( rcCurrentClient.top == 0 && rcCurrentClient.bottom == 0 )
- {
- // Rapidly clicking the task bar to minimize and restore a window
- // can cause a WM_SIZE message with SIZE_RESTORED when
- // the window has actually become minimized due to rapid change
- // so just ignore this message
- }
- else if( SIZE_MAXIMIZED == wParam )
- {
- if( GetDXUTState().GetMinimized() )
- DXUTPause( false, false ); // Unpause since we're no longer minimized
- GetDXUTState().SetMinimized( false );
- GetDXUTState().SetMaximized( true );
- DXUTCheckForWindowSizeChange();
- DXUTCheckForWindowChangingMonitors();
- }
- else if( SIZE_RESTORED == wParam )
- {
- if( GetDXUTState().GetMaximized() )
- {
- GetDXUTState().SetMaximized( false );
- DXUTCheckForWindowSizeChange();
- DXUTCheckForWindowChangingMonitors();
- }
- else if( GetDXUTState().GetMinimized() )
- {
- DXUTPause( false, false ); // Unpause since we're no longer minimized
- GetDXUTState().SetMinimized( false );
- DXUTCheckForWindowSizeChange();
- DXUTCheckForWindowChangingMonitors();
- }
- else if( GetDXUTState().GetInSizeMove() )
- {
- // If we're neither maximized nor minimized, the window size
- // is changing by the user dragging the window edges. In this
- // case, we don't reset the device yet -- we wait until the
- // user stops dragging, and a WM_EXITSIZEMOVE message comes.
- }
- else
- {
- // This WM_SIZE come from resizing the window via an API like SetWindowPos() so
- // resize and reset the device now.
- DXUTCheckForWindowSizeChange();
- DXUTCheckForWindowChangingMonitors();
- }
- }
- }
- break;
- case WM_GETMINMAXINFO:
- ((MINMAXINFO*)lParam)->ptMinTrackSize.x = DXUT_MIN_WINDOW_SIZE_X;
- ((MINMAXINFO*)lParam)->ptMinTrackSize.y = DXUT_MIN_WINDOW_SIZE_Y;
- break;
- case WM_ENTERSIZEMOVE:
- // Halt frame movement while the app is sizing or moving
- DXUTPause( true, true );
- GetDXUTState().SetInSizeMove( true );
- break;
- case WM_EXITSIZEMOVE:
- DXUTPause( false, false );
- DXUTCheckForWindowSizeChange();
- DXUTCheckForWindowChangingMonitors();
- GetDXUTState().SetInSizeMove( false );
- break;
- case WM_MOUSEMOVE:
- if( DXUTIsActive() && !DXUTIsWindowed() )
- {
- IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
- if( pd3dDevice )
- {
- POINT ptCursor;
- GetCursorPos( &ptCursor );
- pd3dDevice->SetCursorPosition( ptCursor.x, ptCursor.y, 0 );
- }
- }
- break;
- case WM_SETCURSOR:
- if( DXUTIsActive() && !DXUTIsWindowed() )
- {
- IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
- if( pd3dDevice && GetDXUTState().GetShowCursorWhenFullScreen() )
- pd3dDevice->ShowCursor( true );
- return true; // prevent Windows from setting cursor to window class cursor
- }
- break;
- case WM_ACTIVATEAPP:
- if( wParam == TRUE && !DXUTIsActive() ) // Handle only if previously not active
- {
- GetDXUTState().SetActive( true );
- // The GetMinimizedWhileFullscreen() varible is used instead of !DXUTIsWindowed()
- // to handle the rare case toggling to windowed mode while the fullscreen application
- // is minimized and thus making the pause count wrong
- if( GetDXUTState().GetMinimizedWhileFullscreen() )
- {
- DXUTPause( false, false ); // Unpause since we're no longer minimized
- GetDXUTState().SetMinimizedWhileFullscreen( false );
- }
- // Upon returning to this app, potentially disable shortcut keys
- // (Windows key, accessibility shortcuts)
- DXUTAllowShortcutKeys( ( DXUTIsWindowed() ) ? GetDXUTState().GetAllowShortcutKeysWhenWindowed() :
- GetDXUTState().GetAllowShortcutKeysWhenFullscreen() );
- }
- else if( wParam == FALSE && DXUTIsActive() ) // Handle only if previously active
- {
- GetDXUTState().SetActive( false );
- // Disable any controller rumble when de-activating app
- DXUTStopRumbleOnAllControllers();
- if( !DXUTIsWindowed() )
- {
- // Going from full screen to a minimized state
- ClipCursor( NULL ); // don't limit the cursor anymore
- DXUTPause( true, true ); // Pause while we're minimized (take care not to pause twice by handling this message twice)
- GetDXUTState().SetMinimizedWhileFullscreen( true );
- }
- // Restore shortcut keys (Windows key, accessibility shortcuts) to original state
- //
- // This is important to call here if the shortcuts are disabled,
- // because if this is not done then the Windows key will continue to
- // be disabled while this app is running which is very bad.
- // If the app crashes, the Windows key will return to normal.
- DXUTAllowShortcutKeys( true );
- }
- break;
- case WM_ENTERMENULOOP:
- // Pause the app when menus are displayed
- DXUTPause( true, true );
- break;
- case WM_EXITMENULOOP:
- DXUTPause( false, false );
- break;
- case WM_MENUCHAR:
- // A menu is active and the user presses a key that does not correspond to any mnemonic or accelerator key
- // So just ignore and don't beep
- return MAKELRESULT(0,MNC_CLOSE);
- break;
- case WM_NCHITTEST:
- // Prevent the user from selecting the menu in full screen mode
- if( !DXUTIsWindowed() )
- return HTCLIENT;
- break;
- case WM_POWERBROADCAST:
- switch( wParam )
- {
- #ifndef PBT_APMQUERYSUSPEND
- #define PBT_APMQUERYSUSPEND 0x0000
- #endif
- case PBT_APMQUERYSUSPEND:
- // At this point, the app should save any data for open
- // network connections, files, etc., and prepare to go into
- // a suspended mode. The app can use the MsgProc callback
- // to handle this if desired.
- return true;
- #ifndef PBT_APMRESUMESUSPEND
- #define PBT_APMRESUMESUSPEND 0x0007
- #endif
- case PBT_APMRESUMESUSPEND:
- // At this point, the app should recover any data, network
- // connections, files, etc., and resume running from when
- // the app was suspended. The app can use the MsgProc callback
- // to handle this if desired.
- // QPC may lose consistency when suspending, so reset the timer
- // upon resume.
- DXUTGetGlobalTimer()->Reset();
- GetDXUTState().SetLastStatsUpdateTime( 0 );
- return true;
- }
- break;
- case WM_SYSCOMMAND:
- // Prevent moving/sizing in full screen mode
- switch( wParam )
- {
- case SC_MOVE:
- case SC_SIZE:
- case SC_MAXIMIZE:
- case SC_KEYMENU:
- if( !DXUTIsWindowed() )
- return 0;
- break;
- }
- break;
- case WM_SYSKEYDOWN:
- {
- switch( wParam )
- {
- case VK_RETURN:
- {
- if( GetDXUTState().GetHandleAltEnter() )
- {
- // Toggle full screen upon alt-enter
- DWORD dwMask = (1 << 29);
- if( (lParam & dwMask) != 0 ) // Alt is down also
- {
- // Toggle the full screen/window mode
- DXUTPause( true, true );
- DXUTToggleFullScreen();
- DXUTPause( false, false );
- return 0;
- }
- }
- }
- }
- break;
- }
- case WM_KEYDOWN:
- {
- if( GetDXUTState().GetHandleDefaultHotkeys() )
- {
- switch( wParam )
- {
- case VK_F3:
- {
- DXUTPause( true, true );
- DXUTToggleREF();
- DXUTPause( false, false );
- break;
- }
- case VK_F8:
- {
- bool bWireFrame = GetDXUTState().GetWireframeMode();
- bWireFrame = !bWireFrame;
- GetDXUTState().SetWireframeMode( bWireFrame );
- IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
- if( pd3dDevice )
- pd3dDevice->SetRenderState( D3DRS_FILLMODE, (bWireFrame) ? D3DFILL_WIREFRAME : D3DFILL_SOLID );
- break;
- }
- case VK_ESCAPE:
- {
- // Received key to exit app
- SendMessage( hWnd, WM_CLOSE, 0, 0 );
- }
- case VK_PAUSE:
- {
- bool bTimePaused = DXUTIsTimePaused();
- bTimePaused = !bTimePaused;
- if( bTimePaused )
- DXUTPause( true, false );
- else
- DXUTPause( false, false );
- break;
- }
- }
- }
- break;
- }
- case WM_CLOSE:
- {
- HMENU hMenu;
- hMenu = GetMenu(hWnd);
- if( hMenu != NULL )
- DestroyMenu( hMenu );
- DestroyWindow( hWnd );
- UnregisterClass( L"Direct3DWindowClass", NULL );
- GetDXUTState().SetHWNDFocus( NULL );
- GetDXUTState().SetHWNDDeviceFullScreen( NULL );
- GetDXUTState().SetHWNDDeviceWindowed( NULL );
- return 0;
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- }
- // Don't allow the F10 key to act as a shortcut to the menu bar
- // by not passing these messages to the DefWindowProc only when
- // there's no menu present
- if( !GetDXUTState().GetCallDefWindowProc() || GetDXUTState().GetMenu() == NULL && (uMsg == WM_SYSKEYDOWN || uMsg == WM_SYSKEYUP) && wParam == VK_F10 )
- return 0;
- else
- return DefWindowProc( hWnd, uMsg, wParam, lParam );
- }
- //--------------------------------------------------------------------------------------
- // Resets the state associated with DXUT
- //--------------------------------------------------------------------------------------
- void DXUTResetFrameworkState()
- {
- GetDXUTState().Destroy();
- GetDXUTState().Create();
- }
- //--------------------------------------------------------------------------------------
- // Closes down the window. When the window closes, it will cleanup everything
- //--------------------------------------------------------------------------------------
- void DXUTShutdown( int nExitCode )
- {
- HWND hWnd = DXUTGetHWND();
- if( hWnd != NULL )
- SendMessage( hWnd, WM_CLOSE, 0, 0 );
- GetDXUTState().SetExitCode(nExitCode);
- DXUTCleanup3DEnvironment( true );
- // Restore shortcut keys (Windows key, accessibility shortcuts) to original state
- // This is important to call here if the shortcuts are disabled,
- // because accessibility setting changes are permanent.
- // This means that if this is not done then the accessibility settings
- // might not be the same as when the app was started.
- // If the app crashes without restoring the settings, this is also true so it
- // would be wise to backup/restore the settings from a file so they can be
- // restored when the crashed app is run again.
- DXUTAllowShortcutKeys( true );
- GetDXUTState().SetD3DEnumeration( NULL );
- IDirect3D9* pD3D = DXUTGetD3DObject();
- SAFE_RELEASE( pD3D );
- GetDXUTState().SetD3D( NULL );
- if( GetDXUTState().GetOverrideRelaunchMCE() )
- DXUTReLaunchMediaCenter();
- }
- //--------------------------------------------------------------------------------------
- // Cleans up the 3D environment by:
- // - Calls the device lost callback
- // - Calls the device destroyed callback
- // - Releases the D3D device
- //--------------------------------------------------------------------------------------
- void DXUTCleanup3DEnvironment( bool bReleaseSettings )
- {
- IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
- if( pd3dDevice != NULL )
- {
- GetDXUTState().SetInsideDeviceCallback( true );
- // Call the app's device lost callback
- if( GetDXUTState().GetDeviceObjectsReset() == true )
- {
- LPDXUTCALLBACKDEVICELOST pCallbackDeviceLost = GetDXUTState().GetDeviceLostFunc();
- if( pCallbackDeviceLost != NULL )
- pCallbackDeviceLost( GetDXUTState().GetDeviceLostFuncUserContext() );
- GetDXUTState().SetDeviceObjectsReset( false );
- // Call the resource cache device lost function
- DXUTGetGlobalResourceCache().OnLostDevice();
- }
- // Call the app's device destroyed callback
- if( GetDXUTState().GetDeviceObjectsCreated() == true )
- {
- LPDXUTCALLBACKDEVICEDESTROYED pCallbackDeviceDestroyed = GetDXUTState().GetDeviceDestroyedFunc();
- if( pCallbackDeviceDestroyed != NULL )
- pCallbackDeviceDestroyed( GetDXUTState().GetDeviceDestroyedFuncUserContext() );
- GetDXUTState().SetDeviceObjectsCreated( false );
- // Call the resource cache device destory function
- DXUTGetGlobalResourceCache().OnDestroyDevice();
- }
- GetDXUTState().SetInsideDeviceCallback( false );
- // Release the D3D device and in debug configs, displays a message box if there
- // are unrelease objects.
- if( pd3dDevice )
- {
- if( pd3dDevice->Release() > 0 )
- {
- DXUTDisplayErrorMessage( DXUTERR_NONZEROREFCOUNT );
- DXUT_ERR( L"DXUTCleanup3DEnvironment", DXUTERR_NONZEROREFCOUNT );
- }
- }
- GetDXUTState().SetD3DDevice( NULL );
- if( bReleaseSettings )
- {
- DXUTDeviceSettings* pOldDeviceSettings = GetDXUTState().GetCurrentDeviceSettings();
- SAFE_DELETE(pOldDeviceSettings);
- GetDXUTState().SetCurrentDeviceSettings( NULL );
- }
- D3DSURFACE_DESC* pbackBufferSurfaceDesc = GetDXUTState().GetBackBufferSurfaceDesc();
- ZeroMemory( pbackBufferSurfaceDesc, sizeof(D3DSURFACE_DESC) );
- D3DCAPS9* pd3dCaps = GetDXUTState().GetCaps();
- ZeroMemory( pd3dCaps, sizeof(D3DCAPS9) );
- GetDXUTState().SetDeviceCreated( false );
- }
- }
- //--------------------------------------------------------------------------------------
- // Stores back buffer surface desc in GetDXUTState().GetBackBufferSurfaceDesc()
- //--------------------------------------------------------------------------------------
- void DXUTUpdateBackBufferDesc()
- {
- HRESULT hr;
- IDirect3DSurface9* pBackBuffer;
- hr = GetDXUTState().GetD3DDevice()->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
- D3DSURFACE_DESC* pBBufferSurfaceDesc = GetDXUTState().GetBackBufferSurfaceDesc();
- ZeroMemory( pBBufferSurfaceDesc, sizeof(D3DSURFACE_DESC) );
- if( SUCCEEDED(hr) )
- {
- pBackBuffer->GetDesc( pBBufferSurfaceDesc );
- SAFE_RELEASE( pBackBuffer );
- }
- }
- //--------------------------------------------------------------------------------------
- // Starts a user defined timer callback
- //--------------------------------------------------------------------------------------
- HRESULT DXUTSetTimer( LPDXUTCALLBACKTIMER pCallbackTimer, float fTimeoutInSecs, UINT* pnIDEvent, void* pCallbackUserContext )
- {
- if( pCallbackTimer == NULL )
- return DXUT_ERR_MSGBOX( L"DXUTSetTimer", E_INVALIDARG );
- HRESULT hr;
- DXUT_TIMER DXUTTimer;
- DXUTTimer.pCallbackTimer = pCallbackTimer;
- DXUTTimer.pCallbackUserContext = pCallbackUserContext;
- DXUTTimer.fTimeoutInSecs = fTimeoutInSecs;
- DXUTTimer.fCountdown = fTimeoutInSecs;
- DXUTTimer.bEnabled = true;
- DXUTTimer.nID = GetDXUTState().GetTimerLastID() + 1;
- GetDXUTState().SetTimerLastID( DXUTTimer.nID );
- CGrowableArray<DXUT_TIMER>* pTimerList = GetDXUTState().GetTimerList();
- if( pTimerList == NULL )
- {
- pTimerList = new CGrowableArray<DXUT_TIMER>;
- if( pTimerList == NULL )
- return E_OUTOFMEMORY;
- GetDXUTState().SetTimerList( pTimerList );
- }
- if( FAILED( hr = pTimerList->Add( DXUTTimer ) ) )
- return hr;
- if( pnIDEvent )
- *pnIDEvent = DXUTTimer.nID;
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Stops a user defined timer callback
- //--------------------------------------------------------------------------------------
- HRESULT DXUTKillTimer( UINT nIDEvent )
- {
- CGrowableArray<DXUT_TIMER>* pTimerList = GetDXUTState().GetTimerList();
- if( pTimerList == NULL )
- return S_FALSE;
- bool bFound = false;
- for( int i=0; i<pTimerList->GetSize(); i++ )
- {
- DXUT_TIMER DXUTTimer = pTimerList->GetAt(i);
- if( DXUTTimer.nID == nIDEvent )
- {
- DXUTTimer.bEnabled = false;
- pTimerList->SetAt(i, DXUTTimer);
- bFound = true;
- break;
- }
- }
- if( !bFound )
- return DXUT_ERR_MSGBOX( L"DXUTKillTimer", E_INVALIDARG );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Internal helper function to handle calling the user defined timer callbacks
- //--------------------------------------------------------------------------------------
- void DXUTHandleTimers()
- {
- float fElapsedTime = DXUTGetElapsedTime();
- CGrowableArray<DXUT_TIMER>* pTimerList = GetDXUTState().GetTimerList();
- if( pTimerList == NULL )
- return;
- // Walk through the list of timer callbacks
- for( int i=0; i<pTimerList->GetSize(); i++ )
- {
- DXUT_TIMER DXUTTimer = pTimerList->GetAt(i);
- if( DXUTTimer.bEnabled )
- {
- DXUTTimer.fCountdown -= fElapsedTime;
- // Call the callback if count down expired
- if( DXUTTimer.fCountdown < 0 )
- {
- DXUTTimer.pCallbackTimer( i, DXUTTimer.pCallbackUserContext );
- DXUTTimer.fCountdown = DXUTTimer.fTimeoutInSecs;
- }
- pTimerList->SetAt(i, DXUTTimer);
- }
- }
- }
- //--------------------------------------------------------------------------------------
- // External state access functions
- //--------------------------------------------------------------------------------------
- IDirect3D9* DXUTGetD3DObject() { return GetDXUTState().GetD3D(); }
- IDirect3DDevice9* DXUTGetD3DDevice() { return GetDXUTState().GetD3DDevice(); }
- const D3DSURFACE_DESC* DXUTGetBackBufferSurfaceDesc() { return GetDXUTState().GetBackBufferSurfaceDesc(); }
- const D3DCAPS9* DXUTGetDeviceCaps() { return GetDXUTState().GetCaps(); }
- HINSTANCE DXUTGetHINSTANCE() { return GetDXUTState().GetHInstance(); }
- HWND DXUTGetHWND() { return DXUTIsWindowed() ? GetDXUTState().GetHWNDDeviceWindowed() : GetDXUTState().GetHWNDDeviceFullScreen(); }
- HWND DXUTGetHWNDFocus() { return GetDXUTState().GetHWNDFocus(); }
- HWND DXUTGetHWNDDeviceFullScreen() { return GetDXUTState().GetHWNDDeviceFullScreen(); }
- HWND DXUTGetHWNDDeviceWindowed() { return GetDXUTState().GetHWNDDeviceWindowed(); }
- RECT DXUTGetWindowClientRect() { RECT rc; GetClientRect( DXUTGetHWND(), &rc ); return rc; }
- RECT DXUTGetWindowClientRectAtModeChange() { RECT rc = { 0, 0, GetDXUTState().GetWindowBackBufferWidthAtModeChange(), GetDXUTState().GetWindowBackBufferHeightAtModeChange() }; return rc; }
- RECT DXUTGetFullsceenClientRectAtModeChange() { RECT rc = { 0, 0, GetDXUTState().GetFullScreenBackBufferWidthAtModeChange(), GetDXUTState().GetFullScreenBackBufferHeightAtModeChange() }; return rc; }
- double DXUTGetTime() { return GetDXUTState().GetTime(); }
- float DXUTGetElapsedTime() { return GetDXUTState().GetElapsedTime(); }
- float DXUTGetFPS() { return GetDXUTState().GetFPS(); }
- LPCWSTR DXUTGetWindowTitle() { return GetDXUTState().GetWindowTitle(); }
- LPCWSTR DXUTGetDeviceStats() { return GetDXUTState().GetDeviceStats(); }
- bool DXUTIsRenderingPaused() { return GetDXUTState().GetPauseRenderingCount() > 0; }
- bool DXUTIsTimePaused() { return GetDXUTState().GetPauseTimeCount() > 0; }
- bool DXUTIsActive() { return GetDXUTState().GetActive(); }
- int DXUTGetExitCode() { return GetDXUTState().GetExitCode(); }
- bool DXUTGetShowMsgBoxOnError() { return GetDXUTState().GetShowMsgBoxOnError(); }
- bool DXUTGetAutomation() { return GetDXUTState().GetAutomation(); }
- bool DXUTGetHandleDefaultHotkeys() { return GetDXUTState().GetHandleDefaultHotkeys(); }
- bool DXUTIsKeyDown( BYTE vKey )
- {
- bool* bKeys = GetDXUTState().GetKeys();
- if( vKey >= 0xA0 && vKey <= 0xA5 ) // VK_LSHIFT, VK_RSHIFT, VK_LCONTROL, VK_RCONTROL, VK_LMENU, VK_RMENU
- return GetAsyncKeyState( vKey ) != 0; // these keys only are tracked via GetAsyncKeyState()
- else if( vKey >= 0x01 && vKey <= 0x06 && vKey != 0x03 ) // mouse buttons (VK_*BUTTON)
- return DXUTIsMouseButtonDown(vKey);
- else
- return bKeys[vKey];
- }
- bool DXUTIsMouseButtonDown( BYTE vButton )
- {
- bool* bMouseButtons = GetDXUTState().GetMouseButtons();
- int nIndex = DXUTMapButtonToArrayIndex(vButton);
- return bMouseButtons[nIndex];
- }
- void DXUTSetMultimonSettings( bool bAutoChangeAdapter )
- {
- GetDXUTState().SetAutoChangeAdapter( bAutoChangeAdapter );
- }
- void DXUTSetCursorSettings( bool bShowCursorWhenFullScreen, bool bClipCursorWhenFullScreen )
- {
- GetDXUTState().SetClipCursorWhenFullScreen(bClipCursorWhenFullScreen);
- GetDXUTState().SetShowCursorWhenFullScreen(bShowCursorWhenFullScreen);
- DXUTSetupCursor();
- }
- void DXUTSetWindowSettings( bool bCallDefWindowProc )
- {
- GetDXUTState().SetCallDefWindowProc( bCallDefWindowProc );
- }
- void DXUTSetConstantFrameTime( bool bEnabled, float fTimePerFrame )
- {
- if( GetDXUTState().GetOverrideConstantFrameTime() )
- {
- bEnabled = GetDXUTState().GetOverrideConstantFrameTime();
- fTimePerFrame = GetDXUTState().GetOverrideConstantTimePerFrame();
- }
- GetDXUTState().SetConstantFrameTime(bEnabled);
- GetDXUTState().SetTimePerFrame(fTimePerFrame);
- }
- //--------------------------------------------------------------------------------------
- // Return if windowed in the current device. If no device exists yet, then returns false
- //--------------------------------------------------------------------------------------
- bool DXUTIsWindowed()
- {
- DXUTDeviceSettings* pDeviceSettings = GetDXUTState().GetCurrentDeviceSettings();
- if(pDeviceSettings)
- return (pDeviceSettings->pp.Windowed != 0);
- else
- return false;
- }
- //--------------------------------------------------------------------------------------
- // Return the present params of the current device. If no device exists yet, then
- // return blank present params
- //--------------------------------------------------------------------------------------
- D3DPRESENT_PARAMETERS DXUTGetPresentParameters()
- {
- DXUTDeviceSettings* pDS = GetDXUTState().GetCurrentDeviceSettings();
- if( pDS )
- {
- return pDS->pp;
- }
- else
- {
- D3DPRESENT_PARAMETERS pp;
- ZeroMemory( &pp, sizeof(D3DPRESENT_PARAMETERS) );
- return pp;
- }
- }
- //--------------------------------------------------------------------------------------
- // Return the device settings of the current device. If no device exists yet, then
- // return blank device settings
- //--------------------------------------------------------------------------------------
- DXUTDeviceSettings DXUTGetDeviceSettings()
- {
- DXUTDeviceSettings* pDS = GetDXUTState().GetCurrentDeviceSettings();
- if( pDS )
- {
- return *pDS;
- }
- else
- {
- DXUTDeviceSettings ds;
- ZeroMemory( &ds, sizeof(DXUTDeviceSettings) );
- return ds;
- }
- }
- #ifndef SM_REMOTESESSION // needs WINVER >= 0x0500
- #define SM_REMOTESESSION 0x1000
- #endif
- //--------------------------------------------------------------------------------------
- // Display an custom error msg box
- //--------------------------------------------------------------------------------------
- void DXUTDisplayErrorMessage( HRESULT hr )
- {
- WCHAR strBuffer[512];
- int nExitCode;
- bool bFound = true;
- switch( hr )
- {
- case DXUTERR_NODIRECT3D: nExitCode = 2; StringCchCopy( strBuffer, 512, L"Could not initialize Direct3D. You may want to check that the latest version of DirectX is correctly installed on your system. Also make sure that this program was compiled with header files that match the installed DirectX DLLs." ); break;
- case DXUTERR_INCORRECTVERSION: nExitCode = 10; StringCchCopy( strBuffer, 512, L"Incorrect version of Direct3D and/or D3DX." ); break;
- case DXUTERR_MEDIANOTFOUND: nExitCode = 4; StringCchCopy( strBuffer, 512, L"Could not find required media. Ensure that the DirectX SDK is correctly installed." ); break;
- case DXUTERR_NONZEROREFCOUNT: nExitCode = 5; StringCchCopy( strBuffer, 512, L"The D3D device has a non-zero reference count, meaning some objects were not released." ); break;
- case DXUTERR_CREATINGDEVICE: nExitCode = 6; StringCchCopy( strBuffer, 512, L"Failed creating the Direct3D device." ); break;
- case DXUTERR_RESETTINGDEVICE: nExitCode = 7; StringCchCopy( strBuffer, 512, L"Failed resetting the Direct3D device." ); break;
- case DXUTERR_CREATINGDEVICEOBJECTS: nExitCode = 8; StringCchCopy( strBuffer, 512, L"Failed creating Direct3D device objects." ); break;
- case DXUTERR_RESETTINGDEVICEOBJECTS: nExitCode = 9; StringCchCopy( strBuffer, 512, L"Failed resetting Direct3D device objects." ); break;
- case DXUTERR_NOCOMPATIBLEDEVICES:
- nExitCode = 3;
- if( GetSystemMetrics(SM_REMOTESESSION) != 0 )
- StringCchCopy( strBuffer, 512, L"Direct3D does not work over a remote session." );
- else
- StringCchCopy( strBuffer, 512, L"Could not find any compatible Direct3D devices." );
- break;
- default: bFound = false; nExitCode = 1;break;
- }
- GetDXUTState().SetExitCode(nExitCode);
- bool bShowMsgBoxOnError = GetDXUTState().GetShowMsgBoxOnError();
- if( bFound && bShowMsgBoxOnError )
- {
- if( DXUTGetWindowTitle()[0] == 0 )
- MessageBox( DXUTGetHWND(), strBuffer, L"DirectX Application", MB_ICONERROR|MB_OK );
- else
- MessageBox( DXUTGetHWND(), strBuffer, DXUTGetWindowTitle(), MB_ICONERROR|MB_OK );
- }
- }
- //--------------------------------------------------------------------------------------
- // Display error msg box to help debug
- //--------------------------------------------------------------------------------------
- HRESULT WINAPI DXUTTrace( const CHAR* strFile, DWORD dwLine, HRESULT hr,
- const WCHAR* strMsg, bool bPopMsgBox )
- {
- bool bShowMsgBoxOnError = GetDXUTState().GetShowMsgBoxOnError();
- if( bPopMsgBox && bShowMsgBoxOnError == false )
- bPopMsgBox = false;
- return DXTrace( strFile, dwLine, hr, strMsg, bPopMsgBox );
- }
- //--------------------------------------------------------------------------------------
- // Checks to see if the HWND changed monitors, and if it did it creates a device
- // from the monitor's adapter and recreates the scene.
- //--------------------------------------------------------------------------------------
- void DXUTCheckForWindowChangingMonitors()
- {
- // Skip this check for various reasons
- if( !GetDXUTState().GetAutoChangeAdapter() ||
- GetDXUTState().GetIgnoreSizeChange() ||
- !GetDXUTState().GetDeviceCreated() ||
- !GetDXUTState().GetCurrentDeviceSettings()->pp.Windowed )
- {
- return;
- }
- HRESULT hr;
- HMONITOR hWindowMonitor = DXUTMonitorFromWindow( DXUTGetHWND(), MONITOR_DEFAULTTOPRIMARY );
- HMONITOR hAdapterMonitor = GetDXUTState().GetAdapterMonitor();
- if( hWindowMonitor != hAdapterMonitor )
- {
- UINT newOrdinal;
- if( SUCCEEDED( DXUTGetAdapterOrdinalFromMonitor( hWindowMonitor, &newOrdinal ) ) )
- {
- // Find the closest valid device settings with the new ordinal
- DXUTDeviceSettings deviceSettings = DXUTGetDeviceSettings();
- deviceSettings.AdapterOrdinal = newOrdinal;
- DXUTMatchOptions matchOptions;
- matchOptions.eAdapterOrdinal = DXUTMT_PRESERVE_INPUT;
- matchOptions.eDeviceType = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eWindowed = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eAdapterFormat = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eVertexProcessing = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eResolution = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eBackBufferFormat = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eBackBufferCount = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eMultiSample = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eSwapEffect = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eDepthFormat = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eStencilFormat = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.ePresentFlags = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.eRefreshRate = DXUTMT_CLOSEST_TO_INPUT;
- matchOptions.ePresentInterval = DXUTMT_CLOSEST_TO_INPUT;
- hr = DXUTFindValidDeviceSettings( &deviceSettings, &deviceSettings, &matchOptions );
- if( SUCCEEDED(hr) )
- {
- // Create a Direct3D device using the new device settings.
- // If there is an existing device, then it will either reset or recreate the scene.
- hr = DXUTChangeDevice( &deviceSettings, NULL, false, false );
- // If hr == E_ABORT, this means the app rejected the device settings in the ModifySettingsCallback
- if( hr == E_ABORT )
- {
- // so nothing changed and keep from attempting to switch adapters next time
- GetDXUTState().SetAutoChangeAdapter( false );
- }
- else if( FAILED(hr) )
- {
- DXUTShutdown();
- DXUTPause( false, false );
- return;
- }
- }
- }
- }
- }
- //--------------------------------------------------------------------------------------
- // Look for an adapter ordinal that is tied to a HMONITOR
- //--------------------------------------------------------------------------------------
- HRESULT DXUTGetAdapterOrdinalFromMonitor( HMONITOR hMonitor, UINT* pAdapterOrdinal )
- {
- *pAdapterOrdinal = 0;
- CD3DEnumeration* pd3dEnum = DXUTPrepareEnumerationObject();
- IDirect3D9* pD3D = DXUTGetD3DObject();
- CGrowableArray<CD3DEnumAdapterInfo*>* pAdapterList = pd3dEnum->GetAdapterInfoList();
- for( int iAdapter=0; iAdapter<pAdapterList->GetSize(); iAdapter++ )
- {
- CD3DEnumAdapterInfo* pAdapterInfo = pAdapterList->GetAt(iAdapter);
- HMONITOR hAdapterMonitor = pD3D->GetAdapterMonitor( pAdapterInfo->AdapterOrdinal );
- if( hAdapterMonitor == hMonitor )
- {
- *pAdapterOrdinal = pAdapterInfo->AdapterOrdinal;
- return S_OK;
- }
- }
- return E_FAIL;
- }
- //--------------------------------------------------------------------------------------
- // Internal function to map MK_* to an array index
- //--------------------------------------------------------------------------------------
- int DXUTMapButtonToArrayIndex( BYTE vButton )
- {
- switch( vButton )
- {
- case MK_LBUTTON: return 0;
- case VK_MBUTTON:
- case MK_MBUTTON: return 1;
- case MK_RBUTTON: return 2;
- case VK_XBUTTON1:
- case MK_XBUTTON1: return 3;
- case VK_XBUTTON2:
- case MK_XBUTTON2: return 4;
- }
- return 0;
- }
- //--------------------------------------------------------------------------------------
- // Setup cursor based on current settings (window/fullscreen mode, show cursor state, clip cursor state)
- //--------------------------------------------------------------------------------------
- void DXUTSetupCursor()
- {
- // Show the cursor again if returning to fullscreen
- IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
- if( !DXUTIsWindowed() && pd3dDevice )
- {
- if( GetDXUTState().GetShowCursorWhenFullScreen() )
- {
- SetCursor( NULL ); // Turn off Windows cursor in full screen mode
- HCURSOR hCursor = (HCURSOR)(ULONG_PTR)GetClassLongPtr( DXUTGetHWNDDeviceFullScreen(), GCLP_HCURSOR );
- DXUTSetDeviceCursor( pd3dDevice, hCursor, false );
- DXUTGetD3DDevice()->ShowCursor( true );
- }
- else
- {
- SetCursor( NULL ); // Turn off Windows cursor in full screen mode
- DXUTGetD3DDevice()->ShowCursor( false );
- }
- }
- // Clip cursor if requested
- if( !DXUTIsWindowed() && GetDXUTState().GetClipCursorWhenFullScreen() )
- {
- // Confine cursor to full screen window
- RECT rcWindow;
- GetWindowRect( DXUTGetHWNDDeviceFullScreen(), &rcWindow );
- ClipCursor( &rcWindow );
- }
- else
- {
- ClipCursor( NULL );
- }
- }
- //--------------------------------------------------------------------------------------
- // Gives the D3D device a cursor with image and hotspot from hCursor.
- //--------------------------------------------------------------------------------------
- HRESULT DXUTSetDeviceCursor( IDirect3DDevice9* pd3dDevice, HCURSOR hCursor, bool bAddWatermark )
- {
- HRESULT hr = E_FAIL;
- ICONINFO iconinfo;
- bool bBWCursor;
- LPDIRECT3DSURFACE9 pCursorSurface = NULL;
- HDC hdcColor = NULL;
- HDC hdcMask = NULL;
- HDC hdcScreen = NULL;
- BITMAP bm;
- DWORD dwWidth;
- DWORD dwHeightSrc;
- DWORD dwHeightDest;
- COLORREF crColor;
- COLORREF crMask;
- UINT x;
- UINT y;
- BITMAPINFO bmi;
- COLORREF* pcrArrayColor = NULL;
- COLORREF* pcrArrayMask = NULL;
- DWORD* pBitmap;
- HGDIOBJ hgdiobjOld;
- ZeroMemory( &iconinfo, sizeof(iconinfo) );
- if( !GetIconInfo( hCursor, &iconinfo ) )
- goto End;
- if (0 == GetObject((HGDIOBJ)iconinfo.hbmMask, sizeof(BITMAP), (LPVOID)&bm))
- goto End;
- dwWidth = bm.bmWidth;
- dwHeightSrc = bm.bmHeight;
- if( iconinfo.hbmColor == NULL )
- {
- bBWCursor = TRUE;
- dwHeightDest = dwHeightSrc / 2;
- }
- else
- {
- bBWCursor = FALSE;
- dwHeightDest = dwHeightSrc;
- }
- // Create a surface for the fullscreen cursor
- if( FAILED( hr = pd3dDevice->CreateOffscreenPlainSurface( dwWidth, dwHeightDest,
- D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pCursorSurface, NULL ) ) )
- {
- goto End;
- }
- pcrArrayMask = new DWORD[dwWidth * dwHeightSrc];
- ZeroMemory(&bmi, sizeof(bmi));
- bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
- bmi.bmiHeader.biWidth = dwWidth;
- bmi.bmiHeader.biHeight = dwHeightSrc;
- bmi.bmiHeader.biPlanes = 1;
- bmi.bmiHeader.biBitCount = 32;
- bmi.bmiHeader.biCompression = BI_RGB;
- hdcScreen = GetDC( NULL );
- hdcMask = CreateCompatibleDC( hdcScreen );
- if( hdcMask == NULL )
- {
- hr = E_FAIL;
- goto End;
- }
- hgdiobjOld = SelectObject(hdcMask, iconinfo.hbmMask);
- GetDIBits(hdcMask, iconinfo.hbmMask, 0, dwHeightSrc,
- pcrArrayMask, &bmi, DIB_RGB_COLORS);
- SelectObject(hdcMask, hgdiobjOld);
- if (!bBWCursor)
- {
- pcrArrayColor = new DWORD[dwWidth * dwHeightDest];
- hdcColor = CreateCompatibleDC( hdcScreen );
- if( hdcColor == NULL )
- {
- hr = E_FAIL;
- goto End;
- }
- SelectObject(hdcColor, iconinfo.hbmColor);
- GetDIBits(hdcColor, iconinfo.hbmColor, 0, dwHeightDest,
- pcrArrayColor, &bmi, DIB_RGB_COLORS);
- }
- // Transfer cursor image into the surface
- D3DLOCKED_RECT lr;
- pCursorSurface->LockRect( &lr, NULL, 0 );
- pBitmap = (DWORD*)lr.pBits;
- for( y = 0; y < dwHeightDest; y++ )
- {
- for( x = 0; x < dwWidth; x++ )
- {
- if (bBWCursor)
- {
- crColor = pcrArrayMask[dwWidth*(dwHeightDest-1-y) + x];
- crMask = pcrArrayMask[dwWidth*(dwHeightSrc-1-y) + x];
- }
- else
- {
- crColor = pcrArrayColor[dwWidth*(dwHeightDest-1-y) + x];
- crMask = pcrArrayMask[dwWidth*(dwHeightDest-1-y) + x];
- }
- if (crMask == 0)
- pBitmap[dwWidth*y + x] = 0xff000000 | crColor;
- else
- pBitmap[dwWidth*y + x] = 0x00000000;
- // It may be helpful to make the D3D cursor look slightly
- // different from the Windows cursor so you can distinguish
- // between the two when developing/testing code. When
- // bAddWatermark is TRUE, the following code adds some
- // small grey "D3D" characters to the upper-left corner of
- // the D3D cursor image.
- if( bAddWatermark && x < 12 && y < 5 )
- {
- // 11.. 11.. 11.. .... CCC0
- // 1.1. ..1. 1.1. .... A2A0
- // 1.1. .1.. 1.1. .... A4A0
- // 1.1. ..1. 1.1. .... A2A0
- // 11.. 11.. 11.. .... CCC0
- const WORD wMask[5] = { 0xccc0, 0xa2a0, 0xa4a0, 0xa2a0, 0xccc0 };
- if( wMask[y] & (1 << (15 - x)) )
- {
- pBitmap[dwWidth*y + x] |= 0xff808080;
- }
- }
- }
- }
- pCursorSurface->UnlockRect();
- // Set the device cursor
- if( FAILED( hr = pd3dDevice->SetCursorProperties( iconinfo.xHotspot,
- iconinfo.yHotspot, pCursorSurface ) ) )
- {
- goto End;
- }
- hr = S_OK;
- End:
- if( iconinfo.hbmMask != NULL )
- DeleteObject( iconinfo.hbmMask );
- if( iconinfo.hbmColor != NULL )
- DeleteObject( iconinfo.hbmColor );
- if( hdcScreen != NULL )
- ReleaseDC( NULL, hdcScreen );
- if( hdcColor != NULL )
- DeleteDC( hdcColor );
- if( hdcMask != NULL )
- DeleteDC( hdcMask );
- SAFE_DELETE_ARRAY( pcrArrayColor );
- SAFE_DELETE_ARRAY( pcrArrayMask );
- SAFE_RELEASE( pCursorSurface );
- return hr;
- }