- A+
所属分类:Windows游戏开发 原创文章
很多时候,在游戏中
我们都需要在界面上输出一些字体
现在来看看DirectX怎么输出字体
首先,声明全局变量
1 |
LPD3DXFONT font = NULL; |
初始化font变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
D3DXFONT_DESC desc = {}; //desc.Height = 24; //desc.Width = 0; //desc.Weight = 0; //desc.MipLevels = D3DX_DEFAULT; //desc.Italic = FALSE; //desc.CharSet = DEFAULT_CHARSET; //desc.OutputPrecision = OUT_TT_ONLY_PRECIS; //desc.Quality = CLIP_DEFAULT_PRECIS; //desc.PitchAndFamily = DEFAULT_PITCH; //strcpy_s(desc.FaceName, faceName.c_str()); D3DXCreateFontIndirect(d3ddev, &desc, &font); if (font == NULL) { MessageBox(NULL, "font创建失败", "失败", MB_OK); } |
在这里主要的是字体的描述
D3DXCreateFontIndirect函数的寓意很明显
参数也很少,一看就知道
所以下面来看看用来描述字体的结构体
D3DXFONT_DESC
123456789101112 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
- 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
他告诉我们,当你需要某些功能效果时
就去指定某一属性,到时再查文档也不迟
最后就是输出了
代码
1 2 3 4 5 6 7 8 9 |
if (d3ddev->BeginScene()) { string text = "Hello World"; RECT rect = { 100,10,200,200 }; font->DrawTextW(NULL, L"Hello World", text.length(), &rect, DT_WORDBREAK, D3DCOLOR_XRGB(255, 255, 255)); d3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL); } |
主要的就是DrawTextW了
12345678 INT DrawText([in] LPD3DXSPRITE pSprite,[in] LPCTSTR pString,[in] INT Count,[in] LPRECT pRect,[in] DWORD Format,[in] D3DCOLOR Color);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
字体的颜色