【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 3774|回复: 3

【Quartz控件系列教程】Tut01_CreateDevice 基本设备的建立

[复制链接]
发表于 2007-12-18 15:44:08 | 显示全部楼层 |阅读模式

QuartzUI--针对游戏设计爱好者和初学者的免费GUI库。

欢迎您对Quartz--v0.041(初步测试版)的测试!这个版本是Quartz--v0.04版本经过Dx8.1改进到Dx9.0c以后的第一个稳定版本,这次的修改涉及到了很多的底层部分重写,性能和易用性上都有了很大的提高,但是功能上还存在着很多的没有完善的地方以及缺陷,这些除了我已知的以外,在测试中遇到的问题,也希望大家能够提出来,我会根据需求分类尽快完善。

Quartz--的使用很简单,例题我也直接采用MS的SDK中的例题,这样就更方便了大家的调试,在Tutorial文件夹中我们用第一个例题CreateDevice来做。本例题要实现的目标是GUI系统基本设备的关联和鼠标的显示,这些东西即是最基本的也是最简单的。

首先拷贝这个例题到新的目录,然后编译确保可以通过(很重要,经常会有初学者本地代码编译不了,类似于这样的乌龙事件遇到过不少了)。然后将GUI.rar解压到Tut01_CreateDevice当前目录的GUI中。然后用下面的代码更换掉当前代码,然后我们在做具体的代码分析。

QUOTE:

//-----------------------------------------------------------------------------
// File: CreateDevice.cpp
//
// Desc: This is the first tutorial for using Direct3D. In this tutorial, all
// we are doing is creating a Direct3D device and using it to clear the
// window.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include "gui/GUI.h" //QT_GUI系统头文件
#pragma warning( disable : 4996 ) // disable deprecated warning
#include <strsafe.h>
#pragma warning( default : 4996 )


//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
CUIWindowManager g_pWindowManager;
CMouse g_pMouse;
CKeyboard g_Keyboard;


//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object, which is needed to create the D3DDevice.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;

// Set up the structure used to create the D3DDevice. Most parameters are
// zeroed out. We set Windowed to TRUE, since we want to do D3D in a
// window, and then set the SwapEffect to "discard", which is the most
// efficient method of presenting the back buffer to the display. And
// we request a back buffer format that matches the current desktop display
// format.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

// Create the Direct3D device. Here we are using the default adapter (most
// systems only have one, unless they have multiple graphics hardware cards
// installed) and requesting the HAL (which is saying we want the hardware
// device rather than a software one). Software vertex processing is
// specified since we know it will work on all cards. On cards that support
// hardware vertex processing, though, we would see a big performance gain
// by specifying hardware vertex processing.
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}


//在这里声明有关传入UI系统内部的设备
//*********************************************************
g_pWindowManager.SetQuartzDevice(hWnd,g_pd3dDevice);
g_pWindowManager.SetQuartzMouse(&g_pMouse);
g_pWindowManager.SetQuartzKeyboard(&g_Keyboard);
g_pWindowManager.CUIWindowManagerRUN();
//********************************************************

RECT r1,r2,r3;
SetRect(&r1,0,0,35,37);
SetRect(&r2,35,0,70,37);
SetRect(&r3,70,0,105,37);
g_pMouse.SetBasicCursorImg(r1);
//g_pMouse.SetInputCursorImg(r3);
g_pMouse.HidSYSCursor(true);

// Device state would normally be set here

return S_OK;
}


//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( g_pd3dDevice != NULL)
g_pd3dDevice->Release();

if( g_pD3D != NULL)
g_pD3D->Release();
}


//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
if( NULL == g_pd3dDevice )
return;

// Clear the backbuffer to a blue color
//更改了背景色,主要为了显示鼠标指针的半透明效果
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,190,0), 1.0f, 0 );

// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
// Rendering of scene objects can happen here

if ( SUCCEEDED( g_pSprite->Begin(D3DXSPRITE_ALPHABLEND) ) )
{
g_pWindowManager.Render();//GUI系统渲染函数
g_pSprite->End();
}

// End the scene
g_pd3dDevice->EndScene();
}

// Present the backbuffer contents to the display
g_pd3dDevice->resent( NULL, NULL, NULL, NULL );
}


//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;

//case WM_PAINT:
//return 0;
case WM_LBUTTONUP:
case WM_LBUTTONDOWN:
case WM_RBUTTONUP:
case WM_RBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MBUTTONDOWN:
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
case WM_MOUSEMOVE:
case 0x020A:
case WM_CHAR:
case WM_IME_SETCONTEXT:
case WM_INPUTLANGCHANGEREQUEST:
case WM_INPUTLANGCHANGE:
case WM_IME_STARTCOMPOSITION:
case WM_IME_ENDCOMPOSITION:
case WM_IME_NOTIFY:
case WM_IME_COMPOSITION:
case WM_ACTIVATEAPP:
//GUI系统消息响应函数
return g_pWindowManager.GetWinMassage( msg, wParam, lParam);
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}


//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL };
RegisterClassEx( &wc );

// Create the application's window
HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice",
WS_OVERLAPPEDWINDOW, 200, 200, 800, 600,
NULL, NULL, wc.hInstance, NULL );

// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );

// Enter the message loop
MSG msg;
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
Render();//游戏绘制
}
}

UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}

声明一个
CUIWindowManager g_pWindowManager;
这个UI管理者非常重要且唯一,它的作用是将游戏窗口的数据通知UI控件,包括当前使用的是什么设备,什么鼠标,什么键盘,以及用什么对UI进行渲染。

我们在DX下绘制2D做演示,就选用很方便D3DXSPRITE接口作为渲染接口,你也可以选其他的。比如DDRaw。

 

其次就是鼠标和键盘,GUI中已经定义好了非常好用的鼠标和键盘,直接调用就可以了
//*********************************************************
g_pWindowManager.SetQuartzDevice(hWnd,g_pd3dDevice);
g_pWindowManager.SetQuartzMouse(&g_pMouse);
g_pWindowManager.SetQuartzKeyboard(&g_Keyboard);
g_pWindowManager.CUIWindowManagerRUN();
//********************************************************

在以后的使用当中你会发现这些并非是必须要的,但是为了更好的让你的游戏与UI系统结合,建议使用这些初始化。这里的CUIWindowManagerRUN();函数表示这些设备开始运行,函数名称后来发现有些不妥当,会在以后的版本中更换。

鼠标的设置:

g_pMouse.SetBasicCursorImg(r1);
g_pMouse.HidSYSCursor(true);

这里可以通过一些函数设定鼠标指针的形状和状态。SetBasicCursorImg()中设置的是鼠标指针正常状态,你还可以通过SetBusyCursorImg()或者SetHelpCursorImg()来设置鼠标在忙或者帮助的时候的图标,然后通过SetCursorImgHelp()这样的方法改变鼠标形状,但是要记住,状态使用完毕要用SetCursorImgNormal()改回来。HidSYSCursor(true);这个函数就是隐藏系统鼠标,显示玩家自定义鼠标,如果你不需要,还可以通过这个函数把系统鼠标设置回来。不仅如此,cMouse类还提供了 Hide(),Show()的功能,可以灵活控制玩家指针的状态。

GUI系统消息响应函数
g_pWindowManager.GetWinMassage( msg, wParam, lParam); 这个非常重要,UI中需要的消息响应全靠这个函数通知。

GUI系统渲染函数

另外一个重要的函数就是 g_pWindowManager.Render(); 这个函数负责GUI元素的渲染。 现在所有的GUI控件都需要通过这个函数来实现绘制!

以上就是最基本的一些GUI设置,完成以上的设置基本上就完成了GUI框架,以后的工作就方便了.(建议渲染之前关闭 m_pDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);)

 

这个地方有些朋友喜欢用指针,也是完全接受的。

CMouse *g_pMouse;
CKeyboard *g_Keyboard;

 

在传参数的时候就可以直接传

g_pWindowManager.SetQuartzMouse(g_pMouse);
g_pWindowManager.SetQuartzKeyboard(g_Keyboard);

就可以了,当然也不要忘了

g_pMouse = new cMouse;
g_Keyboard = new cKeyboard;    指针使用需要new 的!

 

[此贴子已经被作者于2009-4-26 11:43:53编辑过]
发表于 2007-12-19 12:43:08 | 显示全部楼层
好东西~
发表于 2008-2-16 23:08:10 | 显示全部楼层

代码呢?

 楼主| 发表于 2008-2-17 21:11:27 | 显示全部楼层
QUOTE:
以下是引用magiclhd在2008-2-16 23:08:10的发言:

代码呢?

代码不都在上面么...

[em63]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-2-6 06:56

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表