BitmapFont
Last updated
Was this helpful?
Last updated
Was this helpful?
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:
While it may not be immediately obvious, there is a difference between BitmapFonts and Texts (the FlatRedBall.Graphics.Text class). A Text contains information such as location, rotation, and the string to write out. The BitmapFont contains the texture information to use which represents the font. Specifically, it contains a Texture2D as well as the texture coordinates for each character. One notable consideration is that the properties in both the BitmapFont class as well as the Text class can contain the size of the letters in a Text object. Let's investigate why this is the case. Ultimately, there are three properties which are related to the size that a Text takes up when it is rendered:
Scale
Spacing
NewlineDistance
Information and code samples on these properties can be found here. These three properties ultimately control the absolute size and spacing of each letter in the Text object. However, a BitmapFont that has letters at a higher resolution will result in a larger Text on screen. The reason for this is because the TextManager adjusts these three values according to the current Camera setup when the AddText method is called so that the Text is drawn to-the-pixel. This means that a Text that is created through the TextManager which is displaying a large BitmapFont will have higher Scale, Spacing, and NewlineDistance values than one displaying a smaller BitmapFont. This behavior simply exists as a convenience, and it can be easily overridden if necessary by changing these values.
Add the two font files to the Content folder. You can simply drag them into the Solution Explorer and they will be added.
Highlight the .fnt file and press F4 to bring up the Properties window. Make sure that its "Build Action" is "None" and its "Copy to Output Directory" is "Copy if newer"