FreeType Text

Text system using FreeType and OpenGL.

Description: Use of text in games is almost always necessary and you want high quality visual results. I get frustrated when you go to add text and find it convoluted and difficult to do. Worst things like kerning and inaccurate measurements can make something that should be simple into a nightmare.

I wanted to create a fast, accurate and easy to use system. I also wanted to support standard TrueType fonts, unicode for multilingual support and high visual quality. FreeType looked like a good solution.

Tools: Visual C++ Pro, OpenGL.

Topics: Text rendering, font metrics, dynamic texture generation, 2D blit system.

Technique overview:

For each font you want to use a texture is generated with all the glyphs you specify. Ideally you would have a string table so you don't generate unused glyphs but you can also specify a generic set of commonly used glyphs.

The text system works with my 2D blit system which has a pool of 2D quads it renders from. When you create or update text blits are assigned for each character and rendered with any other blits.

Using the blit system is nice because it easily allows setting up rotation pivot, scaling, translation. It is also handy for string alignment and bounding information and allows per character vertex colors and even access to the quad verts if desired for effects and animation.

Here you can see some of the font textures dynamically generated and saved for diagnostic purposes. FreeType generates the image for each glyph then they are copied into the texture. UVs, offsets, advance and kerning pairs are saved for each glyph. You can specify the font, size, color, stroke thickness and stroke color.

FreeType Image 1

FreeType Image 2

Here you can see some bounding diagnostics that helped me get everything accurate to the pixel.

FreeType Image 3

Here is a little demo created to make sure everything works.

Obligatory code screen shot.

FreeType Image 4

Please contact me if you are interested in acquiring a copy of the program, seeing the source code, or have any comments.

Back to the projects page.