DXUT框架用来帮助程序员花更少的时间来解决下列问题:创建窗口、创建Direct3D设备、进行消息循环和处理设备事件。在DXUT框架基础上编写代码,可以快速高效地进行Direct3D程序设计,大多数Direct3D SDK示例程序使用了DXUT框架。
下面的代码是AppFrame示例程序的WinMain函数:
INT WINAPI wWinMain( HINSTANCE, HINSTANCE, LPWSTR, int )
{
// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
// Set the callback functions
DXUTSetCallbackD3D9DeviceAcceptable(IsD3D9DeviceAcceptable);
DXUTSetCallbackD3D9DeviceCreated(OnD3D9CreateDevice);
DXUTSetCallbackD3D9DeviceReset(OnD3D9ResetDevice);
DXUTSetCallbackD3D9FrameRender(OnD3D9FrameRender);
DXUTSetCallbackD3D9DeviceLost(OnD3D9LostDevice);
DXUTSetCallbackD3D9DeviceDestroyed(OnD3D9DestroyDevice);
DXUTSetCallbackDeviceChanging(ModifyDeviceSettings);
DXUTSetCallbackMsgProc(MsgProc);
DXUTSetCallbackFrameMove(OnFrameMove);
// TODO: Perform any application-level initialization here
// Initialize DXUT and create the desired Win32 window and Direct3D device for the application
DXUTInit( true, true ); // Parse the command line and show msgboxes
DXUTSetHotkeyHandling( true, true, true ); // handle the default hotkeys
DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
DXUTCreateWindow( L"AppFrame Sample" );
DXUTCreateDevice( true, 640, 480 );
// Start the render loop
DXUTMainLoop();
// TODO: Perform any application-level cleanup here
return DXUTGetExitCode();
}
在上面的代码中,DXUT框架做了大部分的工作。它创建了一个窗口和一个Direct3D设备,处理消息循环、并当事件发生时(例如重新设置设备或渲染一帧)调用应用程序提供的回调函数。 DXUT框架是模块化的,所以应用程序可以使用DXUT框架的所有函数或其中的一部分。
下面这组代码时一组注册函数的调用:
// Set the callback functions
DXUTSetCallbackD3D9DeviceAcceptable(IsD3D9DeviceAcceptable);
DXUTSetCallbackD3D9DeviceCreated(OnD3D9CreateDevice);
DXUTSetCallbackD3D9DeviceReset(OnD3D9ResetDevice);
DXUTSetCallbackD3D9FrameRender(OnD3D9FrameRender);
DXUTSetCallbackD3D9DeviceLost(OnD3D9LostDevice);
DXUTSetCallbackD3D9DeviceDestroyed(OnD3D9DestroyDevice);
DXUTSetCallbackDeviceChanging(ModifyDeviceSettings);
DXUTSetCallbackMsgProc(MsgProc);
DXUTSetCallbackFrameMove(OnFrameMove);
以函数DXUTSetCallbackD3D9DeviceCreated为例,它的声明如下:
Sets the Direct3D 9 device created callback function.
VOID DXUTSetCallbackD3D9DeviceCreated(
LPDXUTCALLBACKD3D9DEVICECREATED pCallback,
void* pUserContext
);
Parameters
- pCallback
- [in] Pointer to a LPDXUTCALLBACKD3D9DEVICECREATED callback function. If the callback function is supplied, it will be called after the Direct3D 9 device has been created. Device creation will happen during application initialization and if the device is changed. If NULL, DXUT will not notify the application about device creation.
- pUserContext
- [in] Pointer to a user-defined value which is passed to the callback function. Typically used by an application to pass a pointer to a data structure that provides context information for the callback function. The default value is NULL
Return Values
No return value.
Remarks
This function only needs to be called if the application supports rendering with Direct3D 9 device.
The LPDXUTCALLBACKD3D9DEVICECREATED callback function is the appropriate location for the application to create Direct3D 9 device resources that will live through a device reset such as D3DPOOL_MANAGED or D3DPOOL_SYSTEMMEM memory and that aren't tied to the back buffer size. Resources created in the LPDXUTCALLBACKD3D9DEVICECREATED callback function should be released in the LPDXUTCALLBACKD3D9DEVICEDESTROYED callback function.
LPDXUTCALLBACKD3D9DEVICECREATED
Application-defined resource creation callback function, called by DXUT after the Direct3D 9 device is created. Passes a pointer to the newly created Direct3D 9 device.
HRESULT LPDXUTCALLBACKD3D9DEVICECREATED(
IDirect3DDevice9 * pd3dDevice,
CONST D3DSURFACE_DESC * pBackBufferSurfaceDesc,
void* pUserContext
);
Parameters
- pd3dDevice
- [out] Pointer to the newly created Direct3D 9 device.
- pBackBufferSurfaceDesc
- [out] Pointer to the back buffer surface description
- pUserContext
- [in] Pointer to a user-defined value which is passed to the callback function. Typically used by an application to pass a pointer to a data structure that provides context information for the callback function. The default value is NULL
Return Values
In general, if no error occurs, program the function to return S_OK. Program the function to return an HRESULT failure code if the function fails. If DXUT receives a failure HRESULT code, it shuts down the application.
Remarks
The LPDXUTCALLBACKD3D9DEVICECREATED callback function is the appropriate location for the application to create Direct3D 9 device resources that will live through a device reset such as D3DPOOL_MANAGED or D3DPOOL_SYSTEMMEM memory and that aren't tied to the back buffer size. Resources created in the LPDXUTCALLBACKD3D9DEVICECREATED callback function should be released in the LPDXUTCALLBACKD3D9DEVICEDESTROYED callback function.
该注册函数的作用在于通知应用程序,在应用程序的初始化期间或当设备改变时,如果需要创建D3DPOOL_MANAGED类型的资源,就会自动调用函数OnD3D9CreateDevice()进行创建。而程序员需要做的就是编写OnD3D9CreateDevice()函数,告诉应用程序创建哪些资源以及如何创建。其他注册函数的作用同样是通知应用程序,使应用程序在特定时机调用注册函数指定的具体回调函数。程序员的核心工作就是实现这些具体的回调函数,事实上,这种构架正是DXUT框架的核心,也可以把它看成是区别于Direct3D API程序的地方。
DXUT框架提供了下列服务,帮助程序员创建一个应用程序:
(1)简化窗口和设备的创建。
(2)声明设备事件(创建、重置、丢失、销毁)和窗口事件(消息、键盘、鼠标)。
(3)在窗口模式和全屏模式间切换,在硬件抽象层设备和参考设备间切换。
(4)高分辨率计时器。
(5)为自动测试提供命令行支持。
(6)通过对话框或API选择设备。
(7)纹理GUI控件组,包括IME-enable文本框。
(8)附加杂类,例如简单的摄像机类。
为使用方便,DXUT框架支持Direct3D设备和窗口一一对应(一个设备只能对应一个窗口)。对于需要同时使用多个设备或显示多个Direct3D窗口的高级应用程序,该框架不支持。不过,大多数应用程序只使用一个窗口和一个Direct3D设备,所以大部分应用程序都能使用该框架。