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() {
42  Unload();
43  }
44 
45  void Unload() {
46  UnloadFont(*this);
47  }
48 
49  GETTERSETTER(int, BaseSize, baseSize)
50  GETTERSETTER(int, CharsCount, charsCount)
51  GETTERSETTER(int, CharsPadding, charsPadding)
52  GETTERSETTER(::Texture2D, Texture, texture)
53  GETTERSETTER(::Rectangle*, Recs, recs)
54  GETTERSETTER(::CharInfo*, Chars, chars)
55 
56  Font& operator=(const ::Font& font) {
57  set(font);
58  return *this;
59  }
60 
64  inline Font& DrawText(const std::string& text, ::Vector2 position, float fontSize,
65  float spacing, ::Color tint = WHITE) {
66  ::DrawTextEx(*this, text.c_str(), position, fontSize, spacing, tint);
67  return *this;
68  }
69 
70  inline Font& DrawText(const std::string& text, ::Rectangle rec, float fontSize, float spacing,
71  bool wordWrap = false, ::Color tint = WHITE) {
72  ::DrawTextRec(*this, text.c_str(), rec, fontSize, spacing, wordWrap, tint);
73  return *this;
74  }
75 
79  inline Font& DrawText(const std::string& text, ::Rectangle rec, float fontSize, float spacing,
80  bool wordWrap, ::Color tint, int selectStart, int selectLength, ::Color selectText,
81  ::Color selectBack) {
82  ::DrawTextRecEx(*this, text.c_str(), rec, fontSize, spacing, wordWrap, tint,
83  selectStart, selectLength, selectText, selectBack);
84  return *this;
85  }
86 
90  inline Font& DrawText(int codepoint,
91  ::Vector2 position,
92  float fontSize,
93  ::Color tint = { 255, 255, 255, 255 }) {
94  ::DrawTextCodepoint(*this, codepoint, position, fontSize, tint);
95  return *this;
96  }
97 
101  inline Vector2 MeasureText(const std::string& text, float fontSize, float spacing) const {
102  return ::MeasureTextEx(*this, text.c_str(), fontSize, spacing);
103  }
104 
108  inline int GetGlyphIndex(int character) const {
109  return ::GetGlyphIndex(*this, character);
110  }
111 
115  inline ::Image ImageText(const std::string& text, float fontSize,
116  float spacing, ::Color tint) const {
117  return ::ImageTextEx(*this, text.c_str(), fontSize, spacing, tint);
118  }
119 
120  private:
121  void set(const ::Font& font) {
122  baseSize = font.baseSize;
123  charsCount = font.charsCount;
124  charsPadding = font.charsPadding;
125  texture = font.texture;
126  recs = font.recs;
127  chars = font.chars;
128  }
129 };
130 } // namespace raylib
131 
132 #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:64
raylib::Font::DrawText
Font & DrawText(int codepoint, ::Vector2 position, float fontSize, ::Color tint={ 255, 255, 255, 255 })
Draw one character (codepoint)
Definition: Font.hpp:90
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:108
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:115
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:101
raylib::Font::DrawText
Font & DrawText(const std::string &text, ::Rectangle rec, float fontSize, float spacing, bool wordWrap, ::Color tint, int selectStart, int selectLength, ::Color selectText, ::Color selectBack)
Draw text using font inside rectangle limits with support for text selection.
Definition: Font.hpp:79