Gamedev Framework (gf)
0.3.0
A C++11 framework for 2D games
|
Graphical text that can be drawn to a render target. More...
#include <gf/Text.h>
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 Font * | getFont () const |
Get the text's font. More... | |
void | setColor (const Color4f &color) |
Set the fill color of the text. More... | |
const Color4f & | getColor () const |
Get the fill color of the text. More... | |
void | setOutlineColor (const Color4f &color) |
Set the outline color of the text. More... | |
const Color4f & | getOutlineColor () 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... | |
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:
gf::Text::Text | ( | ) |
Default constructor.
Creates an empty text.
gf::Text::Text | ( | std::string | string, |
Font & | font, | ||
unsigned | characterSize = 30 |
||
) |
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.
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.
|
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.
target | Render target to draw to |
states | Current render states |
Implements gf::Drawable.
|
inline |
|
inline |
|
inline |
|
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.
|
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.
|
inline |
|
inline |
Get the outline thickness of the text.
|
inline |
|
inline |
void gf::Text::setAlignment | ( | Alignment | align | ) |
Set the alignement of the text.
By default, the text is not aligned.
align | New alignement |
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()
.
anchor | The anchor of the entity |
void gf::Text::setCharacterSize | ( | unsigned | characterSize | ) |
Set the character size.
The default size is 30.
characterSize | New character size, in pixels |
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.
color | New fill color of the text |
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.
font | New font |
void gf::Text::setOutlineColor | ( | const Color4f & | color | ) |
Set the outline color of the text.
By default, the text's outline color is opaque black.
color | New outline color of the text |
void gf::Text::setOutlineThickness | ( | float | thickness | ) |
Set the thickness of the text's outline.
By default, the outline thickness is 0.
thickness | New outline thickness, in pixels |
void gf::Text::setParagraphWidth | ( | float | paragraphWidth | ) |
Set the paragraph width for aligned text.
By default, the paragraph width is 0.
paragraphWidth | New paragraph width in pixels |
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.
string | New string in UTF-8 format |