VB6 Programming: DirectX 2D Graphics Learning Log 11 Summary of Lesson 7

VB6 programming: DirectX 2D graphics learning log 11 summarizes Lesson 7 as follows:
Tutorial download link: https://download.csdn.net/download/gosub60/13696651
Draw text

First define the referenced function type:

Private Main_Font As D3DXFont
Private Main_Font_Description As IFont 'IFont 是VB6内部自带的stdole.tlb的一个引用
Private Text_Rect As RECT

One: D3DXCreateFont : Create font objects for devices and fonts. The relevant definitions in the tutorial example are as follows:

    Font.Name = "Arial"'(Font为VB6内部字体函数。)
    Font.Size = 14
    Set Main_Font_Description = Font
    Set Main_Font = Direct3DX.CreateFont(Direct3D_Device, Main_Font_Description.hFont)

Among them: ① [D3DXFONT_DESC structure: defines the attributes of the font. ] (https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3dxfont-desc) The C++ source code structure is as follows:

typedef struct D3DXFONT_DESC {
         INT   Height;//Height, in logical units,指的是字体的高度,
         UINT  Width;//Width, in logical units 字体中字符的宽度,以逻辑单位表示。
         UINT  Weight;//字体的权重,范围从01000。
         UINT  MipLevels;//请求的Mip级别数。如果该值为零或D3DX_DEFAULT,则创建完整的mipmap链。如果值为1,则将纹理空间映射到屏幕空间。
         BOOL  Italic;//是否斜体,设置为TRUE表示斜体字体。
         BYTE  CharSet;//字符集
         BYTE  OutputPrecision;// 输出精度定义输出必须与请求的字体高度,宽度,字符方向,擒纵,间距和字体类型紧密匹配的程度。
         BYTE  Quality;//输出质量
         BYTE  PitchAndFamily;//斜度和family索引
         TCHAR FaceName;//字体类型名称,以空值结尾的字符串或字符,用于指定字体的字体名称。字符串的长度不能超过32个字符,包括终止的空字符。如果FaceName是一个空字符串,则将使用与其他指定属性匹配的第一个字体。如果编译器设置需要Unicode,则数据类型TCHAR解析为WCHAR;否则,默认格式为WCHAR。否则,数据类型解析为CHAR。
} D3DXFONT_DESC, *LPD3DXFONT_DESC;

It should be noted that the creation of objects requires the device to support 32-bit colors. If Unicode is defined, the function call resolves to D3DXCreateFontW. Otherwise, the function call will resolve to D3DXCreateFontA because the ANSI string is being used.

[D3DXFont] (): The ID3DXFont interface encapsulates the textures and resources needed to render a specific font on a specific device.

Text_Rect is a font coordinate, it is a RECT structure, and a rectangle is defined by the coordinates of its upper left corner and lower right corner. The C++ source code is as follows:

typedef struct tagRECT {
  LONG left;指定矩形左上角的x坐标。
  LONG top;指定矩形左上角的y坐标。
  LONG right;指定矩形右下角的x坐标。
  LONG bottom;指定矩形右下角的y坐标。
} RECT, *PRECT, *NPRECT, *LPRECT;

Use it directly in this example:

    Text_Rect.Left = 30
    Text_Rect.Top = 30
    Text_Rect.Right = Text_Rect.Left + 100
    Text_Rect.bottom = Text_Rect.Top + 20

2. Draw text. [ID3DXFont :: DrawText] (https://docs.microsoft.com/en-us/windows/win32/direct3d9/id3dxfont–drawtext) method to draw formatted text. This method supports ANSI and Unicode strings. The C++ prototype code of this function is as follows:

INT DrawText(
  [in] LPD3DXSPRITE pSprite,
  [in] LPCTSTR      pString,
  [in] INT          Count,
  [in] LPRECT       pRect,
  [in] DWORD        Format,
  [in] D3DCOLOR     Color
);

The parameters are explained as follows:
pSprite: Pointer to ID3DXSprite object containing string. Can be NULL, in which case Direct3D will use its own sprite object to render the string. In order to improve efficiency, if you want to call DrawText multiple times in succession, you should specify a sprite object.
pString: Pointer to the string to be drawn. If the Count parameter is -1, the string must end with a null value.
INT: Specify the number of characters in the string. If Count is -1, the pString parameter is assumed to be a pointer to a null-terminated string, and DrawText automatically calculates the character count.
**pRect:** Pointer to the RECT structure that contains the rectangle in which to format the text in logical coordinates. The coordinate value on the right side of the rectangle must be greater than the coordinate value on the left side of the rectangle. Similarly, the bottom coordinate value must be greater than the top coordinate value.

**Format:** Specify the method of formatting text. It can be any combination of the following values:

DT_BOTTOM
将文本对齐到矩形的底部。该值必须与DT_SINGLELINE结合使用。
DT_CALCRECT
确定矩形的宽度和高度。如果有多行文本,则DrawText使用pRect参数指向的矩形的宽度,并扩展矩形的底部以绑定文本的最后一行。如果只有一行文本,则DrawText会修改矩形的右侧,以使其限制该行中的最后一个字符。在任何一种情况下,DrawText返回格式化文本的高度,但不绘制文本。
DT_CENTER
在矩形中将文本水平居中。
DT_EXPANDTABS
扩展制表符。每个选项卡的默认字符数是8。
DT_LEFT
将文本向左对齐。
DT_NOCLIP
绘制时不剪裁。使用DT_NOCLIP时,DrawText的速度更快。
DT_RIGHT
将文本向右对齐。
DT_RTLREADING
选择希伯来语或阿拉伯字体时,以从右到左的阅读顺序显示双向文本。所有文本的默认阅读顺序是从左到右。
DT_SINGLELINE
仅在一行上显示文本。回车和换行不中断行。
DT_TOP
使文本最上对齐。
DT_VCENTER
垂直居中放置文本(仅单行)。
DT_WORDBREAK
断话。如果单词会超出pRect参数指定的矩形的边缘,则单词之间的线会自动断开。回车/换行的顺序也使行中断。

**D3DCOLOR: **Color, I talked about it before, if you don't remember, you can check the previous article.
DrawText is used in this example (note that the function description above is DX9, and the order of DX8 is a bit different. Refer to the DX8 call statement as follows: Sub DrawText(d3dFont As D3DXFont, color As Long, TextString As String, RECT As RECT, Format As Long )) wrote a "Hello World".

Direct3DX.DrawText Main_Font, D3DColorRGBA(255, 255, 255, 255), "Hello World", Text_Rect, DT_TOP Or DT_LEFT

Three: extend the drawing of 3D fonts (there will be a tutorial introduction later, here is only the function.) D3DXCreateText function, create a grid, when drawing text is to draw the corresponding grid. (The DX8 reference format is as follows: Sub CreateText(D3DDevice As Direct3DDevice8, hDC As Long, Text As String, Deviation As Single, Extrusion As Single, retMesh As D3DXMesh, adjacencyOut As D3DXBuffer, GlyphMetrics As Any))
D3DXCreateText function, the prototype C++ code is as follows:

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
);

pDevice: Pointer to the device that created the grid.
hDC: Device context, including fonts used for output. The font selected by the device context must be a TrueType font.
pText: Pointer to a character string specifying the text to be generated. If the compiler settings require Unicode, the data type LPCTSTR is parsed as LPCWSTR. Otherwise, the string data type is parsed as LPCSTR.
Deviation: The maximum chord deviation from TrueType font outline. Generally set to 0.001
Extrusion: The amount of text to be extruded along the negative z direction.
ppMesh: Pointer to the returned mesh.
ppAdjacency: Pointer to the buffer containing adjacency information. Can be NULL.
pGlyphMetrics: Pointer to the GLYPHMETRICSFLOAT structure array containing glyph indicator data. Each element contains information about the position and orientation of the corresponding glyph in the string. The number of elements in the array should be equal to the number of characters in the string. Please note that the origin in each structure is not relative to the entire string, but relative to the character cell. To calculate the entire bounding box, add increments to each glyph as you traverse the string. If you don't care about the size of the font, please set this parameter to NULL.

You can see that there is an option of hdc in the function parameters. This is mainly to select HFONT into the device context, and then use the font to draw a 3D text grid. , So we need to create the font first, and then select the font into the device.
There is no doubt that creating a font is to use the CreateFont or CreateFontIndirect function mentioned above (indirectly create font objects for devices and fonts.).

Guess you like

Origin blog.csdn.net/gosub60/article/details/111166275