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中的源码:
- 1 // Which characters to include in the font (eg. "/CharacterRegion:0x20-0x7F /CharacterRegion:0x123")
- 2 [CommandLineParser.Name("CharacterRegion")]
- 3 public readonly List<CharacterRegion> CharacterRegions = new List<CharacterRegion>();
- 4
- 5 // Fallback character used when asked to render a codepoint that is not
- 6 // included in the font. If zero, missing characters throw exceptions.
- 7 public readonly int DefaultCharacter = 0;
因为是链表结构,所以可以输入若干不连续的区域.这里建议参照unicode选择所需的编码区域.
当然工程项目也应该是unicode的,若采用其他字符集,则需要自己做编码转换.
4.渲染字体
- 1 //声明
- 2 std::unique_ptr<DirectX:: SpriteBatch> spriteBatch;
- 3 std::unique_ptr<DirectX::SpriteFont> spriteFont;
- 4
- 5 //初始化
- 6 using namespace DirectX;
- 7 spriteBatch = std::unique_ptr<SpriteBatch>(new SpriteBatch(g_pImmediateContext));
- 8 spriteFont = std::unique_ptr<SpriteFont>(new SpriteFont(g_pd3dDevice, L"./wryh.sfont"));
- 9
- 10 //渲染
- 11 spriteBatch->Begin();
- 12 spriteFont->DrawString(spriteBatch.get(), L"文体开花!", DirectX::XMFLOAT2(10, 20));
- 13 spriteBatch->End();
5.效果
