游戏UI设计(1.2)--Textures & Sprite之封装

类别:编程语言 点击:0 评论:0 推荐:

1.2  Textures & Sprite 封装CXTexture & CXSprite

[http://blog.csdn.net/mythma]

1、  何为Textures

Textures是在屏幕上显示的平面图形,它能够提供比 surface 更多的图形处理效果——移动、缩放、旋转及作为纹理皮肤粘贴在3D模型上。在Direct3D中,其封装的接口为IDirect3DTexture9。

2、  何为Sprite

IDirect3DTexture9能从文件中读取纹理数据,但由于Textures不能直接复制到 back buffer,因此在屏幕上绘制Textures之前,需要另一个接口——ID3DXSprite(精灵)。ID3DXSprite能够把若干个Textures 复制给back buffer,因此需要ID3DXSprite的一个实例就可以绘制所有的纹理。

 

所以,IDirect3DTexture9用来存放程序所需的纹理,但它本身又不能绘制纹理,需要借助接口ID3DXSprite。

3、  IDirect3DTexture9ID3DXSprite使用过程

定义:             ~

创建:                   D3DXCreateTextureFromFile

D3DXCreateSprite

   创建变换矩阵:       D3DXMatrixTransformation2D

   变换:                  SetTransform

   绘制图象:              Draw

   释放内存:        ~

 

代码如下:

D3DXCreateTextureFromFile(g_pd3dDevice, "c:\\image.bmp”, &g_Texture);

D3DXCreateSprite(g_pd3dDevice, &g_Sprite);

//--------------------------------------------------------------------------

            D3DXVECTOR2 Translation;

            Translation.x = 500;

            Translation.y = 500;

            D3DXVECTOR2 Scaling;

            Scaling.x = 1.0;f

            Scaling.y = 1.0f;

            D3DXMATRIX Mat;

            D3DXMatrixTransformation2D(&Mat, NULL, 0, &Scaling, NULL, 0, &

            Translation);

            g_Sprite->Begin(0);

            g_Sprite->SetTransform(&

            g_Sprite->Draw(g_Texture,

            g_Sprite->End();

 

4、  如何封装

从以上的基本过程可以看到,CXTexture需要完成的功能:提供与LPDIRECT3DDEVICE9的接口,与纹理文件文件关联,对纹理进行处理(缩放、旋转……)。

class CXTexture

{

protected:

LPDIRECT3DTEXTURE9 m_Texture;

LPDIRECT3DDEVICE9 m_pDevice;

D3DXVECTOR2 m_RotationCenter;

D3DXVECTOR2 m_Translation;

D3DXVECTOR2 m_Scaling;

FLOAT m_Rotation;

RECT m_SrcRect;

public:

CXTexture(LPDIRECT3DDEVICE9 pDevice);

~CXTexture();

LPDIRECT3DTEXTURE9 GetTexture() const {return m_Texture;}

void SetTexture(LPDIRECT3DTEXTURE9 Texture) const {m_Texture = Texture;}

LPDIRECT3DDEVICE9 GetDevice() const {return m_pDevice;}

void SetDevice(LPDIRECT3DDEVICE9 pDevice) const {m_pDevice = pDevice;}

D3DXVECTOR2 GetRotationCenter() const {return m_RotationCenter;}

void SetRotationCenter(D3DXVECTOR2 RotationCenter) {m_RotationCenter =

RotationCenter;}

D3DXVECTOR2 GetTranslation() const {return m_Translation;}

void SetTranslation (D3DXVECTOR2 Translation) const {m_Translation =

Translation;}

D3DXVECTOR2 GetScaling() const {return m_Scaling;}

void SetScaling(D3DXVECTOR2 Scaling) const {m_Scaling = Scaling;}

FLOAT GetRotation() const {return m_Rotation;}

void SetRotation (FLOAT Rotation) const {m_Rotation = Rotation;}

RECT GetRect() const {return m_SrcRect;}

void SetRect(RECT SrcRect) const {m_SrcRect = SrcRect;}

HRESULT LoadFromFile(char* Path);

};

 

CXSprite的主要功能就是在屏幕上显示 CXTexture,因此需要有与 LPDIRECT3DDEVICE9 接口和 CXTexture连接的函数。

class CXSprite

{

protected:

LPD3DXSPRITE m_Sprite;

LPDIRECT3DDEVICE9 m_pDevice;

public:

CXSprite (LPDIRECT3DDEVICE9 pDevice);

~CXSprite ();

LPD3DXSPRITE GetSprite() const {return m_Sprite;}

void SetSprite(LPD3DXSPRITE Sprite) const {m_Sprite = Sprite;}

LPDIRECT3DDEVICE9 GetDevice() const {return m_pDevice;}

void SetDevice(LPDIRECT3DDEVICE9 pDevice) const {m_pDevice = pDevice;}

HRESULT DrawTexture(CXTexture* Texture);

};

 

本文地址:http://com.8s8s.com/it/it26948.htm