GameKit  0.0.1a
C++ gamedev tools
Text.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Text.hpp
5  *
6  * Description:
7  *
8  * Created: 30/12/2018 19:03:58
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_TEXT_HPP_
15 #define GK_TEXT_HPP_
16 
17 #include "gk/graphics/Color.hpp"
18 #include "gk/core/Rect.hpp"
19 #include "gk/gl/IDrawable.hpp"
20 #include "gk/gl/Texture.hpp"
21 #include "gk/gl/Transformable.hpp"
22 #include "gk/graphics/Font.hpp"
23 #include "gk/graphics/Image.hpp"
24 
25 namespace gk {
26 
27 class Text : public IDrawable, public Transformable {
28  public:
29  Text() = default;
30  Text(const std::string &fontResourceName, int ptsize);
31  Text(const std::string &text, const std::string &fontResourceName, int ptsize);
32  Text(const std::string &text, const Font &font, int ptsize);
33 
35 
36  void setFont(const Font &font) { m_font = &font; m_isUpdateNeeded = true; }
37  void setFont(const std::string &resourceName);
38 
39  const std::string &text() const { return m_text; }
40  void setText(const std::string &text) { m_text = text; m_isUpdateNeeded = true; }
41 
42  enum Style {
43  Normal = TTF_STYLE_NORMAL,
44  Bold = TTF_STYLE_BOLD,
45  Italic = TTF_STYLE_ITALIC,
46  Underline = TTF_STYLE_UNDERLINE,
47  Strikethrough = TTF_STYLE_STRIKETHROUGH,
48  };
49 
50  void setStyle(Style style) { m_style = style; m_isUpdateNeeded = true; }
51  void setColor(const Color &color) { m_color = color; m_isUpdateNeeded = true; }
52  void setCharacterSize(int size) { m_characterSize = size; m_isUpdateNeeded = true; }
53 
54  void setSize(const gk::Vector2i &size) { m_size = size; m_isUpdateNeeded = true; }
55  void setCentered(bool isCentered) { m_isCentered = isCentered; m_isUpdateNeeded = true; }
56  void setScaled(bool isScaled) { m_isScaled = isScaled; m_isUpdateNeeded = true; }
57  void setWrapped(bool isWrapped) { m_isWrapped = isWrapped; m_isUpdateNeeded = true; }
58 
59  private:
60  void update() const;
61 
62  void draw(RenderTarget &target, RenderStates states) const override;
63 
64  const Font *m_font = nullptr;
65 
68 
69  mutable bool m_isUpdateNeeded = false;
70  mutable Image m_image;
71  mutable Texture m_texture;
72 
73  std::string m_text;
74  int m_characterSize = -1;
75 
77  bool m_isCentered = false;
78  bool m_isScaled = false;
79  bool m_isWrapped = false;
80 };
81 
82 } // namespace gk
83 
84 #endif // GK_TEXT_HPP_
const std::string & text() const
Definition: Text.hpp:39
Texture m_texture
Definition: Text.hpp:71
bool m_isCentered
Definition: Text.hpp:77
void setText(const std::string &text)
Definition: Text.hpp:40
void draw(RenderTarget &target, RenderStates states) const override
Draw the object to a render target.
Definition: Text.cpp:103
bool m_isWrapped
Definition: Text.hpp:79
Utility class for manipulating RGBA colors.
Definition: Color.hpp:25
const Font * m_font
Definition: Text.hpp:64
Image m_image
Definition: Text.hpp:70
Style m_style
Definition: Text.hpp:66
IntRect getLocalBounds()
Definition: Text.cpp:43
void update() const
Definition: Text.cpp:53
Image living on the graphics card that can be used for drawing.
Definition: Texture.hpp:30
void setScaled(bool isScaled)
Definition: Text.hpp:56
gk::Vector2i m_size
Definition: Text.hpp:76
void setColor(const Color &color)
Definition: Text.hpp:51
int m_characterSize
Definition: Text.hpp:74
Text()=default
void setStyle(Style style)
Definition: Text.hpp:50
Abstract base class for objects that can be drawn to a render target.
Definition: IDrawable.hpp:25
void setCharacterSize(int size)
Definition: Text.hpp:52
void setFont(const Font &font)
Definition: Text.hpp:36
bool m_isScaled
Definition: Text.hpp:78
Class for loading and manipulating character fonts.
Definition: Font.hpp:30
std::string m_text
Definition: Text.hpp:73
void setWrapped(bool isWrapped)
Definition: Text.hpp:57
bool m_isUpdateNeeded
Definition: Text.hpp:69
Color m_color
Definition: Text.hpp:67
void setCentered(bool isCentered)
Definition: Text.hpp:55
void setSize(const gk::Vector2i &size)
Definition: Text.hpp:54