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(); foreach (var fontCharacter in _fontFile.Chars) { char c = (char)fontCharacter.ID; _characterMap.Add(c, fontCharacter); } } private Dictionary _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; } } } } }