Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
bitmap_font.hpp
Go to the documentation of this file.
1
3#pragma once
4
7#include <aegis2d/render/export.hpp>
11
12#include <cstdint>
13#include <string>
14#include <string_view>
15
16namespace aegis2d::render {
17
21struct GlyphGrid {
23 int atlas_width = 0;
25 int atlas_height = 0;
27 int columns = 16;
29 int cell_width = 0;
31 int cell_height = 0;
33 int glyph_width = 0;
35 int glyph_height = 0;
37 int advance = 0;
39 int line_height = 0;
41 char first = ' ';
43 char last = '~';
45 char fallback = '?';
46};
47
49struct TextStyle {
51 float scale = 1.0F;
53 Color color{.r = 1.0F, .g = 1.0F, .b = 1.0F, .a = 1.0F};
55 std::int32_t layer = 0;
56};
57
68class AEGIS2D_RENDER_EXPORT BitmapFont {
69public:
75 BitmapFont(TextureId atlas, const GlyphGrid& grid);
76
83 [[nodiscard]] static BitmapFont builtin(TextureManager& textures);
84
87 [[nodiscard]] static Image builtin_image();
88
91 [[nodiscard]] static GlyphGrid builtin_grid() noexcept;
92
95 [[nodiscard]] TextureId texture() const noexcept;
96
99 [[nodiscard]] const GlyphGrid& grid() const noexcept;
100
104 [[nodiscard]] float line_height(float scale = 1.0F) const noexcept;
105
112 [[nodiscard]] Vec2 measure(std::string_view text, float scale = 1.0F) const noexcept;
113
122 [[nodiscard]] std::string wrap(std::string_view text, float width, float scale = 1.0F) const;
123
130 void draw(IRenderer& renderer, std::string_view text, Vec2 position,
131 const TextStyle& style = {}) const;
132
133private:
134 TextureId atlas_;
135 GlyphGrid grid_;
136};
137
138} // namespace aegis2d::render
Monospaced font whose glyphs are cells of a texture, drawn as sprites.
Definition bitmap_font.hpp:68
static Image builtin_image()
Atlas image of the built-in font.
static GlyphGrid builtin_grid() noexcept
Layout of the built-in font's atlas.
static BitmapFont builtin(TextureManager &textures)
The engine's built-in font: printable ASCII, 5 x 8 pixel glyphs (descenders included),...
BitmapFont(TextureId atlas, const GlyphGrid &grid)
Uses an atlas texture laid out as grid.
What the game draws with, whatever the graphics API behind it.
Definition renderer.hpp:81
The game's textures, each loaded once and found by a name such as "player".
Definition texture_manager.hpp:33
RGBA color used by the renderer.
Images in memory: loaded from files, generated by the engine, saved as PNG.
Vector and matrix types of the engine (GLM behind engine names).
OpenGL renderer: sprites, text and camera (client only).
Definition bitmap_font.hpp:16
aegis2d::Vec2 Vec2
2D vector of floats, in logical pixels: the engine-wide aegis2d::Vec2.
Definition renderer.hpp:17
std::uint32_t TextureId
Identifier of a texture loaded by the resource manager.
Definition renderer.hpp:32
Definition endpoint.hpp:85
Backend-independent renderer interface and the sprite it draws.
RGBA color with components in [0, 1].
Definition color.hpp:10
float r
Red component.
Definition color.hpp:12
Layout of a font's atlas: a grid of cells holding consecutive characters.
Definition bitmap_font.hpp:21
int glyph_width
Width of the drawn part of a cell, in pixels.
Definition bitmap_font.hpp:33
char fallback
Character drawn for those the font does not have.
Definition bitmap_font.hpp:45
char last
Character of the last cell.
Definition bitmap_font.hpp:43
int cell_height
Height of a cell, in pixels.
Definition bitmap_font.hpp:31
char first
Character of the first cell.
Definition bitmap_font.hpp:41
int columns
Cells per row of the atlas.
Definition bitmap_font.hpp:27
int cell_width
Width of a cell, in pixels.
Definition bitmap_font.hpp:29
int atlas_height
Height of the atlas, in pixels.
Definition bitmap_font.hpp:25
int advance
Distance from one character to the next on a line, in pixels.
Definition bitmap_font.hpp:37
int atlas_width
Width of the atlas, in pixels.
Definition bitmap_font.hpp:23
int line_height
Distance from one line to the next, in pixels.
Definition bitmap_font.hpp:39
int glyph_height
Height of the drawn part of a cell, in pixels.
Definition bitmap_font.hpp:35
RGBA image with 8 bits per channel, ready to become a texture.
Definition image.hpp:25
How a text is drawn.
Definition bitmap_font.hpp:49
float scale
Size of a font pixel on screen, in logical pixels: 1 for the font's own size.
Definition bitmap_font.hpp:51
std::int32_t layer
Layer of the text's sprites.
Definition bitmap_font.hpp:55
Color color
Color of the text, multiplied with the atlas.
Definition bitmap_font.hpp:53
Textures loaded once and found by name.