Game Development Reference
In-Depth Information
float xOffset = ((float)sprite.Data.XOffset) / 2;
float yOffset = ((float)sprite.Data.YOffset) / 2;
sprite.Sprite.SetPosition(currentX + xOffset, currentY - yOffset);
currentX += sprite.Data.XAdvance;
_bitmapText.Add(sprite);
}
}
}
The class is quite straightforward. The
CreateText
function is the heart of the
class; it positions the character sprites correctly. For each sprite, the
Char-
acterData
is checked and the x position is advanced by the amount specified.
Each character sprite also has an offset. All sprites are positioned around their
center, but the offset values are taken from the top left. To convert this offset to a
central position, the offset values are halved. As the offsets are integers, they need
to be converted to floating point numbers before being divided. If the numbers
aren't cast to floats, then the floating point information is thrown away and the
text letters won't be aligned correctly.
The final class to consider is the
Font
class. The
Font
class holds the dictionary
that translates a character into a
CharacterData
object. Given a word or
sentence, all the letters can be used to index the dictionary and a set
Char-
acterData
will be returned, ready to be used to create text sprites.
public class Font
{
Texture _texture;
Dictionary<char, CharacterData> _characterData;
public Font(Texture texture, Dictionary<char, CharacterData>
characterData)
{
_texture = texture;
_characterData = characterData;
}
public CharacterSprite CreateSprite(char c)
{
CharacterData charData = _characterData[c];
Sprite sprite = new Sprite();
sprite.Texture = _texture;
// Setup UVs
Point topLeft = new Point((float)charData.X / (float)_texture.Width,
Search WWH ::
Custom Search