TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_text.hpp
1 
11 #ifndef TurtleBrains_Text_hpp
12 #define TurtleBrains_Text_hpp
13 
14 #include <turtle_brains/graphics/tb_graphic.hpp>
15 #include <turtle_brains/graphics/tb_color.hpp>
16 #include <turtle_brains/graphics/tb_texture_manager.hpp> //for PixelSpace definition.
17 #include <turtle_brains/core/tb_noncopyable.hpp>
18 #include <turtle_brains/core/tb_string.hpp>
19 
20 // 2025-03-19: This was first enabled on 2025-03-19 and has not been disabled since ...
21 // When this is eventually removed, also remove any reference to the colorMatrix (and I think even colorTint) in the
22 // basic shader for TurtleBrains as they were for text support without a branch, changing single-channel texture into
23 // argb like values for the shader to manipulate.
24 //#define TURTLEBRAINS_SDF_FONTS
25 //#define tb_silently_fail_without_sdf_fonts
26 
27 namespace TurtleBrains::Graphics
28 {
29  namespace Implementation
30  {
31  struct TextData;
32  };
33 
34  // 2025-11-22: This could be a TypedInteger, but that would require the callsite to specify FontSize() and would
35  // slow the developer down slightly. Say from a project it would create a line like;
36  //
37  // tbGraphics::Text myText("Hello World!", tbGraphics::FontSize(32)); vs
38  // tbGraphics::Text myText("Hello World!", 32);
39  //
40  //enum class FontSizeType : uint16_t { };
41  //using FontSize = tbCore::TypedInteger<FontSizeType>;
42  using FontSize = tbCore::uint16;
43 
56  class Text : public Graphic, public tbCore::Noncopyable
57  {
58  public:
62  static FontSize DefaultFontSize(void);
63 
67  static const String& DefaultFontFile(void);
68 
81  explicit Text(const String& text = tb_string(""), const FontSize pointSize = DefaultFontSize(),
82  const String& fontFilepath = DefaultFontFile());
83 
89  virtual ~Text(void);
90 
98  static void SetLineHeightForFont(const String& fontFilepath, const float multiplier);
99 
103  String GetText(void) const { return mTextString; }
104 
111  String GetFont(void) const { return mTextFont; }
112 
119  FontSize GetPointSize(void) const { return mTextSize; }
120 
121  enum class BaselineFrom { Top, Center, Bottom };
122 
126  float Unstable_GetBaseline(const BaselineFrom from = BaselineFrom::Top) const;
127  float Unstable_GetScaledBaseline(const BaselineFrom from = BaselineFrom::Top) const;
128 
142  void SetText(const String& text, const tbGraphics::AnchorLocation& alignment,
143  const FontSize pointSize, const String& fontFilepath);
144 
148  void SetText(const String& text, const FontSize pointSize, const String& fontFilepath);
149 
153  void SetText(const String& text, const FontSize pointSize);
154 
158  void SetText(const String& text);
159 
160  void SetAlignment(const AnchorLocation& alignment);
161 
162  virtual void SetColor(const Color& newColor) override;
163 
164  void SetColor(const Color& topColor, const Color& bottomColor);
165 
170  const Color& GetOutlineColor(void) const;
171  void SetOutlineColor(const Color& outlineColor);
172  float GetOutlineThickness(void) const;
173  void SetOutlineThickness(const float thickness);
174  const Color& GetShadowColor(void) const;
175  void SetShadowColor(const Color& shadowColor);
176  Math::Vector2 GetShadowOffset(void) const;
177  void SetShadowOffset(const Math::Vector2& shadowOffset);
178  void SetShadowOffset(const float offsetX, const float offsetY);
182 
186  virtual PixelSpace GetPixelWidth(void) const override;
187 
191  virtual PixelSpace GetPixelHeight(void) const override;
192 
196  static void CreateGraphicsContext(void);
197  static void DestroyGraphicsContext(void);
198 
199  protected:
204  virtual void OnRender(void) const override;
205 
206  private:
207  Implementation::TextData* mTextData;
208  String mTextFont;
209  String mTextString;
210  AnchorLocation mAlignment;
211 
212  tbGraphics::Color mBottomColor;
213 
214  tbGraphics::Color mOutlineColor;
215  tbGraphics::Color mShadowColor;
216  tbMath::Vector4 mShaderParams; //x,y is shadowOffset, z is outlineThickness, w is edgeThreshold.
217 
218  FontSize mTextSize;
219  bool mIsSDF;
220  };
221 
222 }; /* namespace TurtleBrains::Graphics */
223 
225 
226 #endif /* TurtleBrains_Text_hpp */
Definition: tb_noncopyable.hpp:21
Definition: tb_color.hpp:24
Definition: tb_graphic.hpp:50
Definition: tb_text.hpp:57
static const String & DefaultFontFile(void)
const Color & GetOutlineColor(void) const
FontSize GetPointSize(void) const
Definition: tb_text.hpp:119
virtual PixelSpace GetPixelHeight(void) const override
static void SetLineHeightForFont(const String &fontFilepath, const float multiplier)
static FontSize DefaultFontSize(void)
String GetText(void) const
Definition: tb_text.hpp:103
void SetText(const String &text)
String GetFont(void) const
Definition: tb_text.hpp:111
virtual void SetColor(const Color &newColor) override
static void CreateGraphicsContext(void)
void SetText(const String &text, const FontSize pointSize)
void SetText(const String &text, const tbGraphics::AnchorLocation &alignment, const FontSize pointSize, const String &fontFilepath)
Text(const String &text=tb_string(""), const FontSize pointSize=DefaultFontSize(), const String &fontFilepath=DefaultFontFile())
virtual void OnRender(void) const override
void SetText(const String &text, const FontSize pointSize, const String &fontFilepath)
float Unstable_GetBaseline(const BaselineFrom from=BaselineFrom::Top) const
virtual PixelSpace GetPixelWidth(void) const override
Definition: tb_vector.hpp:99
Definition: tb_vector.hpp:927
#define tb_string(object)
Definition: tb_string.hpp:30
std::uint16_t uint16
Unsigned integer with a size of 16 bits. Supports values from 0 to 65535.
Definition: tb_types.hpp:25
Give the GameScene and Entities something to display, Text, Sprites and AnimatedSprites help bring th...
Anchor
Definition: tb_anchor_location.hpp:22
@ Center
This anchor will set the origin to the center of the Sprite horizontally and vertically.
tbCore::uint16 PixelSpace
Definition: tb_texture_manager.hpp:34