public static void DrawString(this SpriteBatch spriteBatch, SpriteBitmapFont font, string text, Vector2 position, Color color, Color outlineColor) { var previousCharacter = ' '; var normalizedText = text.Replace("\r\n", "\n").Replace("\r", "\n"); var x = 0; var y = 0; foreach (var character in normalizedText) { if (character == '\n') { x = 0; y += font.LineHeight; continue; } var data = font[character]; var kerning = font.GetKerning(previousCharacter, character); DrawSymbol(spriteBatch, font, data, (int)position.X + (x + data.Offset.X + kerning), (int)position.Y + (y + data.Offset.Y), color, outlineColor); x += data.XAdvance + kerning; previousCharacter = character; } }
public static SpriteBitmapFont RegisterFont(string fileName, string alias = null) { if (!fileName.StartsWith(_assetFolder)) { fileName = Path.Combine(_assetFolder, fileName); } try { var key = alias ?? fileName; if (Fonts.ContainsKey(key)) { return(null); } using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { var font = SpriteBitmapFont.LoadFile(fs, _gd); Fonts.Add(key, font); return(font); } } catch (Exception) { throw new Exception($"Missing or invalid font '{fileName}'."); } }
private static void DrawSymbol(SpriteBatch spriteBatch, SpriteBitmapFont font, ISymbol symbol, int x, int y, Color color, Color outlineColor) { if (symbol.SymbolType == SymbolType.Icon) { var iconSymbol = (IconSymbol)symbol; spriteBatch.Draw(iconSymbol.Texture, new Vector2(x, y), null, Color.White); return; } spriteBatch.Draw(font.Textures[symbol.TexturePage], new Vector2(x, y), symbol.Bounds, color); }
protected override void LoadContent() { SpriteBatch = new SpriteBatch(GraphicsDevice); Assets.Initialize(GraphicsDevice, Content.RootDirectory); Assets.RegisterTextureCollection("animals", new[] { "monkey.png", "parrot.png", "penguin.png", "pig.png", "snake.png", "rabbit.png", "giraffe.png" }, true); Assets.RegisterTexture("starburst_1024_blue.png", "background"); Assets.RegisterFont("Content/Fonts/dsfont.fnt", "dsfont"); _font = Assets.GetFont("dsfont"); base.LoadContent(); }
public static void DrawString(this SpriteBatch spriteBatch, SpriteBitmapFont font, string text, Vector2 position, Color color) { spriteBatch.DrawString(font, text, position, color, color); }