欢迎您对Quartz--v0.041(初步测试版)的测试!这个版本是Quartz--v0.04版本经过Dx8.1改进到Dx9.0c以后的第一个稳定版本,这次的修改涉及到了很多的底层部分重写,性能和易用性上都有了很大的提高,但是功能上还存在着很多的没有完善的地方以及缺陷,这些除了我已知的以外,在测试中遇到的问题,也希望大家能够提出来,我会根据需求分类尽快完善。
Quartz--的使用很简单,例题我也直接采用MS的SDK中的例题,这样就更方便了大家的调试,在Tutorial文件夹中我们用第一个例题CreateDevice来做。本例题要实现的目标是GUI系统基本设备的关联和鼠标的显示,这些东西即是最基本的也是最简单的。
首先拷贝这个例题到新的目录,然后编译确保可以通过(很重要,经常会有初学者本地代码编译不了,类似于这样的乌龙事件遇到过不少了)。然后将GUI.rar解压到Tut01_CreateDevice当前目录的GUI中。然后用下面的代码更换掉当前代码,然后我们在做具体的代码分析。
//-----------------------------------------------------------------------------
// 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;
}
在以后的使用当中你会发现这些并非是必须要的,但是为了更好的让你的游戏与UI系统结合,建议使用这些初始化。这里的CUIWindowManagerRUN();函数表示这些设备开始运行,函数名称后来发现有些不妥当,会在以后的版本中更换。