Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
Transformable.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_TRANSFORMABLE_H
25 #define GF_TRANSFORMABLE_H
26 
27 #include "Anchor.h"
28 #include "Drawable.h"
29 #include "Matrix.h"
30 #include "Portability.h"
31 #include "Rect.h"
32 #include "Vector.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
39  /**
40  * @ingroup graphics
41  * @brief Decomposed transform defined by a position, a rotation and a scale
42  *
43  * gf::Matrix3f, as a low-level class, offers a great level of
44  * flexibility but it is not always convenient to manage. Indeed,
45  * one can easily combine any kind of operation, such as a translation
46  * followed by a rotation followed by a scaling, but once the result
47  * transform is built, there's no way to go backward and, let's say,
48  * change only the rotation without modifying the translation and scaling.
49  * The entire transform must be recomputed, which means that you
50  * need to retrieve the initial translation and scale factors as
51  * well, and combine them the same way you did before updating the
52  * rotation. This is a tedious operation, and it requires to store
53  * all the individual components of the final transform.
54  *
55  * That's exactly what gf::Transformable was written for: it hides
56  * these variables and the composed transform behind an easy to use
57  * interface. You can set or get any of the individual components
58  * without worrying about the others. It also provides the composed
59  * transform (as a gf::Matrix3f), and keeps it up-to-date.
60  *
61  * In addition to the position, rotation and scale, gf::Transformable
62  * provides an "origin" component, which represents the local origin
63  * of the three other components. Let's take an example with a 10x10
64  * pixels sprite. By default, the sprite is positioned/rotated/scaled
65  * relatively to its top-left corner, because it is the local point
66  * @f$(0, 0)@f$. But if we change the origin to be @f$(5, 5)@f$, the
67  * sprite will be positioned/rotated/scaled around its center instead.
68  * And if we set the origin to @f$(10, 10)@f$, it will be transformed
69  * around its bottom-right corner.
70  *
71  * To keep the gf::Transformable class simple, there's only one
72  * origin for all the components. You cannot position the sprite
73  * relatively to its top-left corner while rotating it around its
74  * center, for example. To do such things, use gf::Matrix3f directly.
75  *
76  * gf::Transformable can be used as a base class. That's what
77  * gf's sprites, texts, curves and shapes do.
78  *
79  * ~~~{.cc}
80  * class MyEntity : public gf::Transformable {
81  * virtual void draw(gf::RenderTarget& target, gf::RenderStates states) override {
82  * states.transform *= getTransform();
83  * target.draw(..., states);
84  * }
85  * };
86  *
87  * MyEntity entity;
88  * entity.setPosition(10, 20);
89  * entity.setRotation(45);
90  * renderer.draw(entity);
91  * ~~~
92  *
93  * @sa gf::Matrix3f
94  */
95  class GF_API Transformable : public Drawable {
96  public:
97  /**
98  * @brief Default constructor
99  *
100  * By default:
101  *
102  * - the origin is set to @f$(0,0)@f$
103  * - the position is set to @f$(0,0)@f$
104  * - the rotation is set to @f$ 0 @f$
105  * - the scale is set to @f$(1,1)@f$
106  */
107  Transformable();
108 
109  /**
110  * @brief Set the local origin of the object
111  *
112  * The origin of an object defines the center point for
113  * all transformations (position, scale, rotation).
114  * The coordinates of this point must be relative to the
115  * top-left corner of the object, and ignore all
116  * transformations (position, scale, rotation).
117  *
118  * The default origin of a transformable object is @f$(0, 0)@f$.
119  *
120  * @param origin New origin
121  *
122  * @sa getOrigin()
123  */
124  void setOrigin(Vector2f origin);
125 
126  /**
127  * @brief Get the local origin of the object
128  *
129  * @return Current origin
130  *
131  * @sa setOrigin()
132  */
133  Vector2f getOrigin() const {
134  return m_origin;
135  }
136 
137  /**
138  * @brief Set the position of the object
139  *
140  * This function completely overwrites the previous position.
141  * See the move() function to apply an offset based on the previous
142  * position instead.
143  *
144  * The default position of a transformable object is @f$(0, 0)@f$.
145  *
146  * @param position New position
147  *
148  * @sa move(), getPosition()
149  */
150  void setPosition(Vector2f position);
151 
152  /**
153  * @brief Get the position of the object
154  *
155  * @return Current position
156  *
157  * @sa setPosition
158  */
159  Vector2f getPosition() const {
160  return m_position;
161  }
162 
163  /**
164  * @brief Move the object by a given offset
165  *
166  * This function adds to the current position of the object,
167  * unlike setPosition() which overwrites it.
168  * Thus, it is equivalent to the following code:
169  *
170  * ~~~{.cc}
171  * object.setPosition(object.getPosition() + offset);
172  * ~~~
173  *
174  * @param offset Offset
175  *
176  * @sa setPosition()
177  */
178  void move(Vector2f offset);
179 
180  /**
181  * @brief Set the orientation of the object
182  *
183  * This function completely overwrites the previous rotation.
184  * See the rotate() function to add an angle based on the previous
185  * rotation instead.
186  *
187  * The default rotation of a transformable object is @f$ 0 @f$.
188  *
189  * @param angle New rotation, in radians
190  *
191  * @sa rotate(), getRotation()
192  */
193  void setRotation(float angle);
194 
195  /**
196  * @brief Get the orientation of the object
197  *
198  * @return Current rotation, in radians
199  *
200  * @sa setRotation()
201  */
202  float getRotation() const {
203  return m_rotation;
204  }
205 
206  /**
207  * @brief Rotate the object
208  *
209  * This function adds to the current rotation of the object,
210  * unlike setRotation() which overwrites it.
211  * Thus, it is equivalent to the following code:
212  *
213  * ~~~{.cc}
214  * object.setRotation(object.getRotation() + angle);
215  * ~~~
216  *
217  * @param angle Angle of rotation, in radians
218  */
219  void rotate(float angle);
220 
221  /**
222  * @brief Set the scale factors of the object
223  *
224  * This function completely overwrites the previous scale.
225  * See the scale() function to add a factor based on the previous
226  * scale instead.
227  *
228  * The default scale of a transformable object is @f$(1, 1)@f$.
229  *
230  * @param factors New scale factors
231  *
232  * @sa scale(), getScale()
233  */
234  void setScale(Vector2f factors);
235 
236 
237  /**
238  * @brief Set the scale factor of the object
239  *
240  * This function is a shortcut when the scale factors for @f$ x @f$
241  * and @f$ y @f$ are the same. It is equivalent to:
242  *
243  * ~~~{.cc}
244  * object.setScale({ factor, factor });
245  * ~~~
246  *
247  * @param factor New scale factor
248  *
249  * @sa scale(), getScale()
250  */
251  void setScale(float factor) {
252  setScale({ factor, factor });
253  }
254 
255  /**
256  * @brief Get the current scale of the object
257  *
258  * @return Current scale factors
259  *
260  * @sa setScale()
261  */
262  Vector2f getScale() const {
263  return m_scale;
264  }
265 
266  /**
267  * @brief Scale the object
268  *
269  * This function multiplies the current scale of the object,
270  * unlike setScale() which overwrites it.
271  * Thus, it is equivalent to the following code:
272  *
273  * ~~~{.cc}
274  * object.setScale(object.getScale() * factors);
275  * ~~~
276  *
277  * @param factors Scale factors
278  *
279  * @sa setScale()
280  */
281  void scale(Vector2f factors);
282 
283  /**
284  * @brief Scale the object
285  *
286  * This function is a shortcut when the scale factors for @f$ x @f$
287  * and @f$ y @f$ are the same. It is equivalent to:
288  *
289  * ~~~{.cc}
290  * object.scale({ factor, factor });
291  * ~~~
292  *
293  * @param factor Scale factor
294  *
295  * @sa setScale()
296  */
297  void scale(float factor) {
298  scale({ factor, factor });
299  }
300 
301  /**
302  * @brief Get the combined transform of the object
303  *
304  * The combined transform of the object is (in this order):
305  *
306  * - a translation of -origin
307  * - a scaling of the defined scaling factors
308  * - a rotation of the defined angle
309  * - a translation of the defined position
310  *
311  * @return Transform combining the position/rotation/scale/origin of the object
312  *
313  * @sa getInverseTransform()
314  */
315  Matrix3f getTransform() const;
316 
317  /**
318  * @brief Get the inverse of the combined transform of the object
319  *
320  * @return Inverse of the combined transformations applied to the object
321  *
322  * @sa getTransform()
323  */
324  Matrix3f getInverseTransform() const;
325 
326  protected:
327  /**
328  * @brief Set the origin from an anchor and bounds
329  *
330  * This function can be called from derived classes for setting the origin
331  * properly thanks to an anchor and the bounds they computed.
332  *
333  * @param anchor An anchor
334  * @param bounds The bounds of the entity
335  */
336  void setOriginFromAnchorAndBounds(Anchor anchor, const RectF& bounds);
337 
338  private:
339  Vector2f m_origin;
340  Vector2f m_position;
341  float m_rotation;
342  Vector2f m_scale;
343  };
344 
345 #ifndef DOXYGEN_SHOULD_SKIP_THIS
346 }
347 #endif
348 }
349 
350 #endif // GF_TRANSFORMABLE_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
void scale(float factor)
Scale the object.
Definition: Transformable.h:297
void setPosition(Vector2f position)
Set the position of the object.
Vector2f getPosition() const
Get the position of the object.
Definition: Transformable.h:159
void rotate(float angle)
Rotate the object.
float getRotation() const
Get the orientation of the object.
Definition: Transformable.h:202
void setScale(float factor)
Set the scale factor of the object.
Definition: Transformable.h:251
constexpr Vector(T x, T y)
Constructor that takes 2 components.
Definition: Vector.h:348
void setRotation(float angle)
Set the orientation of the object.
Vector2f getScale() const
Get the current scale of the object.
Definition: Transformable.h:262
void scale(Vector2f factors)
Scale the object.
Abstract base class for objects that can be drawn to a render window.
Definition: Drawable.h:79
void move(Vector2f offset)
Move the object by a given offset.
Transformable()
Default constructor.
Matrix3f getInverseTransform() const
Get the inverse of the combined transform of the object.
The namespace for gf classes.
Definition: Action.h:34
void setOrigin(Vector2f origin)
Set the local origin of the object.
void setOriginFromAnchorAndBounds(Anchor anchor, const RectF &bounds)
Set the origin from an anchor and bounds.
Anchor
An anchor of a box.
Definition: Anchor.h:41
Matrix3f getTransform() const
Get the combined transform of the object.
#define GF_API
Definition: Portability.h:35
Vector2f getOrigin() const
Get the local origin of the object.
Definition: Transformable.h:133
void setScale(Vector2f factors)
Set the scale factors of the object.