Windows游戏开发之Direct输出字体

  • Windows游戏开发之Direct输出字体已关闭评论
  • 16,997 views
  • A+

很多时候,在游戏中

我们都需要在界面上输出一些字体

现在来看看DirectX怎么输出字体


首先,声明全局变量


初始化font变量

在这里主要的是字体的描述

D3DXCreateFontIndirect函数的寓意很明显

参数也很少,一看就知道

所以下面来看看用来描述字体的结构体

D3DXFONT_DESC

参数

* Height
Type: INT
字体单位高度
* Width
Type: UINT
字体单位宽度
*Weight
Type: UINT
字体粗度,范围(0,1000)
*MipLevels
Type: 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
Type: BOOL
是否斜体
*CharSet
Type: BYTE
字体特征
*OutputPrecision
Type: BYTE
输出的精密度,尽量匹配字体设置
*Quality
Type: BYTE
输出质量
*PitchAndFamily
Type: BYTE
Pitch and family of the font.
FaceName
Type: TCHAR
指定字型名字,如果为空,就会匹配其他指定属性

由图片可以看出

整个结构体都是0

他告诉我们,当你需要某些功能效果时

就去指定某一属性,到时再查文档也不迟


最后就是输出了

代码

主要的就是DrawTextW了

Parameters

pSprite [in]
Type: 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]
Type: LPCTSTR

要输出的字符串

Count [in]
Type: INT

字符串的长度

pRect [in]
Type: LPRECT

渲染字体的矩形区域

Format [in]
Type: DWORD

Specifies the method of formatting the text. It can be any combination of the following values:

Value Meaning
 

DT_BOTTOM
Justifies the text to the bottom of the rectangle. This value must be combined with DT_SINGLELINE.
 

DT_CALCRECT
Determines the width and height of the rectangle. If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by the pRect parameter and extends the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text but does not draw the text.
 

DT_CENTER
Centers text horizontally in the rectangle.
 

DT_EXPANDTABS
Expands tab characters. The default number of characters per tab is eight.
 

DT_LEFT
Aligns text to the left.
 

DT_NOCLIP
Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used.
 

DT_RIGHT
Aligns text to the right.
 

DT_RTLREADING
Displays text in right-to-left reading order for bidirectional text when a Hebrew or Arabic font is selected. The default reading order for all text is left-to-right.
 

DT_SINGLELINE
Displays text on a single line only. Carriage returns and line feeds do not break the line.
 

DT_TOP
Top-justifies text.
 

DT_VCENTER
Centers text vertically (single line only).
 

DT_WORDBREAK
Breaks words. Lines are automatically broken between words if a word would extend past the edge of the rectangle specified by the pRect parameter. A carriage return/line feed sequence also breaks the line.

 

Color [in]
Type: D3DCOLOR

字体的颜色