1.下载DirectX11 Tool Kit SDK,解压后编译,生成DirectXTK.lib库文件和MakeSpriteFont应用工具;
2.在Dx11环境基础上,用生成的库文件搭建XTK环境;
3.利用MakeSpriteFont.exe生成想要的字体纹理库;
命令格式:
path "字体名" "生成路径" /参数:值
如 C://MakeSpriteFont.exe "微软雅黑" "G://wryh.sfont" /CharacterRegion:0x0-0xfff /DefaultCharacter:0x0 /CharacterRegion:0x4E00-0x9FA5 /DefaultCharacter:0x4E00 /FastPack
关于字符区域可以参考MakeSpriteFont中的源码:
// Which characters to include in the font (eg. "/CharacterRegion:0x20-0x7F /CharacterRegion:0x123")
[CommandLineParser.Name("CharacterRegion")]
public readonly List<CharacterRegion> CharacterRegions = new List<CharacterRegion>(); // Fallback character used when asked to render a codepoint that is not
// included in the font. If zero, missing characters throw exceptions.
public readonly int DefaultCharacter = ;
因为是链表结构,所以可以输入若干不连续的区域.这里建议参照unicode选择所需的编码区域.
当然工程项目也应该是unicode的,若采用其他字符集,则需要自己做编码转换.
4.渲染字体
//声明
std::unique_ptr<DirectX:: SpriteBatch> spriteBatch;
std::unique_ptr<DirectX::SpriteFont> spriteFont; //初始化
using namespace DirectX;
spriteBatch = std::unique_ptr<SpriteBatch>(new SpriteBatch(g_pImmediateContext));
spriteFont = std::unique_ptr<SpriteFont>(new SpriteFont(g_pd3dDevice, L"./wryh.sfont")); //渲染
spriteBatch->Begin();
spriteFont->DrawString(spriteBatch.get(), L"文体开花!", DirectX::XMFLOAT2(, ));
spriteBatch->End();
5.效果