- AndEngine for Android Game Development Cookbook
- Jayme Schroeder Brian Broyles
- 880字
- 2021-08-05 18:19:49
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.
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.
- 數據挖掘原理與實踐
- 信息系統與數據科學
- Python數據分析、挖掘與可視化從入門到精通
- PySpark大數據分析與應用
- Libgdx Cross/platform Game Development Cookbook
- Lean Mobile App Development
- 數據庫原理與應用(Oracle版)
- Starling Game Development Essentials
- 數據庫技術及應用教程
- 區塊鏈技術應用與實踐案例
- 數據分析師養成寶典
- Spring Boot 2.0 Cookbook(Second Edition)
- Python 3爬蟲、數據清洗與可視化實戰
- 推薦系統全鏈路設計:原理解讀與業務實踐
- 數據時代的品牌智造