【转贴】DXUT 框架入门 2
2016-06-20 00:00
459 查看
这章主要介绍一下DXUT 里面的GUI元素。要在图形界面中添加GUI元素,首先要定义一个DialogResourceManager对象用来管理对话框资源。DialogResourceManager 管理渲染时状态、Sprite控制批量显示更新、对话框字体、纹理等等。CDXUTDialog 相当于MFC里面的对话框,作为各种控件资源的容器。CD3DSettingsDlg 是一个ms已经写好的对话框类,可以用来设置各种Direct3DDevice9 创建时的参数。点击该对话框的ok 按钮,D3D设备将会重建。
这里先建立了DialogResourceManager全局变量g_DialogResourceManager和CD3DSettingsDlg 全局变量g_SettingsDlg。并且要在 OnCreateDevice OnResetDevice MsgProc OnLostDevice OnDestroyDevice 回调函数中调用自己相应的函数如g_DialogResourceManager.OnCreateDevice(...) 等等。
对于对话框对象使用前必须初始化 init() 参数为DialogResourceManager类对象,即g_DialogResourceManager.之后对于CDXUTDialog类对象g_HUD需要设置自己的消息回调函数 OnGUIEvent()。并且在 dxut 的消息处理函数MsgProc中调用自己的消息处理函数 g_HUD->MsgProc(), 如果是该对话框的消息,Dxut回调函数将不再处理这个消息。而交由对话框处理。
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
使用DXUT 框架GUI程序
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
#include
<
dxstdafx.h
>
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
自定义顶点结构
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
struct
CUSTOMVERTEX
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
float x,y,z,rhw;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DWORD diffuse;
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
ID3DXFont
*
g_pFont
=
NULL;
//
Font for drawing text
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
ID3DXSprite
*
g_pTextSprite
=
NULL;
//
Sprite for batching draw text calls
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
bool
g_bShowHelp
=
true
;
//
If true, it renders the UI control text
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
CDXUTDialogResourceManager g_DialogResourceManager;
//
manager for shared resources of dialogs
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
CD3DSettingsDlg g_SettingsDlg;
//
Device settings dialog
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
CDXUTDialog g_HUD;
//
dialog for standard controls
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
static
const
int
D3DFVF_CUSTOMVERTEX
=
D3DFVF_XYZRHW
|
D3DFVF_DIFFUSE;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
LPDIRECT3DVERTEXBUFFER9 g_pVB;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
bool
g_vsb
=
true
;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
定义g_HUD对话框上按钮ID
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
static
const
int
IDC_TOGGLEFULLSCREEN
=
1
;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
static
const
int
IDC_TOGGLEREF
=
2
;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
static
const
int
IDC_CHANGEDEVICE
=
3
;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9
*
pd3dDevice,
const
D3DSURFACE_DESC
*
pBackBufferSurfaceDesc,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9
*
pd3dDevice,
const
D3DSURFACE_DESC
*
pBackBufferSurfaceDesc,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnFrameMove( IDirect3DDevice9
*
pd3dDevice,
double
fTime,
float
fElapsedTime,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnFrameRender( IDirect3DDevice9
*
pd3dDevice,
double
fTime,
float
fElapsedTime,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
bool
*
pbNoFurtherProcessing,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK KeyboardProc( UINT nChar,
bool
bKeyDown,
bool
bAltDown,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnGUIEvent( UINT nEvent,
int
nControlID, CDXUTControl
*
pControl,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnLostDevice(
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnDestroyDevice(
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
InitApp();
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
RenderText();
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR,
int
)
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//为DEBUG模式激活运行时内存检查
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
#if defined(DEBUG) | defined(_DEBUG)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
#endif
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 设置回调函数,这些函数允许DXUT通知应用程序更换设备,用户输入和窗口消息。
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 回调函数是可选的,因此你要做的仅是设置你感兴趣的事件的回调函数。
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackDeviceCreated( OnCreateDevice );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackDeviceReset( OnResetDevice );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackDeviceLost( OnLostDevice );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackFrameRender( OnFrameRender );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackFrameMove( OnFrameMove );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackMsgProc( MsgProc );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackKeyboard( KeyboardProc );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 全屏时显示鼠标
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCursorSettings( true, true );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//InitApp();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 初始化DXUT并创建想要的Win32窗口和应用程序的Direct3D设备。调用这些
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 可选函数中的每一个,此外它们允许你设置几个选项来控制框架的行为。
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTInit( TRUE, TRUE, TRUE );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTCreateWindow( L"ameng" );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTCreateDevice( D3DADAPTER_DEFAULT, TRUE, 640, 480 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 通过DXUT来处理消息循环并分派渲染调用。当在空闲时间和处理窗口消息的
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 时间间隔时,框架将调用OnFrameMove和OnFrameRender回调函数。
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTMainLoop();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return DXUTGetExitCode();
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
InitApp()
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 初始化对话框
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.Init(&g_DialogResourceManager );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.Init(&g_DialogResourceManager );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//设置对话框消息处理函数。
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.SetCallback( OnGUIEvent );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
int iY = 10;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 35, iY, 125, 22 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 35, iY += 24, 125, 22,VK_F3 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 35, iY += 24, 125, 22, VK_F2 );
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9
*
pd3dDevice,
const
D3DSURFACE_DESC
*
pBackBufferSurfaceDesc,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
HRESULT hr;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( g_DialogResourceManager.OnCreateDevice( pd3dDevice ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
InitApp();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( g_SettingsDlg.OnCreateDevice( pd3dDevice ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// Initialize the font
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
L"Arial", &g_pFont ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return S_OK;
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9
*
pd3dDevice,
const
D3DSURFACE_DESC
*
pBackBufferSurfaceDesc,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
HRESULT hr;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
static CUSTOMVERTEX vertices[] =
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{ 300.0f, 50.0f, 0.1f, 1.0f, D3DCOLOR_ARGB(255,255,0,0) }, // x, y, z, rhw, color
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{ 500.0f, 350.0f, 0.1f, 1.0f, D3DCOLOR_ARGB(255,0,255,0) },
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{ 100.0f, 350.0f, 0.1f, 1.0f, D3DCOLOR_ARGB(255,0,0,255) },
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
};
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
pd3dDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB,NULL);
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
BYTE* pVertices;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 )))
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return E_FAIL;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
memcpy(pVertices,vertices,sizeof(vertices));
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_pVB->Unlock();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( g_DialogResourceManager.OnResetDevice() );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( g_SettingsDlg.OnResetDevice() );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( g_pFont )
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( g_pFont->OnResetDevice() );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// Create a sprite to help batch calls when drawing many lines of text
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pTextSprite ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.SetLocation( pBackBufferSurfaceDesc->Width-170, 0 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.SetSize( 170, 170 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return S_OK;
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnFrameMove( IDirect3DDevice9
*
pd3dDevice,
double
fTime,
float
fElapsedTime,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnFrameRender( IDirect3DDevice9
*
pd3dDevice,
double
fTime,
float
fElapsedTime,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
HRESULT hr;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V(pd3dDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX)));
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V(pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX));
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( g_SettingsDlg.IsActive() )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.OnRender( fElapsedTime );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return;
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if(SUCCEEDED(pd3dDevice->BeginScene()))
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//更新图像
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V(pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1));
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if(g_vsb)
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
RenderText();
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V( g_HUD.OnRender( fElapsedTime ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// V( g_SampleUI.OnRender( fElapsedTime ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
pd3dDevice->EndScene();
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
RenderText()
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// The helper object simply helps keep track of text position, and color
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// and then it calls pFont->DrawText( m_pSprite, strMsg, -1, &rc, DT_NOCLIP, m_clr );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// If NULL is passed in as the sprite object, then it will work however the
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// pFont->DrawText() will not be batched together. Batching calls will improves performance.
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// Output statistics
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.Begin();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetInsertionPos( 5, 5 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( DXUTGetFrameStats(true) ); //为true 显现FPS,默认为false不显使FPS
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( DXUTGetDeviceStats() );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//TODO: add UI text as needed
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( L"Put whatever misc status here" );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 获取图象后备缓冲区
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
const D3DSURFACE_DESC* pd3dsdBackBuffer = DXUTGetBackBufferSurfaceDesc();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( g_bShowHelp )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetInsertionPos( 10, pd3dsdBackBuffer->Height-15*6 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 0.75f, 0.0f, 1.0f ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( L"Controls (F1 to hide):" );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetInsertionPos( 40, pd3dsdBackBuffer->Height-15*5 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( L"Quit: ESC" );
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
else
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//txtHelper.SetInsertionPos( 10, pd3dsdBackBuffer->Height-15*2 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( L"Press F1 for help" );
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.End();
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
Before handling window messages, DXUT passes incoming windows
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
messages to the application through this callback function. If the application sets
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
*pbNoFurtherProcessing to TRUE, then DXUT will not process this message.
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
bool
*
pbNoFurtherProcessing,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// Always allow dialog resource manager calls to handle global messages
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// so GUI state is updated correctly
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
*pbNoFurtherProcessing = g_DialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( *pbNoFurtherProcessing )
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return 0;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( g_SettingsDlg.IsActive() )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.MsgProc( hWnd, uMsg, wParam, lParam );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return 0;
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// Give the dialogs a chance to handle the message first
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
*pbNoFurtherProcessing = g_HUD.MsgProc( hWnd, uMsg, wParam, lParam );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( *pbNoFurtherProcessing )
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return 0;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return 0;
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
As a convenience, DXUT inspects the incoming windows messages for
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
keystroke messages and decodes the message parameters to pass relevant keyboard
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
messages to the application. The framework does not remove the underlying keystroke
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
messages, which are still passed to the application's MsgProc callback.
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK KeyboardProc( UINT nChar,
bool
bKeyDown,
bool
bAltDown,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( bKeyDown )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
switch( nChar )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
case VK_F1:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_bShowHelp = !g_bShowHelp;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
case VK_F4:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.SetVisible(!g_HUD.GetVisible());
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_vsb = !g_vsb;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
default:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
Handles the GUI events
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnGUIEvent( UINT nEvent,
int
nControlID, CDXUTControl
*
pControl,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
switch( nControlID )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
case IDC_TOGGLEFULLSCREEN:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTToggleFullScreen();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
case IDC_TOGGLEREF:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTToggleREF();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
case IDC_CHANGEDEVICE:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.SetActive( !g_SettingsDlg.IsActive() );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnLostDevice(
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if(g_pVB != NULL)
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_pVB->Release();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_pVB = NULL;
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_DialogResourceManager.OnLostDevice();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.OnLostDevice();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( g_pFont )
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_pFont->OnLostDevice();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
SAFE_RELEASE( g_pTextSprite );
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnDestroyDevice(
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_DialogResourceManager.OnDestroyDevice();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.OnDestroyDevice();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
SAFE_RELEASE( g_pFont );
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
这里先建立了DialogResourceManager全局变量g_DialogResourceManager和CD3DSettingsDlg 全局变量g_SettingsDlg。并且要在 OnCreateDevice OnResetDevice MsgProc OnLostDevice OnDestroyDevice 回调函数中调用自己相应的函数如g_DialogResourceManager.OnCreateDevice(...) 等等。
对于对话框对象使用前必须初始化 init() 参数为DialogResourceManager类对象,即g_DialogResourceManager.之后对于CDXUTDialog类对象g_HUD需要设置自己的消息回调函数 OnGUIEvent()。并且在 dxut 的消息处理函数MsgProc中调用自己的消息处理函数 g_HUD->MsgProc(), 如果是该对话框的消息,Dxut回调函数将不再处理这个消息。而交由对话框处理。
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
使用DXUT 框架GUI程序
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
#include
<
dxstdafx.h
>
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
自定义顶点结构
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
struct
CUSTOMVERTEX
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
float x,y,z,rhw;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DWORD diffuse;
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
ID3DXFont
*
g_pFont
=
NULL;
//
Font for drawing text
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
ID3DXSprite
*
g_pTextSprite
=
NULL;
//
Sprite for batching draw text calls
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
bool
g_bShowHelp
=
true
;
//
If true, it renders the UI control text
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
CDXUTDialogResourceManager g_DialogResourceManager;
//
manager for shared resources of dialogs
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
CD3DSettingsDlg g_SettingsDlg;
//
Device settings dialog
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
CDXUTDialog g_HUD;
//
dialog for standard controls
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
static
const
int
D3DFVF_CUSTOMVERTEX
=
D3DFVF_XYZRHW
|
D3DFVF_DIFFUSE;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
LPDIRECT3DVERTEXBUFFER9 g_pVB;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
bool
g_vsb
=
true
;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
定义g_HUD对话框上按钮ID
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
static
const
int
IDC_TOGGLEFULLSCREEN
=
1
;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
static
const
int
IDC_TOGGLEREF
=
2
;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
static
const
int
IDC_CHANGEDEVICE
=
3
;
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9
*
pd3dDevice,
const
D3DSURFACE_DESC
*
pBackBufferSurfaceDesc,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9
*
pd3dDevice,
const
D3DSURFACE_DESC
*
pBackBufferSurfaceDesc,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnFrameMove( IDirect3DDevice9
*
pd3dDevice,
double
fTime,
float
fElapsedTime,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnFrameRender( IDirect3DDevice9
*
pd3dDevice,
double
fTime,
float
fElapsedTime,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
bool
*
pbNoFurtherProcessing,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK KeyboardProc( UINT nChar,
bool
bKeyDown,
bool
bAltDown,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnGUIEvent( UINT nEvent,
int
nControlID, CDXUTControl
*
pControl,
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnLostDevice(
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnDestroyDevice(
void
*
pUserContext );
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
InitApp();
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
RenderText();
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR,
int
)
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//为DEBUG模式激活运行时内存检查
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
#if defined(DEBUG) | defined(_DEBUG)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
#endif
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 设置回调函数,这些函数允许DXUT通知应用程序更换设备,用户输入和窗口消息。
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 回调函数是可选的,因此你要做的仅是设置你感兴趣的事件的回调函数。
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackDeviceCreated( OnCreateDevice );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackDeviceReset( OnResetDevice );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackDeviceLost( OnLostDevice );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackFrameRender( OnFrameRender );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackFrameMove( OnFrameMove );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackMsgProc( MsgProc );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCallbackKeyboard( KeyboardProc );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 全屏时显示鼠标
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTSetCursorSettings( true, true );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//InitApp();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 初始化DXUT并创建想要的Win32窗口和应用程序的Direct3D设备。调用这些
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 可选函数中的每一个,此外它们允许你设置几个选项来控制框架的行为。
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTInit( TRUE, TRUE, TRUE );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTCreateWindow( L"ameng" );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTCreateDevice( D3DADAPTER_DEFAULT, TRUE, 640, 480 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 通过DXUT来处理消息循环并分派渲染调用。当在空闲时间和处理窗口消息的
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 时间间隔时,框架将调用OnFrameMove和OnFrameRender回调函数。
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTMainLoop();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return DXUTGetExitCode();
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
InitApp()
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 初始化对话框
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.Init(&g_DialogResourceManager );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.Init(&g_DialogResourceManager );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//设置对话框消息处理函数。
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.SetCallback( OnGUIEvent );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
int iY = 10;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 35, iY, 125, 22 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 35, iY += 24, 125, 22,VK_F3 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 35, iY += 24, 125, 22, VK_F2 );
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9
*
pd3dDevice,
const
D3DSURFACE_DESC
*
pBackBufferSurfaceDesc,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
HRESULT hr;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( g_DialogResourceManager.OnCreateDevice( pd3dDevice ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
InitApp();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( g_SettingsDlg.OnCreateDevice( pd3dDevice ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// Initialize the font
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
L"Arial", &g_pFont ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return S_OK;
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9
*
pd3dDevice,
const
D3DSURFACE_DESC
*
pBackBufferSurfaceDesc,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
HRESULT hr;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
static CUSTOMVERTEX vertices[] =
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{ 300.0f, 50.0f, 0.1f, 1.0f, D3DCOLOR_ARGB(255,255,0,0) }, // x, y, z, rhw, color
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{ 500.0f, 350.0f, 0.1f, 1.0f, D3DCOLOR_ARGB(255,0,255,0) },
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{ 100.0f, 350.0f, 0.1f, 1.0f, D3DCOLOR_ARGB(255,0,0,255) },
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
};
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
pd3dDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB,NULL);
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
BYTE* pVertices;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 )))
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return E_FAIL;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
memcpy(pVertices,vertices,sizeof(vertices));
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_pVB->Unlock();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( g_DialogResourceManager.OnResetDevice() );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( g_SettingsDlg.OnResetDevice() );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( g_pFont )
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( g_pFont->OnResetDevice() );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// Create a sprite to help batch calls when drawing many lines of text
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pTextSprite ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.SetLocation( pBackBufferSurfaceDesc->Width-170, 0 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.SetSize( 170, 170 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return S_OK;
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnFrameMove( IDirect3DDevice9
*
pd3dDevice,
double
fTime,
float
fElapsedTime,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnFrameRender( IDirect3DDevice9
*
pd3dDevice,
double
fTime,
float
fElapsedTime,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
HRESULT hr;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V(pd3dDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX)));
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V(pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX));
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( g_SettingsDlg.IsActive() )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.OnRender( fElapsedTime );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return;
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if(SUCCEEDED(pd3dDevice->BeginScene()))
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//更新图像
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V(pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1));
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if(g_vsb)
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
RenderText();
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
V( g_HUD.OnRender( fElapsedTime ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// V( g_SampleUI.OnRender( fElapsedTime ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
pd3dDevice->EndScene();
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
RenderText()
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// The helper object simply helps keep track of text position, and color
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// and then it calls pFont->DrawText( m_pSprite, strMsg, -1, &rc, DT_NOCLIP, m_clr );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// If NULL is passed in as the sprite object, then it will work however the
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// pFont->DrawText() will not be batched together. Batching calls will improves performance.
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// Output statistics
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.Begin();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetInsertionPos( 5, 5 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( DXUTGetFrameStats(true) ); //为true 显现FPS,默认为false不显使FPS
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( DXUTGetDeviceStats() );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//TODO: add UI text as needed
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( L"Put whatever misc status here" );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// 获取图象后备缓冲区
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
const D3DSURFACE_DESC* pd3dsdBackBuffer = DXUTGetBackBufferSurfaceDesc();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( g_bShowHelp )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetInsertionPos( 10, pd3dsdBackBuffer->Height-15*6 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 0.75f, 0.0f, 1.0f ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( L"Controls (F1 to hide):" );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetInsertionPos( 40, pd3dsdBackBuffer->Height-15*5 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( L"Quit: ESC" );
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
else
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
//txtHelper.SetInsertionPos( 10, pd3dsdBackBuffer->Height-15*2 );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.DrawTextLine( L"Press F1 for help" );
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
txtHelper.End();
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
Before handling window messages, DXUT passes incoming windows
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
messages to the application through this callback function. If the application sets
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
*pbNoFurtherProcessing to TRUE, then DXUT will not process this message.
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
bool
*
pbNoFurtherProcessing,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// Always allow dialog resource manager calls to handle global messages
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// so GUI state is updated correctly
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
*pbNoFurtherProcessing = g_DialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( *pbNoFurtherProcessing )
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return 0;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( g_SettingsDlg.IsActive() )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.MsgProc( hWnd, uMsg, wParam, lParam );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return 0;
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
// Give the dialogs a chance to handle the message first
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
*pbNoFurtherProcessing = g_HUD.MsgProc( hWnd, uMsg, wParam, lParam );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( *pbNoFurtherProcessing )
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return 0;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
return 0;
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
As a convenience, DXUT inspects the incoming windows messages for
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
keystroke messages and decodes the message parameters to pass relevant keyboard
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
messages to the application. The framework does not remove the underlying keystroke
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
messages, which are still passed to the application's MsgProc callback.
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK KeyboardProc( UINT nChar,
bool
bKeyDown,
bool
bAltDown,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( bKeyDown )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
switch( nChar )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
case VK_F1:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_bShowHelp = !g_bShowHelp;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
case VK_F4:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_HUD.SetVisible(!g_HUD.GetVisible());
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_vsb = !g_vsb;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
default:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
Handles the GUI events
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
//
--------------------------------------------------------------------------------------
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnGUIEvent( UINT nEvent,
int
nControlID, CDXUTControl
*
pControl,
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
switch( nControlID )
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
case IDC_TOGGLEFULLSCREEN:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTToggleFullScreen();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
case IDC_TOGGLEREF:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
DXUTToggleREF();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
case IDC_CHANGEDEVICE:
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.SetActive( !g_SettingsDlg.IsActive() );
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
break;
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnLostDevice(
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if(g_pVB != NULL)
![](http://static.oschina.net/uploads/img/201606/20134118_DBq8.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_sDr9.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_pVB->Release();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_pVB = NULL;
![](http://static.oschina.net/uploads/img/201606/20134118_4zqt.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_DialogResourceManager.OnLostDevice();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.OnLostDevice();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
if( g_pFont )
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_pFont->OnLostDevice();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
SAFE_RELEASE( g_pTextSprite );
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}
![](http://static.oschina.net/uploads/img/201606/20134117_W4ph.gif)
void
CALLBACK OnDestroyDevice(
void
*
pUserContext )
![](http://static.oschina.net/uploads/img/201606/20134117_nHT0.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_HRVx.gif)
![](http://static.oschina.net/uploads/img/201606/20134118_RVbE.gif)
{
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_DialogResourceManager.OnDestroyDevice();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
g_SettingsDlg.OnDestroyDevice();
![](http://static.oschina.net/uploads/img/201606/20134118_TcKv.gif)
SAFE_RELEASE( g_pFont );
![](http://static.oschina.net/uploads/img/201606/20134118_sAP1.gif)
}