? 這章主要介紹一下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回調函數將不再處理這個消息。而交由對話框處理。 //使用DXUT?框架GUI程序
#include?<dxstdafx.h>

//自定義頂點結構
struct?CUSTOMVERTEX
  {
?float?x,y,z,rhw;
?DWORD?diffuse;
};

ID3DXFont*?????g_pFont?=?NULL;?????//?Font?for?drawing?text
ID3DXSprite*????g_pTextSprite?=?NULL;???//?Sprite?for?batching?draw?text?calls
bool??????g_bShowHelp?=?true;????//?If?true,?it?renders?the?UI?control?text
CDXUTDialogResourceManager?g_DialogResourceManager;??//?manager?for?shared?resources?of?dialogs
CD3DSettingsDlg????g_SettingsDlg;?????//?Device?settings?dialog
CDXUTDialog?????g_HUD;???????//?dialog?for?standard?controls
static?const?int???D3DFVF_CUSTOMVERTEX?=?D3DFVF_XYZRHW?|?D3DFVF_DIFFUSE;
LPDIRECT3DVERTEXBUFFER9??g_pVB;
bool??????g_vsb?=?true;

//定義g_HUD對話框上按鈕ID
static?const?int?IDC_TOGGLEFULLSCREEN=1;??
static?const?int?IDC_TOGGLEREF=2;
static?const?int?IDC_CHANGEDEVICE=3;

HRESULT?CALLBACK?OnCreateDevice(?IDirect3DDevice9*?pd3dDevice,?const?D3DSURFACE_DESC*?pBackBufferSurfaceDesc,?void*?pUserContext?);
HRESULT?CALLBACK?OnResetDevice(?IDirect3DDevice9*?pd3dDevice,?const?D3DSURFACE_DESC*?pBackBufferSurfaceDesc,?void*?pUserContext?);
void????CALLBACK?OnFrameMove(?IDirect3DDevice9*?pd3dDevice,?double?fTime,?float?fElapsedTime,?void*?pUserContext?);
void????CALLBACK?OnFrameRender(?IDirect3DDevice9*?pd3dDevice,?double?fTime,?float?fElapsedTime,?void*?pUserContext?);
LRESULT?CALLBACK?MsgProc(?HWND?hWnd,?UINT?uMsg,?WPARAM?wParam,?LPARAM?lParam,?bool*?pbNoFurtherProcessing,?void*?pUserContext?);
void????CALLBACK?KeyboardProc(?UINT?nChar,?bool?bKeyDown,?bool?bAltDown,?void*?pUserContext?);
void????CALLBACK?OnGUIEvent(?UINT?nEvent,?int?nControlID,?CDXUTControl*?pControl,?void*?pUserContext?);
void????CALLBACK?OnLostDevice(?void*?pUserContext?);
void????CALLBACK?OnDestroyDevice(?void*?pUserContext?);
void????InitApp();
void????RenderText();

INT?WINAPI?WinMain(?HINSTANCE,?HINSTANCE,?LPSTR,?int?)
  {
?//為DEBUG模式激活運行時內存檢查
#if?defined(DEBUG)?|?defined(_DEBUG)
?_CrtSetDbgFlag(?_CRTDBG_ALLOC_MEM_DF?|?_CRTDBG_LEAK_CHECK_DF?);
#endif
?//?設置回調函數,這些函數允許DXUT通知應用程序更換設備,用戶輸入和窗口消息。
?//?回調函數是可選的,因此你要做的僅是設置你感興趣的事件的回調函數。
?DXUTSetCallbackDeviceCreated(?OnCreateDevice?);
?DXUTSetCallbackDeviceReset(?OnResetDevice?);
?DXUTSetCallbackDeviceLost(?OnLostDevice?);
?DXUTSetCallbackDeviceDestroyed(?OnDestroyDevice?);
?DXUTSetCallbackFrameRender(?OnFrameRender?);
?DXUTSetCallbackFrameMove(?OnFrameMove?);
?DXUTSetCallbackMsgProc(?MsgProc?);
?DXUTSetCallbackKeyboard(?KeyboardProc?);
?//?全屏時顯示鼠標
?DXUTSetCursorSettings(?true,?true?);
?//InitApp();
?//?初始化DXUT并創建想要的Win32窗口和應用程序的Direct3D設備。調用這些
?//?可選函數中的每一個,此外它們允許你設置幾個選項來控制框架的行為。
?DXUTInit(?TRUE,?TRUE,?TRUE?);
?DXUTCreateWindow(?L"ameng"?);
?DXUTCreateDevice(?D3DADAPTER_DEFAULT,?TRUE,?640,?480?);
?//?通過DXUT來處理消息循環并分派渲染調用。當在空閑時間和處理窗口消息的
?//?時間間隔時,框架將調用OnFrameMove和OnFrameRender回調函數。
?DXUTMainLoop();
?return?DXUTGetExitCode();
}
void?InitApp()
  {
?//?初始化對話框
?g_SettingsDlg.Init(&g_DialogResourceManager?);
?g_HUD.Init(&g_DialogResourceManager?);
?//設置對話框消息處理函數。
?g_HUD.SetCallback(?OnGUIEvent?);?
?int?iY?=?10;?
?g_HUD.AddButton(?IDC_TOGGLEFULLSCREEN,?L"Toggle?full?screen",?35,?iY,?125,?22?);
?g_HUD.AddButton(?IDC_TOGGLEREF,?L"Toggle?REF?(F3)",?35,?iY?+=?24,?125,?22,VK_F3?);
?g_HUD.AddButton(?IDC_CHANGEDEVICE,?L"Change?device?(F2)",?35,?iY?+=?24,?125,?22,?VK_F2?);
}


HRESULT?CALLBACK?OnCreateDevice(?IDirect3DDevice9*?pd3dDevice,?const?D3DSURFACE_DESC*?pBackBufferSurfaceDesc,?void*?pUserContext?)
  {
?HRESULT?hr;
?V_RETURN(?g_DialogResourceManager.OnCreateDevice(?pd3dDevice?)?);
??InitApp();
?V_RETURN(?g_SettingsDlg.OnCreateDevice(?pd3dDevice?)?);
?//?Initialize?the?font
?V_RETURN(?D3DXCreateFont(?pd3dDevice,?15,?0,?FW_BOLD,?1,?FALSE,?DEFAULT_CHARSET,?
??OUT_DEFAULT_PRECIS,?DEFAULT_QUALITY,?DEFAULT_PITCH?|?FF_DONTCARE,?
??L"Arial",?&g_pFont?)?);
?return?S_OK;
}
HRESULT?CALLBACK?OnResetDevice(?IDirect3DDevice9*?pd3dDevice,?const?D3DSURFACE_DESC*?pBackBufferSurfaceDesc,?void*?pUserContext?)
  {
?HRESULT?hr;
?static?CUSTOMVERTEX?vertices[]?=
 ? {
 ?? {?300.0f,??50.0f,?0.1f,?1.0f,?D3DCOLOR_ARGB(255,255,0,0)?},?//?x,?y,?z,?rhw,?color
 ?? {?500.0f,?350.0f,?0.1f,?1.0f,?D3DCOLOR_ARGB(255,0,255,0)?},
 ?? {?100.0f,?350.0f,?0.1f,?1.0f,?D3DCOLOR_ARGB(255,0,0,255)?},
?};
?pd3dDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB,NULL);
?BYTE*?pVertices;
?if(?FAILED(?g_pVB->Lock(?0,?sizeof(vertices),?(void**)&pVertices,?0?)))
??return?E_FAIL;
?memcpy(pVertices,vertices,sizeof(vertices));
?g_pVB->Unlock();
?V_RETURN(?g_DialogResourceManager.OnResetDevice()?);
?V_RETURN(?g_SettingsDlg.OnResetDevice()?);
?if(?g_pFont?)
??V_RETURN(?g_pFont->OnResetDevice()?);
?//?Create?a?sprite?to?help?batch?calls?when?drawing?many?lines?of?text
?V_RETURN(?D3DXCreateSprite(?pd3dDevice,?&g_pTextSprite?)?);
?g_HUD.SetLocation(?pBackBufferSurfaceDesc->Width-170,?0?);
?g_HUD.SetSize(?170,?170?);
?return?S_OK;
}
void?CALLBACK?OnFrameMove(?IDirect3DDevice9*?pd3dDevice,?double?fTime,?float?fElapsedTime,?void*?pUserContext?)
  {

}
void?CALLBACK?OnFrameRender(?IDirect3DDevice9*?pd3dDevice,?double?fTime,?float?fElapsedTime,?void*?pUserContext?)
  {
?HRESULT?hr;
?V(?pd3dDevice->Clear(0,?NULL,?D3DCLEAR_TARGET?|?D3DCLEAR_ZBUFFER,?D3DCOLOR_ARGB(0,?0,?0,?0),?1.0f,?0)?);
?V(pd3dDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX)));
?V(pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX));
?if(?g_SettingsDlg.IsActive()?)
 ? {
??g_SettingsDlg.OnRender(?fElapsedTime?);
??return;
?}
?if(SUCCEEDED(pd3dDevice->BeginScene()))
 ? {
??//更新圖像
??V(pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1));
??if(g_vsb)
 ?? {
???RenderText();
??}
??V(?g_HUD.OnRender(?fElapsedTime?)?);
//??V(?g_SampleUI.OnRender(?fElapsedTime?)?);
??pd3dDevice->EndScene();
?}
}
void?RenderText()
  {
?//?The?helper?object?simply?helps?keep?track?of?text?position,?and?color
?//?and?then?it?calls?pFont->DrawText(?m_pSprite,?strMsg,?-1,?&rc,?DT_NOCLIP,?m_clr?);
?//?If?NULL?is?passed?in?as?the?sprite?object,?then?it?will?work?however?the?
?//?pFont->DrawText()?will?not?be?batched?together.??Batching?calls?will?improves?performance.
?CDXUTTextHelper?txtHelper(?g_pFont,?g_pTextSprite,?15?);
?//?Output?statistics
?txtHelper.Begin();
?txtHelper.SetInsertionPos(?5,?5?);
?txtHelper.SetForegroundColor(?D3DXCOLOR(?1.0f,?1.0f,?0.0f,?1.0f?)?);
?txtHelper.DrawTextLine(?DXUTGetFrameStats(true)?);?//為true?顯現FPS,默認為false不顯使FPS
?txtHelper.DrawTextLine(?DXUTGetDeviceStats()?);

?
?//TODO:?add?UI?text?as?needed
?txtHelper.SetForegroundColor(?D3DXCOLOR(?1.0f,?1.0f,?1.0f,?1.0f?)?);
?txtHelper.DrawTextLine(?L"Put?whatever?misc?status?here"?);

?//?獲取圖象后備緩沖區
?const?D3DSURFACE_DESC*?pd3dsdBackBuffer?=?DXUTGetBackBufferSurfaceDesc();
?if(?g_bShowHelp?)
 ? {
??txtHelper.SetInsertionPos(?10,?pd3dsdBackBuffer->Height-15*6?);
??txtHelper.SetForegroundColor(?D3DXCOLOR(?1.0f,?0.75f,?0.0f,?1.0f?)?);
??txtHelper.DrawTextLine(?L"Controls?(F1?to?hide):"?);
??txtHelper.SetInsertionPos(?40,?pd3dsdBackBuffer->Height-15*5?);
??txtHelper.DrawTextLine(?L"Quit:?ESC"?);
?}
?else
 ? {
??//txtHelper.SetInsertionPos(?10,?pd3dsdBackBuffer->Height-15*2?);
??txtHelper.SetForegroundColor(?D3DXCOLOR(?1.0f,?1.0f,?1.0f,?1.0f?)?);
??txtHelper.DrawTextLine(?L"Press?F1?for?help"?);
?}
?
?txtHelper.End();
}

//--------------------------------------------------------------------------------------
//?Before?handling?window?messages,?DXUT?passes?incoming?windows?
//?messages?to?the?application?through?this?callback?function.?If?the?application?sets?
//?*pbNoFurtherProcessing?to?TRUE,?then?DXUT?will?not?process?this?message.
//--------------------------------------------------------------------------------------
LRESULT?CALLBACK?MsgProc(?HWND?hWnd,?UINT?uMsg,?WPARAM?wParam,?LPARAM?lParam,?bool*?pbNoFurtherProcessing,?void*?pUserContext?)
  {
?//?Always?allow?dialog?resource?manager?calls?to?handle?global?messages
?//?so?GUI?state?is?updated?correctly
?*pbNoFurtherProcessing?=?g_DialogResourceManager.MsgProc(?hWnd,?uMsg,?wParam,?lParam?);
?if(?*pbNoFurtherProcessing?)
??return?0;
?if(?g_SettingsDlg.IsActive()?)
 ? {
??g_SettingsDlg.MsgProc(?hWnd,?uMsg,?wParam,?lParam?);
??return?0;
?}
?//?Give?the?dialogs?a?chance?to?handle?the?message?first
?*pbNoFurtherProcessing?=?g_HUD.MsgProc(?hWnd,?uMsg,?wParam,?lParam?);
?if(?*pbNoFurtherProcessing?)
??return?0;
?return?0;
}

//--------------------------------------------------------------------------------------
//?As?a?convenience,?DXUT?inspects?the?incoming?windows?messages?for
//?keystroke?messages?and?decodes?the?message?parameters?to?pass?relevant?keyboard
//?messages?to?the?application.??The?framework?does?not?remove?the?underlying?keystroke?
//?messages,?which?are?still?passed?to?the?application's?MsgProc?callback.
//--------------------------------------------------------------------------------------
void?CALLBACK?KeyboardProc(?UINT?nChar,?bool?bKeyDown,?bool?bAltDown,?void*?pUserContext?)
  {
?if(?bKeyDown?)
 ? {
??switch(?nChar?)
 ?? {
???case?VK_F1:?
????g_bShowHelp?=?!g_bShowHelp;?
????break;
???case?VK_F4:
????g_HUD.SetVisible(!g_HUD.GetVisible());
????g_vsb?=?!g_vsb;
????break;
???default:
????break;
??}
?}
}

//--------------------------------------------------------------------------------------
//?Handles?the?GUI?events
//--------------------------------------------------------------------------------------
void?CALLBACK?OnGUIEvent(?UINT?nEvent,?int?nControlID,?CDXUTControl*?pControl,?void*?pUserContext?)
  {
?switch(?nControlID?)
 ? {
??case?IDC_TOGGLEFULLSCREEN:?
???DXUTToggleFullScreen();?
???break;
??case?IDC_TOGGLEREF:????????
???DXUTToggleREF();?
???break;
??case?IDC_CHANGEDEVICE:?????
???g_SettingsDlg.SetActive(?!g_SettingsDlg.IsActive()?);?
???break;
?}
}
void?CALLBACK?OnLostDevice(?void*?pUserContext?)
  {
?if(g_pVB?!=?NULL)
 ? {
??g_pVB->Release();
??g_pVB?=?NULL;
?}
?g_DialogResourceManager.OnLostDevice();
?g_SettingsDlg.OnLostDevice();
?if(?g_pFont?)
??g_pFont->OnLostDevice();
?
?SAFE_RELEASE(?g_pTextSprite?);
}
void?CALLBACK?OnDestroyDevice(?void*?pUserContext?)
  {
?g_DialogResourceManager.OnDestroyDevice();
?g_SettingsDlg.OnDestroyDevice();
?SAFE_RELEASE(?g_pFont?);
}
|