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 
9 namespace raylib {
13 class Font : public ::Font {
14  public:
15  Font() {
16  set(::GetFontDefault());
17  }
18 
19  Font(const ::Font& font) {
20  set(font);
21  }
22 
23  Font(const std::string& fileName) {
24  set(::LoadFont(fileName.c_str()));
25  }
26 
27  Font(const std::string& fileName, int fontSize, int* fontChars, int charCount) {
28  set(::LoadFontEx(fileName.c_str(), fontSize, fontChars, charCount));
29  }
30 
31  Font(const ::Image& image, ::Color key, int firstChar) {
32  set(::LoadFontFromImage(image, key, firstChar));
33  }
34 
35  Font(const std::string& fileType, const unsigned char* fileData, int dataSize, int fontSize,
36  int *fontChars, int charsCount) {
37  set(::LoadFontFromMemory(fileType.c_str(), fileData, dataSize, fontSize, fontChars,
38  charsCount));
39  }
40 
41  Font(const Font&) = delete;
42 
43  Font(Font&& other) {
44  set(other);
45 
46  other.baseSize = 0;
47  other.glyphCount = 0;
48  other.glyphPadding = 0;
49  other.texture = {};
50  other.recs = nullptr;
51  other.glyphs = nullptr;
52  }
53 
54  ~Font() {
55  Unload();
56  }
57 
58  void Unload() {
59  UnloadFont(*this);
60  }
61 
62  GETTERSETTER(int, BaseSize, baseSize)
63  GETTERSETTER(int, GlyphCount, glyphCount)
64  GETTERSETTER(int, GlyphPadding, glyphPadding)
65  GETTERSETTER(::Texture2D, Texture, texture)
66  GETTERSETTER(::Rectangle*, Recs, recs)
67  GETTERSETTER(::GlyphInfo*, Glyphs, glyphs)
68 
69  Font& operator=(const ::Font& font) {
70  set(font);
71  return *this;
72  }
73 
74  Font& operator=(const Font&) = delete;
75 
76  Font& operator=(Font&& other) {
77  if (this == &other) {
78  return *this;
79  }
80 
81  Unload();
82  set(other);
83 
84  other.baseSize = 0;
85  other.glyphCount = 0;
86  other.glyphPadding = 0;
87  other.texture = {};
88  other.recs = nullptr;
89  other.glyphs = nullptr;
90 
91  return *this;
92  }
93 
97  inline Font& DrawText(const std::string& text, ::Vector2 position, float fontSize,
98  float spacing, ::Color tint = WHITE) {
99  ::DrawTextEx(*this, text.c_str(), position, fontSize, spacing, tint);
100  return *this;
101  }
102 
103  inline Font& DrawText(
104  const std::string& text,
105  ::Vector2 position,
106  ::Vector2 origin,
107  float rotation,
108  float fontSize,
109  float spacing,
110  ::Color tint = WHITE) {
111  ::DrawTextPro(*this, text.c_str(), position, origin, rotation, fontSize, spacing, tint);
112  return *this;
113  }
114 
118  inline Font& DrawText(int codepoint,
119  ::Vector2 position,
120  float fontSize,
121  ::Color tint = { 255, 255, 255, 255 }) {
122  ::DrawTextCodepoint(*this, codepoint, position, fontSize, tint);
123  return *this;
124  }
125 
129  inline Vector2 MeasureText(const std::string& text, float fontSize, float spacing) const {
130  return ::MeasureTextEx(*this, text.c_str(), fontSize, spacing);
131  }
132 
136  inline int GetGlyphIndex(int character) const {
137  return ::GetGlyphIndex(*this, character);
138  }
139 
143  inline ::Image ImageText(const std::string& text, float fontSize,
144  float spacing, ::Color tint) const {
145  return ::ImageTextEx(*this, text.c_str(), fontSize, spacing, tint);
146  }
147 
148  private:
149  void set(const ::Font& font) {
150  baseSize = font.baseSize;
151  glyphCount = font.glyphCount;
152  glyphPadding = font.glyphPadding;
153  texture = font.texture;
154  recs = font.recs;
155  glyphs = font.glyphs;
156  }
157 };
158 } // namespace raylib
159 
160 #endif // RAYLIB_CPP_INCLUDE_FONT_HPP_
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:7
raylib::Font::DrawText
Font & DrawText(const std::string &text, ::Vector2 position, float fontSize, float spacing, ::Color tint=WHITE)
Draw text using font and additional parameters.
Definition: Font.hpp:97
raylib::Font::DrawText
Font & DrawText(int codepoint, ::Vector2 position, float fontSize, ::Color tint={ 255, 255, 255, 255 })
Draw one character (codepoint)
Definition: Font.hpp:118
raylib::DrawTextPro
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:264
raylib::Rectangle
Rectangle type.
Definition: Rectangle.hpp:12
raylib::Font
Font type, includes texture and charSet array data.
Definition: Font.hpp:13
raylib::Font::GetGlyphIndex
int GetGlyphIndex(int character) const
Get index position for a unicode character on font.
Definition: Font.hpp:136
raylib::Vector2
Vector2 type.
Definition: Vector2.hpp:16
raylib::Color
Color type, RGBA (32bit)
Definition: Color.hpp:14
raylib::Font::ImageText
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:143
raylib::Texture
Texture type.
Definition: Texture.hpp:15
raylib::Font::MeasureText
Vector2 MeasureText(const std::string &text, float fontSize, float spacing) const
Measure string size for Font.
Definition: Font.hpp:129
raylib::DrawTextEx
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:256