TrueCraft/TrueCraft.Client/FontRenderer.cs
2015-05-16 21:50:10 -06:00

44 lines
1.3 KiB
C#

using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace TrueCraft.Client
{
public class FontRenderer
{
public FontRenderer(FontFile fontFile, Texture2D fontTexture)
{
_fontFile = fontFile;
_texture = fontTexture;
_characterMap = new Dictionary<char, FontChar>();
foreach (var fontCharacter in _fontFile.Chars)
{
char c = (char)fontCharacter.ID;
_characterMap.Add(c, fontCharacter);
}
}
private Dictionary<char, FontChar> _characterMap;
private FontFile _fontFile;
private Texture2D _texture;
public void DrawText(SpriteBatch spriteBatch, int x, int y, string text)
{
int dx = x;
int dy = y;
foreach(char c in text)
{
FontChar fc;
if(_characterMap.TryGetValue(c, out fc))
{
var sourceRectangle = new Rectangle(fc.X, fc.Y, fc.Width, fc.Height);
var position = new Vector2(dx + fc.XOffset, dy + fc.YOffset);
spriteBatch.Draw(_texture, position, sourceRectangle, Color.White);
dx += fc.XAdvance;
}
}
}
}
}