DirectX 中的两种字体 处理方式

1、D3DX库 提供接口 ID3DXFont,该接口用于在Direct3D 应用程序中绘制文本。该接口内部使用GDI(图形设备接口)来绘制文本,因此该接口在性能上略有损失。然而正是由于使用的GDI,所以该接口才能处理一些复杂的字体和格式。

 

1 HRESULT D3DXCreatFontInDirect(
2 LPDIRECT3DDEVICE9 pDev,     //device to associated with font
3 CONST D3DXFONT_DESC *pDesc, //D3DXFONT_DESC structure
4 LPD3DXFONT* ppFont //return the created font
);

如何使用该函数:

 

 1 D3DXFONT_DESC df;
 2 ZeroMemory(&df, sizeof(D3DXFONT_DESC));
 3 df.Height = 25;
 4 df.Width =12;
 5 df.MipLevels = D3DX_DEFAULT; //boldness range 0(light)--
 6                                                  //--1000(bold)
 7 df.Italic = false;      //是否是斜体
 8 df.charset = DEFAULT_CHARSET;
 9 df.OutputPrecision = 0;
10 df.Quality = 0;
11 df.PitchAndFamily = 0;
12 
13 strcpy(df.FAceName, "Times New Roman");  //FontStyle
14 
15 ID3DXFont *font;
16 D3DXCreateFontIndirect(Device, &df, &font);     //引用           

 

当获取了指针后,就只需要调用方法ID3DXFont::DrawText就可以完成文本的绘制了,函数原型如下:

INT ID3DXFont::DrawText(

LPD3DXSPRITE pSprite,     

LPCSTR pString,      

LPRECT pRect,      

DWORD Format,

D3DCOLOR Color

);

 

参数列表:

pSprite [in]LPD3DXSPRITE

Pointer to an ID3DXSprite object that contains the string. Can be NULL, in which case Direct3D will render the string with its own sprite object. To improve efficiency, a sprite object should be specified if DrawText is to be called more than once in a row.

pString [in]LPCTSTR

Pointer to a string to draw. If the Count parameter is -1, the string must be null-terminated.

Count [in]INT

Specifies the number of characters in the string. If Count is -1, then the pString parameter is assumed to be a pointer to a null-terminated string and DrawText computes the character count automatically.

pRect [in]LPRECT

Pointer to a RECT structure that contains the rectangle, in logical coordinates, in which the text is to be formatted. The coordinate value of the rectangle's right side must be greater than that of its left side. Likewise, the coordinate value of the bottom must be greater than that of the top.

Format [in]DWORD

 

2.DirectX还提供了CD3DFont类来处理字串绘制,相比于ID3DXFont接口,C3DFont使用的是DirectX而非GDI使得其绘制速度比ID3DXFont要快的多。也正因为如此,CD3DFont不支持ID3DXFont所支持的那些复杂的字体和格式。在要求速度并且简单字体就能满足要求的时候可以使用CD3DFont类。

CD3DFont(const TCHAR* strFontName, DWORD dwHeight, DWORD dwFlags = 0);

strFontName  一个以NULL为结尾的字串,指定了字体的名称。

dWHeight  字体高度

dWFlags 创建标记,字体形式,可选。参数可以设置为0或者D3DFONT_BOLD,D3DFONT_ITALIC,D3DFONT_ZENABLE的组合.

 

当实例化一个CD3DFont类后:

Font new CD3DFont("Times new Roman",  16, 0);

Font->InitDeviceObject(Device);

Font->RestoreDeviceObjects();

 

绘制文本,调用DrawText()方法:

HRESULT CD3DFont:: DrawText(FlOAT x, FLOAT y, DWORD dwColor, const TCHAR* strText, DWORD dwFlag = 0);

x 屏幕坐标中文本绘制的起点X坐标

y屏幕坐标中文本绘制的起点y坐标

dwColor 文本颜色

strText 指向所要绘制的文本字串的指针。

dwFlags 绘制标记,可选。可设置为0或者D3DXFONT_CENTERED、D3DXFONT_TWOSIDED、D3DXFONT_FILTERED的组合

 

例:Font->DrawText(20, 20 ,0xff000000, "hello world");

 

删除CD3DFont类的对象之前需要调用一些清理函数:

Font->InvalidateDeviceObjects();

Font->DeleteDeviceObjects();

delete Font;

 

 另外,还提供D3DXCreateText函数来产生文本的3D网格。

posted @ 2014-02-09 10:21  Moniza  阅读(1237)  评论(0)    收藏  举报