DirectX 9 绘制2D文本

在 DirectX 9 绘制2D文本一直都是一个坑,但是我们又不能不填这个坑,因为游戏和3D应用都需要用到文本来提示玩家或者是用户。要用文本绘制函数必须要先引用 DirectX 9 的库,然后才能使用,在这里就不一一说明了。下面的教程只会讲到如何使用绘制函数来绘制文本,其他例如调用库、二次封装都不会讲到。

使用绘制函数之前,要先声明一个 ID3DXFont 的对象,然后才能调用绘制函数。

ID3DXFont* Text = 0;

然后填写 D3DXFONT_DESC 结构体,这个结构体是拿来设置文本属性的。先看看这个结构体的原型:

typedef struct D3DXFONT_DESC {
  INT   Height;
  UINT  Width;
  UINT  Weight;
  UINT  MipLevels;
  BOOL  Italic;
  BYTE  CharSet;
  BYTE  OutputPrecision;
  BYTE  Quality;
  BYTE  PitchAndFamily;
  TCHAR FaceName;
} D3DXFONT_DESC, *LPD3DXFONT_DESC;

[结构体成员](这里我偷懒了,不懂就查查英文翻译吧。)

Height
类型:INT
Height, in logical units, of the font's character cell or character.

Width
类型:UINT
Width, in logical units, of characters in the font.

Weight
类型:UINT
Weight of the font in the range from 0 through 1000.

MipLevels
类型:UINT
Number of mip levels requested. If this value is zero or D3DX_DEFAULT, a complete mipmap chain is created. If the value is 1, the texture space is mapped identically to the screen space.

Italic
类型:BOOL
Set to TRUE for an Italic font.

CharSet
类型:BYTE
Character set.

OutputPrecision
类型:BYTE
Output precision. The output precision defines how closely the output must match the requested font height, width, character orientation, escapement, pitch, and font type.

Quality
类型:BYTE
Output quality.

PitchAndFamily
类型:BYTE
Pitch and family of the font.

FaceName
类型:TCHAR
A null-terminated string or characters that specifies the typeface name of the font. The length of the string must not exceed 32 characters, including the terminating null character. If FaceName is an empty string, the first font that matches the other specified attributes will be used. If the compiler settings require Unicode, the data type TCHAR resolves to WCHAR; otherwise, the data type resolves to CHAR. See Remarks.

备注
The compiler setting also determines the structure type. If Unicode is defined, the D3DXFONT_DESC structure type resolves to a D3DXFONT_DESCW; otherwise the structure type resolves to a D3DXFONT_DESCA.

Possible values of the above members are given in the GDI LOGFONT structure.

需要的头文件:
D3dx9core.h

看完原型和结构体成员现在来看看如何设置 D3DXFONT_DESC 结构体:

D3DXFONT_DESC fontDesc; 
ZeroMemory(&fontDesc, sizeof(D3DXFONT_DESC));
fontDesc.Height = 25;    //字体的高度
fontDesc.Width = 0;    //不填由高度决定拉伸宽度
fontDesc.Weight = 0;    // 加粗范围:0(光滑) - 1000(粗体)
fontDesc.MipLevels = D3DX_DEFAULT; 
fontDesc.Italic = false;    //斜体
fontDesc.CharSet = DEFAULT_CHARSET; 
fontDesc.OutputPrecision = 0;                      
fontDesc.Quality = 0;           
fontDesc.PitchAndFamily = 0;           
lstrcpy (fontDesc.FaceName, _T("微软雅黑"));    //字体

设置完文本的属性,然后就是调用:

if (FAILED(D3DXCreateFontIndirect(Device, &fontDesc, &Text))){
    return false;
}

上面的这段代码非常重要,千万不要忘记写上了。如果不需要错误跳出,可以把If语句删掉。(Device为DirectX设备对象)

为了方便说明,我还是把绘制函数的原型粘出来吧。

int DrawText(
  HDC hDC, 
  LPCTSTR lpString, 
  int nCount, 
  LPRECT lpRect, 
  UNIT uFormat
);

至于绘制函数的成员,我就不写出来了。有需要可以去访问M$的MSDN

下面是绘制函数的设置:

RECT rect = {5, 0, Width, Height};
Text->DrawText(NULL, _T("测试文本1"), -1, &rect, DT_TOP | DT_LEFT, 0xffff00ff);

rect.top = 525;    //窗体Y轴
rect.left = 100;    //窗体X轴
Text->DrawText(NULL, _T("测试文本2"), -1, &rect, DT_TOP | DT_LEFT, 0xff77ff00);

这样就可以绘制文本了。

标签:directx, 绘制, 2d, 文本

该文章由 Shiqi 原创并发布在 被遗忘的曙光 技术博客

转载请标明来源:https://fdawn.com/CPP/10.html

已有 2 条评论

  1. heel lifts

    Hi there, You have done an excellent job.
    I'll definitely digg it and in my view suggest to my friends.

    I'm sure they'll be benefited from this web site.

  2. 运气不好怎么转运

    我又来看你啦。

添加新评论