Font application in DirectX

Source: Internet
Author: User
Tags drawtext
1 DX font creation Function
 HRESULT  D3DXCreateFont(  __in   LPDIRECT3DDEVICE9 pDevice,  __in   INT Height,  __in   UINT Width,  __in   UINT Weight,  __in   UINT MipLevels,  __in   BOOL Italic,  __in   DWORD CharSet,  __in   DWORD OutputPrecision,  __in   DWORD Quality,  __in   DWORD PitchAndFamily,  __in   LPCTSTR pFacename,  __out  LPD3DXFONT *ppFont);
 HRESULT  D3DXCreateFontIndirect(  __in   LPDIRECT3DDEVICE9 pDevice,  __in   const D3DXFONT_DESC *pDesc,  __out  LPD3DXFONT *ppFont);
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;

It can be seen that the d3dxcreatefont function is exactly the same as the d3dxcreatefontindirect function, except that the d3dxfont_desc structure is used in d3dxcreatefontindirect to replace many messy parameters in d3dxcreatefont.


The following describes in detail the meaning of each parameter:
Height: The font height.
Width: The font width.
Weight: The font weight, ranging from 0 to 1000, that is, the font width, the finest of 0, and the thickest of 1000.
Miplevels: font filtering attribute. If the value is 0 or d3dx_default, the complete mipmap chain is created. If the value is 1, the texture space is mapped to the screen space.
Italic: A boolean variable. True indicates italic, and false indicates not italic.
Charset: character set, which is generally set to the default value default_charset
Outputprecision: the precision of the output text. It refers to the fit of the output text to the font width, height, character direction, line spacing, spacing, and font type. It is usually set to out_default_prects, for details, see wingdi. type defined in H
Quality: output quality, which is also set to default_quality.
Pitchandfamily: spacing and font family, usually set to 0. For details, see wingdi.
# Define default_pitch 0
# Define fixed_pitch 1
# Define variable_pitch 2
# If (winver> = 0x0400)
# Define mono_font 8


# Define ff_dontcare (0 <4)/* don't care or don't know .*/
# Define ff_roman (1 <4)/* variable stroke width, serifed .*/
/* Times Roman, century schoolbook, etc .*/
# Define ff_swiss (2 <4)/* variable stroke width, sans-serifed .*/
/* Helvetica, Swiss, etc .*/
# Define ff_modern (3 <4)/* constant stroke width, serifed or sans-serifed .*/
/* Pica, elite, courier, etc .*/
# Define ff_script (4 <4)/* cursive, etc .*/
# Define ff_decorative (5 <4)/* Old English, etc .*/
In this way, I can freely match the font family and word interval,


Facename: font name, such as "" and "文 "

Example:
// Create the font HR (d3dxcreatefont (m_pdevice, 30,20, 0, d3dx_default, false, default_charset, out_tt_only_precis, default_quality, 81, text (""), & m_pfont )); // draw the font rect; getclientrect (m_hwnd, & rect); tchar Buf [50] = {0}; int nsize = _ stprintf (BUF, text ("FPS: % 0.2f "), getfps (dtime); HR (m_pfont-> drawtext (null, Buf, nsize, & rect, dt_right, d3dcolor_xrgb (2,190,100); rect. top = 100; HR (m_pfont-> drawtext (null, text ("indelible passion forever"),-1, & rect, dt_center, d3dcolor_xrgb (, 90, 100); rect. top = 200; HR (m_pfont-> drawtext (null, text ("arrogant teenager forever"),-1, & rect, dt_center, d3dcolor_xrgb (255,120, 10 )));

2. Create a font function in the corresponding GDI

Createfont and createfontindirect


3. 3D Fonts
The d3dxcreatetext function creates a grid, which is used to draw the corresponding grid when drawing text.
 HRESULT  D3DXCreateText(  __in   LPDIRECT3DDEVICE9 pDevice,  __in   HDC hDC,  __in   LPCTSTR pText,  __in   FLOAT Deviation,  __in   FLOAT Extrusion,  __out  LPD3DXMESH *ppMesh,  __out  LPD3DXBUFFER *ppAdjacency,  __out  LPGLYPHMETRICSFLOAT pGlyphMetrics);

Ptext: the content to be displayed.
Deviation: Maximum string deviation of the TrueType font contour. The value must be a non-negative string deviation equal to a design unit of the original font and generally set to 0.001.
Extrusion: font depth along the negative direction of the Z axis
Ppmesh: returns the created font mesh.
Ppadjacency: returns the information about the adjacent State of the created grid. If not required, you can specify this parameter as null.
Pglyphmetrics: pointer to the lpglyphmetricsfloat type struct, which contains metric data. If this value is not required, set it to null.

We can see that the HDC option is included in the parameters of the function, which is mainly used to select the hfont into the device context and then draw a 3D text grid using the font ., Therefore, we need to first create a font, and then select the font into the device.
You can use the createfont or createfontindirect function mentioned above to create a font.

Example:
// Create the font hfont; hfont hfontold; HDC = createcompatibledc (null); hfont = createfont (500, false, default_charset, out_default_precis, limit, default_quality, default_pitch | ff_dontcare, text (""); hfontold = (hfont) SelectObject (HDC, hfont); d3dxcreatetext (m_pdevice, HDC, text ("innocent"), 0.001f, 0.5f, & m_pfontmesh, null, null); SelectObject (HDC, hfontold); deleteobject (hfont); deletedc (HDC ); // draw the font m_pfontmesh-> drawsubset (0 );

Final display effect:


Program:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.