1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_DRAWABLE_HPP
\r
26 #define SFML_DRAWABLE_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/System/Vector2.hpp>
\r
32 #include <SFML/Graphics/Color.hpp>
\r
33 #include <SFML/Graphics/Matrix3.hpp>
\r
40 ////////////////////////////////////////////////////////////
\r
41 /// Enumerate the blending modes for drawable objects
\r
42 ////////////////////////////////////////////////////////////
\r
47 Alpha, ///< Pixel = Src * a + Dest * (1 - a)
\r
48 Add, ///< Pixel = Src + Dest
\r
49 Multiply, ///< Pixel = Src * Dest
\r
50 None ///< No blending
\r
54 ////////////////////////////////////////////////////////////
\r
55 /// Abstract base class for every object that can be drawn
\r
56 /// into a render window
\r
57 ////////////////////////////////////////////////////////////
\r
58 class SFML_API Drawable
\r
62 ////////////////////////////////////////////////////////////
\r
63 /// Default constructor
\r
65 /// \param Position : Position of the object (0, 0 by default)
\r
66 /// \param Scale : Scale factor (1, 1 by default)
\r
67 /// \param Rotation : Orientation, in degrees (0 by default)
\r
68 /// \param Col : Color of the object (white by default)
\r
70 ////////////////////////////////////////////////////////////
\r
71 Drawable(const Vector2f& Position = Vector2f(0, 0), const Vector2f& Scale = Vector2f(1, 1), float Rotation = 0.f, const Color& Col = Color(255, 255, 255, 255));
\r
73 ////////////////////////////////////////////////////////////
\r
74 /// Virtual destructor
\r
76 ////////////////////////////////////////////////////////////
\r
77 virtual ~Drawable();
\r
79 ////////////////////////////////////////////////////////////
\r
80 /// Set the position of the object (take 2 values)
\r
82 /// \param X : New X coordinate
\r
83 /// \param Y : New Y coordinate
\r
85 ////////////////////////////////////////////////////////////
\r
86 void SetPosition(float X, float Y);
\r
88 ////////////////////////////////////////////////////////////
\r
89 /// Set the position of the object (take a 2D vector)
\r
91 /// \param Position : New position
\r
93 ////////////////////////////////////////////////////////////
\r
94 void SetPosition(const Vector2f& Position);
\r
96 ////////////////////////////////////////////////////////////
\r
97 /// Set the X position of the object
\r
99 /// \param X : New X coordinate
\r
101 ////////////////////////////////////////////////////////////
\r
102 void SetX(float X);
\r
104 ////////////////////////////////////////////////////////////
\r
105 /// Set the Y position of the object
\r
107 /// \param Y : New Y coordinate
\r
109 ////////////////////////////////////////////////////////////
\r
110 void SetY(float Y);
\r
112 ////////////////////////////////////////////////////////////
\r
113 /// Set the scale of the object (take 2 values)
\r
115 /// \param ScaleX : New horizontal scale (must be strictly positive)
\r
116 /// \param ScaleY : New vertical scale (must be strictly positive)
\r
118 ////////////////////////////////////////////////////////////
\r
119 void SetScale(float ScaleX, float ScaleY);
\r
121 ////////////////////////////////////////////////////////////
\r
122 /// Set the scale of the object (take a 2D vector)
\r
124 /// \param Scale : New scale (both values must be strictly positive)
\r
126 ////////////////////////////////////////////////////////////
\r
127 void SetScale(const Vector2f& Scale);
\r
129 ////////////////////////////////////////////////////////////
\r
130 /// Set the X scale factor of the object
\r
132 /// \param X : New X scale factor
\r
134 ////////////////////////////////////////////////////////////
\r
135 void SetScaleX(float FactorX);
\r
137 ////////////////////////////////////////////////////////////
\r
138 /// Set the Y scale factor of the object
\r
140 /// \param Y : New Y scale factor
\r
142 ////////////////////////////////////////////////////////////
\r
143 void SetScaleY(float FactorY);
\r
145 ////////////////////////////////////////////////////////////
\r
146 /// Set the center of the object, in coordinates relative to the
\r
147 /// top-left of the object (take 2 values).
\r
148 /// The default center is (0, 0)
\r
150 /// \param CenterX : X coordinate of the center
\r
151 /// \param CenterY : Y coordinate of the center
\r
153 ////////////////////////////////////////////////////////////
\r
154 void SetCenter(float CenterX, float CenterY);
\r
156 ////////////////////////////////////////////////////////////
\r
157 /// Set the center of the object, in coordinates relative to the
\r
158 /// top-left of the object (take a 2D vector).
\r
159 /// The default center is (0, 0)
\r
161 /// \param Center : New center
\r
163 ////////////////////////////////////////////////////////////
\r
164 void SetCenter(const Vector2f& Center);
\r
166 ////////////////////////////////////////////////////////////
\r
167 /// Set the orientation of the object
\r
169 /// \param Rotation : Angle of rotation, in degrees
\r
171 ////////////////////////////////////////////////////////////
\r
172 void SetRotation(float Rotation);
\r
174 ////////////////////////////////////////////////////////////
\r
175 /// Set the color of the object.
\r
176 /// The default color is white
\r
178 /// \param Col : New color
\r
180 ////////////////////////////////////////////////////////////
\r
181 void SetColor(const Color& Col);
\r
183 ////////////////////////////////////////////////////////////
\r
184 /// Set the blending mode for the object.
\r
185 /// The default blend mode is Blend::Alpha
\r
187 /// \param Mode : New blending mode
\r
189 ////////////////////////////////////////////////////////////
\r
190 void SetBlendMode(Blend::Mode Mode);
\r
192 ////////////////////////////////////////////////////////////
\r
193 /// Get the position of the object
\r
195 /// \return Current position
\r
197 ////////////////////////////////////////////////////////////
\r
198 const Vector2f& GetPosition() const;
\r
200 ////////////////////////////////////////////////////////////
\r
201 /// Get the current scale of the object
\r
203 /// \return Current scale factor (always positive)
\r
205 ////////////////////////////////////////////////////////////
\r
206 const Vector2f& GetScale() const;
\r
208 ////////////////////////////////////////////////////////////
\r
209 /// Get the center of the object
\r
211 /// \return Current position of the center
\r
213 ////////////////////////////////////////////////////////////
\r
214 const Vector2f& GetCenter() const;
\r
216 ////////////////////////////////////////////////////////////
\r
217 /// Get the orientation of the object.
\r
218 /// Rotation is always in the range [0, 360]
\r
220 /// \return Current rotation, in degrees
\r
222 ////////////////////////////////////////////////////////////
\r
223 float GetRotation() const;
\r
225 ////////////////////////////////////////////////////////////
\r
226 /// Get the color of the object
\r
228 /// \return Current color
\r
230 ////////////////////////////////////////////////////////////
\r
231 const Color& GetColor() const;
\r
233 ////////////////////////////////////////////////////////////
\r
234 /// Get the current blending mode
\r
236 /// \return Current blending mode
\r
238 ////////////////////////////////////////////////////////////
\r
239 Blend::Mode GetBlendMode() const;
\r
241 ////////////////////////////////////////////////////////////
\r
242 /// Move the object of a given offset (take 2 values)
\r
244 /// \param OffsetX : X offset
\r
245 /// \param OffsetY : Y offset
\r
247 ////////////////////////////////////////////////////////////
\r
248 void Move(float OffsetX, float OffsetY);
\r
250 ////////////////////////////////////////////////////////////
\r
251 /// Move the object of a given offset (take a 2D vector)
\r
253 /// \param Offset : Amount of units to move the object of
\r
255 ////////////////////////////////////////////////////////////
\r
256 void Move(const Vector2f& Offset);
\r
258 ////////////////////////////////////////////////////////////
\r
259 /// Scale the object (take 2 values)
\r
261 /// \param FactorX : Scaling factor on X (must be strictly positive)
\r
262 /// \param FactorY : Scaling factor on Y (must be strictly positive)
\r
264 ////////////////////////////////////////////////////////////
\r
265 void Scale(float FactorX, float FactorY);
\r
267 ////////////////////////////////////////////////////////////
\r
268 /// Scale the object (take a 2D vector)
\r
270 /// \param Factor : Scaling factors (both values must be strictly positive)
\r
272 ////////////////////////////////////////////////////////////
\r
273 void Scale(const Vector2f& Factor);
\r
275 ////////////////////////////////////////////////////////////
\r
276 /// Rotate the object
\r
278 /// \param Angle : Angle of rotation, in degrees
\r
280 ////////////////////////////////////////////////////////////
\r
281 void Rotate(float Angle);
\r
283 ////////////////////////////////////////////////////////////
\r
284 /// Transform a point from global coordinates into local coordinates
\r
285 /// (ie it applies the inverse of object's center, translation, rotation and scale to the point)
\r
287 /// \param Point : Point to transform
\r
289 /// \return Transformed point
\r
291 ////////////////////////////////////////////////////////////
\r
292 sf::Vector2f TransformToLocal(const sf::Vector2f& Point) const;
\r
294 ////////////////////////////////////////////////////////////
\r
295 /// Transform a point from local coordinates into global coordinates
\r
296 /// (ie it applies the object's center, translation, rotation and scale to the point)
\r
298 /// \param Point : Point to transform
\r
300 /// \return Transformed point
\r
302 ////////////////////////////////////////////////////////////
\r
303 sf::Vector2f TransformToGlobal(const sf::Vector2f& Point) const;
\r
307 ////////////////////////////////////////////////////////////
\r
308 /// Get the transform matrix of the drawable
\r
310 /// \return Transform matrix
\r
312 ////////////////////////////////////////////////////////////
\r
313 const Matrix3& GetMatrix() const;
\r
315 ////////////////////////////////////////////////////////////
\r
316 /// Get the inverse transform matrix of the drawable
\r
318 /// \return Inverse transform matrix
\r
320 ////////////////////////////////////////////////////////////
\r
321 const Matrix3& GetInverseMatrix() const;
\r
325 friend class RenderTarget;
\r
327 ////////////////////////////////////////////////////////////
\r
328 /// Draw the object into the specified window
\r
330 /// \param Target : Target into which render the object
\r
332 ////////////////////////////////////////////////////////////
\r
333 void Draw(RenderTarget& Target) const;
\r
335 ////////////////////////////////////////////////////////////
\r
336 /// Render the specific geometry of the object
\r
338 /// \param Target : Target into which render the object
\r
340 ////////////////////////////////////////////////////////////
\r
341 virtual void Render(RenderTarget& Target) const = 0;
\r
343 ////////////////////////////////////////////////////////////
\r
345 ////////////////////////////////////////////////////////////
\r
346 Vector2f myPosition; ///< Position of the object on screen
\r
347 Vector2f myScale; ///< Scale of the object
\r
348 Vector2f myCenter; ///< Origin of translation / rotation / scaling of the object
\r
349 float myRotation; ///< Orientation of the object, in degrees
\r
350 Color myColor; ///< Overlay color of the object
\r
351 Blend::Mode myBlendMode; ///< Blending mode
\r
352 mutable bool myNeedUpdate; ///< Do we need to recompute the transform matrix ?
\r
353 mutable bool myInvNeedUpdate; ///< Do we need to recompute the inverse transform matrix ?
\r
354 mutable Matrix3 myMatrix; ///< Precomputed transform matrix gathering the translation / rotation / scale / center
\r
355 mutable Matrix3 myInvMatrix; ///< Precomputed inverse transform matrix gathering the translation / rotation / scale / center
\r
361 #endif // SFML_DRAWABLE_HPP
\r