smk::Sprite Class Reference

#include <Sprite.hpp>

Inheritance diagram for smk::Sprite:

Description

A Drawable specialised in displaying rectangular texture.

Example:

auto sprite = smk::Sprite(texture_ball);
sprite.SetCenter(32,32);
sprite.SetPosition(player.x, player.y);
sprite.SetRotation(player.rotation);
window.Draw(sprite);
Examples
framebuffer.cpp, sprite.cpp, sprite_move.cpp, and texture_subrectangle.cpp.

Definition at line 34 of file Sprite.hpp.

Public Member Functions

 Sprite (const Texture &texture)
 A Sprite for drawing a texture. More...
 
 Sprite (const Texture &texture, const Rectangle rectangle)
 A Sprite for drawing a part of a Texture. More...
 
 Sprite (Framebuffer &framebuffer)
 A sprite for drawing the content of a Framebuffer. More...
 
 Sprite (Sprite &&)=default
 
 Sprite (const Sprite &)=default
 
Spriteoperator= (Sprite &&)=default
 
Spriteoperator= (const Sprite &)=default
 
void SetTexture (const Texture &texture)
 Update the sprite's texture. More...
 
void SetTextureRectangle (const Rectangle &rectangle)
 Update the sprite texture area. More...
 
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
 
void Draw (RenderTarget &target, RenderState state) const override
 

Constructor & Destructor Documentation

◆ Sprite() [1/3]

smk::Sprite::Sprite ( const Texture texture)

A Sprite for drawing a texture.

Parameters
textureThe Texture to be displayed.

Definition at line 16 of file Sprite.cpp.

16 : Sprite() {
17 SetTexture(texture);
18}
void SetTexture(const Texture &texture)
Update the sprite's texture.
Definition: Sprite.cpp:50

◆ Sprite() [2/3]

smk::Sprite::Sprite ( const Texture texture,
const Rectangle  rectangle 
)

A Sprite for drawing a part of a Texture.

Parameters
textureThe Texture to be displayed.
rectangleA rectangle in the texture to be used.

Definition at line 23 of file Sprite.cpp.

23 {
25 SetTextureRectangle(rectangle);
26}
void SetTextureRectangle(const Rectangle &rectangle)
Update the sprite texture area.
Definition: Sprite.cpp:63
void SetTexture(Texture texture)
Set the object's texture.

◆ Sprite() [3/3]

smk::Sprite::Sprite ( Framebuffer framebuffer)
explicit

A sprite for drawing the content of a Framebuffer.

Parameters
framebufferThe framebuffer to be used.

Definition at line 30 of file Sprite.cpp.

30 {
31 Transformable::SetTexture(framebuffer.color_texture());
32 float l = 0.F;
33 float r = 1.F;
34 float t = 0.F;
35 float b = 1.F;
36 auto www = float(framebuffer.color_texture().width());
37 auto hhh = float(framebuffer.color_texture().height());
38 SetVertexArray(VertexArray(std::vector<Vertex>({
39 {{0.F, 0.F}, {l, b}},
40 {{0.F, hhh}, {l, t}},
41 {{www, hhh}, {r, t}},
42 {{0.F, 0.F}, {l, b}},
43 {{www, hhh}, {r, t}},
44 {{www, 0.F}, {r, b}},
45 })));
46}
void SetVertexArray(VertexArray vertex_array)
Set the object's shape.

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_; }

◆ Draw()

void smk::TransformableBase::Draw ( RenderTarget target,
RenderState  state 
) const
overridevirtualinherited

Draw the object on a RenderTarget.

Parameters
targetA RenderTarget to be drawn on.
stateThe RenderState to derive from.

Implements smk::Drawable.

Definition at line 147 of file Transformable.cpp.

147 {
148 state.color *= color();
149 state.texture = texture();
150 state.view *= transformation();
151 state.vertex_array = vertex_array();
152 state.blend_mode = blend_mode();
153 target.Draw(state);
154}

◆ 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}

◆ 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}

◆ SetTexture() [1/2]

void smk::Sprite::SetTexture ( const Texture texture)

Update the sprite's texture.

Parameters
textureThe Texture to be displayed.

Definition at line 50 of file Sprite.cpp.

50 {
53 0.F,
54 0.F,
55 float(texture.width()),
56 float(texture.height()),
57 });
58}
int height() const
Access the height of the texture.
Definition: Texture.cpp:201
int width() const
Access the width of the texture.
Definition: Texture.cpp:195

◆ SetTexture() [2/2]

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}

◆ SetTextureRectangle()

void smk::Sprite::SetTextureRectangle ( const Rectangle rectangle)

Update the sprite texture area.

Parameters
textureThe Texture to be displayed.
rectangleA rectangle in the texture to be used.

Definition at line 63 of file Sprite.cpp.

63 {
64 float l = (rectangle.left + 0.5F) / texture().width(); // NOLINT
65 float r = (rectangle.right - 0.5F) / texture().width(); // NOLINT
66 float t = (rectangle.top + 0.5F) / texture().height(); // NOLINT
67 float b = (rectangle.bottom - 0.5F) / texture().height(); // NOLINT
68 float www = rectangle.width();
69 float hhh = rectangle.height();
70 SetVertexArray(VertexArray(std::vector<Vertex>({
71 {{0.F, 0.F}, {l, t}},
72 {{0.F, hhh}, {l, b}},
73 {{www, hhh}, {r, b}},
74 {{0.F, 0.F}, {l, t}},
75 {{www, hhh}, {r, b}},
76 {{www, 0.F}, {r, t}},
77 })));
78}

◆ 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_; }

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