Text rendering with dynamic sprite fonts, using FreeType.
Screenshots:
MonoGame.Extended.Text mainly exposes MonoGame.Extended.Text.DynamicSpriteFont
class.
With the extension methods, you can use a syntax similar to spriteBatch.DrawString(...)
methods, to any string content using custom fonts.
Requirements:
- .NET 6.0
- MonoGame (≥ 3.8)
- libfreetype
Game1.cs
:
FontManager fontManager;
Font font;
DynamicSpriteFont spriteFont;
protected override void LoadContent() {
fontManager = new FontManager();
const float fontSize = 20;
font = fontManager.LoadFont("some_font.ttf", fontSize);
spriteFont = new DynamicSpriteFont(GraphicsDevice, font);
}
protected override void UnloadContent() {
spriteFont.Dispose();
fontManager.Dispose();
}
protected override void Draw(GameTime gameTime) {
spriteBatch.Begin();
var location = new Vector2(100, 100);
spriteBatch.DrawString(spriteFont, "Hello world! 世界你好!", location, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
The code is written in C# 7.0, therefore you need a C# 7.0-compatible compiler. If you are using Visual Studio, you should use Visual Studio 2022.
The code is supplied with detailed comments and XML documentation. Hope these help to understand the logic behind the code.