我到底应该如何使用ID3DXFont接口?(Direct3D 11, c++)

How exactly am I supposed to use the ID3DXFont interface? (Direct3D 11, c++)

本文关键字:Direct3D c++ 接口 ID3DXFont 何使用      更新时间:2023-10-16

我知道微软更喜欢人们在游戏中使用Direct2D和DirectWrite来渲染文本,但在Direct3D11中使用这些api可能有点乏味。所以我想使用ID3DXFont接口来渲染文本,但问题是,我不知道如何使用它。

我试着在谷歌上搜索如何使用这个界面的教程,但我真的找不到。

无论如何,下面是我实现接口的尝试。

void Create_Font_Device()
{
D3DPRESENT_PARAMETERS presentation;
IDirect3D9 * FontDeviceDesc = NULL;
IDirect3DDevice9 * FontDevice = NULL;
ZeroMemory(&presentation, sizeof(D3DPRESENT_PARAMETERS));
presentation.SwapEffect = D3DSWAPEFFECT_DISCARD;
presentation.Windowed = TRUE;
FontDeviceDesc = Direct3DCreate9(D3D_SDK_VERSION);
FontDeviceDesc->CreateDevice(
            D3DADAPTER_DEFAULT,
            D3DDEVTYPE_HAL,
            MainWindow,
            D3DCREATE_HARDWARE_VERTEXPROCESSING,
            &presentation,
            &FontDevice
            );
D3DXCreateFont(
        FontDevice,
        20,
        20,
        1,
        false,
        DEFAULT_CHARSET,
        OUT_TT_ONLY_PRECIS,
        ANTIALIASED_QUALITY,
        DEFAULT_PITCH | FF_DONTCARE,
        NULL,
        L"Arial",
        &Font
        );
}

/* The Draw_Text() function is called in my game loop */

void Draw_Text()
{
RECT WindowCoordinates;
WindowCoordinates.left = 200;
WindowCoordinates.right = 200;
WindowCoordinates.top = 200;
WindowCoordinates.bottom = 200;
Font->DrawText(
            NULL,
            L"FPS: ",
            5,
            &WindowCoordinates,
            DT_TOP,
            D3DCOLOR_XRGB(1, 1, 255)
            );
}

这段代码是不够的,实际上写任何文本到屏幕上,我到底错过了什么?我应该如何实现ID3DXFont接口?

没有适用于Direct3D 11的D3DXFont版本D3DX11没有字体API,因为你注意到一般的期望是你会使用Direct2D/DirectWrite。

所有版本的D3DX已弃用,包括D3DX9, D3DX10和D3DX11。参见MSDN和没有D3DX的生活

对于Direct3D 11上的简单文本渲染,DirectX工具包中的SpriteFont类非常易于使用。参见绘图文本教程。

std::unique_ptr<DirectX::SpriteFont> font(new SpriteFont(device, L"myfile.spritefont"));
std::unique_ptr<DirectX::SpriteBatch> spriteBatch(new SpriteBatch(context));
spriteBatch->Begin();
font->DrawString(spriteBatch.get(), L"FPS: ", XMFLOAT2(200,200));
spriteBatch->End();

SpriteFont是一个简单的位图捕获字体,它非常适合较小的字体,如ASCII或扩展ASCII字符集。它并不特别适合呈现大字体语言(如日语、韩语或中文)或替代书写系统(如阿拉伯语)。在这些情况下,Direct2D/DirectWrite确实是最好的方法。