在窗口和設備創建好之后,應用程序需要使用消息循環處理窗口消息、更新和渲染場景、處理設備事件。應用程序可以實現自己的消息循環,也可以使用DXUT消息循環,注冊相應的回調函數,可以讓DXUT處理設備、幀消息事件。
進入消息循環
為使用DXUT框架的消息循環,可以調用DXUTMainLoop()函數:
Starts the main execution loop of DXUT.
HRESULT DXUTMainLoop(
HACCEL hAccel
) ;
Parameters
- hAccel
- [in] Handle to an accelerator table to use in
translating keyboard messages from the Windows message queue, or NULL if not
using an accelerator table. The default value is NULL.
Return Values
If the function succeeds, the return value is S_OK. If
the function fails, the return value can be one of the error codes in DXUTERR.
Remarks
This function starts the message loop that will run for
the lifetime of the application. During execution, DXUTMainLoop
calls the registered callback functions to ask the application to update and
render the frame, as well as handle any device or input events.
Custom Main Loop
For some advanced applications a custom main loop may be a better design. It
is possible to use DXUT with a custom main loop. An example of how to do this is
shown below.
INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, INT )
{
DXUTSetCallbackD3D9DeviceAcceptable( IsDeviceAcceptable );
DXUTSetCallbackD3D9DeviceCreated( OnCreateDevice );
DXUTSetCallbackD3D9DeviceReset( OnResetDevice );
DXUTSetCallbackD3D9FrameRender( OnFrameRender );
DXUTSetCallbackD3D9DeviceLost( OnLostDevice );
DXUTSetCallbackD3D9DeviceDestroyed( OnDestroyDevice );
DXUTSetCallbackMsgProc( MsgProc );
DXUTSetCallbackKeyboard( KeyboardProc );
DXUTSetCallbackFrameMove( OnFrameMove );
DXUTSetCallbackDeviceChanging( ModifyDeviceSettings );
DXUTInit( true, true );
DXUTCreateWindow( L"Example" );
DXUTCreateDevice( true, 640, 480 );
// Custom main loop
HWND hWnd = DXUTGetHWND();
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( 0 == TranslateAccelerator( hWnd, NULL, &msg ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
else
{
// Render a frame during idle time (no messages are waiting)
DXUTRender3DEnvironment();
}
}
return DXUTGetExitCode();
}
This example calls DXUTRender3DEnvironment to have DXUT update and render the
scene and handle device events. While it is possible for the application to
completely replicate this functionality, it is not recommended.
DXUTRender3DEnvironment
Renders the 3D environment.
VOID DXUTRender3DEnvironment() ;
Parameters
None.
Return Values
No return value.
Remarks
This method does not normally need to be called. It is
useful only when the application does not use DXUTMainLoop but still wants DXUT
to assist with rendering.
This method checks whether the device is lost. If so,
the method attempts to reset the device and then calls the
LPDXUTCALLBACKFRAMEMOVE and LPDXUTCALLBACKD3D10FRAMERENDER callback functions.
If the application window is minimized or the
application is paused, CPU time is yielded to other processes.
處理事件
框架使用回調函數機制來使應用程序對事件做出反應。應用程序只需對框架注冊和設置相應的函數指針,則當事件發生時,框架就會調用相應的函數。框架不需要注冊所有的回調函數,所以應用程序只須對所需要的回調函數進行注冊即可。通過為回調函數設置參數pUserContext,回調函數可以從應用程序接受內容,比如將該參數設置為一個指向類對象的指針。
DXUT框架可以處理以下事件類型:
(1)設備事件
當應用程序使用Direct3D設備渲染圖形時,該設備有可能處于丟失狀態。這種情況的發生有多種原因,例如按下Alt +
Tab鍵離開一個全屏模式的應用程序,或者按下Ctrl + Alt +
Del鍵,或者啟動了另一個全屏3D應用程序。發生這種情況時,當調用一些函數(如Present)時,Direct3D
API通過返回D3DERR_DEVICELOST通知應用程序設備丟失。
當設備丟失時,應用程序負責釋放所有不能在設備丟失時存在的Direct3D資源對象,如在D3DPOOL_DEFAULT內存池中創建的對象。如果沒有釋放這些對象,那么該設備從丟失狀態返回時就不能被重新設置。當設備丟失時,應用程序必須等待。當設備返回時,應用程序必須調用函數
IDirect3DDevice9::Reset(),并重新創建所有不能在Reset()函數中存在的對象。
通過DXUT框架,這個過程可以通過在應用程序中使用回調函數來簡化,這些回調函數處理各種設備事件:設備改變、創建、重新設置、丟失或銷毀。當設備丟失時,框架會有提示;當它從丟失狀態返回時,框架會適當調用相應的回調函數,重新設置該設備,即框架使用應用程序的回調函數在適當的時間釋放和重新創建設備對象。應用程序需要做的是注冊并實現相關回調函數,各回調函數的類型、注冊、調用時機等細節見下表:
注冊函數 |
應用程序回調函數 |
框架調用時機 |
創建資源 |
釋放資源 |
DXUTSetCallback-
DeviceChanging |
LPDXUTCALLBACK-
MODIFYDEVICESETTINGS |
在創建Direct3D設備之前調用,應用程序可以返回FALSE,拒絕改變該設備。 |
x |
x |
DXUTSetCallback-
D3D9DeviceCreated |
LPDXUTCALLBACK-
D3D9DEVICECREATED |
當應用程序初始化和重新創建設備時,在Direct3D設備創建之后立即調用。 |
創建D3DPOOL_MANAGED資源,因為這些資源無論什么時候被銷毀都需要重新加載,但這些資源被重新設置時不需要重新加載。在這里創建的資源需要在LPDXUTCALLBACK-DEVICEDESTROYED中釋放。 |
x |
DXUTSetCallback-
D3D9DeviceReset |
LPDXUTCALLBACK-
D3D9DEVICERESET |
當Direct3D設備丟失又被重新設置后立即調用。 |
創建D3DPOOL_DEFAULT資源,因為這些資源無論什么時候丟失或重新設置時都需要重新加載,在這里創建的資源需要在LPDXUTCALLBACK-DEVICELOST中釋放。 |
x |
DXUTSetCallback-
D3D9DeviceLost |
LPDXUTCALLBACK-
D3D9DEVICELOST |
當Direct3D設備變為丟失狀態且在Reset調用之前,立即調用。 |
x |
釋放在回調函數LPDXUTCALLBACK-D3D9DEVICERESET中創建的資源,這些資源通常包括所有的D3DPOOL_DEFAULT資源。 |
DXUTSetCallback-
D3D9DeviceDestroyed |
LPDXUTCALLBACK-
D3D9DEVICEDESTROYED |
當應用程序終止或重新創建設備時,Direct3D設備被銷毀后,立即調用。 |
x |
釋放在回調函數LPDXUTCALLBACK-
D3D9DEVICECREATED中創建的資源,這些資源通常包括所有的D3DPOOL_MANAGED資源。 |
當設備在窗口和全屏模式間切換時常常需要重新設置,但有時它必須通過Direct3D重新創建。
調用這些回調函數是可選的,但如果應用程序沒有使用函數DXUTSetCallbackD3D9DeviceDestroyed()和
DXUTSetCallbackD3D9DeviceCreated()注冊銷毀回調函數和創建回調函數,則改變設備或在硬件抽象層設備和參考設備間切換都不能進行。
類似地,如果沒有用函數DXUTSetCallbackD3D9DeviceLost()和
DXUTSetCallbackD3D9DeviceReset()注冊丟失回調函數和重置回調函數,則當設備丟失或重置設備時,框架無法通知應用程序。這樣一來,所有不在D3DPOOL_MANAGED內存中的設備對象都不能重新設置。