TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
TurtleBrains::Graphics::Text Class Reference

#include <tb_text.hpp>

Inheritance diagram for TurtleBrains::Graphics::Text:
TurtleBrains::Graphics::Graphic TurtleBrains::Core::Noncopyable TurtleBrains::Express::Counters::Counter

Public Types

enum class  BaselineFrom { Top , Center , Bottom }
 

Public Member Functions

 Text (const String &text=tb_string(""), const FontSize pointSize=DefaultFontSize(), const String &fontFilepath=DefaultFontFile())
 
virtual ~Text (void)
 
String GetText (void) const
 
String GetFont (void) const
 
FontSize GetPointSize (void) const
 
float Unstable_GetBaseline (const BaselineFrom from=BaselineFrom::Top) const
 
float Unstable_GetScaledBaseline (const BaselineFrom from=BaselineFrom::Top) const
 
void SetText (const String &text, const tbGraphics::AnchorLocation &alignment, const FontSize pointSize, const String &fontFilepath)
 
void SetText (const String &text, const FontSize pointSize, const String &fontFilepath)
 
void SetText (const String &text, const FontSize pointSize)
 
void SetText (const String &text)
 
void SetAlignment (const AnchorLocation &alignment)
 
virtual void SetColor (const Color &newColor) override
 
void SetColor (const Color &topColor, const Color &bottomColor)
 
const ColorGetOutlineColor (void) const
 
void SetOutlineColor (const Color &outlineColor)
 
float GetOutlineThickness (void) const
 
void SetOutlineThickness (const float thickness)
 
const ColorGetShadowColor (void) const
 
void SetShadowColor (const Color &shadowColor)
 
Math::Vector2 GetShadowOffset (void) const
 
void SetShadowOffset (const Math::Vector2 &shadowOffset)
 
void SetShadowOffset (const float offsetX, const float offsetY)
 
virtual PixelSpace GetPixelWidth (void) const override
 
virtual PixelSpace GetPixelHeight (void) const override
 
- Public Member Functions inherited from TurtleBrains::Graphics::Graphic
 Graphic (void)
 
 Graphic (const Graphic &other)=default
 
virtual ~Graphic (void)=default
 
Graphicoperator= (const Graphic &other)=default
 
bool IsActive (void) const
 
void SetActive (const bool isActive)
 
void Update (const float deltaTime)
 
bool IsVisible (void) const
 
void SetVisible (const bool isVisible)
 
void Render (void) const
 
const tbMath::Vector2GetPosition (void) const
 
void SetPosition (const float x, const float y)
 
void SetPosition (const tbMath::Vector2 &position)
 
const tbMath::Vector2GetOrigin (void) const
 
void SetOrigin (const AnchorLocation &anchor, const float offsetX, const float offsetY)
 
void SetOrigin (const AnchorLocation &anchor, const tbMath::Vector2 &offset=tbMath::Vector2::Zero())
 
void SetOrigin (const float originX, const float originY)
 
void SetOrigin (const tbMath::Vector2 &origin)
 
float GetDepth (void) const
 
void SetDepth (const float newDepth)
 
const tbMath::Vector2GetScale (void) const
 
void SetScale (const float scale)
 
void SetScale (const tbMath::Vector2 &scale)
 
void SetScale (const float scaleX, const float scaleY)
 
tbMath::Angle GetRotation (void) const
 
void SetRotation (const tbMath::Angle &angle)
 
void SetScroll (const float scroll)
 
void SetScroll (const float scrollX, const float scrollY)
 
bool IsRelative (void) const
 
const ColorGetColor (void) const
 
float GetWidth (void) const
 
float GetHeight (void) const
 
float GetScaledWidth (void) const
 
float GetScaledHeight (void) const
 
tbMath::Vector2 GetAnchorPosition (const tbGraphics::AnchorLocation &anchor, const float offsetX, const float offsetY) const
 
tbMath::Vector2 GetAnchorPosition (const tbGraphics::AnchorLocation &anchor, const tbMath::Vector2 &offset=tbMath::Vector2::Zero()) const
 
bool UnstableIsPointContained (const tbMath::Vector2 &point) const
 

Static Public Member Functions

static FontSize DefaultFontSize (void)
 
static const String & DefaultFontFile (void)
 
static void SetLineHeightForFont (const String &fontFilepath, const float multiplier)
 
static void CreateGraphicsContext (void)
 
static void DestroyGraphicsContext (void)
 

Protected Member Functions

virtual void OnRender (void) const override
 
- Protected Member Functions inherited from TurtleBrains::Graphics::Graphic
virtual void OnUpdate (const float deltaTime)
 
virtual void ApplyTransform (void) const
 
virtual void PopTransform (void) const
 

Detailed Description

The basic TextObject provides a minimalistic, but powerful true-type font api, each instance of the TextObject creates a GL texture that will be rendered to the screen as appropriate. It does take a little overhead to create the TextObject, so don't create a bunch of them over and over each frame.

Note
the Constructor seems to take a lot of parameters, but the primary use case in mind with the particular design is for the TextObject to be derived and changed so only the first parameter, text, is actually used.

class MySpecialTextObject : public TextObject { public: MySpecialText(const String& text) : TextObject(text, "Path/To/Some/Font.ttf", 20.0f) { } }

Constructor & Destructor Documentation

◆ Text()

TurtleBrains::Graphics::Text::Text ( const String &  text = tb_string(""),
const FontSize  pointSize = DefaultFontSize(),
const String &  fontFilepath = DefaultFontFile() 
)
explicit

This constructor does all the heavy lifting for the TextObject. If the font specified by fontFilepath is not loaded, it will be loaded, and then a small GL texture will be rasterized and stored to be used by the Draw() method. There are several ways the constructor can fail, and if it does fail the tb_error_if behavior will be invoked, either throwing exceptions, asserts, or other.

Parameters
textA string representing the actual letters/symbols to be displayed by the object.
fontFilepathThe filepath to the truetype-font to use for this text object, file must exist or an error condition will be triggered. Set as kDefaultFontFile, the font will be the default font as supplied by TurtleBrains.
pointSizeHow large this TextObject should be rasterized at.

◆ ~Text()

virtual TurtleBrains::Graphics::Text::~Text ( void  )
virtual

Cleans up any resources used by the Text object, with the current exception that a Font will remain loaded for other Text objects to use. This will likely change in the future to unload Fonts, except for the default font, that have no Text objects referencing.

Member Function Documentation

◆ CreateGraphicsContext()

static void TurtleBrains::Graphics::Text::CreateGraphicsContext ( void  )
static

TODO: TurtleBrains: Documentation: Find a better name and teach the user how to use this.

◆ DefaultFontFile()

static const String& TurtleBrains::Graphics::Text::DefaultFontFile ( void  )
static

The default font file automatically created by TurtleBrains if a Text object is created with default.

◆ DefaultFontSize()

static FontSize TurtleBrains::Graphics::Text::DefaultFontSize ( void  )
static

the default font size for a Text object created without specifying a size.

◆ GetFont()

String TurtleBrains::Graphics::Text::GetFont ( void  ) const
inline

Returns the filepath of the font being used to display the text.

Note
To change the font you can use SetText overload and pass in the desired font which requires rasterizing a new texture.

◆ GetOutlineColor()

const Color& TurtleBrains::Graphics::Text::GetOutlineColor ( void  ) const
Note
The following set of functions are only available if TurtleBrains Text has SDF support which may be disabled because it is very early in development and has had some performance issues. 2025-10-27

◆ GetPixelHeight()

virtual PixelSpace TurtleBrains::Graphics::Text::GetPixelHeight ( void  ) const
overridevirtual

Returns the height of the text in PixelSpace which may include some padding around the edges.

Reimplemented from TurtleBrains::Graphics::Graphic.

◆ GetPixelWidth()

virtual PixelSpace TurtleBrains::Graphics::Text::GetPixelWidth ( void  ) const
overridevirtual

@end of set

Returns the width of the text in PixelSpace which may include some padding around the edges.

Reimplemented from TurtleBrains::Graphics::Graphic.

◆ GetPointSize()

FontSize TurtleBrains::Graphics::Text::GetPointSize ( void  ) const
inline

Returns the size of the font being displayed.

Note
To change the font you can use SetText overload and pass in the desired size which requires rasterizing a new texture.

◆ GetText()

String TurtleBrains::Graphics::Text::GetText ( void  ) const
inline

Returns the string that the text object is displaying.

◆ OnRender()

virtual void TurtleBrains::Graphics::Text::OnRender ( void  ) const
overrideprotectedvirtual

Draws the precomputed image from the constructor to the screen. The location of the TextObject is determined by the last call to SetPosition(), as is the color with SetColor().

Implements TurtleBrains::Graphics::Graphic.

◆ SetColor()

virtual void TurtleBrains::Graphics::Text::SetColor ( const Color newColor)
overridevirtual

Change the color of the sprite so it can fade in/out with alpha, or modify the color for other effects.

Reimplemented from TurtleBrains::Graphics::Graphic.

◆ SetLineHeightForFont()

static void TurtleBrains::Graphics::Text::SetLineHeightForFont ( const String &  fontFilepath,
const float  multiplier 
)
static

Change the line-height of a Font underneath ALL text objects that use that font. This only effects Text objects that have multiple lines in their string, separated by '
'.

Parameters
multiplieris 1.0 to represent the line height directly from ttf font file, smaller will make the lines scrunch up and larger spaces them out even further. This effects ALL text objects with the font.

◆ SetText() [1/4]

void TurtleBrains::Graphics::Text::SetText ( const String &  text)

◆ SetText() [2/4]

void TurtleBrains::Graphics::Text::SetText ( const String &  text,
const FontSize  pointSize 
)

◆ SetText() [3/4]

void TurtleBrains::Graphics::Text::SetText ( const String &  text,
const FontSize  pointSize,
const String &  fontFilepath 
)

◆ SetText() [4/4]

void TurtleBrains::Graphics::Text::SetText ( const String &  text,
const tbGraphics::AnchorLocation alignment,
const FontSize  pointSize,
const String &  fontFilepath 
)

SetText is the heavy lifter of the object, it actually creates the small GL texture for of the rasterized letters/symbols from the true-type font and prepares everything to be displayed. It can be a fairly expensive call, so avoid changing the text, size or font when possible.

Parameters
textthe string of letters to display when rendered.
pointSizeis optional to change the size of the text should when rasterized. This will effect the width and height of the displayed text.
fontFilepathis optional to change the font of the text object. If supplied an empty string "" will use the kDefaultFontFile.
Note
Rasterizing the text is the heavy lifter of the Text object and should only be called when needed.

◆ Unstable_GetBaseline()

float TurtleBrains::Graphics::Text::Unstable_GetBaseline ( const BaselineFrom  from = BaselineFrom::Top) const
Note
BaselineFrom::Bottom will be negative value. The concept being the "direction" to baseline always points downward, no matter which anchor position it is calculated from. This allows you to account for positioning the text by always subtracting the baseline value.