现在的位置: 首页 > 综合 > 正文

DXUT设计指南

2013年10月12日 ⁄ 综合 ⁄ 共 7615字 ⁄ 字号 评论关闭

DXUT是DirectX的框架.这篇文章也在我的左边链接里,这里我做了整合.

我对这篇文章的定级是:手册(要求懂DirectX和DXUT基础)

GameRes游戏开发资源网 http://www.gameres.com译者:zzprogram

DXUT设计指南

    DXUT是一个建立在Direct3D API之上的,被大部分Direct3D指南和例子所使用的层。它的目标是创建Direct3D例子、原型、工具,更容易的建立坚固、专业的游戏。

·           DXUT概观

·           初始DXUT

·           使用基DXUT的程序窗口

·           使用DXUT设备

·           使用DXUT

·           处理DXUT错误

·           DXUT高级设选择

·           DXUT功能

 

 

See also: DXUT参考.

 

 

 

DXUT概观

本主题提供对DXUT概观的高级介绍。

·         概观

·         特点

·        

·         启动一个工程

·         DirectX April 2005 SDK中DXUT的改进

·         自DirectX Summer 2003 SDK以来DXUT的变化

概观

DXUT框架的设计是为了帮助开发者在创建窗口、设备,处理窗口消息和设备事件时,更有效率(消耗较少的时间)

 

这是使用该框架的应用程序的主函数:

INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, INT )

{

    // 设置回调函数,这些函数允许DXUT通知应用程序更换设备,用户输入和窗口消息。

    // 回调函数是可选的,因此你要做的仅是设置你感兴趣的事件的回调函数。

    DXUTSetCallbackDeviceCreated( OnCreateDevice );

    DXUTSetCallbackDeviceReset( OnResetDevice );

    DXUTSetCallbackDeviceLost( OnLostDevice );

    DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );

 

    DXUTSetCallbackFrameRender( OnFrameRender );

    DXUTSetCallbackFrameMove( OnFrameMove );

 

    // 初始化DXUT并创建想要的Win32窗口和应用程序的Direct3D设备。调用这些

// 可选函数中的每一个,此外它们允许你设置几个选项来控制框架的行为。

    DXUTInit( TRUE, TRUE, TRUE );

    DXUTCreateWindow( L"BasicHLSL" );

    DXUTCreateDevice( D3DADAPTER_DEFAULT, TRUE, 640, 480 );

 

    // 通过DXUT来处理消息循环并分派渲染调用。当在空闲时间和处理窗口消息的

// 时间间隔时,框架将调用OnFrameMoveOnFrameRender回调函数。

    DXUTMainLoop();

    return DXUTGetExitCode();

}

在例子代码中,框架做了大部分的工作。它创建窗口、设备,处理主消息循环,当应用程序事件触发时,提供相应的回调函数,例如:在设备重置或渲染每帧时。DXUT框架是组件化的,应用程序可以使用框架的全部功能或部分功能。

这个设计指南的其他部分详细的包括了这些步骤,并着重于应用程序可选择的控制或可替代的步骤。

更详细的语法和函数的使用,回调函数,结构,列举和常量等信息可以在DXUT参考中找到。

特点

为了帮助你创建一个应用程序,框架提供下列服务:

·           简单的窗口和设备的创建。

·           设备事件 (created, reset, lost, destroyed) 和窗口事件 (messages, keyboard, mouse)的通知。

·           窗口模式与全屏模式之间的转换, 硬件设备和软件设备之间的转换。

·           精确的计时器。

·           命令行支持和自动化测试。

·           使用对话框或API的设备选择。

·           一套纹理化的GUI控件,包括一个可使用输入法编辑器的编辑框。

·           各种扩展类,例如简单的摄像机类型。

局限性

为了便于使用,框架只支持绑定一个设备的窗口。需要同时使用多个设备,或显示多个Direct3D窗口的高级程序,是本框架不支持的,大部分类型的应用程序将能够使用本框架来实现。

启动一个新工程

最简单的方法,启动一个使用新的Visual Studio .NET中的DXUT开发的项目:

1.       启动例子浏览器(SampleBrowser.exe),在下面位置找到DirectX SDK(SDK root)/Samples/SampleBrowser/

2.       在例子浏览器里,选择一个已有的Direct3D 例子项目,将从这开始。

3.       点击 “安装项目”("Install Project")链接,然后拷贝Visual Studio .NET项目文件到一个新的本地目录。

4.       你可以给项目重命名,这时浏览器将根据提供的新工程名,去更改相应的文件和源代码。

DirectX April 2005 SDKDXUT的改进

基于用户的反溃, DXUT框架在DirectX April 2005 SDK中进行了改进更新。下面是主要的差异和改进的列表。

·         Callback functions now pass a void* pUserContext from the DXUTSetCallback* function to the callback. This allows the callback functions to receive context from the application such as a class pointer.

·         The framework's GUI is now separate and optional from the core framework. The creation and interfacing of CDXUTDialogResourceManager is now the responsibility of the application if it wishes to use the framework's GUI.

·         The framework now allows applications to reject device changes via the LPDXUTCALLBACKMODIFYDEVICESETTINGS function which returns a bool. Returning false from this callback will notify the framework to keep using the current device instead of changing to the new device. For example, by default on a multiple monitor configuration dragging the window between monitors will cause the framework to change devices. However if the application can not use the other device, it must be able to reject this change and continue to use the current device. This callback can now be set separate of the CreateDevice function by using DXUTSetCallbackDeviceChanging.

·         Passing 0 as the width and height to the DXUTCreateDevice function now creates a backbuffer of the same size as the client window.

·         The DXUTGetExitCode function now returns 11 if the last device was a D3DDEVTYPE_REF device type.

DirectX Summer 2003 SDK以来DXUT的变化

The new framework is significantly redesigned from DXUT that shipped in the DirectX Summer 2003 SDK Update. The following is a list of major differences and improvements.

·         Flat API design. The new framework is a set of global functions that more clearly define how the application interacts with the framework.

·         Clearer naming. The device, frame, and message events have been renamed to be more easily understood.

·         Modular. An application can use only parts of the framework if desired. An application can use the framework to create its own window or its own device, or it can use its own main loop.

·         Better device selection. The application can fully customize the presentation parameters or behavior flags before creating the device. The new framework also has a more advanced algorithm for finding valid device settings.

·         Toggling between hal and reference devices (see Device Types). Built-in toggling support is available, and toggling can be bound to a keystroke for simplified debugging.

·         Better error handling. Error handling and macros can be used by the application to display error message boxes immediately if an API is called incorrectly.

·         Better multiple-monitor support. If this feature is enabled, the new framework will automatically change devices if the window is moved to a different monitor.

·         GUI controls. A set of textured GUI controls features an IME-enabled edit box for use in samples, prototypes, and professional games.

·         No dialog dependency onGDI. Unlike the previous framework, the new framework does not require anyGDI dialog resources in the application.

·         Late binding with DirectX. Late binding verifies that the required DirectX version is installed on the system.

·         Unicode support. The new framework supports Unicode. Windows NT-based operating systems natively support Unicode, which means that ANSI calls need to be converted to Unicode. With this and better international support, Unicode is the better choice because it will result in better performance on these operating systems. For Windows 98 and Windows Millennium Edition (Windows Me) systems, consider using the Microsoft Layer for Unicode (MSLU).

·         Documentation. The framework library functions in the Dxut.h header are documented; see DXUT Reference.

 

初始化DXUT

使用DXUT的第一步是初始化。可以简单的用DXUTInit 函数:

HRESULT DXUTInit (

    BOOL bParseCommandLine     = TRUE,

    BOOL bHandleDefaultHotkeys = TRUE,

    BOOL bShowMsgBoxOnError    = TRUE

);

你将在应用程序的 WinMain 函数开始后不远的地方调用DXUTInit如果DXUTInit没有在应用程序中调用,它将使用默认的参数在框架中自动调用。

如果第一个参数bParseCommandLineTRUE,框架将对命令行参数做出响应。例如,运行BasicHLSL Sample执行下列命令行参数:

BasicHLSL.exe -windowed -width:600 -height:600

DXUT框架将尝试执行这些窗口设定,对于命令行支持参数的全部列表,请查看DXUTInit函数部分的帮助。

第二个参数bHandleDefaultHotkeys通知DXUT框架对一些预先执行的击键做出响应,例如:ALT+ENTER。再说一次,全部的列表请查看DXUTInit函数部分的帮助。如果这个参数为FALSE,程序将不做响应。

最后一个参数bShowMsgBoxOnError,当框架发现一个错误时,显示一个消息框。设置它为FALSE则运行自动测试或完全由用户经验控制的专业程序。

 

 

使用基于DXUT的程序窗口

你的程序可以只使用一个新的DXUT函数,来处理大部分窗口管理的任务。

·         创建窗口

·         使用你自己的窗口

创建窗口

创建一个Direct3D应用程序窗口包括下列步骤:

1.       定义一个响应窗口的消息。

2.       创建。

3.       使用。

4.       使用。

这些步骤如果没做正确将会引起bugs。虽然它对于一个Direct3D程序员来说,这可能很没劲,但却是每个程序所必需的。DXUT框架使用DXUTCreateWindow函数来简单化这个过程:

HRESULT DXUTCreateWindow(

    const WCHAR *strWindowTitle = L"Direct3D Window",

    HINSTANCE hInstance         = NULL,

    HICON hIcon                 = NULL,

    HMENU hMenu                 = NULL,

    INT x                       = CW_USEDEFAULT,

    INT y                       = CW_USEDEFAULT

);

所有参数都是可选的:

·         strWindowTitle是窗口标题,也显示在任务栏,它表示工程名称。

·         hInstance是应用程序实例的句柄。大部分程序的默认值为NULL

·         hIcon是应用程序的图标。如果为NULL,则应用程序的可执行文件里包含的第一个图标将被使用,所以NULL值也能很好的工作。

·         hMenu 处理菜单,如果想的话可以设置它,大部分游戏无论无何不能使用标准菜单,但作为替代,可以用自己定制的游戏类界面来创建它们。

·         最后2个参数描述窗口位置。如果应用程序运行在全屏模式,则忽视这2个参数。

最简单的调用方法,程序可以像下面这样调用DXUTCreateWindow

DXUTCreateWindow( L"My New Game" );

只用一个参数来调用这个函数,strWindowTitle将促使DXUT框架去创建窗口,并自动处理重要的窗口消息。如果需要重新取得窗口的句柄,可以调用DXUTGetHWND

如果想让程序响应窗口消息,可以用DXUTSetCallbackMsgProc 去设置回调函数:

void DXUTSetCallbackMsgProc( LPDXUTCALLBACKMSGPROC pCallbackMsgProc,

  void* pUserContext = NULL );

PCallbackMsgProc参数是一个LPDXUTCALLBACKMSGPROC类型的回调函数,下面是定义:

LRESULT CALLBACK FUNCTION MsgProc(

    HWND   hWnd,

    UINT   uMsg,

    WPARAM wParam,

    LPARAM lParam,

    BOOL*  pbNoFurtherProcessing,

    void*  pUserContext

    )

{

    return 0;

}

在这个回调函数里,程序不需要去响应任何消息,因为所有重要的消息将被DXUT框架处理。为了阻止DXUT框架处理消息,应用程序可以设置*pbNoFurtherProcessingTRUE。(查看LPDXUTCALLBACKMSGPROC),然而,当使用这个设置时要小心,因为它可能会阻止DXUT框架的正确行为。

使用自己的窗口

如果你想让程序去创建自己的窗口,并且和框架一起使用,而不是让DXUT处理窗口的操作,那么你可以使用DXUTSetWindow函数:

抱歉!评论已关闭.