Using AndEngine font resources
AndEngine fonts are simple to set up and include for use in our Text
objects to be displayed on screen. We can choose from preset fonts or we can add our own via the assets
folder.
Getting ready
Perform the Know the life cycle recipe given in this chapter, so that we've got a basic AndEngine project set up in our IDE, then continue on to the How to do it... section.
How to do it…
The following code snippets display the four different options we have for creating preset, custom asset, preset stroke, and custom asset stroke font objects. Font creation should take place in the onCreateResources()
method of our BaseGameActivity
class.
The
create()
method for preset fonts is as follows:Font mFont = FontFactory.create(mEngine.getFontManager(), mEngine.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.NORMAL), 32f, true, org.andengine.util.adt.color.Color.WHITE_ABGR_PACKED_INT) mFont.load();
The
createFromAsset()
method for custom fonts is as follows:Font mFont = FontFactory.createFromAsset(mEngine.getFontManager(), mEngine.getTextureManager(), 256, 256, this.getAssets(), "Arial.ttf", 32f, true, org.andengine.util.adt.color.Color.WHITE_ABGR_PACKED_INT); mFont.load();
The
createStroke()
andcreateStrokeFromAsset()
methods for outlined fonts are:BitmapTextureAtlas mFontTexture = new BitmapTextureAtlas(mEngine.getTextureManager(), 256, 256, TextureOptions.BILINEAR); Font mFont = FontFactory.createStroke(mEngine.getFontManager(), mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, org.andengine.util.adt.color.Color.WHITE_ABGR_PACKED_INT, 3, org.andengine.util.adt.color.Color.BLACK_ABGR_PACKED_INT); mFont.load();
How it works…
As we can see, there are different approaches we can take to create our Font
objects depending on how we'd like the font to look. However, all fonts share the need for us to define a texture width and texture height, whether it be directly as parameters in the FontFactory
class' create
methods or indirectly through the use of a BitmapTextureAtlas
object. In the previous code snippets, we'd created all three Font
objects using a texture size of 256
pixels in width by 256
pixels in height. Unfortunately, there is currently no easy way to automatically determine the texture size needed at runtime in order to support different languages, text sizes, stroke value, or font style.
For now, the most common approach is to set the texture width and height to about 256
pixels and make small adjustments upward or downward until the texture is just the right size so as to not cause artifacts in the Text
objects. The font size plays the biggest role in determining the final texture size needed for the Font
object, so exceedingly large fonts, such as 32 and higher, may need larger texture sizes.
Note
All Font
objects require a method call to load()
before they can properly display characters in the Text
objects.
Let's take a look at how each of the methods presented in the How to do it... section work:
The
create()
method: Thecreate()
method doesn't allow for too much customization. This method's parameters, starting at the fifth parameter, include supplying a typeface, font size, anti-aliasing option, and a color. We're using the Android native typeface class which only supports a few different fonts and styles.The
createFromAsset()
method: We can use this method in order to introduce custom fonts into our project via ourassets
folder. Let's assume that we have a true-type font calledArial.ttf
located in our project'sassets
folder. We can see that the general creation is the same. In this method, we must pass the activity'sAssetManager
class, which can be obtained through our activity'sgetAssets()
method. The parameter following that is the true type font we would like to import.The
createStroke()
andcreateStrokeFromAsset()
methods: Finally, we have our stroke fonts. The stroke font gives us the ability to add outlines to the characters in ourText
object. These fonts are useful in situations where we would like our text to "pop". For creating stroke fonts, we'll need to supply a texture atlas as the second parameter rather than passing the engine's texture manager. From this point, we can either create the stroke font via a typeface or through ourassets
folder. Additionally, we're given the option to define two new color values which have been added as the final two parameters. With these new parameters, we are able to adjust the thickness of the outline as well as the color.
There's more…
The way the Font
class is currently set up, it is best to preload the characters that we expect to display via a Text
object. Unfortunately, AndEngine currently makes calls to the garbage collector when new letters are still to be drawn, so in order to avoid hiccups when a Text
object is first getting "acquainted" with the letters, we can call the method:
mFont.prepareLetters("abcdefghijklmnopqrstuvwxyz".toCharArray())
This method call would prepare the lowercase letters from a to z. This method should be called during a loading screen at some point within the game in order to avoid any noticeable garbage collection. There's one more important class that we should discuss before moving off the topic of Font
objects. AndEngine includes a class called FontUtils
that allows us to retrieve information regarding a Text
object's width on the screen via the measureText(pFont, pText)
method. This is important when dealing with dynamically-changing strings as it gives us the option to relocate our Text
object, assuming that the width or height of the string in pixels has changed.
See also
Know the life cycle in this chapter.
Working with different types of textures in this chapter.
Applying text to a layer in Chapter 2, Working with Entities.