4.4 Text Output
It is important to be able to display textual information to the user. For example, you may want your game to display subtitles or game statistics (e.g., the number of units killed). In our demos, we will want to display the average number of frames per second and the average time elapsed between frames. To display text, the D3DX library provides the ID3DX10Font interface.
To obtain a pointer to an ID3DX10Font object, we first must fill out an instance of the D3DX10_FONT_DESC structure to describe the characteristics of the font we want to create. Once that is done, a pointer to an ID3DX10Font object can be obtained with the D3DX10CreateFontIndirect function. The following code illustrates:
2 fontDesc.Height = 24;
3 fontDesc.Width = 0;
4 fontDesc.Weight = 0;
5 fontDesc.MipLevels = 1;
6 fontDesc.Italic = false;
7 fontDesc.CharSet = DEFAULT_CHARSET;
8 fontDesc.OutputPrecision = OUT_DEFAULT_PRECIS;
9 fontDesc.Quality = DEFAULT_QUALITY;
10 fontDesc.PitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
11 wcscpy(fontDesc.FaceName, L"Times New Roman");
12
13 ID3DX10Font* mFont;
14 D3DX10CreateFontIndirect(md3dDevice, &fontDesc, &mFont);
15
The first parameter of the D3DX10CreateFontIndirect function takes a pointer to a valid ID3D10Device object. The second parameter is a pointer to a D3DX10_FONT_DESC instance describing the font to create; a pointer to the created font object is returned through the third parameter.
The main method of ID3DX10Font we will be using is the ID3DX10Font::DrawText method; this method has the following prototype:
1 INT ID3DX10Font::DrawText(
2 LPD3DX10SPRITE pSprite,
3 LPCTSTR pString,
4 INT Count,
5 LPRECT pRect,
6 UINT Format,
7 D3DXCOLOR Color
8 );
-
pSprite: Pointer to an ID3DX10Sprite interface. This can be null, but if you are doing a lot of text drawing per frame, it is more efficient to supply your own sprite interface; see the SDK documentation for details. Because we only call ID3DX10Font::DrawText once per frame to output the frame statistics, we specify null in our sample code.
-
pString: Pointer to the string to draw.
-
Count: The number of characters in the string. We can specify −1 if the string is null terminating.
-
pRect: Pointer to a RECT structure, relative to the window’s client area, that defines the area to which the text is to be drawn and formatted.
-
Format: Optional flags that specify how the text should be formatted in the RECT specified by pRect. In the sample call below, we specify DT_NOCLIP, which means the text will not be clipped if it goes outside the boundaries of pRect. Other common flags are DT_CENTER and DT_VCENTER; these flags center the text horizontally and vertically, respectively, inside the rectangle specified by pRect.
-
Color: The text color. As we will learn in the next chapter, a color is represented by the D3DXCOLOR class. An instance of this class has four floating-point data members, describing the red, green, blue, and alpha components of the color.
Here is an example call:
1 // We specify DT_NOCLIP, so we do not care about width/height of the rect.
2 const D3DXCOLOR BLACK(0.0f, 0.0f, 0.0f, 1.0f);
3 RECT R = {5, 5, 0, 0};
4 mFont->DrawText(0, L"Hello, Direct3D!", -1, &R, DT_NOCLIP, BLACK);

浙公网安备 33010602011771号