Tuesday, 11 August 2009

Sprite Fonts

I decided it was time to add text capabilites to the application. Initially, I looked into it and thought that rather than writing my own sprite font class, it would be easier to use the free library glFont2. However, after failing to get it to work correctly (It was just displaying coloured squares on the screen...) I decided to write my own handler.

The result is this:


The font colour can be changed by simply using glColor3f, and is written with a function DrawString(char *, int, int) which allows you to specify a string, and x ad y coordinates on the screen.

It is currently in use to display the camera location within the field in the upper-left corner of the screen, like so:


Implementing this was actually suprisingly easy. ASCII values for the characters needed are in the range 32 to 127, so converting from a char* to a set of ints was easy. With the value for each character, texture coordinates are calculated on a base "font" bitmap file, and drawn to the screen as a textured quad. Simple!

No comments:

Post a Comment