Transformable.hpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#ifndef SMK_TRANSFORMABLE
6#define SMK_TRANSFORMABLE
7
8#include <smk/Drawable.hpp>
9#include <smk/RenderState.hpp>
10
11namespace smk {
12struct Texture;
13class RenderTarget;
14class VertexArray;
15
16/// A Drawable object supporting several transformations:
17/// - Translation
18/// - Rotation.
19/// - Color filtering.
20/// - Modify blending mode. @see smk::BlendMode.
22 public:
23 // Tranformation.
24 virtual glm::mat4 transformation() const = 0;
25
26 /// Color
27 void SetColor(const glm::vec4& color);
28 const glm::vec4& color() const { return color_; }
29
30 // Texture
31 void SetTexture(Texture texture);
32 const Texture& texture() const { return texture_; }
33
34 // BlendMode
35 void SetBlendMode(const BlendMode&);
36 const BlendMode& blend_mode() const { return blend_mode_; }
37
38 // VertexArray
39 void SetVertexArray(VertexArray vertex_array);
40 const VertexArray& vertex_array() const { return vertex_array_; }
41
42 // Drawable override
43 void Draw(RenderTarget& target, RenderState state) const override;
44
45 // Movable-copyable class.
46 TransformableBase() = default;
47 TransformableBase(TransformableBase&&) noexcept = default;
48 TransformableBase(const TransformableBase&) = default;
49 TransformableBase& operator=(TransformableBase&&) noexcept = default;
50 TransformableBase& operator=(const TransformableBase&) = default;
51
52 private:
53 glm::vec4 color_ = {1.0, 1.0, 1.0, 1.0};
54 Texture texture_;
55 BlendMode blend_mode_ = BlendMode::Alpha;
56 VertexArray vertex_array_;
57};
58
59/// A 2D Drawable object supporting several transformations:
60/// - Translation
61/// - Rotation.
62/// - Color filtering.
63/// - Modify blending mode. @see smk::BlendMode.
65 public:
66 virtual ~Transformable() = default;
67
68 // Center
69 void SetCenter(float center_x, float center_y);
70 void SetCenter(const glm::vec2& center);
71
72 // Position
73 void Move(const glm::vec2& move);
74 void Move(float x, float y);
75 void SetPosition(float x, float y);
76 void SetPosition(const glm::vec2& position);
77
78 // Rotation
79 void Rotate(float rotation);
80 void SetRotation(float rotation);
81
82 // Scale
83 void SetScale(float scale);
84 void SetScale(const glm::vec2& scale);
85 void SetScale(float scale_x, float scale_y);
86 void SetScaleX(float scale_x);
87 void SetScaleY(float scale_y);
88
89 // Transformable override;
90 glm::mat4 transformation() const override;
91
92 // Movable-copyable class.
93 Transformable() = default;
94 Transformable(Transformable&&) noexcept = default;
95 Transformable(const Transformable&) = default;
96 Transformable& operator=(Transformable&&) = default;
97 Transformable& operator=(const Transformable&) = default;
98
99 private:
100 float rotation_ = 0.f;
101 glm::vec2 center_ = {0.f, 0.f};
102 glm::vec2 position_ = {0.f, 0.f};
103 glm::vec2 scale_ = {1.0, 1.0};
104};
105
106/// A 2D Drawable object supporting several transformations:
107/// - Translation
108/// - Rotation.
109/// - Color filtering.
110/// - Modify blending mode. @see smk::BlendMode.
112 public:
113 virtual ~Transformable3D() = default;
114
115 void SetTransformation(const glm::mat4& mat);
116 glm::mat4 transformation() const override;
117
118 // Movable-copyable class.
119 Transformable3D() = default;
120 Transformable3D(Transformable3D&&) = default;
121 Transformable3D(const Transformable3D&) = default;
122 Transformable3D& operator=(Transformable3D&&) = default;
123 Transformable3D& operator=(const Transformable3D&) = default;
124
125 private:
126 glm::mat4 transformation_ = glm::mat4(1.f);
127};
128
130
131} // namespace smk
132
133#endif /* end of include guard: SMK_TRANSFORMABLE */
Interface for class that can be draw to a smk::RenderTarget.
Definition: Drawable.hpp:14
void SetTransformation(const glm::mat4 &mat)
set the transformation to use for drawing this object, represented as 4x4 matrix.
void SetColor(const glm::vec4 &color)
Color.
void SetTexture(Texture texture)
Set the object's texture.
void Draw(RenderTarget &target, RenderState state) const override
void SetVertexArray(VertexArray vertex_array)
Set the object's shape.
void SetBlendMode(const BlendMode &)
Set the blending mode to be used for drawing the object.
void SetRotation(float rotation)
Set the rotation to apply before drawing the object.
void SetScale(float scale)
Increase or decrease the size of the object being drawn.
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...
void Move(const glm::vec2 &move)
glm::mat4 transformation() const override
Increase or decrease the size of the object being drawn.
void SetPosition(float x, float y)
Set the position of the object to be drawn.
void SetScaleX(float scale_x)
Increase or decrease the size of the object being drawn.
void Rotate(float rotation)
Increase the rotation of the object to apply before drawing it.
void SetScaleY(float scale_y)
Increase or decrease the size of the object being drawn.
An array of smk::Vertex moved to the GPU memory. This represent a set of triangles to be drawn by the...
Definition: VertexArray.hpp:20
static const BlendMode Alpha
destination = source * source.a + destination * (1 - souce.a)
Definition: BlendMode.hpp:68
Contain all the data needed to draw.
Definition: RenderState.hpp:17