DirectX9:基础篇 第七章 字体

旧街凉风 提交于 2020-01-23 19:17:30

一.简介

在Direct3D中,有三种输出文字的方法:

  • (1) ID3DXFont 接口渲染文字
  • (2) CD3DFont 类渲染文字
  • (3) D3DXCreateText() 函数渲染文字

 

二.ID3DXFont

1.创建 ID3DXFont 接口

HRESULT D3DXCreateFontIndirect(

  LPDIRECT3DDEVICE9 pDevice,

  CONST LOGFONT* pLogFont,

  LPD3DXFONT* ppFont

);

 

LOGFONT lf;
ZeroMemory(&lf, sizeof(LOGFONT));
lf.lfHeight = 25;
lf.lfWidth = 12;
lf.lfWeight = 500;
lf.lfItalic = false;
lf.lfUnderline = false;
lf.lfStrikeOut = false;
lf.lfCharSet = DEFAULT_CHARSET;

strcpy(lf.lfFaceName, "Times New Roman");
ID3DXFont* font = 0;
D3DXCreateFontIndirect(Device, &lf, &font);

 

2.绘制文本

INT ID3DXFont::DrawText(

  LPCSTR pString,    // 要绘制的文字

  INT Count,       // 字符数量

  LPRECT pRect,      // 文本绘制范围

  DWORD Format,    // 文字格式化

  D3DCOLOR Color     // 文字颜色

);

 

Font->DrawText(
    "Hello World",
    -1,
    &rect,
    DT_TOP | DT_LEFT,
    0xff000000
);

 

3.计算每秒的渲染帧数

DWORD FrameCnt;
float TimeElapsed;
float FPS;

void CalcFPS(float timeDelta)
{
    FrameCnt++;
    TimeElapsed += timeDelta;
    if (TimeElapsed >= 1.0f)
    {
        FPS = (float)FrameCnt / TimeElapsed;
        TimeElapsed = 0.0f;
        FrameCnt = 0;
    }
}

 

三.CD3DFont

1.创建 CD3DFont 类

CD3DFont(

     const TCHAR* strFontName,  // 以null结束的字符串,

      DWORD dwHeight,      // 字体的高度

      DWORD dwFlags = 0L     // 可选参数: D3DFONT_BOLD, D3DFONT_ITALIC, D3DFONT_ZENABLE

);

Font  = new CD3DFont("Times New Roman", 16, 0);
Font->InitDeviceObjects(Device);
Font->RestoreDeviceObjects();

 

2.绘制文本

HRESULT CD3DFont::DrawText(FLOAT x, FLOAT y, DWORD dwColor, const TCHAR* strText, DWORD dwFlags = 0L);

  • x

    文字在屏幕上开始绘制的 x 坐标

  • y

    文字在屏幕上开始绘制的 y 坐标

  • dwColor

    文字的颜色

  • strText

    要绘制的文字

  • dwFlags

    可选参数:

    0

    D3DFONT_CENTERED,

    D3DFONT_TWOSIDED,

    D3DFONT_FILTERED

Font->DrawText(20, 20, 0xff000000, "Hello World");

 

3.清除文本

Font->InvalidateDeviceObjects();
Font->DeleteDeviceObjects();
delete Font;

 

四.D3DXCreateText()

HRESULT D3DXCreateText(

  LPDIRECT3DDEVICE9 pDevice,

  HDC hDC,

  LPCTSTR pText,

  FLOAT Deviation,

  FLOAT Extrusion,

  LPD3DXMESH* ppMesh,

  LPD3DXBUFFER* ppAdjacency,

  LPGLYPHMETRICSFLOAT pGlyphMetrics

);

  • pDevice

    设备

  • hDC

    设备环境句柄

  • pText

    指向以 null 结束的字符串指针

  • Deviation

    字形轮廓,值越接近0越光滑

  • Extrusion

    文字在 z 轴方向的深度

  • ppMesh

    

  • ppAdjacency

    

  • pGlyphMetrics

    一个指向 LPGLYPHMETRICSFLOAT 结构数组的指针

 

HDC hdc = CreateCompatibleDC(0);

LOGFONT lf;
ZeroMemory(&lf, sizeof(LOGFONT));

lf.lfHeight = 25;
lf.lfWidth = 12;
lf.lfWeight = 500;
lf.lfItalic = false;
lf.lfUnderline = false;
lf.lfStrikeOut = false;
lf.lfCharSet = DEFAULT_CHARSET;
// font style
strcpy(lf.lfFaceName, "Times New Roman");
// Create a font and select that
HFONT hFont;
HFONT hFontOld;
hFont = CreateFontIndirect(&lf);
hFontOld = (HFONT)SelectObject(hdc, hFont);// Create the 3D mesh of textID3DXMesh* Text = 0;D3DXCreateText(_device, hdc, "Direct3D", 0.001f, 0.4f, &Text, 0, 0);// Reselect the old font, and free resourcesSelectObject(hdc, hFontOld);DeleteObject(hFont);DeleteDC(hdc);Text->DrawSubset(0);

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!