Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The LineHeightInPixels represents the height of a line of text in pixels. A line of text includes the tallest letters (such as capital letters) as well as letters which have pixels that fall below the base of the line (such as the lower-case letters 'p', 'q', and 'y').
The LineHeightInPixels is the value used by FlatRedBall when determining the size of text in the SetPixelPerfectScale. A Text object which is drawn in 2D mode (where one unit equals one pixel) will have the following relationship to the LineHeightInPixels property of its BitmapFont:
Glyphs in fonts are custom images which are typically not part of the font but which can be added through Bitmap Font Generator. This tutorial will walk you through:
How to add glyphs to a font in Bitmap Font Generator
How to use these glyphs in Text objects through code
The end result will be:
INSERT IMAGE OF GLYPH USAGE
The BitmapCharacterInfo contains information about a single character in a FlatRedBall.Graphics.BitmapFont. Usually BitmapCharacterInfos are not manually created - they are created automatically when loading a .fnt file into a BitmapFont; however, if you are creating custom fonts in code, you can do so by creating BitmapCharacterInfos.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
The RenderToTexture2D method renders a given line and instantiates a Texture2D. The Texture2D will not be part of a ContentManager; therefore, you must add it to a content manager or manually dispose it when you are finished with it to prevent accumulation errors.
The following code shows how to create a Texture and display it on screen:
The BMFC file format (which stands for "bitmap font configuration") is a file that can be loaded and saved by the Bitmap Font Generator, which also produces the .fnt file format that the FlatRedBall engine natively works with. The .bmfc file format is a powerful file format because it can be saved and loaded later to make changes to an existing font. Furthermore, Glue can use the Bitmap Font Generator to automatically create a .fnt file from a .bmfc file.
A .bmfc file can be created in the Bitmap Font Generator. To create a .bmfc file:
Open the Bitmap Font Generator
Set up a bitmap font - for more information see this page
Click Options->"Save configuration as..."
Enter a name for the file and click OK
The .bmfc file is a file which must be converted into a .fnt file. This can be done by using Glue and the Bitmap Font Generator.
Note: Glue uses command line arguments to convert a .bmfc file into a .fnt file. This is not the only way to convert a .bmfc file into a .fnt. You can also manually run a command line yourself, or you can simply open the Bitmap Font Generator program, load the configuration, then manually save out the font file.
First, you must make sure Glue is set up to use the Bitmap Font Generator as a build tool. To add the Bitmap Font Generator as a build tool:
Open Glue
Select Settings->"File Build Tools"
Click "Add new build tool"
Navigate to the location of the Bitmap Font Generator. If you installed it to the default location on Windows 7, it should be located at"C:\Program Files (x86)\AngelCode\BMFont\bmfont.exe"
Set the SourceFileType to bmfc
Set the DestinationFileType to fnt
Set the SourceFileArgumentPrefix to -c
Set the DestinationFileArgumentPrefix to -o (lower-case letter 'o')
Click OK
Once you have set up the Bitmap Font Generator as a builder in Glue, you can add .bmfc files as externally built files. To do this:
Create a .bmfc file as shown above
Right-click on the Files tree item under a Screen or Entity or Global Content Files
Select "Add Externally Built File"
Select the .bmfc file. Be sure that this file is located in a subfolder of your project.
Select the bmfont.exe builder in the drop-down in the window that appears
Click OK
Enter a name for your font (or leave it as default)
Your project will now include a .fnt file which can be loaded by code into a BitmapFont, which can be used in Glue, or which can be used in a .scnx file.
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"