Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
AndEngine for Android Game Development Cookbook

You're reading from   AndEngine for Android Game Development Cookbook AndEngine is a simple but powerful 2D game engine that's ideal for developers who want to create mobile games. This cookbook will get you up to speed with the latest features and techniques quickly and practically.

Arrow left icon
Product type Paperback
Published in Jan 2013
Publisher Packt
ISBN-13 9781849518987
Length 380 pages
Edition 1st Edition
Arrow right icon
Authors (3):
Arrow left icon
Brian Boyles Brian Boyles
Author Profile Icon Brian Boyles
Brian Boyles
Nicolas Gramlich Nicolas Gramlich
Author Profile Icon Nicolas Gramlich
Nicolas Gramlich
JAYME SCHROEDER JAYME SCHROEDER
Author Profile Icon JAYME SCHROEDER
JAYME SCHROEDER
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

AndEngine for Android Game Development Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. AndEngine Game Structure FREE CHAPTER 2. Working with Entities 3. Designing Your Menu 4. Working with Cameras 5. Scene and Layer Management 6. Applications of Physics 7. Working with Update Handlers 8. Maximizing Performance 9. AndEngine Extensions Overview 10. Getting More From AndEngine Source Code for MagneTank Index

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() and createStrokeFromAsset() 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: The create() 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 our assets folder. Let's assume that we have a true-type font called Arial.ttf located in our project's assets folder. We can see that the general creation is the same. In this method, we must pass the activity's AssetManager class, which can be obtained through our activity's getAssets() method. The parameter following that is the true type font we would like to import.

  • The createStroke() and createStrokeFromAsset() methods: Finally, we have our stroke fonts. The stroke font gives us the ability to add outlines to the characters in our Text 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 our assets 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.

You have been reading a chapter from
AndEngine for Android Game Development Cookbook
Published in: Jan 2013
Publisher: Packt
ISBN-13: 9781849518987
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime