BitmapFont
Last updated
Last updated
The BitmapFont class defines the texture and rendering values used to render a bitmap font. The Text object uses BitmapFonts when drawing itself. Note that the recommended approach for displaying production-ready text to the screen (including with custom fonts) is to use Gum, which automates the creation of font files and simplifies loading these files.
The main benefit of the FRB Text object is that it can sort with other visual objects and it can have better performance than Gum text objects in some cases.
Bitmap Fonts require two files - a texture file and a .fnt file. Both can be created with AngelCode.com's Bitmap Font Generator. The following steps outline the creation of the two files used in BitmapFonts:
Open Bitmap font generator.
Click or push+drag to select the letters that you'd like included in your exported font. Selected letters will highlight.
Click Options->Font Settings
Select the font that you'd like to use in the "Font" drop down. All fonts installed on your machine will appear here.
Enter the font size that you want next to "Size (px)": The larger this is, the larger each letter will be in your game. If you are using Photoshop, you can test font sizes there (see next item)
Check the "Match char height" option. This will make the resulting font match sizes in Photoshop
Press Ok. The font display will update to the newly-selected font.
Select Options->Export Options.
Select a Bit depth of 32 (or else transparencies won't come through).
Select the texture width and height. Ideally you should select a texture size large enough to fit all characters, but small enough to minimize empty space. This may require some trial and error.
Change the "Textures:" option to "png - Portable Network Graphics"
Press Ok.
Select Options->Visualize. If you see "No Output!" then you need to select characters to export. See the above step for more information.
Return to change the width/height if necessary. Verify that your font fits on one image by seeing if View->Next Page is disabled in the preview. If everything does not fit in one page consider decreasing the size of your font or increasing the source image size. Remember to keep your texture dimension a power of 2.
Click Options->Save bitmap font as... to save your .fnt and .png files.
Copy the files to their appropriate locations for your application.
For information on how to use BitmapFonts in the FRB Editor, see the Font (.fnt) page.
BitmapFontGenerator supports adding custom images to a font file. This can be used in the following situations:
Inserting images in the middle of text, such as "Press the <A button image> to continue"
Using custom images for letters instead of having BitmapFontGenerator generate them from True Type Fonts
To add a custom image to a font:
Open Bitmap Font Generator
Select "Edit"->"Open Image Manager"
In the Image Manager window, select "Image"->"Import Image..."
Navigate to the image that you want to import and click "Open". Notice that as of BMFont 1.13 you cannot import PNGs as custom glyphs. See the "Troubleshooting BMFont"
Enter the information for the new letter
Id refers to the unicode character. A full list of unicode characters can be found here. For reference, the capital letter 'A' has an ID of 65; the lower case 'a' has an ID of 97.
X advance refers to how much horizontal space is taken up by the letter. The default of 0 means that the size of the image defines the space taken up.
BMFont 1.13 includes a number of bugs which can make glyph creation more difficult. Fortunately workarounds exist for these bugs:
BMFont will crash if a .bmcc file is saved in the same folder as a referenced image. To solve this, save the .bmfc file in a different folder, or update to the 1.14 beta build of BMFont.
BMFont will not allow .png files to be added as glyphs through the steps outlined above. To work around this, the .bmfc file can be modified manually.
Note that if you are using Glue you may not have to add fonts to your project manually, as Glue will manage the project and generate the code to load the font. However, to manually add a bitmap font to your Visual Studio project:
Create or download a .fnt file and matching image file (.png is typical)
Drag+drop the files into your project's Content folder
Mark both files as Copy if Newer in their properties
Once the files are created they need to be loaded into a BitmapFont. The following code loads a BitmapFont and assigns it to a Text object. Files used: font18arial_0.png, font18arial.fnt Add the following using statement
Add the following in your Initialize method after initializing FlatRedBall
A Text's font can also be changed after it has been created. Assuming customFont is still in scope:
The .fnt file is simply a text file which must be copied to build folder, while the referenced texture must be built through the content pipeline. Therefore, each file must be handled slightly differently. The following steps outline how to add a BitmapFont to a project through the content pipeline:
Add the following code to load the bitmapFont:
Add the following using statements:
Add the following wherever you are loading your font: