ASGE  3.2.0
Simple Game Framework in GL
ASGE::Text Struct Reference

#include <Text.hpp>

Public Member Functions

 Text (const Text &)=delete
 
Textoperator= (const Text &)=delete
 
 Text (Text &&) noexcept
 
Textoperator= (Text &&other) noexcept
 
 Text (const ASGE::Font &font_face)
 
 Text (const ASGE::Font &font_face, std::string text)
 
 Text (const ASGE::Font &font_face, const std::string &text, int x, int y)
 
 Text (const ASGE::Font &font_face, std::string &&text, int x, int y)
 
 Text (const ASGE::Font &font_face, const std::string &text, int x, int y, const ASGE::Colour &colour)
 
 Text (const ASGE::Font &font_face, std::string &&text, int x, int y, const ASGE::Colour &colour)
 
bool validFont () const noexcept
 
int16_t getZOrder () const noexcept
 
int getLineSpacing () const
 
float getHeight () const noexcept
 
float getOpacity () const noexcept
 
float getScale () const noexcept
 
float getWidth () const noexcept
 
const Point2DgetPosition () const noexcept
 
const ColourgetColour () const noexcept
 
const std::string & getString () const noexcept
 
const FontgetFont () const noexcept
 
TextBounds getWorldBounds () const
 
TextBounds getLocalBounds () const
 
TextsetZOrder (int16_t z_order) noexcept
 
TextsetScale (float scale) noexcept
 
TextsetOpacity (float opacity) noexcept
 
TextsetPositionX (float baseline_x) noexcept
 
TextsetPositionY (float baseline_y) noexcept
 
TextsetPosition (const Point2D &baseline) noexcept
 
TextsetPosition (Point2D &&baseline) noexcept
 
TextsetColour (const ASGE::Colour &colour) noexcept
 
TextsetString (const std::string &string) noexcept
 
TextsetString (std::string &&string) noexcept
 
TextsetFont (const Font &font_face) noexcept
 

Detailed Description

Text is designed to allow rendering of text to the screen.

Rendering text to a screen is not a simple process. There are lots of parameters involved, which can result in many different overloaded render functions. To resolve this, the ASGE::Text was created. All the information needed to successfully render text to the screen is stored within the struct. This includes positioning, colours, scaling, the text and even the font face to use.

Advantages of using this class allow a single instance to be constructed and used repeatedly as well as the ability to add additional features to the Text class without having to modify the renderer API.

Positioning

There are always a lot of questions around positioning of text on the screen. The issue comes from the x and y actually controlling the position of the baseline. The baseline is the point at which all characters should lay along and allows consistent positioning of text irrespective of the strings context.

The FreeType project

Usage

// implicit construction
renderer->renderText({font_face, "hello world", 50, 50});
// explicit construction
ASGE::Text text{font_face};
text.setString("Hello World. How are you today?")
.setPosition({ 100, 100 });
constexpr const Colour RED
Definition: Colours.hpp:173
Text is designed to allow rendering of text to the screen.
Definition: Text.hpp:69
Text & setString(const std::string &string) noexcept
Text & setPosition(const Point2D &baseline) noexcept
Text & setColour(const ASGE::Colour &colour) noexcept
Warning
In order for text to be rendered and for any width and height calculations to be performed a font face is required. If a font face is not provided the width and height functions will return 0 and the renderer will fallback on the engine's debug font for rendering.

Definition at line 68 of file Text.hpp.

Constructor & Destructor Documentation

◆ Text() [1/6]

ASGE::Text::Text ( const ASGE::Font font_face)
explicit

Constructor that takes a loaded font face

Parameters
[in]font_faceThe font face to use for rendering.

◆ Text() [2/6]

ASGE::Text::Text ( const ASGE::Font font_face,
std::string  text 
)

Constructs a simple text object capable of rendering a string.

Parameters
[in]font_faceThe font face to use.
[in]textThe text to render or perform calculations on.

◆ Text() [3/6]

ASGE::Text::Text ( const ASGE::Font font_face,
const std::string &  text,
int  x,
int  y 
)

Constructs a simple text object and positions it.

Parameters
[in]font_faceThe font face to use.
[in]textThe text to render or perform calculations on.
[in]xThe x starting position of the baseline.
[in]yThe y position of the baseline.

◆ Text() [4/6]

ASGE::Text::Text ( const ASGE::Font font_face,
std::string &&  text,
int  x,
int  y 
)

Constructs a simple text object and positions it.

Parameters
[in]font_faceThe font face to use.
[in]textA temporary text object to store as the font's contents.
[in]xThe x starting position of the baseline.
[in]yThe y position of the baseline.

◆ Text() [5/6]

ASGE::Text::Text ( const ASGE::Font font_face,
const std::string &  text,
int  x,
int  y,
const ASGE::Colour colour 
)

Constructs a simple text object, set's it's colour and positions it.

Parameters
[in]font_faceThe font face to use.
[in]textThe text to render or perform calculations on.
[in]xThe x starting position of the baseline.
[in]yThe y position of the baseline.
[in]colourThe colour to render the text.

◆ Text() [6/6]

ASGE::Text::Text ( const ASGE::Font font_face,
std::string &&  text,
int  x,
int  y,
const ASGE::Colour colour 
)

Constructs a simple text object and positions it.

Parameters
[in]font_faceThe font face to use.
[in]textA temporary text object to store as the font's contents.
[in]xThe x starting position of the baseline.
[in]yThe y position of the baseline.
[in]colourThe colour to render the text.

Member Function Documentation

◆ getColour()

const Colour& ASGE::Text::getColour ( ) const
noexcept

Retrieves the colour of the font.

Returns
The ASGE::Colour value to use when rendering.

◆ getFont()

const Font& ASGE::Text::getFont ( ) const
noexcept

Retrieves the font face currently assigned to the text object.

Returns
The font being used.
See also
ASGE::Font

◆ getHeight()

float ASGE::Text::getHeight ( ) const
noexcept

Calculates the max height of the stored text.

This is obviously font face dependent and also dependent on the scale factor applied to the font object. This can be used to calculate an offset in the Y when positioning the text on the screen.

Returns
The height.

◆ getLineSpacing()

int ASGE::Text::getLineSpacing ( ) const

Attempts to calculate the spacing between lines.

Returns
The line spacing between adjacent lines.

◆ getLocalBounds()

TextBounds ASGE::Text::getLocalBounds ( ) const

Returns the bounds of the rendered text in local space.

Whilst this does not provide the absolute positioning of the text in the game world, it does allow an easy way to calculate the bounding box dimensions of the rendered text i.e. how wide and tall it is.

Returns
The local bounds.

◆ getOpacity()

float ASGE::Text::getOpacity ( ) const
noexcept

The opacity of the font.

Returns
The alpha value used when rendering the font.

◆ getPosition()

const Point2D& ASGE::Text::getPosition ( ) const
noexcept

The position of the text's baseline.

Returns
The starting point used for the baseline when rendering.
See also
ASGE::Point2D

◆ getScale()

float ASGE::Text::getScale ( ) const
noexcept

The scale to apply to the text.

Returns
The scale to be applied when rendering.

◆ getString()

const std::string& ASGE::Text::getString ( ) const
noexcept

Retrieves the string to be rendered.

Returns
The string being managed by this text.

◆ getWidth()

float ASGE::Text::getWidth ( ) const
noexcept

Calculates the width of the rendered string.

This is obviously font face dependent and also dependent on the scale factor applied to the font object. This can be used to calculate an offset in the x when positioning the text on the screen.

Returns
The width.

◆ getWorldBounds()

TextBounds ASGE::Text::getWorldBounds ( ) const

Returns the bounds of the rendered text in world space.

Calculates the width and height of the text in local space and then positions it within a bounding box that retains it's position in world space.

Returns
The world bounds.

◆ getZOrder()

int16_t ASGE::Text::getZOrder ( ) const
noexcept

The z-order to control rendering.

Like any other renderable object, the z-order can be use to manipulate the rendering order. Text with lower values will be hidden by sprites with higher values.

Returns
Returns the text objects current z order.

◆ setColour()

Text& ASGE::Text::setColour ( const ASGE::Colour colour)
noexcept

Sets the colour to render the text in.

Parameters
[in]colourThe colour to render the text.
Returns
The text instance.
See also
ASGE::Colour

◆ setFont()

Text& ASGE::Text::setFont ( const Font font_face)
noexcept

Replaces the font face.

Parameters
[in]font_faceThe font face to use for this text object.
Returns
The text instance.

◆ setOpacity()

Text& ASGE::Text::setOpacity ( float  opacity)
noexcept

Sets the opacity of the rendered output (alpha channel).

Parameters
[in]opacityThe alpha channel level.
Returns
The text instance.

◆ setPosition() [1/2]

Text& ASGE::Text::setPosition ( const Point2D baseline)
noexcept

Updates the baseline position used to control the text's location

Parameters
[in]baselineThe starting position for the text.
Returns
The text instance.
See also
ASGE::Point2D

◆ setPosition() [2/2]

Text& ASGE::Text::setPosition ( Point2D &&  baseline)
noexcept

Updates the baseline position used to control the text's location

Parameters
[in]baselineThe starting position for the text.
Returns
The text instance.
See also
ASGE::Point2D

◆ setPositionX()

Text& ASGE::Text::setPositionX ( float  baseline_x)
noexcept

Updates the baseline x starting position.

Parameters
[in]baseline_xThe starting position of the baseline on the X axis.
Returns
The text instance.

◆ setPositionY()

Text& ASGE::Text::setPositionY ( float  baseline_y)
noexcept

Updates the baseline y starting position.

Parameters
[in]baseline_yThestarting position of the baseline on the Y axis.
Returns
The text instance.

◆ setScale()

Text& ASGE::Text::setScale ( float  scale)
noexcept

Sets the scale factor of the rendered output.

Parameters
[in]scaleThe scaling to apply.
Returns
The text instance.

◆ setString() [1/2]

Text& ASGE::Text::setString ( const std::string &  string)
noexcept

Replaces the text to be rendered using this object.

Parameters
[in]stringThe replacement text.
Returns
The text instance.

◆ setString() [2/2]

Text& ASGE::Text::setString ( std::string &&  string)
noexcept

Replaces the text to be rendered using this object.

Parameters
[in]stringThe replacement text.
Returns
The text instance.

◆ setZOrder()

Text& ASGE::Text::setZOrder ( int16_t  z_order)
noexcept

Sets the Z order of the rendered output.

Parameters
[in]z_orderThe new z-order to apply.
Returns
The text instance.

◆ validFont()

bool ASGE::Text::validFont ( ) const
noexcept

Checks to see if a valid font has been assigned.

Returns
Whether or not a font is attached.

The documentation for this struct was generated from the following file: