3b91a8a2603d0974874a2e5e74ecbbbebedb2de3
[TDDC76_proj.git] /
1 ////////////////////////////////////////////////////////////
2 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages 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 freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
23 ////////////////////////////////////////////////////////////
24
25 #ifndef SFML_TRANSFORM_HPP
26 #define SFML_TRANSFORM_HPP
27
28 ////////////////////////////////////////////////////////////
29 // Headers
30 ////////////////////////////////////////////////////////////
31 #include <SFML/Graphics/Export.hpp>
32 #include <SFML/Graphics/Rect.hpp>
33 #include <SFML/System/Vector2.hpp>
34
35
36 namespace sf
37 {
38 ////////////////////////////////////////////////////////////
39 /// \brief Define a 3x3 transform matrix
40 ///
41 ////////////////////////////////////////////////////////////
42 class SFML_GRAPHICS_API Transform
43 {
44 public :
45
46 ////////////////////////////////////////////////////////////
47 /// \brief Default constructor
48 ///
49 /// Creates an identity transform (a transform that does nothing).
50 ///
51 ////////////////////////////////////////////////////////////
52 Transform();
53
54 ////////////////////////////////////////////////////////////
55 /// \brief Construct a transform from a 3x3 matrix
56 ///
57 /// \param a00 Element (0, 0) of the matrix
58 /// \param a01 Element (0, 1) of the matrix
59 /// \param a02 Element (0, 2) of the matrix
60 /// \param a10 Element (1, 0) of the matrix
61 /// \param a11 Element (1, 1) of the matrix
62 /// \param a12 Element (1, 2) of the matrix
63 /// \param a20 Element (2, 0) of the matrix
64 /// \param a21 Element (2, 1) of the matrix
65 /// \param a22 Element (2, 2) of the matrix
66 ///
67 ////////////////////////////////////////////////////////////
68 Transform(float a00, float a01, float a02,
69 float a10, float a11, float a12,
70 float a20, float a21, float a22);
71
72 ////////////////////////////////////////////////////////////
73 /// \brief Return the transform as a 4x4 matrix
74 ///
75 /// This function returns a pointer to an array of 16 floats
76 /// containing the transform elements as a 4x4 matrix, which
77 /// is directly compatible with OpenGL functions.
78 ///
79 /// \code
80 /// sf::Transform transform = ...;
81 /// glLoadMatrixf(transform.getMatrix());
82 /// \endcode
83 ///
84 /// \return Pointer to a 4x4 matrix
85 ///
86 ////////////////////////////////////////////////////////////
87 const float* getMatrix() const;
88
89 ////////////////////////////////////////////////////////////
90 /// \brief Return the inverse of the transform
91 ///
92 /// If the inverse cannot be computed, an identity transform
93 /// is returned.
94 ///
95 /// \return A new transform which is the inverse of self
96 ///
97 ////////////////////////////////////////////////////////////
98 Transform getInverse() const;
99
100 ////////////////////////////////////////////////////////////
101 /// \brief Transform a 2D point
102 ///
103 /// \param x X coordinate of the point to transform
104 /// \param y Y coordinate of the point to transform
105 ///
106 /// \return Transformed point
107 ///
108 ////////////////////////////////////////////////////////////
109 Vector2f transformPoint(float x, float y) const;
110
111 ////////////////////////////////////////////////////////////
112 /// \brief Transform a 2D point
113 ///
114 /// \param point Point to transform
115 ///
116 /// \return Transformed point
117 ///
118 ////////////////////////////////////////////////////////////
119 Vector2f transformPoint(const Vector2f& point) const;
120
121 ////////////////////////////////////////////////////////////
122 /// \brief Transform a rectangle
123 ///
124 /// Since SFML doesn't provide support for oriented rectangles,
125 /// the result of this function is always an axis-aligned
126 /// rectangle. Which means that if the transform contains a
127 /// rotation, the bounding rectangle of the transformed rectangle
128 /// is returned.
129 ///
130 /// \param rectangle Rectangle to transform
131 ///
132 /// \return Transformed rectangle
133 ///
134 ////////////////////////////////////////////////////////////
135 FloatRect transformRect(const FloatRect& rectangle) const;
136
137 ////////////////////////////////////////////////////////////
138 /// \brief Combine the current transform with another one
139 ///
140 /// The result is a transform that is equivalent to applying
141 /// *this followed by \a transform. Mathematically, it is
142 /// equivalent to a matrix multiplication.
143 ///
144 /// \param transform Transform to combine with this transform
145 ///
146 /// \return Reference to *this
147 ///
148 ////////////////////////////////////////////////////////////
149 Transform& combine(const Transform& transform);
150
151 ////////////////////////////////////////////////////////////
152 /// \brief Combine the current transform with a translation
153 ///
154 /// This function returns a reference to *this, so that calls
155 /// can be chained.
156 /// \code
157 /// sf::Transform transform;
158 /// transform.translate(100, 200).rotate(45);
159 /// \endcode
160 ///
161 /// \param x Offset to apply on X axis
162 /// \param y Offset to apply on Y axis
163 ///
164 /// \return Reference to *this
165 ///
166 /// \see rotate, scale
167 ///
168 ////////////////////////////////////////////////////////////
169 Transform& translate(float x, float y);
170
171 ////////////////////////////////////////////////////////////
172 /// \brief Combine the current transform with a translation
173 ///
174 /// This function returns a reference to *this, so that calls
175 /// can be chained.
176 /// \code
177 /// sf::Transform transform;
178 /// transform.translate(sf::Vector2f(100, 200)).rotate(45);
179 /// \endcode
180 ///
181 /// \param offset Translation offset to apply
182 ///
183 /// \return Reference to *this
184 ///
185 /// \see rotate, scale
186 ///
187 ////////////////////////////////////////////////////////////
188 Transform& translate(const Vector2f& offset);
189
190 ////////////////////////////////////////////////////////////
191 /// \brief Combine the current transform with a rotation
192 ///
193 /// This function returns a reference to *this, so that calls
194 /// can be chained.
195 /// \code
196 /// sf::Transform transform;
197 /// transform.rotate(90).translate(50, 20);
198 /// \endcode
199 ///
200 /// \param angle Rotation angle, in degrees
201 ///
202 /// \return Reference to *this
203 ///
204 /// \see translate, scale
205 ///
206 ////////////////////////////////////////////////////////////
207 Transform& rotate(float angle);
208
209 ////////////////////////////////////////////////////////////
210 /// \brief Combine the current transform with a rotation
211 ///
212 /// The center of rotation is provided for convenience as a second
213 /// argument, so that you can build rotations around arbitrary points
214 /// more easily (and efficiently) than the usual
215 /// translate(-center).rotate(angle).translate(center).
216 ///
217 /// This function returns a reference to *this, so that calls
218 /// can be chained.
219 /// \code
220 /// sf::Transform transform;
221 /// transform.rotate(90, 8, 3).translate(50, 20);
222 /// \endcode
223 ///
224 /// \param angle Rotation angle, in degrees
225 /// \param centerX X coordinate of the center of rotation
226 /// \param centerY Y coordinate of the center of rotation
227 ///
228 /// \return Reference to *this
229 ///
230 /// \see translate, scale
231 ///
232 ////////////////////////////////////////////////////////////
233 Transform& rotate(float angle, float centerX, float centerY);
234
235 ////////////////////////////////////////////////////////////
236 /// \brief Combine the current transform with a rotation
237 ///
238 /// The center of rotation is provided for convenience as a second
239 /// argument, so that you can build rotations around arbitrary points
240 /// more easily (and efficiently) than the usual
241 /// translate(-center).rotate(angle).translate(center).
242 ///
243 /// This function returns a reference to *this, so that calls
244 /// can be chained.
245 /// \code
246 /// sf::Transform transform;
247 /// transform.rotate(90, sf::Vector2f(8, 3)).translate(sf::Vector2f(50, 20));
248 /// \endcode
249 ///
250 /// \param angle Rotation angle, in degrees
251 /// \param center Center of rotation
252 ///
253 /// \return Reference to *this
254 ///
255 /// \see translate, scale
256 ///
257 ////////////////////////////////////////////////////////////
258 Transform& rotate(float angle, const Vector2f& center);
259
260 ////////////////////////////////////////////////////////////
261 /// \brief Combine the current transform with a scaling
262 ///
263 /// This function returns a reference to *this, so that calls
264 /// can be chained.
265 /// \code
266 /// sf::Transform transform;
267 /// transform.scale(2, 1).rotate(45);
268 /// \endcode
269 ///
270 /// \param scaleX Scaling factor on the X axis
271 /// \param scaleY Scaling factor on the Y axis
272 ///
273 /// \return Reference to *this
274 ///
275 /// \see translate, rotate
276 ///
277 ////////////////////////////////////////////////////////////
278 Transform& scale(float scaleX, float scaleY);
279
280 ////////////////////////////////////////////////////////////
281 /// \brief Combine the current transform with a scaling
282 ///
283 /// The center of scaling is provided for convenience as a second
284 /// argument, so that you can build scaling around arbitrary points
285 /// more easily (and efficiently) than the usual
286 /// translate(-center).scale(factors).translate(center).
287 ///
288 /// This function returns a reference to *this, so that calls
289 /// can be chained.
290 /// \code
291 /// sf::Transform transform;
292 /// transform.scale(2, 1, 8, 3).rotate(45);
293 /// \endcode
294 ///
295 /// \param scaleX Scaling factor on X axis
296 /// \param scaleY Scaling factor on Y axis
297 /// \param centerX X coordinate of the center of scaling
298 /// \param centerY Y coordinate of the center of scaling
299 ///
300 /// \return Reference to *this
301 ///
302 /// \see translate, rotate
303 ///
304 ////////////////////////////////////////////////////////////
305 Transform& scale(float scaleX, float scaleY, float centerX, float centerY);
306
307 ////////////////////////////////////////////////////////////
308 /// \brief Combine the current transform with a scaling
309 ///
310 /// This function returns a reference to *this, so that calls
311 /// can be chained.
312 /// \code
313 /// sf::Transform transform;
314 /// transform.scale(sf::Vector2f(2, 1)).rotate(45);
315 /// \endcode
316 ///
317 /// \param factors Scaling factors
318 ///
319 /// \return Reference to *this
320 ///
321 /// \see translate, rotate
322 ///
323 ////////////////////////////////////////////////////////////
324 Transform& scale(const Vector2f& factors);
325
326 ////////////////////////////////////////////////////////////
327 /// \brief Combine the current transform with a scaling
328 ///
329 /// The center of scaling is provided for convenience as a second
330 /// argument, so that you can build scaling around arbitrary points
331 /// more easily (and efficiently) than the usual
332 /// translate(-center).scale(factors).translate(center).
333 ///
334 /// This function returns a reference to *this, so that calls
335 /// can be chained.
336 /// \code
337 /// sf::Transform transform;
338 /// transform.scale(sf::Vector2f(2, 1), sf::Vector2f(8, 3)).rotate(45);
339 /// \endcode
340 ///
341 /// \param factors Scaling factors
342 /// \param center Center of scaling
343 ///
344 /// \return Reference to *this
345 ///
346 /// \see translate, rotate
347 ///
348 ////////////////////////////////////////////////////////////
349 Transform& scale(const Vector2f& factors, const Vector2f& center);
350
351 ////////////////////////////////////////////////////////////
352 // Static member data
353 ////////////////////////////////////////////////////////////
354 static const Transform Identity; ///< The identity transform (does nothing)
355
356 private:
357
358 ////////////////////////////////////////////////////////////
359 // Member data
360 ////////////////////////////////////////////////////////////
361 float m_matrix[16]; ///< 4x4 matrix defining the transformation
362 };
363
364 ////////////////////////////////////////////////////////////
365 /// \relates sf::Transform
366 /// \brief Overload of binary operator * to combine two transforms
367 ///
368 /// This call is equivalent to calling Transform(left).combine(right).
369 ///
370 /// \param left Left operand (the first transform)
371 /// \param right Right operand (the second transform)
372 ///
373 /// \return New combined transform
374 ///
375 ////////////////////////////////////////////////////////////
376 SFML_GRAPHICS_API Transform operator *(const Transform& left, const Transform& right);
377
378 ////////////////////////////////////////////////////////////
379 /// \relates sf::Transform
380 /// \brief Overload of binary operator *= to combine two transforms
381 ///
382 /// This call is equivalent to calling left.combine(right).
383 ///
384 /// \param left Left operand (the first transform)
385 /// \param right Right operand (the second transform)
386 ///
387 /// \return The combined transform
388 ///
389 ////////////////////////////////////////////////////////////
390 SFML_GRAPHICS_API Transform& operator *=(Transform& left, const Transform& right);
391
392 ////////////////////////////////////////////////////////////
393 /// \relates sf::Transform
394 /// \brief Overload of binary operator * to transform a point
395 ///
396 /// This call is equivalent to calling left.transformPoint(right).
397 ///
398 /// \param left Left operand (the transform)
399 /// \param right Right operand (the point to transform)
400 ///
401 /// \return New transformed point
402 ///
403 ////////////////////////////////////////////////////////////
404 SFML_GRAPHICS_API Vector2f operator *(const Transform& left, const Vector2f& right);
405
406 } // namespace sf
407
408
409 #endif // SFML_TRANSFORM_HPP
410
411
412 ////////////////////////////////////////////////////////////
413 /// \class sf::Transform
414 /// \ingroup graphics
415 ///
416 /// A sf::Transform specifies how to translate, rotate, scale,
417 /// shear, project, whatever things. In mathematical terms, it defines
418 /// how to transform a coordinate system into another.
419 ///
420 /// For example, if you apply a rotation transform to a sprite, the
421 /// result will be a rotated sprite. And anything that is transformed
422 /// by this rotation transform will be rotated the same way, according
423 /// to its initial position.
424 ///
425 /// Transforms are typically used for drawing. But they can also be
426 /// used for any computation that requires to transform points between
427 /// the local and global coordinate systems of an entity (like collision
428 /// detection).
429 ///
430 /// Example:
431 /// \code
432 /// // define a translation transform
433 /// sf::Transform translation;
434 /// translation.translate(20, 50);
435 ///
436 /// // define a rotation transform
437 /// sf::Transform rotation;
438 /// rotation.rotate(45);
439 ///
440 /// // combine them
441 /// sf::Transform transform = translation * rotation;
442 ///
443 /// // use the result to transform stuff...
444 /// sf::Vector2f point = transform.transformPoint(10, 20);
445 /// sf::FloatRect rect = transform.transformRect(sf::FloatRect(0, 0, 10, 100));
446 /// \endcode
447 ///
448 /// \see sf::Transformable, sf::RenderStates
449 ///
450 ////////////////////////////////////////////////////////////