Public Types | Public Member Functions | List of all members
cpp3ds::Text Class Reference

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

#include <Text.hpp>

Inheritance diagram for cpp3ds::Text:
cpp3ds::Drawable cpp3ds::Transformable

Public Types

enum  Style {
  Regular = 0,
  Bold = 1 << 0,
  Italic = 1 << 1,
  Underlined = 1 << 2
}
 Enumeration of the string drawing styles. More...
 

Public Member Functions

 Text ()
 Default constructor. More...
 
 Text (const String &string, const Font &font, unsigned int characterSize=30)
 Construct the text from a string, font and size. More...
 
void setString (const String &string)
 Set the text's string. More...
 
void setFont (const Font &font)
 Set the text's font. More...
 
void setCharacterSize (unsigned int size)
 Set the character size. More...
 
void setStyle (Uint32 style)
 Set the text's style. More...
 
void setColor (const Color &color)
 Set the global color of the text. More...
 
const StringgetString () const
 Get the text's string. More...
 
const FontgetFont () const
 Get the text's font. More...
 
unsigned int getCharacterSize () const
 Get the character size. More...
 
Uint32 getStyle () const
 Get the text's style. More...
 
const ColorgetColor () const
 Get the global color of the text. More...
 
Vector2f findCharacterPos (std::size_t index) const
 Return the position of the index-th character. More...
 
FloatRect getLocalBounds () const
 Get the local bounding rectangle of the entity. More...
 
FloatRect getGlobalBounds () const
 Get the global bounding rectangle of the entity. More...
 
void setPosition (float x, float y)
 
void setPosition (const Vector2f &position)
 
void setRotation (float angle)
 set the orientation of the object More...
 
void setScale (float factorX, float factorY)
 set the scale factors of the object More...
 
void setScale (const Vector2f &factors)
 set the scale factors of the object More...
 
void setOrigin (float x, float y)
 set the local origin of the object More...
 
void setOrigin (const Vector2f &origin)
 set the local origin of the object More...
 
const Vector2fgetPosition () const
 get the position of the object More...
 
float getRotation () const
 get the orientation of the object More...
 
const Vector2fgetScale () const
 get the current scale of the object More...
 
const Vector2fgetOrigin () const
 get the local origin of the object More...
 
void move (float offsetX, float offsetY)
 Move the object by a given offset. More...
 
void move (const Vector2f &offset)
 Move the object by a given offset. More...
 
void rotate (float angle)
 Rotate the object. More...
 
void scale (float factorX, float factorY)
 Scale the object. More...
 
void scale (const Vector2f &factor)
 Scale the object. More...
 
const TransformgetTransform () const
 get the combined transform of the object More...
 
const TransformgetInverseTransform () const
 get the inverse of the combined transform of the object More...
 

Detailed Description

Graphical text that can be drawn to a render target.

cpp3ds::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 cpp3ds::Transformable: position, rotation, scale, origin. It also adds text-specific properties such as the font to use, the character size, the font style (bold, italic, underlined), the global color and the text to display of course. It also provides convenience functions to calculate the graphical size of the text, or to get the global position of a given character.

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

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

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

See also the note on coordinates and undistorted rendering in cpp3ds::Transformable.

Usage example:

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

Definition at line 47 of file Text.hpp.

Member Enumeration Documentation

Enumeration of the string drawing styles.

Enumerator
Regular 

Regular characters, no style.

Bold 

Bold characters.

Italic 

Italic characters.

Underlined 

Underlined characters.

Definition at line 55 of file Text.hpp.

Constructor & Destructor Documentation

cpp3ds::Text::Text ( )

Default constructor.

Creates an empty text.

cpp3ds::Text::Text ( const String string,
const Font font,
unsigned int  characterSize = 30 
)

Construct the text from a string, font and size.

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

Member Function Documentation

Vector2f cpp3ds::Text::findCharacterPos ( std::size_t  index) const

Return the position of the index-th character.

This function computes the visual position of a character from its index in the string. The returned position is in global coordinates (translation, rotation, scale and origin are applied). If index is out of range, the position of the end of the string is returned.

Parameters
indexIndex of the character
Returns
Position of the character
unsigned int cpp3ds::Text::getCharacterSize ( ) const

Get the character size.

Returns
Size of the characters, in pixels
See also
setCharacterSize
const Color& cpp3ds::Text::getColor ( ) const

Get the global color of the text.

Returns
Global color of the text
See also
setColor
const Font* cpp3ds::Text::getFont ( ) const

Get the text's font.

If the text has no font attached, a NULL pointer is returned. The returned reference 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
FloatRect cpp3ds::Text::getGlobalBounds ( ) const

Get the global bounding rectangle of the entity.

The returned rectangle is in global coordinates, which means that it takes in account the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the sprite in the global 2D world's coordinate system.

Returns
Global bounding rectangle of the entity
const Transform& cpp3ds::Transformable::getInverseTransform ( ) const
inherited

get the inverse of the combined transform of the object

Returns
Inverse of the combined transformations applied to the object
See also
getTransform
FloatRect cpp3ds::Text::getLocalBounds ( ) const

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 Vector2f& cpp3ds::Transformable::getOrigin ( ) const
inherited

get the local origin of the object

Returns
Current origin
See also
setOrigin
const Vector2f& cpp3ds::Transformable::getPosition ( ) const
inherited

get the position of the object

Returns
Current position
See also
setPosition
float cpp3ds::Transformable::getRotation ( ) const
inherited

get the orientation of the object

The rotation is always in the range [0, 360].

Returns
Current rotation, in degrees
See also
setRotation
const Vector2f& cpp3ds::Transformable::getScale ( ) const
inherited

get the current scale of the object

Returns
Current scale factors
See also
setScale
const String& cpp3ds::Text::getString ( ) const

Get the text's string.

The returned string is a cpp3ds::String, which can automatically be converted to standard string types. So, the following lines of code are all valid:

cpp3ds::String s1 = text.getString();
std::string s2 = text.getString();
std::wstring s3 = text.getString();
Returns
Text's string
See also
setString
Uint32 cpp3ds::Text::getStyle ( ) const

Get the text's style.

Returns
Text's style
See also
setStyle
const Transform& cpp3ds::Transformable::getTransform ( ) const
inherited

get the combined transform of the object

Returns
Transform combining the position/rotation/scale/origin of the object
See also
getInverseTransform
void cpp3ds::Transformable::move ( float  offsetX,
float  offsetY 
)
inherited

Move the object by a given offset.

This function adds to the current position of the object, unlike setPosition which overwrites it. Thus, it is equivalent to the following code:

cpp3ds::Vector2f pos = object.getPosition();
object.setPosition(pos.x + offsetX, pos.y + offsetY);
Parameters
offsetXX offset
offsetYY offset
See also
setPosition
void cpp3ds::Transformable::move ( const Vector2f offset)
inherited

Move the object by a given offset.

This function adds to the current position of the object, unlike setPosition which overwrites it. Thus, it is equivalent to the following code:

object.setPosition(object.getPosition() + offset);
Parameters
offsetOffset
See also
setPosition
void cpp3ds::Transformable::rotate ( float  angle)
inherited

Rotate the object.

This function adds to the current rotation of the object, unlike setRotation which overwrites it. Thus, it is equivalent to the following code:

object.setRotation(object.getRotation() + angle);
Parameters
angleAngle of rotation, in degrees
void cpp3ds::Transformable::scale ( float  factorX,
float  factorY 
)
inherited

Scale the object.

This function multiplies the current scale of the object, unlike setScale which overwrites it. Thus, it is equivalent to the following code:

cpp3ds::Vector2f scale = object.getScale();
object.setScale(scale.x * factorX, scale.y * factorY);
Parameters
factorXHorizontal scale factor
factorYVertical scale factor
See also
setScale
void cpp3ds::Transformable::scale ( const Vector2f factor)
inherited

Scale the object.

This function multiplies the current scale of the object, unlike setScale which overwrites it. Thus, it is equivalent to the following code:

cpp3ds::Vector2f scale = object.getScale();
object.setScale(scale.x * factor.x, scale.y * factor.y);
Parameters
factorScale factors
See also
setScale
void cpp3ds::Text::setCharacterSize ( unsigned int  size)

Set the character size.

The default size is 30.

Parameters
sizeNew character size, in pixels
See also
getCharacterSize
void cpp3ds::Text::setColor ( const Color color)

Set the global color of the text.

By default, the text's color is opaque white.

Parameters
colorNew color of the text
See also
getColor
void cpp3ds::Text::setFont ( const 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 behaviour is undefined.

Parameters
fontNew font
See also
getFont
void cpp3ds::Transformable::setOrigin ( float  x,
float  y 
)
inherited

set the local origin of the object

The origin of an object defines the center point for all transformations (position, scale, rotation). The coordinates of this point must be relative to the top-left corner of the object, and ignore all transformations (position, scale, rotation). The default origin of a transformable object is (0, 0).

Parameters
xX coordinate of the new origin
yY coordinate of the new origin
See also
getOrigin
void cpp3ds::Transformable::setOrigin ( const Vector2f origin)
inherited

set the local origin of the object

The origin of an object defines the center point for all transformations (position, scale, rotation). The coordinates of this point must be relative to the top-left corner of the object, and ignore all transformations (position, scale, rotation). The default origin of a transformable object is (0, 0).

Parameters
originNew origin
See also
getOrigin
void cpp3ds::Transformable::setRotation ( float  angle)
inherited

set the orientation of the object

This function completely overwrites the previous rotation. See the rotate function to add an angle based on the previous rotation instead. The default rotation of a transformable object is 0.

Parameters
angleNew rotation, in degrees
See also
rotate, getRotation
void cpp3ds::Transformable::setScale ( float  factorX,
float  factorY 
)
inherited

set the scale factors of the object

This function completely overwrites the previous scale. See the scale function to add a factor based on the previous scale instead. The default scale of a transformable object is (1, 1).

Parameters
factorXNew horizontal scale factor
factorYNew vertical scale factor
See also
scale, getScale
void cpp3ds::Transformable::setScale ( const Vector2f factors)
inherited

set the scale factors of the object

This function completely overwrites the previous scale. See the scale function to add a factor based on the previous scale instead. The default scale of a transformable object is (1, 1).

Parameters
factorsNew scale factors
See also
scale, getScale
void cpp3ds::Text::setString ( const String string)

Set the text's string.

The string argument is a cpp3ds::String, which can automatically be constructed from standard string types. So, the following calls are all valid:

text.setString("hello");
text.setString(L"hello");
text.setString(std::string("hello"));
text.setString(std::wstring(L"hello"));

A text's string is empty by default.

Parameters
stringNew string
See also
getString
void cpp3ds::Text::setStyle ( Uint32  style)

Set the text's style.

You can pass a combination of one or more styles, for example cpp3ds::Text::Bold | cpp3ds::Text::Italic. The default style is cpp3ds::Text::Regular.

Parameters
styleNew style
See also
getStyle

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