Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Classes | Public Member Functions | List of all members
gf::Text Class Reference

Graphical text that can be drawn to a render target. More...

#include <gf/Text.h>

Inheritance diagram for gf::Text:
Inheritance graph
[legend]

Public Member Functions

 Text ()
 Default constructor. More...
 
 Text (std::string string, Font &font, unsigned characterSize=30)
 Construct the text from a string, font and size. More...
 
void setString (std::string string)
 Set the text's string. More...
 
const std::string & getString () const
 Get the text's string. More...
 
void setCharacterSize (unsigned characterSize)
 Set the character size. More...
 
unsigned getCharacterSize () const
 Get the character size. More...
 
void setFont (Font &font)
 Set the text's font. More...
 
const FontgetFont () const
 Get the text's font. More...
 
void setColor (const Color4f &color)
 Set the fill color of the text. More...
 
const Color4fgetColor () const
 Get the fill color of the text. More...
 
void setOutlineColor (const Color4f &color)
 Set the outline color of the text. More...
 
const Color4fgetOutlineColor () const
 Get the outline color of the text. More...
 
void setOutlineThickness (float thickness)
 Set the thickness of the text's outline. More...
 
float getOutlineThickness ()
 Get the outline thickness of the text. More...
 
void setParagraphWidth (float paragraphWidth)
 Set the paragraph width for aligned text. More...
 
float getParagraphWidth () const
 Get the paragraph width. More...
 
void setAlignment (Alignment align)
 Set the alignement of the text. More...
 
Alignment getAlignment () const
 Get the alignment of the text. More...
 
RectF getLocalBounds () const
 Get the local bounding rectangle of the entity. More...
 
void setAnchor (Anchor anchor)
 Set the anchor origin of the entity. More...
 
VertexBuffer commitGeometry () const
 Create a buffer with the current geometry. More...
 
VertexBuffer commitOutlineGeometry () const
 Create a buffer with the current outline geometry. More...
 
virtual void draw (RenderTarget &target, RenderStates states) override
 Draw the object to a render target. More...
 
- Public Member Functions inherited from gf::Transformable
 Transformable ()
 Default constructor. More...
 
void setOrigin (Vector2f origin)
 Set the local origin of the object. More...
 
Vector2f getOrigin () const
 Get the local origin of the object. More...
 
void setPosition (Vector2f position)
 Set the position of the object. More...
 
Vector2f getPosition () const
 Get the position of the object. More...
 
void move (Vector2f offset)
 Move the object by a given offset. More...
 
void setRotation (float angle)
 Set the orientation of the object. More...
 
float getRotation () const
 Get the orientation of the object. More...
 
void rotate (float angle)
 Rotate the object. More...
 
void setScale (Vector2f factors)
 Set the scale factors of the object. More...
 
void setScale (float factor)
 Set the scale factor of the object. More...
 
Vector2f getScale () const
 Get the current scale of the object. More...
 
void scale (Vector2f factors)
 Scale the object. More...
 
void scale (float factor)
 Scale the object. More...
 
Matrix3f getTransform () const
 Get the combined transform of the object. More...
 
Matrix3f getInverseTransform () const
 Get the inverse of the combined transform of the object. More...
 
- Public Member Functions inherited from gf::Drawable
virtual ~Drawable ()
 Virtual desctructor. More...
 

Additional Inherited Members

- Protected Member Functions inherited from gf::Transformable
void setOriginFromAnchorAndBounds (Anchor anchor, const RectF &bounds)
 Set the origin from an anchor and bounds. More...
 

Detailed Description

Graphical text that can be drawn to a render target.

gf::Text is a drawable class that allows to easily display some text with custom style and color on a render target.

It inherits all the functions from gf::Transformable: position, rotation, scale, origin. It also adds text-specific properties such as the font to use, the character size, the global color and the text to display of course. It also provides convenience functions to calculate the graphical size of the text.

gf::Text works in combination with the gf::Font class, which loads and provides the glyphs (visual characters) of a given font.

The separation of gf::Font and gf::Text allows more flexibility and better performances: indeed a gf::Font is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a gf::Text is a lightweight object which can combine the glyphs data and metrics of a gf::Font to display any text on a render target.

It is important to note that the gf::Text instance doesn't copy the font that it uses, it only keeps a reference to it. Thus, a gf::Font must not be destructed while it is used by a gf::Text (i.e. never write a function that uses a local gf::Font instance for creating a text).

Usage example:

// Declare and load a font
gf::Font font;
font.loadFromFile("arial.ttf");
// Create a text
gf::Text text("hello", font);
text.setCharacterSize(30);
text.setColor(gf::Color::Red);
// Draw it
rendered.draw(text);
See also
gf::Font

Constructor & Destructor Documentation

gf::Text::Text ( )

Default constructor.

Creates an empty text.

gf::Text::Text ( std::string  string,
Font font,
unsigned  characterSize = 30 
)

Construct the text from a string, font and size.

Parameters
stringText assigned to the string in UTF-8 format
fontFont used to draw the string
characterSizeBase size of characters, in pixels

Member Function Documentation

VertexBuffer gf::Text::commitGeometry ( ) const

Create a buffer with the current geometry.

The geometry is uploaded in the graphics memory so that it's faster to draw.

Returns
A buffer with the current geometry
VertexBuffer gf::Text::commitOutlineGeometry ( ) const

Create a buffer with the current outline geometry.

The geometry is uploaded in the graphics memory so that it's faster to draw.

Returns
A buffer with the current outline geometry
virtual void gf::Text::draw ( RenderTarget target,
RenderStates  states 
)
overridevirtual

Draw the object to a render target.

This is a pure virtual function that has to be implemented by the derived class to define how the drawable should be drawn.

Parameters
targetRender target to draw to
statesCurrent render states

Implements gf::Drawable.

Alignment gf::Text::getAlignment ( ) const
inline

Get the alignment of the text.

Returns
Current alignment of the text
See also
setAlignment()
unsigned gf::Text::getCharacterSize ( ) const
inline

Get the character size.

Returns
Size of the characters, in pixels
See also
setCharacterSize()
const Color4f& gf::Text::getColor ( ) const
inline

Get the fill color of the text.

Returns
Fill color of the text
See also
setColor()
const Font* gf::Text::getFont ( ) const
inline

Get the text's font.

If the text has no font attached, a nullptr pointer is returned. The returned pointer is const, which means that you cannot modify the font when you get it from this function.

Returns
Pointer to the text's font
See also
setFont()
RectF gf::Text::getLocalBounds ( ) const
inline

Get the local bounding rectangle of the entity.

The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the entity in the entity's coordinate system.

Returns
Local bounding rectangle of the entity
const Color4f& gf::Text::getOutlineColor ( ) const
inline

Get the outline color of the text.

Returns
Outline color of the text
See also
setOutlineColor()
float gf::Text::getOutlineThickness ( )
inline

Get the outline thickness of the text.

Returns
Outline thickness of the text, in pixels
See also
setOutlineThickness()
float gf::Text::getParagraphWidth ( ) const
inline

Get the paragraph width.

Returns
Paragraph width in pixels
See also
setParagraphWidth()
const std::string& gf::Text::getString ( ) const
inline

Get the text's string.

The text string is in UTF-8 format.

Returns
Text's string
See also
setString()
void gf::Text::setAlignment ( Alignment  align)

Set the alignement of the text.

By default, the text is not aligned.

Parameters
alignNew alignement
See also
getAlignment()
void gf::Text::setAnchor ( Anchor  anchor)

Set the anchor origin of the entity.

Compute the origin of the entity based on the local bounds and the specified anchor. Internally, this function calls Transformable::setOrigin().

Parameters
anchorThe anchor of the entity
See also
getLocalBounds(), Transformable::setOrigin()
void gf::Text::setCharacterSize ( unsigned  characterSize)

Set the character size.

The default size is 30.

Parameters
characterSizeNew character size, in pixels
See also
getCharacterSize()
void gf::Text::setColor ( const Color4f color)

Set the fill color of the text.

By default, the text's fill color is opaque black. Setting the fill color to a transparent color with an outline will cause the outline to be displayed in the fill area of the text.

Parameters
colorNew fill color of the text
See also
getColor()
void gf::Text::setFont ( Font font)

Set the text's font.

The font argument refers to a font that must exist as long as the text uses it. Indeed, the text doesn't store its own copy of the font, but rather keeps a pointer to the one that you passed to this function. If the font is destroyed and the text tries to use it, the behavior is undefined.

Parameters
fontNew font
See also
getFont()
void gf::Text::setOutlineColor ( const Color4f color)

Set the outline color of the text.

By default, the text's outline color is opaque black.

Parameters
colorNew outline color of the text
See also
getOutlineColor()
void gf::Text::setOutlineThickness ( float  thickness)

Set the thickness of the text's outline.

By default, the outline thickness is 0.

Parameters
thicknessNew outline thickness, in pixels
See also
getOutlineThickness()
void gf::Text::setParagraphWidth ( float  paragraphWidth)

Set the paragraph width for aligned text.

By default, the paragraph width is 0.

Parameters
paragraphWidthNew paragraph width in pixels
See also
getParagraphWidth()
void gf::Text::setString ( std::string  string)

Set the text's string.

The text string is in UTF-8 format. A text's string is empty by default.

Parameters
stringNew string in UTF-8 format
See also
getString()