libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
font.hpp
Go to the documentation of this file.
1/**
2 * @file font.hpp
3 * @brief SDL_ttf font and 2D text rendering wrappers.
4 */
5#ifndef ___FONT__H__
6#define ___FONT__H__
7
8#include"SDL.h"
9#include"SDL_ttf.h"
10#include<string>
11#include"tee_stream.hpp"
12#include<optional>
13#include"wrapper.hpp"
14
15namespace mx {
16
17/**
18 * @class Font
19 * @brief RAII wrapper around a TTF_Font handle.
20 *
21 * Loads a TrueType font from disk and manages its lifetime. The
22 * underlying TTF_Font pointer is accessible via handle(), unwrap(),
23 * or wrapper() for safe optional-style access.
24 */
25 class Font {
26 public:
27 /** @brief Default constructor — font is not loaded. */
28 Font() = default;
29
30 /**
31 * @brief Load a font without specifying a size (size determined later).
32 * @param tf Path to the TTF/OTF file.
33 */
34 Font(const std::string &tf);
35
36 /**
37 * @brief Load a font at the given point size.
38 * @param tf Path to the TTF/OTF file.
39 * @param size Point size to load.
40 */
41 Font(const std::string &tf, int size);
42
43 /**
44 * @brief Wrap an already-open TTF_Font pointer (takes ownership).
45 * @param tf Existing TTF_Font handle.
46 */
47 Font(TTF_Font *tf);
48
49 Font(const Font &f) = delete;
50 /** @brief Move constructor — transfers ownership of the TTF handle. */
51 Font(Font &&f);
52 /** @brief Destructor — closes the TTF_Font if loaded. */
53 ~Font();
54 Font &operator=(const Font &f) = delete;
55 /** @brief Move-assign — transfers ownership of the TTF handle. */
56 Font &operator=(Font && f);
57
58 /**
59 * @brief Attempt to load a font, returning false on failure.
60 * @param fname Path to the font file.
61 * @param size Desired point size.
62 * @return @c true if loaded successfully, @c false otherwise.
63 */
64 bool tryLoadFont(const std::string &fname, int size);
65
66 /**
67 * @brief Load a font, throwing mx::Exception on failure.
68 * @param fname Path to the font file.
69 * @param size Desired point size.
70 */
71 void loadFont(const std::string &fname, int size);
72
73 /**
74 * @brief Return the underlying TTF_Font as an optional.
75 * @return std::optional containing the handle if loaded, else std::nullopt.
76 */
77 std::optional<TTF_Font *> handle() const {
78 if(the_font)
79 return the_font;
80 return std::nullopt;
81 }
82
83 /**
84 * @brief Unwrap the TTF_Font pointer, throwing on null.
85 * @return Raw TTF_Font pointer.
86 * @throws mx::Exception if the font is not loaded.
87 */
88 TTF_Font *unwrap() const {
89 if(the_font) {
90 return the_font;
91 }
92 throw Exception("unwrap: Invalid Font");
93 return 0;
94 }
95
96 /**
97 * @brief Return a Wrapper around the TTF_Font handle.
98 * @return Wrapper<TTF_Font*> that holds the font pointer or nullopt.
99 */
101 if(the_font)
102 return the_font;
103 return std::nullopt;
104 }
105
106 private:
107 TTF_Font *the_font = nullptr; ///< Underlying SDL_ttf font handle.
108 };
109
110/**
111 * @class Text
112 * @brief Helper for rendering text strings with SDL_Renderer.
113 *
114 * Renders text glyphs onto the active SDL_Renderer using either solid
115 * (fast) or blended (anti-aliased) rendering modes.
116 */
117 class Text {
118 public:
119 /** @brief Default constructor. */
120 Text() = default;
121
122 /**
123 * @brief Set the SDL renderer used for all draw calls.
124 * @param renderer Active SDL_Renderer.
125 */
126 void setRenderer(SDL_Renderer *renderer);
127
128 /**
129 * @brief Set the colour used when rendering text.
130 * @param color Desired text colour.
131 */
132 void setColor(SDL_Color color);
133
134 /**
135 * @brief Render text with solid (no anti-aliasing) blending.
136 * @param f Font to use.
137 * @param x X position in renderer coordinates.
138 * @param y Y position in renderer coordinates.
139 * @param text String to render.
140 */
141 void printText_Solid(const Font &f, int x, int y, const std::string &text);
142
143 /**
144 * @brief Render text with blended (anti-aliased) quality.
145 * @param f Font to use.
146 * @param x X position in renderer coordinates.
147 * @param y Y position in renderer coordinates.
148 * @param text String to render.
149 */
150 void printText_Blended(const Font &f, int x, int y, const std::string &text);
151 private:
152 SDL_Renderer *renderer = nullptr; ///< Active renderer.
153 SDL_Color color_value; ///< Current text colour.
154 };
155
156}
157
158#endif
General-purpose exception class for the mx library.
Definition exception.hpp:20
RAII wrapper around a TTF_Font handle.
Definition font.hpp:25
bool tryLoadFont(const std::string &fname, int size)
Attempt to load a font, returning false on failure.
Definition font.cpp:45
Wrapper< TTF_Font * > wrapper() const
Return a Wrapper around the TTF_Font handle.
Definition font.hpp:100
~Font()
Destructor — closes the TTF_Font if loaded.
Definition font.cpp:13
Font & operator=(const Font &f)=delete
void loadFont(const std::string &fname, int size)
Load a font, throwing mx::Exception on failure.
Definition font.cpp:66
Font()=default
Default constructor — font is not loaded.
Font(const Font &f)=delete
std::optional< TTF_Font * > handle() const
Return the underlying TTF_Font as an optional.
Definition font.hpp:77
TTF_Font * unwrap() const
Unwrap the TTF_Font pointer, throwing on null.
Definition font.hpp:88
TTF_Font * the_font
Underlying SDL_ttf font handle.
Definition font.hpp:107
Text()=default
Default constructor.
void setRenderer(SDL_Renderer *renderer)
Set the SDL renderer used for all draw calls.
Definition font.cpp:72
void printText_Blended(const Font &f, int x, int y, const std::string &text)
Render text with blended (anti-aliased) quality.
Definition font.cpp:97
SDL_Color color_value
Current text colour.
Definition font.hpp:153
void setColor(SDL_Color color)
Set the colour used when rendering text.
Definition font.cpp:75
SDL_Renderer * renderer
Active renderer.
Definition font.hpp:152
void printText_Solid(const Font &f, int x, int y, const std::string &text)
Render text with solid (no anti-aliasing) blending.
Definition font.cpp:79
A nullable smart wrapper around a raw pointer.
Definition wrapper.hpp:36
Definition cfg.cpp:11
Tee stream utilities that duplicate output to two sinks simultaneously.
Type-safe nullable pointer wrapper inspired by Rust's Option<T>.