Text
Drawing a score counter or a dialogue box means rendering actual characters on screen, which needs a loaded font behind it rather than a plain image file.
Overview
Text is contained in the header "remake2d/texture.hpp", inheriting from Texture<Rectangle>. It renders a string using a font previously loaded through
FontManager, exposed through the singleton font, and regenerates its underlying image whenever its content changes.
Methods
void anchorX(anchor::x) noexcept; // set horizontal anchor (left, center, right)
void anchorY(anchor::y) noexcept; // set vertical anchor (top, middle, bottom)
std::string text(void) const noexcept; // get current text content
void write(std::string_view); // replace the text content
void append(std::string_view); // append text to the current content
void append(fmt); // append a formatting token
void clear(void); // clear the text content
void maxLengh(u16) noexcept; // set max wrap length, in pixels
u16 maxLengh(void) const noexcept; // get max wrap length
Usage
Loading a font
Before a Text can be created, its font must be loaded once through the singleton font:
Creating text
Drawing text
Updating content
Write replaces the whole content, while append adds to it; both regenerate the underlying texture only when the text actually changes:
Formatting tokens
Append also accepts an fmt token instead of a string, for control characters that don't read naturally as plain text:
enum fmt : u8 {
nl, // insert a newline
tab, // insert a tab
endl, // mark the next append as a fresh write, erasing current content first
flush // clear the text immediately
};
Anchoring
By default text grows from its top-left corner; anchorX and anchorY change the point the text is positioned around, useful for centering a label
regardless of how long its content ends up being:
Wrapping
maxLengh sets a maximum width, in pixels, beyond which the text wraps onto a new line automatically: