Skip to content

GraphicsAPI reference

Class BitmapFont

Namespace: MicroEngine.Graphics
Assembly: MicroEngine.Text.dll
Guide: Text

A proportional pixel font. The built-in face (“Micro 5x7”) is authored in code so every game has crisp, license-free text without loading files. Glyph widths are derived by trimming empty columns, so narrow letters like ‘i’ take less space.

C#
public sealed class BitmapFont : IFont, IDisposable

Inheritance

object ← BitmapFont

Implements

IFont, IDisposable

Extension Methods

FontExtensions.DrawCentered(IFont, IRenderer, string, Vector2, Color), FontExtensions.DrawShadowed(IFont, IRenderer, string, Vector2, Color, Color, Vector2?), FontExtensions.MeasureWidth(IFont, string), FontExtensions.Wrap(IFont, string, float)

Remarks

Every game has the built-in face in Game.Font. It is an IFont (at scale 1), like a ScalableFont at a size, so widgets and layout code take either. Text is drawn in white glyphs multiplied by the colour given, so any colour works. Characters without a glyph are drawn as ‘?’; ‘\n’ starts a new line.

Properties

CapHeight

Height of capital letters above the baseline.

C#
public int CapHeight { get; }

Property Value

int

LineHeight

Distance between the tops of two lines, in pixels (10 for the built-in face).

C#
public int LineHeight { get; }

Property Value

int

Spacing

Pixels between two characters (1).

C#
public int Spacing { get; }

Property Value

int

Methods

Advance(char)

Horizontal distance from c to the next character, spacing included, in unscaled pixels.

C#
public int Advance(char c)

Parameters

c char

Returns

int

CreateDefault(IGraphicsDevice?)

Creates the built-in face. Pass null for a measurement-only font (tests, layout).

C#
public static BitmapFont CreateDefault(IGraphicsDevice? device)

Parameters

device IGraphicsDevice?

Returns

BitmapFont

Dispose()

Releases the font’s texture. The engine disposes Game.Font itself.

C#
public void Dispose()

Draw(IRenderer, string, Vector2, Color, float)

Draws text with its top-left at position.

C#
public void Draw(IRenderer renderer, string text, Vector2 position, Color color, float scale = 1)

Parameters

renderer IRenderer

The renderer.

text string

The text; ‘\n’ starts a new line.

position Vector2

Top-left of the first line, in virtual (or camera world) pixels.

color Color

Text colour.

scale float

Size multiplier; whole numbers keep the pixels crisp.

Exceptions

InvalidOperationException

The font was created without a graphics device.

DrawCentered(IRenderer, string, Vector2, Color, float, Color?)

Draws text centred on center, rounded to whole pixels, with an optional 1px drop shadow.

C#
public void DrawCentered(IRenderer renderer, string text, Vector2 center, Color color, float scale = 1, Color? shadow = null)

Parameters

renderer IRenderer

text string

center Vector2

color Color

scale float

shadow Color?

DrawOutlined(IRenderer, string, Vector2, Color, Color, float)

Draws text with a full 1px outline (8 directions).

C#
public void DrawOutlined(IRenderer renderer, string text, Vector2 position, Color color, Color outline, float scale = 1)

Parameters

renderer IRenderer

text string

position Vector2

color Color

outline Color

scale float

DrawShadowed(IRenderer, string, Vector2, Color, Color, float)

Draws text with a 1px drop shadow, the most legible style over busy pixel art.

C#
public void DrawShadowed(IRenderer renderer, string text, Vector2 position, Color color, Color shadow, float scale = 1)

Parameters

renderer IRenderer

text string

position Vector2

color Color

shadow Color

scale float

Measure(string, float)

Size of text in pixels at scale: the widest line by the height of its lines (8 pixels for one line at scale 1).

C#
public Vector2 Measure(string text, float scale = 1)

Parameters

text string

scale float

Returns

Vector2

MeasureWidth(string)

Width in pixels of the widest line (unscaled).

C#
public int MeasureWidth(string text)

Parameters

text string

Returns

int

Wrap(string, int)

Greedy word wrap that respects explicit newlines. A word wider than maxWidth on its own (a long name, a URL) is broken between characters, so no line is wider than maxWidth.

C#
public IReadOnlyList<string> Wrap(string text, int maxWidth)

Parameters

text string

maxWidth int

Returns

IReadOnlyList<string>