Skip to content

Text and fonts

Saxion GameApp provides methods to load custom fonts (TTF/OTF) and draw text on the screen. You can also add borders, shadows, and colors to make your text look nice.

All fonts in LibGDX are converted into bitmap fonts when loaded. This means the characters of the font are pre-rendered into images (bitmaps) for fast rendering. Because of this:

  • You can draw text quickly in your game without slowing down the render loop.
  • The font size is fixed at the time you load it. If you need a larger or smaller font, you must generate a new bitmap font with the desired size.
  • Effects like color, borders, and shadows are baked into the bitmap when the font is generated, so they are applied efficiently at runtime.
  • Using the Saxion GameApp, you can also style fonts, measure text size, and draw text aligned or centered on the screen.

You should store your fonts in the folder src/main/resources/fonts, so that they are automatically copied to the executable jar file, during deployment.

Loading fonts

Font loading should be done (most of the time) in the show() method of your screen. Make sure you dispose the font in the hide() method, to free up the memory for the next screen.

Basic font

Loads a basic TTF font from a TFF/OTF file. The font can be used later to draw text on the screen. You will have to provide the following parameters:

Parameter Description
name Name to reference the font later (case-insensitive).
filePath Path to the TTF font file in assets.
size Font size in pixels.
GameApp.addFont("main", "fonts/arial.ttf", 24);

Styled fonts

Loads a TTF font with optional base color, border, and shadow. The styling is applied when generating the bitmap font. The base color can be changed when we draw with it. The other colors are fixed. You will have to provide the following parameters:

Parameter Description
name Name to reference the font later.
filePath Path to the TTF/OTF font file.
size Font size in pixels.
colorName Name of the base color.
borderWidth Width of the border (0 = no border).
borderColorName Name of the border color.
shadowOffsetX X offset of shadow (0 = no shadow).
shadowOffsetY Y offset of shadow (0 = no shadow).
shadowColorName Name of the shadow color.
GameApp.addStyledFont("arial-32-styled", "fonts/arial.ttf", 32, "yellow-500", 2f, "black", 2, 2, "gray-500");

Disposing fonts

In the hide() method of your screen you should unload all the fonts that you were using:

GameApp.disposeFont("arial-32-styled");

Drawing text

Drawing text works, just like rendering textures, with the sprite batch. Therefore we will need to start the sprite batch before we want to draw text and end the sprite batch afterwards.

Drawing text with coordinated left-bottom

Draws text aligned from the bottom-left corner at the given position with a specified color.

Parameter Description
fontName Name of the loaded font.
text The text to draw.
x, y Bottom-left coordinates for the text.
color Color to render the text.
GameApp.startSpriteRendering();
GameApp.drawText("arial-32-styled", "Hello World", 100, 50, "red-500");
GameApp.endSpriteRendering();

Drawing text centered

Draws text centered on a point with a specified color.

Parameter Description
fontName Name of the loaded font.
text The text to draw.
centerX, centerY Coordinates for the center of the text.
color Color to render the text.
GameApp.startSpriteRendering();
GameApp.drawTextCentered("arial-32-styled", "Game Over", 400, 300, "red-500");
GameApp.endSpriteRendering();

Drawing text horizontally centered

Parameter Description
fontName Name of the loaded font.
text The text to draw.
centerX Coordinate for the center of the text.
y y-coordinate.
color Color to render the text.
GameApp.startSpriteRendering();
GameApp.drawTextHorizontallyCentered("arial-32-styled", "Game Over", getWorldWidth() / 2, 300, "blue-500");
GameApp.endSpriteRendering();

Drawing text horizontally centered

Parameter Description
fontName Name of the loaded font.
text The text to draw.
x x-coordinate
centerY Coordinate for the center of the text.
color Color to render the text.
GameApp.startSpriteRendering();
GameApp.drawTextVerticallyCentered("arial-32-styled", "Game Over", 400, getWorldHeight() / 2, "red-500");
GameApp.endSpriteRendering();

Determining text size

When drawing text in games, sometimes you need to know exactly how much space it will take. For example:

  • To draw a background behind the text (e.g. determine the size of a button)
  • To align multiple pieces of text neatly.
  • To make sure text doesn’t overflow a certain area.

You can calculate how much space certain text will possess by using the getTextWidth() and getTextHeight() methods. You will have to provide the font and the text that you want to measure.

float width = GameApp.getTextWidth("arial-32-styled", "Hello World");
float height = GameApp.getTextHeight("arial-32-styled", "Hello World");

World-scaled vs pixel-perfect fonts

All the font-loading methods (addFont and addStyledFont) have an overload with a boolean parameter called usePixelSize.

When usePixelSize = false (default), the font size is scaled to match the world height. This is useful for fonts that are drawn in the game world and should grow/shrink proportionally when the world size changes. For example, a label attached to a character or a score display in world coordinates.

When usePixelSize = true, the font size is interpreted literally in screen pixels, independent of the world size or camera. This is ideal for HUD/UI elements, menus, or any text that should remain the same size on screen, no matter the world scaling.

This is very useful when you are using the HUD functionalities.

Example: World-scaled font

// Size 15 will scale relative to the world height
GameApp.addFont("score", "fonts/roboto.ttf", 15);

Example: Pixel-perfect font for HUD

// Size 15 will remain exactly 15 pixels high on screen
GameApp.addFont("hud", "fonts/roboto.ttf", 15, true);

// Or with a styled font
GameApp.addStyledFont(
    "hudStyled", "fonts/roboto.ttf", 20, Color.RED,
    2f, Color.BLACK, 2, 2, Color.GRAY, true
);

Using the boolean overloads ensures that your text looks correct in both world and HUD contexts without manually adjusting font sizes.

Notes

  • Font names are case-insensitive.
  • Only TTF and OTF formats are supported.
  • Text can only be drawn when the sprite batch is active.
  • Font generation is slow, so load fonts once in show() or screen initialization.
  • Don't forget to dispose the font in the hide() method.
  • Bitmap fonts are fixed size; to change size, generate a new font.