In exactly one of the compiled source files of your software, you must #define PDBMFONT_DEFINE
before including PDBMFont.hpp for the definition of the library to be provided. Elsewhere in your software, you just #include "PDBMFont.hpp"
, without the definition macro being defined before those inclusion locations, to get a declaration of the BMFont class.
When using the library for reading and/or writing, one or more of the following constants have to be defined before including PDBMFont.hpp:
PDBMFONT_TEXT
PDBMFONT_BINARY
PDBMFONT_XML
Be sure that you define the same format constants before every inclusion of PDBMFont.hpp in your software.
You can also include the library with no read and write support for all formats, but you'll only be able to use the BMFont class however you wish. You could do that to subclass PDBMFont::BMFont with support for some other format(s) while keeping the same API.
The library doesn't do anything with the image files that fonts reference, so you'll have to handle those in your own code.
Basic use of the library, with the automatic format handling functions:
PDBMFont::BMFont font;
// Will automatically read in the file's format.
// Actually checks the contents of the file; file extension doesn't matter, and isn't checked.
// Use the readText/readBinary/readXML functions if you want to read in a specific format.
font.read("Font.fnt");
// Use the font in your code.
drawText(font, "Example Text");
// You can even write a font out to a file.
// The write() function will write in the format most recently read in,
// or won't write if no read function has been called on the font.
// Use the writeText/writeBinary/writeXML functions if you want to write in a specific format.
font.write("NewFont.fnt");
A test program that demonstrates use of the library with SDL2, with correct typesetting, is provided, and is also available under the terms of the Unlicense. You will have to provide your own bitmap font to use the test program.
XML support requires tinyxml2, but if you don't need XML support for some reason (you won't use it, or don't want to be restricted by tinyxml2's license) XML support can be disabled.
The binary format is the fastest and has the smallest file size, and if you don't intend to hand-edit the font files, it might be the best choice; for example, using the binary format could reduce game load times.
Since this library has full support for reading and writing all formats, the following use cases are possible:
- Simply load fonts for use in your game.
- Create fonts that are generated by your font creation tool.
- Convert existing fonts between formats.
- Read, modify, and write existing fonts.