raylib-cpp
C++ object-oriented wrapper library for raylib.
Font.hpp
1 #ifndef RAYLIB_CPP_INCLUDE_FONT_HPP_
2 #define RAYLIB_CPP_INCLUDE_FONT_HPP_
3 
4 #include <string>
5 
6 #include "./raylib.hpp"
7 #include "./raylib-cpp-utils.hpp"
8 #include "./RaylibException.hpp"
9 #include "./TextureUnmanaged.hpp"
10 
11 namespace raylib {
15 class Font : public ::Font {
16  public:
17  Font(int baseSize,
18  int glyphCount,
19  int glyphPadding,
20  ::Texture2D texture,
21  ::Rectangle *recs = nullptr,
22  ::GlyphInfo *glyphs = nullptr) : ::Font{baseSize, glyphCount, glyphPadding, texture, recs, glyphs} {
23  // Nothing.
24  }
25 
29  Font() {
30  set(::GetFontDefault());
31  }
32 
33  Font(const ::Font& font) {
34  set(font);
35  }
36 
44  Font(const std::string& fileName) {
45  Load(fileName);
46  }
47 
57  Font(const std::string& fileName, int fontSize, int* fontChars = 0, int charCount = 0) {
58  Load(fileName, fontSize, fontChars, charCount);
59  }
60 
70  Font(const ::Image& image, ::Color key, int firstChar) {
71  Load(image, key, firstChar);
72  }
73 
81  Font(const std::string& fileType, const unsigned char* fileData, int dataSize, int fontSize,
82  int *fontChars, int charsCount) {
83  Load(fileType, fileData, dataSize, fontSize, fontChars, charsCount);
84  }
85 
86  Font(const Font&) = delete;
87 
88  Font(Font&& other) {
89  set(other);
90 
91  other.baseSize = 0;
92  other.glyphCount = 0;
93  other.glyphPadding = 0;
94  other.texture = {};
95  other.recs = nullptr;
96  other.glyphs = nullptr;
97  }
98 
99  ~Font() {
100  Unload();
101  }
102 
103  void Unload() {
104  // Protect against calling UnloadFont() twice.
105  if (baseSize != 0) {
106  UnloadFont(*this);
107  baseSize = 0;
108  }
109  }
110 
111  GETTERSETTER(int, BaseSize, baseSize)
112  GETTERSETTER(int, GlyphCount, glyphCount)
113  GETTERSETTER(int, GlyphPadding, glyphPadding)
114  GETTERSETTER(::Rectangle*, Recs, recs)
115  GETTERSETTER(::GlyphInfo*, Glyphs, glyphs)
116 
121  return texture;
122  }
123 
127  inline void SetTexture(const ::Texture& newTexture) {
128  texture = newTexture;
129  }
130 
131  Font& operator=(const ::Font& font) {
132  Unload();
133  set(font);
134  return *this;
135  }
136 
137  Font& operator=(const Font&) = delete;
138 
139  Font& operator=(Font&& other) noexcept {
140  if (this == &other) {
141  return *this;
142  }
143 
144  Unload();
145  set(other);
146 
147  other.baseSize = 0;
148  other.glyphCount = 0;
149  other.glyphPadding = 0;
150  other.texture = {};
151  other.recs = nullptr;
152  other.glyphs = nullptr;
153 
154  return *this;
155  }
156 
166  void Load(const std::string& fileName) {
167  set(::LoadFont(fileName.c_str()));
168  if (!IsReady()) {
169  throw new RaylibException("Failed to load Font with from file: " + fileName);
170  }
171  }
172 
183  void Load(const std::string& fileName, int fontSize, int* fontChars, int charCount) {
184  set(::LoadFontEx(fileName.c_str(), fontSize, fontChars, charCount));
185  if (!IsReady()) {
186  throw new RaylibException("Failed to load Font with from file with font size: " + fileName);
187  }
188  }
189 
190  void Load(const ::Image& image, ::Color key, int firstChar) {
191  set(::LoadFontFromImage(image, key, firstChar));
192  if (!IsReady()) {
193  throw new RaylibException("Failed to load Font with from image");
194  }
195  }
196 
197  void Load(const std::string& fileType, const unsigned char* fileData, int dataSize, int fontSize,
198  int *fontChars, int charsCount) {
199  set(::LoadFontFromMemory(fileType.c_str(), fileData, dataSize, fontSize, fontChars,
200  charsCount));
201  if (!IsReady()) {
202  throw new RaylibException("Failed to load Font " + fileType + " with from file data");
203  }
204  }
205 
209  bool IsReady() {
210  return baseSize > 0;
211  }
212 
216  inline void DrawText(const std::string& text, ::Vector2 position, float fontSize,
217  float spacing, ::Color tint = WHITE) const {
218  ::DrawTextEx(*this, text.c_str(), position, fontSize, spacing, tint);
219  }
220 
224  inline void DrawText(const std::string& text, int posX, int posY, float fontSize,
225  float spacing, ::Color tint = WHITE) const {
226  ::DrawTextEx(*this, text.c_str(),
227  { static_cast<float>(posX), static_cast<float>(posY) },
228  fontSize, spacing, tint);
229  }
230 
231  inline void DrawText(
232  const std::string& text,
233  ::Vector2 position,
234  ::Vector2 origin,
235  float rotation,
236  float fontSize,
237  float spacing,
238  ::Color tint = WHITE) const {
239  ::DrawTextPro(*this, text.c_str(),
240  position, origin,
241  rotation, fontSize,
242  spacing, tint);
243  }
244 
248  inline void DrawText(int codepoint,
249  ::Vector2 position,
250  float fontSize,
251  ::Color tint = { 255, 255, 255, 255 }) const {
252  ::DrawTextCodepoint(*this, codepoint, position, fontSize, tint);
253  }
254 
258  inline void DrawText(const int *codepoints,
259  int count, ::Vector2 position,
260  float fontSize, float spacing,
261  ::Color tint = { 255, 255, 255, 255 }) const {
262  ::DrawTextCodepoints(*this,
263  codepoints, count,
264  position, fontSize,
265  spacing, tint);
266  }
267 
271  inline Vector2 MeasureText(const std::string& text, float fontSize, float spacing) const {
272  return ::MeasureTextEx(*this, text.c_str(), fontSize, spacing);
273  }
274 
278  inline int GetGlyphIndex(int character) const {
279  return ::GetGlyphIndex(*this, character);
280  }
281 
285  inline ::Image ImageText(const std::string& text, float fontSize,
286  float spacing, ::Color tint) const {
287  return ::ImageTextEx(*this, text.c_str(), fontSize, spacing, tint);
288  }
289 
290  private:
291  void set(const ::Font& font) {
292  baseSize = font.baseSize;
293  glyphCount = font.glyphCount;
294  glyphPadding = font.glyphPadding;
295  texture = font.texture;
296  recs = font.recs;
297  glyphs = font.glyphs;
298  }
299 };
300 } // namespace raylib
301 
302 using RFont = raylib::Font;
303 
304 #endif // RAYLIB_CPP_INCLUDE_FONT_HPP_
Color type, RGBA (32bit)
Definition: Color.hpp:14
Font type, includes texture and charSet array data.
Definition: Font.hpp:15
Font(const std::string &fileName, int fontSize, int *fontChars=0, int charCount=0)
Loads a Font from the given file, with generation parameters.
Definition: Font.hpp:57
void DrawText(const int *codepoints, int count, ::Vector2 position, float fontSize, float spacing, ::Color tint={ 255, 255, 255, 255 }) const
Draw multiple character (codepoint)
Definition: Font.hpp:258
void SetTexture(const ::Texture &newTexture)
Set the texture atlas containing the glyphs.
Definition: Font.hpp:127
Vector2 MeasureText(const std::string &text, float fontSize, float spacing) const
Measure string size for Font.
Definition: Font.hpp:271
void Load(const std::string &fileName, int fontSize, int *fontChars, int charCount)
Loads a font from a given file with generation parameters.
Definition: Font.hpp:183
void Load(const std::string &fileName)
Loads a font from a given file.
Definition: Font.hpp:166
Font(const std::string &fileType, const unsigned char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount)
Loads a font from memory, based on the given file type and file data.
Definition: Font.hpp:81
int GetGlyphIndex(int character) const
Get index position for a unicode character on font.
Definition: Font.hpp:278
Font()
Retrieves the default Font.
Definition: Font.hpp:29
void DrawText(int codepoint, ::Vector2 position, float fontSize, ::Color tint={ 255, 255, 255, 255 }) const
Draw one character (codepoint)
Definition: Font.hpp:248
Font(const std::string &fileName)
Loads a Font from the given file.
Definition: Font.hpp:44
TextureUnmanaged GetTexture()
Get the texture atlas containing the glyphs.
Definition: Font.hpp:120
Font(const ::Image &image, ::Color key, int firstChar)
Loads a Font from the given image with a color key.
Definition: Font.hpp:70
void DrawText(const std::string &text, int posX, int posY, float fontSize, float spacing, ::Color tint=WHITE) const
Draw text using font and additional parameters.
Definition: Font.hpp:224
bool IsReady()
Returns if the font is ready to be used.
Definition: Font.hpp:209
void DrawText(const std::string &text, ::Vector2 position, float fontSize, float spacing, ::Color tint=WHITE) const
Draw text using font and additional parameters.
Definition: Font.hpp:216
inline ::Image ImageText(const std::string &text, float fontSize, float spacing, ::Color tint) const
Create an image from text (custom sprite font)
Definition: Font.hpp:285
Exception used for most raylib-related exceptions.
Rectangle type.
Definition: Rectangle.hpp:12
Texture type.
Definition: Texture.hpp:14
A Texture that is not managed by the C++ garbage collector.
Vector2 type.
Definition: Vector2.hpp:16
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
static inline ::Font LoadFontEx(const std::string &fileName, int fontSize, int *fontChars, int charsCount)
Load font from file (filename must include file extension)
Definition: Functions.hpp:281
static inline ::Font LoadFont(const std::string &fileName)
Load font from file (filename must include file extension)
Definition: Functions.hpp:274
static void DrawTextPro(const Font &font, const std::string &text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, ::Color tint)
Draw text using Font and pro parameters (rotation)
Definition: Functions.hpp:266
static void DrawTextEx(const Font &font, const std::string &text, Vector2 position, float fontSize, float spacing, ::Color tint)
Draw text using font and additional parameters.
Definition: Functions.hpp:258