LibGDX and Bitmap Fonts
LibGDX is an amazing tool to develop mobile applications cross-platform. I have been developing a mobile game for Android, and iOS, and it has worked very well so far despite some bumps. However there was one issue I had that I really did struggle with; that is text, and bitmap fonts.
In Objective-C it is fairly simple to write text on the screen, we call the system font, or a font from file, the text we would like to write, and the coordinate on screen. This is very straight forward unlike much of Objective-C. In Java, and with LibGDX, it is not as easy. To do this we need to create a bitmap font, which is not that easy in itself, then define a font from a picture, and a description file. Once we have that, we can define a font in Java:
1 2 3 4 5 6 7 8 9 10
We also need to define a sprite batch which helps manage drawing efficiently. To draw text in the render method we need to start the sprite batch, then use the font to draw the text. This is where it gets more complex, because we need to get the width and height of the text to find the exact position based on the width and height. This is why I create a new class called TextWrapper to handle all of this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
What this class does, is it handles the text rendering and positioning of a piece of text on the screen without the hassle of handling a spam of variables.
It is initialized with a String and Vector2 position, which is stored. The draw method is called when the text needs to be rendered. At this point we will supply the font it needs to be drawn with.
Next, we need to change and add a few things in the create method so we can instate the new class we just made.
1 2 3 4 5 6 7 8 9 10 11 12
Now that we instated the TextWrapper class with a string and the position, we can now render the text with the draw method in the render method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
This code should run, and will print out “#YOLO” at the point (100, 100) on the screen (NOTE: The origin is in the bottom left of the screen unlike normal game engines).
I wrote this because I have had trouble finding documentation to write text to the screen in LibGDX, in a few days, I will write a post about detecting touches, and creating buttons with the text.