Text options

micro::text_options configures how text is rendered. All options have sensible defaults — use the fluent API to configure only what you need.

enum class align { left, center, right };

struct text_options {
    constexpr text_options& tint(color c) noexcept;
    constexpr text_options& scale(float s) noexcept;
    constexpr text_options& align(align a) noexcept;

    constexpr color tint() const noexcept;
    constexpr float scale() const noexcept;
    constexpr auto align() const noexcept -> align;
};

Use method chaining to configure options:

// No options — draw as-is:
rend.draw(fnt, "Hello", {10.f, 20.f});

// Tinted yellow at 2× scale:
rend.draw(fnt, "Game Over", {80.f, 100.f}, text_options{}.tint(color::yellow).scale(2.f));

// Centered title:
rend.draw(fnt, "TITLE", {160.f, 50.f},
          text_options{}.align(align::center).scale(2.f));

tint

A multiplicative color applied to every pixel of the text. color::white (the default) leaves the text unchanged. Use any other color to tint or apply transparency:

// 50% transparent text:
rend.draw(fnt, "Fading...", pos, text_options{}.tint({255, 255, 255, 128}));

// Yellow warning text:
rend.draw(fnt, "Warning!", pos, text_options{}.tint(color::yellow));

scale

Uniform scale applied to both the rendered glyphs and the horizontal advance (spacing between characters):

// Draw at double size:
rend.draw(fnt, "Large text", pos, text_options{}.scale(2.f));

// Compact small text:
rend.draw(fnt, "tiny", pos, text_options{}.scale(0.5f));

align (horizontal alignment)

Controls how each line is positioned within the text block's bounding box:

  • align::left (default): Lines start at the left edge of the text block
  • align::center: Lines are centered within the text block width
  • align::right: Lines are right-aligned within the text block width

The anchor position (pos argument to draw) always represents the top-left corner of the text block's bounding box. Each line is then positioned within that box according to the alignment:

// Multi-line text with centered lines:
micro::text dialog{fnt, "Welcome!\nClick to continue."};
// pos {160, 100} is the top-left of the bounding box,
// but each line is centered within that box
rend.draw(dialog, {160.f, 100.f}, text_options{}.align(align::center));

// Right-aligned multi-line text:
rend.draw(dialog, {300.f, 100.f}, text_options{}.align(align::right));

For single-line text, the effect is relative to the text's own width.

// Will be useful with future line spacing options:
rend.draw(dialog, {20.f, 100.f}, text_options{}.valign(valign::middle));

Combining options

All options can be chained together:

// Large, yellow, centered text:
rend.draw(fnt, "VICTORY", {160.f, 120.f},
          text_options{}
              .tint(color::yellow)
              .scale(3.f)
              .align(align::center));