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 TurtleBrainsExpress::Counters::Counter

Public Member Functions

 Text (const tbCore::tbString &text=tb_string(""), const float pointSize=kDefaultFontSize, const tbCore::tbString &fontFilepath=kDefaultFontFile)
 
virtual ~Text (void)
 
tbCore::tbString GetText (void) const
 
tbCore::tbString GetFont (void) const
 
float GetPointSize (void) const
 
void SetText (const tbCore::tbString &text, const float pointSize, const tbCore::tbString &fontFilepath)
 
void SetText (const tbCore::tbString &text)
 
virtual void SetColor (const tbGraphics::Color &newColor) override
 
void SetColor (const tbGraphics::Color &topColor, const tbGraphics::Color &bottomColor)
 
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)
 
virtual ~Graphic (void)
 
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 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)
 
float GetRotation (void) const
 
void SetRotation (const float rotation)
 
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
 
bool UnstableIsPointContained (const tbMath::Vector2 &point) const
 

Static Public Member Functions

static void CreateGraphicsContext (void)
 
static void DestroyGraphicsContext (void)
 

Static Public Attributes

static const float kDefaultFontSize
 
static const tbCore::tbString kDefaultFontFile
 

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 tbString& text) : TextObject(text, "Path/To/Some/Font.ttf", 20.0f) { } }

Constructor & Destructor Documentation

TurtleBrains::Graphics::Text::Text ( const tbCore::tbString text = tb_string(""),
const float  pointSize = kDefaultFontSize,
const tbCore::tbString fontFilepath = kDefaultFontFile 
)
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.
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

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

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

tbCore::tbString 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.
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.

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

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

Reimplemented from TurtleBrains::Graphics::Graphic.

float 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.
tbCore::tbString TurtleBrains::Graphics::Text::GetText ( void  ) const
inline

Returns the string that the text object is displaying.

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.

virtual void TurtleBrains::Graphics::Text::SetColor ( const tbGraphics::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.

void TurtleBrains::Graphics::Text::SetText ( const tbCore::tbString text,
const float  pointSize,
const tbCore::tbString 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.
Note
Rasterizing the text is the heavy lifter of the Text object and should only be called when needed.
void TurtleBrains::Graphics::Text::SetText ( const tbCore::tbString text)

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.
Note
Rasterizing the text is the heavy lifter of the Text object and should only be called when needed.

Member Data Documentation

const tbCore::tbString TurtleBrains::Graphics::Text::kDefaultFontFile
static

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

const float TurtleBrains::Graphics::Text::kDefaultFontSize
static

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