Game Development Reference
In-Depth Information
character. The xadvance parameter represents the amount to advance on the
x axis once the current character has been rendered. The page value is an index
to a texture that contains the current character. We'll only be using fonts that
have a single texture so the page value can be safely ignored. The channel
information can also be ignored; it is sometimes used for a compression tech-
nique where different characters are written into the different color channels.
The characters need to be read into a suitable class.
public class CharacterData
{
public int Id { get; set; }
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int XOffset { get; set; }
public int YOffset { get; set; }
public int XAdvance { get; set; }
}
This CharacterData class is a simple collection of parameters that
we're interested in. One of these objects will be made per character in the
data file.
Parsing the Font Data
The CharacterData class will be stored in a dictionary. The key to the dic-
tionary will be the character they represent, as the following code shows.
CharacterData aData = _characterDictionary['a'];
Given a string, it's easy to iterate through all the characters and get the relevant
character data for each one. To fill this dictionary from the data file, a parser is
needed. Here's the code for a simple parser; when given a path to a font data file,
it will return a dictionary filled with character data.
public class FontParser
{
static int HeaderSize = 4;
// Gets the value after an equal sign and converts it
 
Search WWH ::




Custom Search