Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::render::BitmapFont Class Reference

Monospaced font whose glyphs are cells of a texture, drawn as sprites. More...

#include <aegis2d/render/bitmap_font.hpp>

Public Member Functions

 BitmapFont (TextureId atlas, const GlyphGrid &grid)
 Uses an atlas texture laid out as grid.
 
TextureId texture () const noexcept
 Texture of the atlas.
 
const GlyphGrid & grid () const noexcept
 Layout of the atlas.
 
float line_height (float scale=1.0F) const noexcept
 Distance between two lines.
 
Vec2 measure (std::string_view text, float scale=1.0F) const noexcept
 Size a text takes on screen.
 
std::string wrap (std::string_view text, float width, float scale=1.0F) const
 Breaks a text into lines that fit a width.
 
void draw (IRenderer &renderer, std::string_view text, Vec2 position, const TextStyle &style={}) const
 Draws a text, one sprite per visible character.
 

Static Public Member Functions

static BitmapFont builtin (TextureManager &textures)
 The engine's built-in font: printable ASCII, 5 x 8 pixel glyphs (descenders included), 6 pixels apart, lines 10 pixels apart.
 
static Image builtin_image ()
 Atlas image of the built-in font.
 
static GlyphGrid builtin_grid () noexcept
 Layout of the built-in font's atlas.
 

Detailed Description

Monospaced font whose glyphs are cells of a texture, drawn as sprites.

Each character is one sprite of the atlas texture, so a whole text, and every text of the same font and layer, is drawn in one call by the batcher. \n starts a new line; characters the font does not have, including each non-ASCII UTF-8 character, are drawn as GlyphGrid::fallback; other control characters are skipped.

const auto font = render::BitmapFont::builtin(textures);
font.draw(renderer, std::format("SCORE {:06}", score), {16.0F, 16.0F},
{.scale = 3.0F, .layer = 100});
static BitmapFont builtin(TextureManager &textures)
The engine's built-in font: printable ASCII, 5 x 8 pixel glyphs (descenders included),...

Constructor & Destructor Documentation

◆ BitmapFont()

aegis2d::render::BitmapFont::BitmapFont ( TextureId  atlas,
const GlyphGrid &  grid 
)

Uses an atlas texture laid out as grid.

Parameters
atlasTexture of the atlas.
gridLayout of the atlas.
Exceptions
std::invalid_argumentif a size of grid is not positive, a glyph is larger than its cell, the cells do not fit the atlas, or fallback is not in the font.

Member Function Documentation

◆ builtin()

static BitmapFont aegis2d::render::BitmapFont::builtin ( TextureManager &  textures)
static

The engine's built-in font: printable ASCII, 5 x 8 pixel glyphs (descenders included), 6 pixels apart, lines 10 pixels apart.

The atlas is added to textures as "aegis2d.font" the first time, and reused afterwards.

Parameters
texturesTextures of the game.
Returns
The font.

◆ builtin_grid()

static GlyphGrid aegis2d::render::BitmapFont::builtin_grid ( )
staticnoexcept

Layout of the built-in font's atlas.

Returns
The grid matching builtin_image().

◆ builtin_image()

static Image aegis2d::render::BitmapFont::builtin_image ( )
static

Atlas image of the built-in font.

Returns
96 x 54 pixels: 16 x 6 cells of 6 x 9, white glyphs on transparent.

◆ draw()

void aegis2d::render::BitmapFont::draw ( IRenderer &  renderer,
std::string_view  text,
Vec2  position,
const TextStyle &  style = {} 
) const

Draws a text, one sprite per visible character.

Parameters
rendererRenderer in a frame.
textUTF-8 text, lines separated by \n.
positionTop-left corner of the first character, in logical pixels.
styleScale, color and layer of the text.
Exceptions
std::logic_erroroutside a frame (as IRenderer::draw()).

◆ grid()

const GlyphGrid & aegis2d::render::BitmapFont::grid ( ) const
noexcept

Layout of the atlas.

Returns
The grid given to the constructor.

◆ line_height()

float aegis2d::render::BitmapFont::line_height ( float  scale = 1.0F) const
noexcept

Distance between two lines.

Parameters
scaleScale of the text (TextStyle::scale).
Returns
line_height * scale, in logical pixels.

◆ measure()

Vec2 aegis2d::render::BitmapFont::measure ( std::string_view  text,
float  scale = 1.0F 
) const
noexcept

Size a text takes on screen.

Parameters
textUTF-8 text, lines separated by \n.
scaleScale of the text (TextStyle::scale).
Returns
Width of the longest line (no spacing after its last character) and height from the top of the first line to the bottom of the last glyph row; zero for an empty text.

◆ texture()

TextureId aegis2d::render::BitmapFont::texture ( ) const
noexcept

Texture of the atlas.

Returns
The texture given to the constructor.

◆ wrap()

std::string aegis2d::render::BitmapFont::wrap ( std::string_view  text,
float  width,
float  scale = 1.0F 
) const

Breaks a text into lines that fit a width.

Lines break at spaces; a word longer than a line is cut. Existing line breaks are kept, and the spaces where a line breaks are dropped.

Parameters
textUTF-8 text.
widthLargest width of a line, in logical pixels.
scaleScale of the text (TextStyle::scale).
Returns
text with line breaks, so that measure() of each line is at most width (at least one character per line, even if it is wider).

The documentation for this class was generated from the following file: