Text and String Utilities

This section is comprised of two utilities that I am using in my game development for text display. One of them is a font engine called PFont and the other is a utility called posprintf.

PFont Engine

The PFont Engine solves the problem of using variable-width fonts on the GBA. There are a number of ways of doing this; none of them are straightforward, unfortunately. The GBA provides a tiled background mode that can be used for fonts, but only if the font glyphs have a width fixed at a multiple of 8, which is unacceptable for many text-driven applications. There is a raster-based "trick" that can use sprites to display variable-width text, but each scanline of the GBA's screen is limited in how many pixels of sprites it can display, so this technique can result in screen tearing if a game is sprite-heavy or uses sprite features such as scaling and rotation (it also consumes VRAM dedicated to sprites, which is typically in higher demand than the VRAM dedicated to backgrounds).

My engine displays fonts on one of the tiled backgrounds by directly "painting" the font onto the background in VRAM. This is tricky business because a tiled background on the GBA is not the same as a traditional all-points-addressable buffer, and a single glyph of a font may have to span several tiles (which means parts of it could have to be written to very different places in VRAM).

The entire glyph-drawing routine is written in ARM assembly and is optimized to take advantage of varying alignment conditions and minimize access times to external memory (ROM, which is extremely slow and where the font data must be stored).

Fonts are converted using an in-house tool I developed and coded that runs in PHP and lets me preview and make adjustments to tracking, leading, spacing, etc. before encoding the data in a binary format.

I am hoping to eventually open-source this engine for public distribution; right now it is still in too much of a development phase, however.


posprintf is a routine I developed to supplant the ANSI C sprintf() routine for outputting numbers, strings, etc. to the C-string format.

The most immediate use for this is to convert numbers to a format that is human-readable; ie. converting from base 2 (internal machine representation) to base 10 (human-readable).

The version of this routine that comes with gcc is terribly inefficient, and uses slow software division to make the base conversion. (The GBA, like most ARM computers, lacks a hardware divide instruction.) My version uses a routine involving only shifts and adds to display numbers up to +/-65,535, and a slightly more complicated routine to display numbers that can exceed +/-500,000,000.

It also supports many other features of sprintf(), including the ability to print other strings to strings (%s) and the ability to print leading spaces or zeros.

My routine is also written entirely in Thumb assembler, which makes it extremely efficient when running from external memory (ROM).

It is open-source and part of the public domain. Both the source, object file and instructions can be obtained from my posprintf page.

Back to the GBA development page