Menu
I just spent a good 20 minutes trying to figure out how to use my own custom fonts for my games so this should is a short little guide on how to use a font file (.tff) to create and use a font in LibGdx. I’m sure there are other ways, but here’s how I was able to do it. Step 1: Find Your Font. CodeProject I just spent a good 20 minutes trying to figure out how to use my own custom fonts for my games so this should is a short little guide on how to use a font file (.tff) to create and use a font in LibGdx. LibGDX Example Code: Experimenting Viewports with Text and Shape Animation for multiple screen Resolutions. I have already written a similar example to do text and line animation but I am writing this post to show how to animate text and Shape i.e. A line just below the text using viewports and cameras of LibGDX and the rendering is same on multiple screen resolutions.
- Libgdx Font Generator Download
- Libgdx Font Generator App
- Libgdx Font Generator
- Libgdx Freetype Font Generator
I'm creating a game using libgdx for the first time in years - when I made my first game, I was in high school, and now I'm a couple years into studying computer engineering in university, and have done a couple software engineering internships too, so I'm a. The LibGDX Wiki and many tutorials do a great job at recommending and explaining the use of atlases for your game world, but it was not obvious to me that you should also pack your Bitmap Fonts with your UI textures.
Did you know that Bitmap Fonts can be added to the same Texture Atlas as the rest of your UI textures? I sure didn't. Read on to see how I measured and optimized my game's UI performance.
? Outline
? Background
In my previous post, we learned about the importance of minimizing texture bindings in OpenGL. Using Texture Atlases is one of the easiest and most effective ways of optimizing your game rendering. Aplicacion para abrir archivos rar en mac.
The LibGDX Wiki and many tutorials do a great job at recommending and explaining the use of atlases for your game world, but it was not obvious to me that you should also pack your Bitmap Fonts with your UI textures.
While making Santa Skate, I used the Hiero tool to generate the
.fnt
and .png
files for my fonts. I generated four fonts, each of which had a 512x512 texture with mostly empty space.? What's the problem?
In the screenshot below, there is a typical scene from Santa Skate. There is a mixture of game world sprites and Scene2D UI elements. I had already gone through efforts to minimize draw calls/texture bindings on the game world, but I hadn't put much thought into the UI layer.
Measure before optimizing
After setting up the GLProfiler, I was surprised to see 13+ texture bindings in the UI layer per frame!
Why so many texture bindings?
The reason this happens is that the Scene2D Stage draws each Actor in the order they were added to the stage. Some of those actors need to draw both an image and text, which required multiple texture files in my project. The progress bar and the lower power-up icons (which render one at a time) are clear examples of this.
Packing your fonts?
While searching online for tips on optimizing Scene2D performance, I found some forum posts that mentioned packing your fonts with the rest of the UI graphics. This post by 'davedes' on java-gaming.org was especially useful. In hindsight, that tip made perfect sense and I'm a little annoyed that I didn't think of it!
Following that advice, I packed up all of my UI layer graphics, fonts, and particles into a single texture atlas.
The result?
Only 5 texture bindings/draw calls in the UI layer per frame!
Why not 1 draw call? My UI uses multiple
Group
actors, which can triggerSpriteBatch flush() calls that force a texture binding.? Packing fonts in your UI Texture Atlas
- Open your favorite texture packer tool. I really like GDX Texture Packer GUI.
- If you have a UI textures project already, open it. Otherwise, add all of your UI graphics to the list of files to pack.
- Add your bitmap font PNG files to the list!
- Turn on the option 'Strip whitespace Y' to get rid of all the empty vertical space in your fonts.
- Configure the other options to your needs.
- Pack it!
? Tell your Bitmap Font to use the new atlas
When you create Bitmap fonts with tools like Hiero, the
.fnt
file references the PNG that was generated with it. We won't be including those PNG files in our assets anymore, so we need to tell the BitmapFont classes where to find the atlas.If you're using an
AssetManager
to load your resources, it only takes a couple lines of code to update your fonts to use the Texture Atlas. Create a BitmapFontParameter
object, and set the atlasName
property to the internal filepath of your texture atlas definition file (normally a .atlas
or .pack
extension). Pass that parameter as the third argument to the AssetManager load()
function.For my game, this is how I updated all 4 fonts. No other code changes were required!
If you are not using
AssetManager
and are creating your BitmapFonts manually, you will need to pass in the TextureRegion to the constructor.Check out my other blog posts and games!
As mentioned in one of the previous posts, in Libgdx you cannot just write – print “Hello World”, in Times New Roman, size 12. You need bitmaps for each of the letters.
One way to create such bitmaps is to use FreeTypeFontGenerator which comes in Libgdx package.
Libgdx Font Generator Download
Installation of FreeTypeFontGenerator
You can either add it during the installation of Libgdx by ticking “Freetype” checkbox:
or by editing (Eclipse) the “build.gradle” file which can be found in your “Project” folder. In that case you need to add some code to “dependencies” subsections of the following sections:
- project(“:desktop”)
- project(“:android”)
- project(“:ios”)
- project(“:core”)
After you have done all of that select all your projects -> Right-click -> Gradle -> Refresh All
Drawing text using FreeTypeFontGenerator
![Libgdx font generator download Libgdx font generator download](https://chrismweb.com/wp-content/uploads/2015/07/hiero.jpg)
FreeTypeFontGenerator uses a font in “.ttf” format – you can easily find some by googling e.g. “royalty free truetype fonts” (just make sure you are not violating any copyrights – you would not want to give away the millions you made on your game to someone else!).
Save the font you are planning to use in the “assets” folder in “Project-android” subfolder.
Font has to be prepared and loaded – ideally you will do it once as this is a resource intensive process:
Once you have the “font” ready you can use it like this:
or if you want to draw a multi line text center aligned:
or simply aligned to the left:
Once you are done don’t forget to dispose of it by writing:
Libgdx Font Generator App
If you need to have the text in different sizes, you will need to create couple of “font”s objects – unless you want to scale them which might lead to loss of their quality.
Libgdx Font Generator
In Ready Steady Teddy, my puzzle game, I’ve used this method e.g. for purposes of score (“37”) and text buttons (“Show legal move”) – that way they scale properly no matter the resolution of the screen:
Libgdx Freetype Font Generator
←Table of contents
←My apps, games and other projects
←My apps, games and other projects