当前位置: 首页 > news >正文

【转贴】DXUT 框架入门 2

这章主要介绍一下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回调函数将不再处理这个消息。而交由对话框处理。
None.gif // 使用DXUT 框架GUI程序
None.gif
#include  < dxstdafx.h >
None.gif
None.gif
// 自定义顶点结构
None.gif
struct  CUSTOMVERTEX
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
float x,y,z,rhw;
InBlock.gif DWORD diffuse;
ExpandedBlockEnd.gif}
;
None.gif
None.gifID3DXFont
*      g_pFont  =  NULL;      //  Font for drawing text
None.gif
ID3DXSprite *     g_pTextSprite  =  NULL;    //  Sprite for batching draw text calls
None.gif
bool       g_bShowHelp  =   true ;     //  If true, it renders the UI control text
None.gif
CDXUTDialogResourceManager g_DialogResourceManager;   //  manager for shared resources of dialogs
None.gif
CD3DSettingsDlg    g_SettingsDlg;      //  Device settings dialog
None.gif
CDXUTDialog     g_HUD;        //  dialog for standard controls
None.gif
static   const   int    D3DFVF_CUSTOMVERTEX  =  D3DFVF_XYZRHW  |  D3DFVF_DIFFUSE;
None.gifLPDIRECT3DVERTEXBUFFER9  g_pVB;
None.gif
bool       g_vsb  =   true ;
None.gif
None.gif
// 定义g_HUD对话框上按钮ID
None.gif
static   const   int  IDC_TOGGLEFULLSCREEN = 1 ;  
None.gif
static   const   int  IDC_TOGGLEREF = 2 ;
None.gif
static   const   int  IDC_CHANGEDEVICE = 3 ;
None.gif
None.gifHRESULT CALLBACK OnCreateDevice( IDirect3DDevice9
*  pd3dDevice,  const  D3DSURFACE_DESC *  pBackBufferSurfaceDesc,  void *  pUserContext );
None.gifHRESULT CALLBACK OnResetDevice( IDirect3DDevice9
*  pd3dDevice,  const  D3DSURFACE_DESC *  pBackBufferSurfaceDesc,  void *  pUserContext );
None.gif
void     CALLBACK OnFrameMove( IDirect3DDevice9 *  pd3dDevice,  double  fTime,  float  fElapsedTime,  void *  pUserContext );
None.gif
void     CALLBACK OnFrameRender( IDirect3DDevice9 *  pd3dDevice,  double  fTime,  float  fElapsedTime,  void *  pUserContext );
None.gifLRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, 
bool *  pbNoFurtherProcessing,  void *  pUserContext );
None.gif
void     CALLBACK KeyboardProc( UINT nChar,  bool  bKeyDown,  bool  bAltDown,  void *  pUserContext );
None.gif
void     CALLBACK OnGUIEvent( UINT nEvent,  int  nControlID, CDXUTControl *  pControl,  void *  pUserContext );
None.gif
void     CALLBACK OnLostDevice(  void *  pUserContext );
None.gif
void     CALLBACK OnDestroyDevice(  void *  pUserContext );
None.gif
void     InitApp();
None.gif
void     RenderText();
None.gif
None.gifINT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, 
int  )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
//为DEBUG模式激活运行时内存检查
InBlock.gif
#if defined(DEBUG) | defined(_DEBUG)
InBlock.gif _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF 
| _CRTDBG_LEAK_CHECK_DF );
InBlock.gif
#endif
InBlock.gif 
// 设置回调函数,这些函数允许DXUT通知应用程序更换设备,用户输入和窗口消息。
InBlock.gif 
// 回调函数是可选的,因此你要做的仅是设置你感兴趣的事件的回调函数。
InBlock.gif
 DXUTSetCallbackDeviceCreated( OnCreateDevice );
InBlock.gif DXUTSetCallbackDeviceReset( OnResetDevice );
InBlock.gif DXUTSetCallbackDeviceLost( OnLostDevice );
InBlock.gif DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
InBlock.gif DXUTSetCallbackFrameRender( OnFrameRender );
InBlock.gif DXUTSetCallbackFrameMove( OnFrameMove );
InBlock.gif DXUTSetCallbackMsgProc( MsgProc );
InBlock.gif DXUTSetCallbackKeyboard( KeyboardProc );
InBlock.gif 
// 全屏时显示鼠标
InBlock.gif
 DXUTSetCursorSettings( truetrue );
InBlock.gif 
//InitApp();
InBlock.gif 
// 初始化DXUT并创建想要的Win32窗口和应用程序的Direct3D设备。调用这些
InBlock.gif 
// 可选函数中的每一个,此外它们允许你设置几个选项来控制框架的行为。
InBlock.gif
 DXUTInit( TRUE, TRUE, TRUE );
InBlock.gif DXUTCreateWindow( L
"ameng" );
InBlock.gif DXUTCreateDevice( D3DADAPTER_DEFAULT, TRUE, 
640480 );
InBlock.gif 
// 通过DXUT来处理消息循环并分派渲染调用。当在空闲时间和处理窗口消息的
InBlock.gif 
// 时间间隔时,框架将调用OnFrameMove和OnFrameRender回调函数。
InBlock.gif
 DXUTMainLoop();
InBlock.gif 
return DXUTGetExitCode();
ExpandedBlockEnd.gif}

None.gif
void  InitApp()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
// 初始化对话框
InBlock.gif
 g_SettingsDlg.Init(&g_DialogResourceManager );
InBlock.gif g_HUD.Init(
&g_DialogResourceManager );
InBlock.gif 
//设置对话框消息处理函数。
InBlock.gif
 g_HUD.SetCallback( OnGUIEvent ); 
InBlock.gif 
int iY = 10
InBlock.gif g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L
"Toggle full screen"35, iY, 12522 );
InBlock.gif g_HUD.AddButton( IDC_TOGGLEREF, L
"Toggle REF (F3)"35, iY += 2412522,VK_F3 );
InBlock.gif g_HUD.AddButton( IDC_CHANGEDEVICE, L
"Change device (F2)"35, iY += 2412522, VK_F2 );
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gifHRESULT CALLBACK OnCreateDevice( IDirect3DDevice9
*  pd3dDevice,  const  D3DSURFACE_DESC *  pBackBufferSurfaceDesc,  void *  pUserContext )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif HRESULT hr;
InBlock.gif V_RETURN( g_DialogResourceManager.OnCreateDevice( pd3dDevice ) );
InBlock.gif  InitApp();
InBlock.gif V_RETURN( g_SettingsDlg.OnCreateDevice( pd3dDevice ) );
InBlock.gif 
// Initialize the font
InBlock.gif
 V_RETURN( D3DXCreateFont( pd3dDevice, 150, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, 
InBlock.gif  OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH 
| FF_DONTCARE, 
InBlock.gif  L
"Arial"&g_pFont ) );
InBlock.gif 
return S_OK;
ExpandedBlockEnd.gif}

None.gifHRESULT CALLBACK OnResetDevice( IDirect3DDevice9
*  pd3dDevice,  const  D3DSURFACE_DESC *  pBackBufferSurfaceDesc,  void *  pUserContext )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif HRESULT hr;
InBlock.gif 
static CUSTOMVERTEX vertices[] =
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif300.0f,  50.0f0.1f1.0f, D3DCOLOR_ARGB(255,255,0,0) }// x, y, z, rhw, color
ExpandedSubBlockStart.gifContractedSubBlock.gif
  dot.gif500.0f350.0f0.1f1.0f, D3DCOLOR_ARGB(255,0,255,0) },
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif100.0f350.0f0.1f1.0f, D3DCOLOR_ARGB(255,0,0,255) },
ExpandedSubBlockEnd.gif }
;
InBlock.gif pd3dDevice
->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB,NULL);
InBlock.gif BYTE
* pVertices;
InBlock.gif 
if( FAILED( g_pVB->Lock( 0sizeof(vertices), (void**)&pVertices, 0 )))
InBlock.gif  
return E_FAIL;
InBlock.gif memcpy(pVertices,vertices,
sizeof(vertices));
InBlock.gif g_pVB
->Unlock();
InBlock.gif V_RETURN( g_DialogResourceManager.OnResetDevice() );
InBlock.gif V_RETURN( g_SettingsDlg.OnResetDevice() );
InBlock.gif 
if( g_pFont )
InBlock.gif  V_RETURN( g_pFont
->OnResetDevice() );
InBlock.gif 
// Create a sprite to help batch calls when drawing many lines of text
InBlock.gif
 V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pTextSprite ) );
InBlock.gif g_HUD.SetLocation( pBackBufferSurfaceDesc
->Width-1700 );
InBlock.gif g_HUD.SetSize( 
170170 );
InBlock.gif 
return S_OK;
ExpandedBlockEnd.gif}

None.gif
void  CALLBACK OnFrameMove( IDirect3DDevice9 *  pd3dDevice,  double  fTime,  float  fElapsedTime,  void *  pUserContext )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
void  CALLBACK OnFrameRender( IDirect3DDevice9 *  pd3dDevice,  double  fTime,  float  fElapsedTime,  void *  pUserContext )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif HRESULT hr;
InBlock.gif V( pd3dDevice
->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0000), 1.0f0) );
InBlock.gif V(pd3dDevice
->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX)));
InBlock.gif V(pd3dDevice
->SetFVF(D3DFVF_CUSTOMVERTEX));
InBlock.gif 
if( g_SettingsDlg.IsActive() )
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  g_SettingsDlg.OnRender( fElapsedTime );
InBlock.gif  
return;
ExpandedSubBlockEnd.gif }

InBlock.gif 
if(SUCCEEDED(pd3dDevice->BeginScene()))
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
//更新图像
InBlock.gif
  V(pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1));
InBlock.gif  
if(g_vsb)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   RenderText();
ExpandedSubBlockEnd.gif  }

InBlock.gif  V( g_HUD.OnRender( fElapsedTime ) );
InBlock.gif
//  V( g_SampleUI.OnRender( fElapsedTime ) );
InBlock.gif
  pd3dDevice->EndScene();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
void  RenderText()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
// The helper object simply helps keep track of text position, and color
InBlock.gif 
// and then it calls pFont->DrawText( m_pSprite, strMsg, -1, &rc, DT_NOCLIP, m_clr );
InBlock.gif 
// If NULL is passed in as the sprite object, then it will work however the 
InBlock.gif 
// pFont->DrawText() will not be batched together.  Batching calls will improves performance.
InBlock.gif
 CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
InBlock.gif 
// Output statistics
InBlock.gif
 txtHelper.Begin();
InBlock.gif txtHelper.SetInsertionPos( 
55 );
InBlock.gif txtHelper.SetForegroundColor( D3DXCOLOR( 
1.0f1.0f0.0f1.0f ) );
InBlock.gif txtHelper.DrawTextLine( DXUTGetFrameStats(
true) ); //为true 显现FPS,默认为false不显使FPS
InBlock.gif
 txtHelper.DrawTextLine( DXUTGetDeviceStats() );
InBlock.gif
InBlock.gif 
InBlock.gif 
//TODO: add UI text as needed
InBlock.gif
 txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f1.0f1.0f1.0f ) );
InBlock.gif txtHelper.DrawTextLine( L
"Put whatever misc status here" );
InBlock.gif
InBlock.gif 
// 获取图象后备缓冲区
InBlock.gif
 const D3DSURFACE_DESC* pd3dsdBackBuffer = DXUTGetBackBufferSurfaceDesc();
InBlock.gif 
if( g_bShowHelp )
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  txtHelper.SetInsertionPos( 
10, pd3dsdBackBuffer->Height-15*6 );
InBlock.gif  txtHelper.SetForegroundColor( D3DXCOLOR( 
1.0f0.75f0.0f1.0f ) );
InBlock.gif  txtHelper.DrawTextLine( L
"Controls (F1 to hide):" );
InBlock.gif  txtHelper.SetInsertionPos( 
40, pd3dsdBackBuffer->Height-15*5 );
InBlock.gif  txtHelper.DrawTextLine( L
"Quit: ESC" );
ExpandedSubBlockEnd.gif }

InBlock.gif 
else
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
//txtHelper.SetInsertionPos( 10, pd3dsdBackBuffer->Height-15*2 );
InBlock.gif
  txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f1.0f1.0f1.0f ) );
InBlock.gif  txtHelper.DrawTextLine( L
"Press F1 for help" );
ExpandedSubBlockEnd.gif }

InBlock.gif 
InBlock.gif txtHelper.End();
ExpandedBlockEnd.gif}

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

InBlock.gif 
// Give the dialogs a chance to handle the message first
InBlock.gif
 *pbNoFurtherProcessing = g_HUD.MsgProc( hWnd, uMsg, wParam, lParam );
InBlock.gif 
if*pbNoFurtherProcessing )
InBlock.gif  
return 0;
InBlock.gif 
return 0;
ExpandedBlockEnd.gif}

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

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
// --------------------------------------------------------------------------------------
None.gif
//  Handles the GUI events
None.gif
// --------------------------------------------------------------------------------------
None.gif
void  CALLBACK OnGUIEvent( UINT nEvent,  int  nControlID, CDXUTControl *  pControl,  void *  pUserContext )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
switch( nControlID )
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
case IDC_TOGGLEFULLSCREEN: 
InBlock.gif   DXUTToggleFullScreen(); 
InBlock.gif   
break;
InBlock.gif  
case IDC_TOGGLEREF:        
InBlock.gif   DXUTToggleREF(); 
InBlock.gif   
break;
InBlock.gif  
case IDC_CHANGEDEVICE:     
InBlock.gif   g_SettingsDlg.SetActive( 
!g_SettingsDlg.IsActive() ); 
InBlock.gif   
break;
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
void  CALLBACK OnLostDevice(  void *  pUserContext )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
if(g_pVB != NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  g_pVB
->Release();
InBlock.gif  g_pVB 
= NULL;
ExpandedSubBlockEnd.gif }

InBlock.gif g_DialogResourceManager.OnLostDevice();
InBlock.gif g_SettingsDlg.OnLostDevice();
InBlock.gif 
if( g_pFont )
InBlock.gif  g_pFont
->OnLostDevice();
InBlock.gif 
InBlock.gif SAFE_RELEASE( g_pTextSprite );
ExpandedBlockEnd.gif}

None.gif
void  CALLBACK OnDestroyDevice(  void *  pUserContext )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif g_DialogResourceManager.OnDestroyDevice();
InBlock.gif g_SettingsDlg.OnDestroyDevice();
InBlock.gif SAFE_RELEASE( g_pFont );
ExpandedBlockEnd.gif}

相关文章:

  • Rawether .NET
  • 我的部分设计作品(DIV+CSS)截屏
  • Ghost系统含漏洞藏杀机 伺机破坏计算机
  • B-BOY十大守则
  • 项目最难的阶段-解决方案的确认
  • ROR seo系列 | 站点地图
  • ORACLE的封锁机制
  • 给你快乐的7个理由
  • [摘]广义企业级PDM系统下的PPM(工艺规划管理)
  • 不知道怎么写这郁闷的标题
  • 关于IT工程师的种类
  • 一个网络工程师的忠告
  • SQL SERVER2000教程-第二章-创建和管理数据库 第一节 创建数据库
  • 路由器接口及连接
  • CEF技术浅析
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • Gradle 5.0 正式版发布
  • JavaScript设计模式与开发实践系列之策略模式
  • Promise初体验
  • PyCharm搭建GO开发环境(GO语言学习第1课)
  • 分布式任务队列Celery
  • 你真的知道 == 和 equals 的区别吗?
  • 小程序button引导用户授权
  • 怎样选择前端框架
  • 通过调用文摘列表API获取文摘
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #我与Java虚拟机的故事#连载07:我放弃了对JVM的进一步学习
  • (Bean工厂的后处理器入门)学习Spring的第七天
  • (Java数据结构)ArrayList
  • (二)Eureka服务搭建,服务注册,服务发现
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (附源码)计算机毕业设计SSM疫情社区管理系统
  • (四)Linux Shell编程——输入输出重定向
  • (转)h264中avc和flv数据的解析
  • ..回顾17,展望18
  • .NET Core 中的路径问题
  • .NET Project Open Day(2011.11.13)
  • .NET 将多个程序集合并成单一程序集的 4+3 种方法
  • .net经典笔试题
  • .net下的富文本编辑器FCKeditor的配置方法
  • [ solr入门 ] - 利用solrJ进行检索
  • [100天算法】-实现 strStr()(day 52)
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——
  • [2019.3.5]BZOJ1934 [Shoi2007]Vote 善意的投票
  • [8481302]博弈论 斯坦福game theory stanford week 1
  • [Android] Android ActivityManager
  • [c#基础]值类型和引用类型的Equals,==的区别
  • [C++] 统计程序耗时
  • [ccc3.0][数字钥匙] UWB配置和使用(二)
  • [CentOs7]图形界面
  • [Deepin 15] 编译安装 MySQL-5.6.35
  • [Django 0-1] Core.Email 模块
  • [GN] Vue3快速上手1
  • [IE编程] 打开/关闭IE8的光标浏览模式(Caret Browsing)