smk::Text Class Reference

#include <Text.hpp>

Inheritance diagram for smk::Text:

A class for displaying text. More...

Description

A class for displaying text.

A Text uses the Font's glyphs and displays them to the screen. A Text is a Transformable object, so you can move/rotate/scale/colorize it.

Example:

auto font = smk::Font("./arial.ttf", 32);
[...]
auto text = smk::Text(font, "The SMK library can draw text");
text.SetPosition(300,300);
text.SetColor(smk::Color::White);
window.Draw(text);
A class for displaying text.
Definition: Text.hpp:37
const glm::vec4 White
White.
Definition: Color.cpp:29
Examples
sound.cpp, text.cpp, and vibrate.cpp.

Definition at line 37 of file Text.hpp.

Public Member Functions

 Text ()
 Construct a null Text. It can't be drawn. More...
 
 Text (Font &font)
 Constructor. More...
 
 Text (Font &font, const std::string &text)
 Constructor. More...
 
 Text (Font &font, const std::wstring &text)
 Constructor. More...
 
void SetString (const std::wstring &wide_string)
 Update the text to be drawn. More...
 
void SetString (const std::string &string)
 Update the text to be drawn. More...
 
void SetFont (Font &font)
 Update the Font to be used. More...
 
void Draw (RenderTarget &target, RenderState state) const override
 Draw the Text to the screen. More...
 
glm::vec2 ComputeDimensions () const
 
 Text (Text &&)=default
 
 Text (const Text &)=default
 
Textoperator= (Text &&) noexcept=default
 
Textoperator= (const Text &)=default
 
void SetCenter (float center_x, float center_y)
 Set the center of the object. It is used as the rotation center. The center of the object will be drawn exactly on (0,0) on the screen (plus its potential translation if any) More...
 
void SetCenter (const glm::vec2 &center)
 Set the center of the object. It is used as the rotation center. The center of the object will be drawn exactly on (0,0) on the screen (plus its potential translation if any) More...
 
void Move (const glm::vec2 &move)
 
void Move (float x, float y)
 
void SetPosition (float x, float y)
 Set the position of the object to be drawn. More...
 
void SetPosition (const glm::vec2 &position)
 Set the position of the object to be drawn. More...
 
void Rotate (float rotation)
 Increase the rotation of the object to apply before drawing it. More...
 
void SetRotation (float rotation)
 Set the rotation to apply before drawing the object. More...
 
void SetScale (float scale)
 Increase or decrease the size of the object being drawn. More...
 
void SetScale (const glm::vec2 &scale)
 Increase or decrease the size of the object being drawn. More...
 
void SetScale (float scale_x, float scale_y)
 Increase or decrease the size of the object being drawn. More...
 
void SetScaleX (float scale_x)
 Increase or decrease the size of the object being drawn. More...
 
void SetScaleY (float scale_y)
 Increase or decrease the size of the object being drawn. More...
 
glm::mat4 transformation () const override
 Increase or decrease the size of the object being drawn. More...
 
void SetColor (const glm::vec4 &color)
 Color. More...
 
const glm::vec4 & color () const
 
void SetTexture (Texture texture)
 Set the object's texture. More...
 
const Texturetexture () const
 
void SetBlendMode (const BlendMode &)
 Set the blending mode to be used for drawing the object. More...
 
const BlendModeblend_mode () const
 
void SetVertexArray (VertexArray vertex_array)
 Set the object's shape. More...
 
const VertexArrayvertex_array () const
 

Public Attributes

Fontfont_ = nullptr
 
std::wstring string_
 

Constructor & Destructor Documentation

◆ Text() [1/4]

smk::Text::Text ( )
default

Construct a null Text. It can't be drawn.

◆ Text() [2/4]

smk::Text::Text ( Font font)

Constructor.

Parameters
fontThe Font to be used for drawing glyphs.

Definition at line 126 of file Text.cpp.

126 {
127 SetFont(font);
128}
void SetFont(Font &font)
Update the Font to be used.
Definition: Text.cpp:155

◆ Text() [3/4]

smk::Text::Text ( Font font,
const std::string &  text 
)

Constructor.

Parameters
fontThe Font to be used for drawing glyphs.
textThe character string to be drawn.

Definition at line 133 of file Text.cpp.

133 : Text(font) {
134 SetString(text);
135}
Text()
Construct a null Text. It can't be drawn.
void SetString(const std::wstring &wide_string)
Update the text to be drawn.
Definition: Text.cpp:145

◆ Text() [4/4]

smk::Text::Text ( Font font,
const std::wstring &  text 
)

Constructor.

Parameters
fontThe Font to be used for drawing glyphs.
textThe character string to be drawn.

Definition at line 140 of file Text.cpp.

140 : Text(font) {
141 SetString(text);
142}

Member Function Documentation

◆ blend_mode()

const BlendMode & smk::TransformableBase::blend_mode ( ) const
inlineinherited

Definition at line 36 of file Transformable.hpp.

36{ return blend_mode_; }

◆ color()

const glm::vec4 & smk::TransformableBase::color ( ) const
inlineinherited

Definition at line 28 of file Transformable.hpp.

28{ return color_; }

◆ ComputeDimensions()

glm::vec2 smk::Text::ComputeDimensions ( ) const

Compute the dimension of the text when drawn to the screen.

Returns
a tuple (width,height).

Definition at line 207 of file Text.cpp.

207 {
208 glm::vec2 dimension(0.f, 0.f);
209 float advance_x = 0.f;
210 dimension.y += font_->line_height();
211 for (const auto& it : string_) {
212 if (it == U'\n') {
213 advance_x = 0.f;
214 dimension.y += font_->line_height();
215 continue;
216 }
217 auto* character = font_->FetchGlyph(it);
218 if (!character) {
219 continue;
220 }
221 advance_x += character->advance;
222
223 dimension.x = std::max(dimension.x, advance_x);
224 }
225 return dimension;
226}

◆ Draw()

void smk::Text::Draw ( RenderTarget target,
RenderState  state 
) const
overridevirtual

Draw the Text to the screen.

Implements smk::Drawable.

Definition at line 160 of file Text.cpp.

160 {
161 state.color *= color();
162 glm::mat4 transformation = state.view * this->transformation();
163 float advance_x = 0.f;
164 float advance_y = font_->baseline_position();
165
166 state.vertex_array = VertexArray(std::vector<Vertex>({
167 {{0.f, 0.f}, {0.f, 0.f}},
168 {{0.f, 1.f}, {0.f, 1.f}},
169 {{1.f, 1.f}, {1.f, 1.f}},
170 {{0.f, 0.f}, {0.f, 0.f}},
171 {{1.f, 1.f}, {1.f, 1.f}},
172 {{1.f, 0.f}, {1.f, 0.f}},
173 }));
174
175 for (const auto& it : string_) {
176 if (it == U'\n') {
177 advance_x = 0.f;
178 advance_y += font_->line_height();
179 continue;
180 }
181
182 auto* character = font_->FetchGlyph(it);
183 if (!character) {
184 continue;
185 }
186
187 if (character->texture.id()) {
188 const float x = advance_x + float(character->bearing.x);
189 const float y = advance_y + float(character->bearing.y);
190 const float w = float(character->texture.width());
191 const float h = float(character->texture.height());
192 state.texture = character->texture;
193 state.view = transformation * glm::mat4(w, 0.f, 0.f, 0.f, //
194 0.f, h, 0.f, 0.f, //
195 0.f, 0.f, 1.f, 0.f, //
196 x, y, 0.f, 1.f //
197 );
198
199 target.Draw(state);
200 }
201 advance_x += character->advance;
202 }
203}
glm::mat4 transformation() const override
Increase or decrease the size of the object being drawn.

◆ Move() [1/2]

void smk::Transformable::Move ( const glm::vec2 &  move)
inherited

Increase the position of the object being drawn.

See also
smk::SetPosition
Parameters
moveThe increment of position (x,y)
Examples
texture_subrectangle.cpp.

Definition at line 44 of file Transformable.cpp.

44 {
45 position_ += move;
46}

◆ Move() [2/2]

void smk::Transformable::Move ( float  x,
float  y 
)
inherited

Increase the position of the object being drawn.

See also
smk::SetPosition
Parameters
xThe increment of position along the horizontal axis.
yThe increment of position along the vertical axis.

Definition at line 52 of file Transformable.cpp.

52 {
53 Move({x, y});
54}
void Move(const glm::vec2 &move)

◆ Rotate()

void smk::Transformable::Rotate ( float  rotation)
inherited

Increase the rotation of the object to apply before drawing it.

See also
Transformable::SetRotation.
Parameters
rotationThe delta of rotation to be added.

Definition at line 22 of file Transformable.cpp.

22 {
23 rotation_ += rotation;
24}

◆ SetBlendMode()

void smk::TransformableBase::SetBlendMode ( const BlendMode blend_mode)
inherited

Set the blending mode to be used for drawing the object.

Parameters
blend_modethe BlendMode to be used.

Definition at line 133 of file Transformable.cpp.

133 {
134 blend_mode_ = blend_mode;
135}

◆ SetCenter() [1/2]

void smk::Transformable::SetCenter ( const glm::vec2 &  center)
inherited

Set the center of the object. It is used as the rotation center. The center of the object will be drawn exactly on (0,0) on the screen (plus its potential translation if any)

Parameters
centerThe center position (x,y) in the object.

Definition at line 60 of file Transformable.cpp.

60 {
61 center_ = center;
62}

◆ SetCenter() [2/2]

void smk::Transformable::SetCenter ( float  x,
float  y 
)
inherited

Set the center of the object. It is used as the rotation center. The center of the object will be drawn exactly on (0,0) on the screen (plus its potential translation if any)

Parameters
xThe center position along the horizontal axis.
yThe center position along the vertical axis.
Examples
sprite_move.cpp.

Definition at line 69 of file Transformable.cpp.

69 {
70 SetCenter({x, y});
71}
void SetCenter(float center_x, float center_y)
Set the center of the object. It is used as the rotation center. The center of the object will be dra...

◆ SetColor()

void smk::TransformableBase::SetColor ( const glm::vec4 &  color)
inherited

Color.

Modify the color of the object. The resulting pixel is the multiplication component wise in between this color and the original pixel color.

Parameters
colorThe color.
Examples
rounded_rectangle.cpp, and shape_2d.cpp.

Definition at line 127 of file Transformable.cpp.

127 {
128 color_ = color;
129}

◆ SetFont()

void smk::Text::SetFont ( Font font)

Update the Font to be used.

Definition at line 155 of file Text.cpp.

155 {
156 font_ = &font;
157}

◆ SetPosition() [1/2]

void smk::Transformable::SetPosition ( const glm::vec2 &  position)
inherited

Set the position of the object to be drawn.

See also
Transformable::Move.
Parameters
positionthe position (x,y) of the object.

Definition at line 29 of file Transformable.cpp.

29 {
30 position_ = position;
31}

◆ SetPosition() [2/2]

void smk::Transformable::SetPosition ( float  x,
float  y 
)
inherited

Set the position of the object to be drawn.

See also
Transformable::Move.
Parameters
xThe position along the horizontal axis.
yThe position along the vertical axis.
Examples
sprite_move.cpp.

Definition at line 37 of file Transformable.cpp.

37 {
38 position_ = {x, y};
39}

◆ SetRotation()

void smk::Transformable::SetRotation ( float  rotation)
inherited

Set the rotation to apply before drawing the object.

See also
Transformable::Rotate.
Parameters
rotationThe angle in radian.

Definition at line 15 of file Transformable.cpp.

15 {
16 rotation_ = rotation;
17}

◆ SetScale() [1/3]

void smk::Transformable::SetScale ( const glm::vec2 &  scale)
inherited

Increase or decrease the size of the object being drawn.

Parameters
scaleThe ratio of magnification.

Definition at line 81 of file Transformable.cpp.

81 {
82 scale_ = scale;
83}

◆ SetScale() [2/3]

void smk::Transformable::SetScale ( float  scale)
inherited

Increase or decrease the size of the object being drawn.

Parameters
scaleThe ratio of magnification.
Examples
sprite_move.cpp.

Definition at line 75 of file Transformable.cpp.

75 {
76 SetScale({scale, scale});
77}
void SetScale(float scale)
Increase or decrease the size of the object being drawn.

◆ SetScale() [3/3]

void smk::Transformable::SetScale ( float  scale_x,
float  scale_y 
)
inherited

Increase or decrease the size of the object being drawn.

Parameters
scale_xThe ratio of magnification along the horizontal axis.
scale_yThe ratio of magnification along the vertical axis.

Definition at line 88 of file Transformable.cpp.

88 {
89 scale_.x = scale_x;
90 scale_.y = scale_y;
91}

◆ SetScaleX()

void smk::Transformable::SetScaleX ( float  scale_x)
inherited

Increase or decrease the size of the object being drawn.

Parameters
scale_xThe ratio of magnification along the horizontal axis.

Definition at line 95 of file Transformable.cpp.

95 {
96 scale_.x = scale_x;
97}

◆ SetScaleY()

void smk::Transformable::SetScaleY ( float  scale_y)
inherited

Increase or decrease the size of the object being drawn.

Parameters
scale_yThe ratio of magnification along the vertical axis.

Definition at line 101 of file Transformable.cpp.

101 {
102 scale_.y = scale_y;
103}

◆ SetString() [1/2]

void smk::Text::SetString ( const std::string &  string)

Update the text to be drawn.

Definition at line 150 of file Text.cpp.

150 {
151 string_ = to_wstring(string);
152}

◆ SetString() [2/2]

void smk::Text::SetString ( const std::wstring &  wide_string)

Update the text to be drawn.

Definition at line 145 of file Text.cpp.

145 {
146 string_ = wide_string;
147}

◆ SetTexture()

void smk::TransformableBase::SetTexture ( Texture  texture)
inherited

Set the object's texture.

Definition at line 138 of file Transformable.cpp.

138 {
139 texture_ = std::move(texture);
140}

◆ SetVertexArray()

void smk::TransformableBase::SetVertexArray ( VertexArray  vertex_array)
inherited

Set the object's shape.

Definition at line 143 of file Transformable.cpp.

143 {
144 vertex_array_ = std::move(vertex_array);
145}

◆ texture()

const Texture & smk::TransformableBase::texture ( ) const
inlineinherited

Definition at line 32 of file Transformable.hpp.

32{ return texture_; }

◆ transformation()

glm::mat4 smk::Transformable::transformation ( ) const
overridevirtualinherited

Increase or decrease the size of the object being drawn.

Returns
the transformation applied to the object. This is the result of applying the translation, rotation, center and scaling to the the object.

Implements smk::TransformableBase.

Definition at line 109 of file Transformable.cpp.

109 {
110 glm::mat4 ret = glm::mat4(1.0);
111 ret = glm::translate(ret, {position_.x, position_.y, 0.0});
112 if (rotation_ != 0.F) {
113 ret =
114 // NOLINTNEXTLINE
115 glm::rotate(ret, -rotation_ * (2.F * 3.1415F / 360.F), {0.0, 0.0, 1.0});
116 }
117 ret =
118 glm::translate(ret, {-center_.x * scale_.x, -center_.y * scale_.y, 0.F});
119 ret = glm::scale(ret, {scale_.x, scale_.y, 1.0});
120 return ret;
121}

◆ vertex_array()

const VertexArray & smk::TransformableBase::vertex_array ( ) const
inlineinherited

Definition at line 40 of file Transformable.hpp.

40{ return vertex_array_; }

Member Data Documentation

◆ font_

Font* smk::Text::font_ = nullptr

Definition at line 59 of file Text.hpp.

◆ string_

std::wstring smk::Text::string_

Definition at line 60 of file Text.hpp.


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