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  UnloadFont(*this);
105  }
106 
107  GETTERSETTER(int, BaseSize, baseSize)
108  GETTERSETTER(int, GlyphCount, glyphCount)
109  GETTERSETTER(int, GlyphPadding, glyphPadding)
110  GETTERSETTER(::Rectangle*, Recs, recs)
111  GETTERSETTER(::GlyphInfo*, Glyphs, glyphs)
112 
117  return texture;
118  }
119 
123  inline void SetTexture(const ::Texture& newTexture) {
124  texture = newTexture;
125  }
126 
127  Font& operator=(const ::Font& font) {
128  Unload();
129  set(font);
130  return *this;
131  }
132 
133  Font& operator=(const Font&) = delete;
134 
135  Font& operator=(Font&& other) noexcept {
136  if (this == &other) {
137  return *this;
138  }
139 
140  Unload();
141  set(other);
142 
143  other.baseSize = 0;
144  other.glyphCount = 0;
145  other.glyphPadding = 0;
146  other.texture = {};
147  other.recs = nullptr;
148  other.glyphs = nullptr;
149 
150  return *this;
151  }
152 
162  void Load(const std::string& fileName) {
163  set(::LoadFont(fileName.c_str()));
164  if (!IsReady()) {
165  throw new RaylibException("Failed to load Font with from file: " + fileName);
166  }
167  }
168 
179  void Load(const std::string& fileName, int fontSize, int* fontChars, int charCount) {
180  set(::LoadFontEx(fileName.c_str(), fontSize, fontChars, charCount));
181  if (!IsReady()) {
182  throw new RaylibException("Failed to load Font with from file with font size: " + fileName);
183  }
184  }
185 
186  void Load(const ::Image& image, ::Color key, int firstChar) {
187  set(::LoadFontFromImage(image, key, firstChar));
188  if (!IsReady()) {
189  throw new RaylibException("Failed to load Font with from image");
190  }
191  }
192 
193  void Load(const std::string& fileType, const unsigned char* fileData, int dataSize, int fontSize,
194  int *fontChars, int charsCount) {
195  set(::LoadFontFromMemory(fileType.c_str(), fileData, dataSize, fontSize, fontChars,
196  charsCount));
197  if (!IsReady()) {
198  throw new RaylibException("Failed to load Font " + fileType + " with from file data");
199  }
200  }
201 
205  bool IsReady() {
206  return baseSize > 0;
207  }
208 
212  inline void DrawText(const std::string& text, ::Vector2 position, float fontSize,
213  float spacing, ::Color tint = WHITE) const {
214  ::DrawTextEx(*this, text.c_str(), position, fontSize, spacing, tint);
215  }
216 
220  inline void DrawText(const std::string& text, int posX, int posY, float fontSize,
221  float spacing, ::Color tint = WHITE) const {
222  ::DrawTextEx(*this, text.c_str(),
223  { static_cast<float>(posX), static_cast<float>(posY) },
224  fontSize, spacing, tint);
225  }
226 
227  inline void DrawText(
228  const std::string& text,
229  ::Vector2 position,
230  ::Vector2 origin,
231  float rotation,
232  float fontSize,
233  float spacing,
234  ::Color tint = WHITE) const {
235  ::DrawTextPro(*this, text.c_str(),
236  position, origin,
237  rotation, fontSize,
238  spacing, tint);
239  }
240 
244  inline void DrawText(int codepoint,
245  ::Vector2 position,
246  float fontSize,
247  ::Color tint = { 255, 255, 255, 255 }) const {
248  ::DrawTextCodepoint(*this, codepoint, position, fontSize, tint);
249  }
250 
254  inline void DrawText(const int *codepoints,
255  int count, ::Vector2 position,
256  float fontSize, float spacing,
257  ::Color tint = { 255, 255, 255, 255 }) const {
258  ::DrawTextCodepoints(*this,
259  codepoints, count,
260  position, fontSize,
261  spacing, tint);
262  }
263 
267  inline Vector2 MeasureText(const std::string& text, float fontSize, float spacing) const {
268  return ::MeasureTextEx(*this, text.c_str(), fontSize, spacing);
269  }
270 
274  inline int GetGlyphIndex(int character) const {
275  return ::GetGlyphIndex(*this, character);
276  }
277 
281  inline ::Image ImageText(const std::string& text, float fontSize,
282  float spacing, ::Color tint) const {
283  return ::ImageTextEx(*this, text.c_str(), fontSize, spacing, tint);
284  }
285 
286  private:
287  void set(const ::Font& font) {
288  baseSize = font.baseSize;
289  glyphCount = font.glyphCount;
290  glyphPadding = font.glyphPadding;
291  texture = font.texture;
292  recs = font.recs;
293  glyphs = font.glyphs;
294  }
295 };
296 } // namespace raylib
297 
298 using RFont = raylib::Font;
299 
300 #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:254
void SetTexture(const ::Texture &newTexture)
Set the texture atlas containing the glyphs.
Definition: Font.hpp:123
Vector2 MeasureText(const std::string &text, float fontSize, float spacing) const
Measure string size for Font.
Definition: Font.hpp:267
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:179
void Load(const std::string &fileName)
Loads a font from a given file.
Definition: Font.hpp:162
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:274
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:244
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:116
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:220
bool IsReady()
Returns if the font is ready to be used.
Definition: Font.hpp:205
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:212
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:281
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