我究竟应该如何使用 ID3DXFont 接口? (Direct3D 11,C++)
How exactly am I supposed to use the ID3DXFont interface? (Direct3D 11, c++)
我知道微软更喜欢人们使用 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.
All version of D3DX are deprecated including D3DX9, D3DX10, and D3DX11. See MSDN and Living without D3DX
对于 Direct3D 11 上的简单文本渲染,DirectX Tool Kit is very easy to use. See the Drawing text 教程中的 SpriteFont
class。
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
is a simple bitmap-captured font, which works very well for smaller fonts like the ASCII or extended ASCII character set. It is not particularly suited to rendering large character font languages like Japanese, Korean, or Chinese or for alternative writing systems like Arabic. In these cases, Direct2D/DirectWrite really is the best way to go.
我知道微软更喜欢人们使用 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.
All version of D3DX are deprecated including D3DX9, D3DX10, and D3DX11. See MSDN and Living without D3DX
对于 Direct3D 11 上的简单文本渲染,DirectX Tool Kit is very easy to use. See the Drawing text 教程中的 SpriteFont
class。
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
is a simple bitmap-captured font, which works very well for smaller fonts like the ASCII or extended ASCII character set. It is not particularly suited to rendering large character font languages like Japanese, Korean, or Chinese or for alternative writing systems like Arabic. In these cases, Direct2D/DirectWrite really is the best way to go.