DirectXTK now includes SpriteFont

DirectXTK now includes SpriteFont

Rate This
  • Comments 5

The DirectX Tool Kit (DirectXTK) has been updated with the addition of a SpriteFont implementation, similar to the one in XNA.

Get it here: DirectXTK

Shamelessly plagiarizing the readme file:

This is a native D3D11 implementation of a bitmap font renderer, similar to the SpriteFont type from XNA Game Studio, plus a command line tool (MakeSpriteFont) for building fonts into bitmap format. It is less fully featured than Direct2D and DirectWrite, but may be useful for those who want something simpler and lighter weight.

At build time:

    MakeSpriteFont.exe "Comic Sans" myfile.spritefont /FontSize:16

During initialization:

    std::unique_ptr<SpriteBatch> spriteBatch(new SpriteBatch(deviceContext));
std::unique_ptr<SpriteFont> spriteFont(new SpriteFont(device, L"myfile.spritefont"));

Simple drawing:

    spriteBatch->Begin();
spriteFont->DrawString(spriteBatch.get(), L"Hello, world!", XMFLOAT2(x, y));
spriteBatch->End();

The Draw method has several overloads with parameters controlling color, rotation, origin point, scaling, horizontal or vertical mirroring, and layer depth. These work the same way as the equivalent SpriteBatch::Draw parameters.

SpriteFont has three constructors:

  • Pass a filename string to read a binary file created by MakeSpriteFont
  • Pass a buffer containing a MakeSpriteFont binary that was already loaded some other way
  • Pass an array of Glyph structs if you prefer to entirely bypass MakeSpriteFont

If you try to draw or call MeasureString with a character that is not included in the font, by default you will get an exception. Use SetDefaultCharacter to specify some other character that will be automatically substituted in place of any that are missing.

This implementation supports sparse fonts, so if you are localizing into languages such as Chinese, Japanese, or Korean, you can build a spritefont including only the specific characters needed by your program. This is usually a good idea for CJK languages, as a complete CJK character set is too large to fit in a Direct3D texture! (if you need full CJK support, Direct2D or DirectWrite would be a better choice). SpriteFont does not support combining characters or right-to-left (RTL) layout, so it will not work for languages with complex layout requirements such as Arabic or Thai.

The MakeSpriteFont tool can process any TrueType font that is installed on your system (using GDI+ to rasterize them into a bitmap) or it can import character glyphs from a specially formatted bitmap file. This latter option allows you to create multicolored fonts, drawing special effects such as gradients or drop shadows directly into your glyph textures. Characters should be arranged in a grid ordered from top left to bottom right. Monochrome fonts should use white for solid areas and black for transparent areas. To include multicolored characters, add an alpha channel to the bitmap and use that to control which parts of each character are solid. The spaces between characters and around the edges of the grid should be filled with bright pink (red=255, green=0, blue=255). It doesn't matter if your grid includes lots of wasted space, because the converter will rearrange characters, packing everything as tightly as possible.

See readme.txt for a list of MakeSpriteFont command line options.

  • Keep this FWLINK around to find the 'latest' DirectXTK <go.microsoft.com/fwlink

  • I think it's about time for this to go up on GitHub :)

  • Thanks a lot Shawn! I recently thought of coding in C++ again from time to time, while continuing to work with C# and XNA. Your posts - and DirectXTK in a more general fashion - provided me with the occasion of doing so.

    Actually, I even decided to expand my XNA tutorials website to alternative frameworks and ports, so I'll be sure to keep on watching your blog. ^^

  • Nice work, Shawn! Seems like you're destined to (re-)implement XNA on every platform / language you come into contact with :)

  • verry nice , i like the idear about github so more people can work on it

    shawn do you know of any sample of an

    windows 8 3d games in c#

    i have been lokking for this and more about the canvas and xaml stack

    Best regards

    Michael

Page 1 of 1 (5 items)
Leave a Comment
  • Please add 7 and 1 and type the answer here:
  • Post