Graphical text that can be drawn to a render target. More...
#include <Text.hpp>
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. | |
Text (const String &string, const Font &font=Font::GetDefaultFont(), unsigned int characterSize=30) | |
Construct the string from a string, font and size. | |
void | SetString (const String &string) |
Set the text's string. | |
void | SetFont (const Font &font) |
Set the text's font. | |
void | SetCharacterSize (unsigned int size) |
Set the character size. | |
void | SetStyle (unsigned long style) |
Set the text's style. | |
const String & | GetString () const |
Get the text's string. | |
const Font & | GetFont () const |
Get the text's font. | |
unsigned int | GetCharacterSize () const |
Get the character size. | |
unsigned long | GetStyle () const |
Get the text's style. | |
Vector2f | GetCharacterPos (std::size_t index) const |
Return the position of the index-th character. | |
FloatRect | GetRect () const |
Get the bounding rectangle of the text. | |
void | SetPosition (float x, float y) |
Set the position of the object. | |
void | SetPosition (const Vector2f &position) |
Set the position of the object. | |
void | SetX (float x) |
Set the X position of the object. | |
void | SetY (float y) |
Set the Y position of the object. | |
void | SetScale (float factorX, float factorY) |
Set the scale factors of the object. | |
void | SetScale (const Vector2f &factors) |
Set the scale factors of the object. | |
void | SetScaleX (float factor) |
Set the X scale factor of the object. | |
void | SetScaleY (float factor) |
Set the Y scale factor of the object. | |
void | SetOrigin (float x, float y) |
Set the local origin of the object. | |
void | SetOrigin (const Vector2f &origin) |
Set the local origin of the object. | |
void | SetRotation (float angle) |
Set the orientation of the object. | |
void | SetColor (const Color &color) |
Set the global color of the object. | |
void | SetBlendMode (Blend::Mode mode) |
Set the blending mode of the object. | |
const Vector2f & | GetPosition () const |
Get the position of the object. | |
const Vector2f & | GetScale () const |
Get the current scale of the object. | |
const Vector2f & | GetOrigin () const |
Get the local origin of the object. | |
float | GetRotation () const |
Get the orientation of the object. | |
const Color & | GetColor () const |
Get the color of the object. | |
Blend::Mode | GetBlendMode () const |
Get the blend mode of the object. | |
void | Move (float offsetX, float offsetY) |
Move the object by a given offset. | |
void | Move (const Vector2f &offset) |
Move the object by a given offset. | |
void | Scale (float factorX, float factorY) |
Scale the object. | |
void | Scale (const Vector2f &factor) |
Scale the object. | |
void | Rotate (float angle) |
Rotate the object. | |
Vector2f | TransformToLocal (const Vector2f &point) const |
Transform a point in object local coordinates. | |
Vector2f | TransformToGlobal (const Vector2f &point) const |
Transform a local point in global coordinates. | |
Protected Member Functions | |
virtual void | Render (RenderTarget &target, Renderer &renderer) const |
Draw the object to a render target. | |
const Matrix3 & | GetMatrix () const |
Get the transform matrix of the object. | |
const Matrix3 & | GetInverseMatrix () const |
Get the inverse transform matrix of the object. |
Graphical text that can be drawn to a render target.
sf::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 sf::Drawable: position, rotation, scale, origin, global color and blend mode. It also adds text-specific properties such as the font to use, the character size, the font style (bold, italic, underlined), and the text to display of course. It also provides convenience functions to calculate the graphical size of the text, or to get the visual position of a given character.
sf::Text works in combination with the sf::Font class, which loads and provides the glyphs (visual characters) of a given font.
The separation of sf::Font and sf::Text allows more flexibility and better performances: indeed a sf::Font is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a sf::Text is a lightweight object which can combine the glyphs data and metrics of a sf::Font to display any text on a render target.
It is important to note that the sf::Text instance doesn't copy the font that it uses, it only keeps a reference to it. Thus, a sf::Font must not be destructed while it is used by a sf::Text (i.e. never write a function that uses a local sf::Font instance for creating a text).
Usage example:
// Declare and load a font sf::Font font; font.LoadFromFile("arial.ttf"); // Create a text sf::Text text("hello"); text.SetFont(font); text.SetCharacterSize(30); text.SetStyle(sf::Text::Regular); // Display it window.Draw(text); // window is a sf::RenderWindow
Definition at line 45 of file Text.hpp.
enum sf::Text::Style |
sf::Text::Text | ( | ) |
Default constructor.
Creates an empty text.
Blend::Mode sf::Drawable::GetBlendMode | ( | ) | const [inherited] |
Vector2f sf::Text::GetCharacterPos | ( | 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 local coordinates (translation, rotation, scale and origin are not applied). You can easily get the corresponding global position with the TransformToGlobal function. If index is out of range, the position of the end of the string is returned.
index | Index of the character |
unsigned int sf::Text::GetCharacterSize | ( | ) | const |
const Color& sf::Drawable::GetColor | ( | ) | const [inherited] |
const Font& sf::Text::GetFont | ( | ) | const |
Get the text's font.
The returned reference is const, which means that you cannot modify the font when you get it from this function.
const Matrix3& sf::Drawable::GetInverseMatrix | ( | ) | const [protected, inherited] |
const Matrix3& sf::Drawable::GetMatrix | ( | ) | const [protected, inherited] |
const Vector2f& sf::Drawable::GetOrigin | ( | ) | const [inherited] |
const Vector2f& sf::Drawable::GetPosition | ( | ) | const [inherited] |
FloatRect sf::Text::GetRect | ( | ) | const |
Get the bounding rectangle of the text.
The returned rectangle is in global coordinates.
float sf::Drawable::GetRotation | ( | ) | const [inherited] |
Get the orientation of the object.
The rotation is always in the range [0, 360].
const Vector2f& sf::Drawable::GetScale | ( | ) | const [inherited] |
const String& sf::Text::GetString | ( | ) | const |
Get the text's string.
The returned string is a sf::String, which can automatically be converted to standard string types. So, the following lines of code are all valid:
sf::String s1 = text.GetString(); std::string s2 = text.GetString(); std::wstring s3 = text.GetString();
unsigned long sf::Text::GetStyle | ( | ) | const |
void sf::Drawable::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);
offset | Offset |
void sf::Drawable::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:
sf::Vector2f pos = object.GetPosition(); object.SetPosition(pos.x + offsetX, pos.y + offsetY);
offsetX | X offset | |
offsetY | Y offset |
virtual void sf::Text::Render | ( | RenderTarget & | target, | |
Renderer & | renderer | |||
) | const [protected, virtual] |
Draw the object to a render target.
target | Render target | |
renderer | Renderer providing low-level rendering commands |
Implements sf::Drawable.
void sf::Drawable::Rotate | ( | float | angle | ) | [inherited] |
Rotate the object.
This function ads 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);
angle | Angle of rotation, in degrees |
void sf::Drawable::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:
sf::Vector2f scale = object.GetScale(); object.SetScale(scale.x * factor.x, scale.y * factor.y);
factor | Scale factors |
void sf::Drawable::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:
sf::Vector2f scale = object.GetScale(); object.SetScale(scale.x * factorX, scale.y * factorY);
factorX | Horizontal scale factor | |
factorY | Vertical scale factor |
void sf::Drawable::SetBlendMode | ( | Blend::Mode | mode | ) | [inherited] |
Set the blending mode of the object.
This property defines how the pixels of an object are blended with the pixels of the render target to which it is drawn. To know more about the blending modes available, see the sf::Blend::Mode enum. The default blend mode is Blend::Alpha.
mode | New blending mode |
void sf::Text::SetCharacterSize | ( | unsigned int | size | ) |
Set the character size.
The default size is 30.
size | New character size, in pixels |
void sf::Drawable::SetColor | ( | const Color & | color | ) | [inherited] |
Set the global color of the object.
This global color affects the entire object, and modulates (multiplies) its original pixels. The default color is white.
color | New color |
void sf::Text::SetFont | ( | const Font & | font | ) |
Set the text's font.
Texts have a valid font by default, which the built-in Font::GetDefaultFont().
font | New font |
void sf::Drawable::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 drawable object is (0, 0).
origin | New origin |
void sf::Drawable::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 drawable object is (0, 0).
x | X coordinate of the new origin | |
y | Y coordinate of the new origin |
void sf::Drawable::SetPosition | ( | const Vector2f & | position | ) | [inherited] |
Set the position of the object.
This function completely overwrites the previous position. See Move to apply an offset based on the previous position instead. The default position of a drawable object is (0, 0).
position | New position |
void sf::Drawable::SetPosition | ( | float | x, | |
float | y | |||
) | [inherited] |
Set the position of the object.
This function completely overwrites the previous position. See Move to apply an offset based on the previous position instead. The default position of a drawable object is (0, 0).
x | X coordinate of the new position | |
y | Y coordinate of the new position |
void sf::Drawable::SetRotation | ( | float | angle | ) | [inherited] |
Set the orientation of the object.
This function completely overwrites the previous rotation. See Rotate to add an angle based on the previous rotation instead. The default rotation of a drawable object is 0.
angle | New rotation, in degrees |
void sf::Drawable::SetScale | ( | const Vector2f & | factors | ) | [inherited] |
Set the scale factors of the object.
scale.x and scale.y must be strictly positive, otherwise they are ignored. This function completely overwrites the previous scale. See Scale to add a factor based on the previous scale instead. The default scale of a drawable object is (1, 1).
factors | New scale factors |
void sf::Drawable::SetScale | ( | float | factorX, | |
float | factorY | |||
) | [inherited] |
Set the scale factors of the object.
factorX and factorY must be strictly positive, otherwise they are ignored. This function completely overwrites the previous scale. See Scale to add a factor based on the previous scale instead. The default scale of a drawable object is (1, 1).
factorX | New horizontal scale factor | |
factorY | New vertical scale factor |
void sf::Drawable::SetScaleX | ( | float | factor | ) | [inherited] |
void sf::Drawable::SetScaleY | ( | float | factor | ) | [inherited] |
void sf::Text::SetString | ( | const String & | string | ) |
Set the text's string.
The string argument is a sf::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.
string | New string |
void sf::Text::SetStyle | ( | unsigned long | style | ) |
Set the text's style.
You can pass a combination of one or more styles, for example sf::Text::Bold | sf::Text::Italic. The default style is sf::Text::Regular.
style | New style |
void sf::Drawable::SetX | ( | float | x | ) | [inherited] |
Set the X position of the object.
x | New X coordinate |
void sf::Drawable::SetY | ( | float | y | ) | [inherited] |
Set the Y position of the object.
y | New Y coordinate |
Transform a local point in global coordinates.
This function takes a point in local coordinates, and transforms it in global coordinates. In other words, it applies the same transformations that are applied to the object (origin, translation, rotation and scale).
point | Point to transform |
Transform a point in object local coordinates.
This function takes a point in global coordinates, and transforms it in coordinates local to the object. In other words, it applies the inverse of all the transformations applied to the object (origin, translation, rotation and scale).
point | Point to transform |