Drawable.cpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 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//
24
26// Headers
28#include <SFML/Graphics/Drawable.hpp>
29#include <SFML/Graphics/GraphicsContext.hpp>
30#include <SFML/Window/Window.hpp>
31#include <math.h>
32
33
34namespace sf
35{
39Drawable::Drawable(const Vector2f& Position, const Vector2f& Scale, float Rotation, const Color& Col) :
40myPosition (Position),
41myScale (Scale),
42myCenter (0, 0),
43myRotation (Rotation),
44myColor (Col),
45myBlendMode (Blend::Alpha),
46myNeedUpdate (true),
47myInvNeedUpdate(true)
48{
49
50}
51
52
57{
58 // Nothing to do
59}
60
61
65void Drawable::SetPosition(float X, float Y)
66{
67 SetX(X);
68 SetY(Y);
69}
70
71
75void Drawable::SetPosition(const Vector2f& Position)
76{
77 SetX(Position.x);
78 SetY(Position.y);
79}
80
81
85void Drawable::SetX(float X)
86{
87 myPosition.x = X;
88 myNeedUpdate = true;
89 myInvNeedUpdate = true;
90}
91
92
96void Drawable::SetY(float Y)
97{
98 myPosition.y = Y;
99 myNeedUpdate = true;
100 myInvNeedUpdate = true;
101}
102
103
107void Drawable::SetScale(float ScaleX, float ScaleY)
108{
109 SetScaleX(ScaleX);
110 SetScaleY(ScaleY);
111}
112
113
117void Drawable::SetScale(const Vector2f& Scale)
118{
119 SetScaleX(Scale.x);
120 SetScaleY(Scale.y);
121}
122
123
127void Drawable::SetScaleX(float FactorX)
128{
129 if (FactorX > 0)
130 {
131 myScale.x = FactorX;
132 myNeedUpdate = true;
133 myInvNeedUpdate = true;
134 }
135}
136
137
141void Drawable::SetScaleY(float FactorY)
142{
143 if (FactorY > 0)
144 {
145 myScale.y = FactorY;
146 myNeedUpdate = true;
147 myInvNeedUpdate = true;
148 }
149}
150
151
157void Drawable::SetCenter(float CenterX, float CenterY)
158{
159 myCenter.x = CenterX;
160 myCenter.y = CenterY;
161 myNeedUpdate = true;
162 myInvNeedUpdate = true;
163}
164
165
171void Drawable::SetCenter(const Vector2f& Center)
172{
173 SetCenter(Center.x, Center.y);
174}
175
176
180void Drawable::SetRotation(float Rotation)
181{
182 myRotation = static_cast<float>(fmod(Rotation, 360));
183 if (myRotation < 0)
184 myRotation += 360.f;
185 myNeedUpdate = true;
186 myInvNeedUpdate = true;
187}
188
189
194void Drawable::SetColor(const Color& Col)
195{
196 myColor = Col;
197}
198
199
205{
206 myBlendMode = Mode;
207}
208
209
213const Vector2f& Drawable::GetPosition() const
214{
215 return myPosition;
216}
217
218
222const Vector2f& Drawable::GetScale() const
223{
224 return myScale;
225}
226
227
231const Vector2f& Drawable::GetCenter() const
232{
233 return myCenter;
234}
235
236
241{
242 return myRotation;
243}
244
245
250{
251 return myColor;
252}
253
254
259{
260 return myBlendMode;
261}
262
263
268void Drawable::Move(float OffsetX, float OffsetY)
269{
270 SetX(myPosition.x + OffsetX);
271 SetY(myPosition.y + OffsetY);
272}
273
274
278void Drawable::Move(const Vector2f& Offset)
279{
280 Move(Offset.x, Offset.y);
281}
282
283
287void Drawable::Scale(float FactorX, float FactorY)
288{
289 SetScaleX(myScale.x * FactorX);
290 SetScaleY(myScale.y * FactorY);
291}
292
293
297void Drawable::Scale(const Vector2f& Factor)
298{
299 Scale(Factor.x, Factor.y);
300}
301
302
306void Drawable::Rotate(float Angle)
307{
308 SetRotation(myRotation + Angle);
309}
310
311
316sf::Vector2f Drawable::TransformToLocal(const sf::Vector2f& Point) const
317{
318 return GetInverseMatrix().Transform(Point);
319}
320
325sf::Vector2f Drawable::TransformToGlobal(const sf::Vector2f& Point) const
326{
327 return GetMatrix().Transform(Point);
328}
329
330
335{
336 // First recompute it if needed
337 if (myNeedUpdate)
338 {
339 myMatrix.SetFromTransformations(myCenter, myPosition, myRotation, myScale);
340 myNeedUpdate = false;
341 }
342
343 return myMatrix;
344}
345
346
351{
352 // First recompute it if needed
353 if (myInvNeedUpdate)
354 {
355 myInvMatrix = GetMatrix().GetInverse();
356 myInvNeedUpdate = false;
357 }
358
359 return myInvMatrix;
360}
361
362
366void Drawable::Draw(RenderTarget& Target) const
367{
368 // Save the current modelview matrix and set the new one
369 GLCheck(glMatrixMode(GL_MODELVIEW));
370 GLCheck(glPushMatrix());
371 GLCheck(glMultMatrixf(GetMatrix().Get4x4Elements()));
372
373 // Setup alpha-blending
374 if (myBlendMode == Blend::None)
375 {
376 GLCheck(glDisable(GL_BLEND));
377 }
378 else
379 {
380 GLCheck(glEnable(GL_BLEND));
381
382 switch (myBlendMode)
383 {
384 case Blend::Alpha : GLCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); break;
385 case Blend::Add : GLCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE)); break;
386 case Blend::Multiply : GLCheck(glBlendFunc(GL_DST_COLOR, GL_ZERO)); break;
387 default : break;
388 }
389 }
390
391 // Set color
392 GLCheck(glColor4f(myColor.r / 255.f, myColor.g / 255.f, myColor.b / 255.f, myColor.a / 255.f));
393
394 // Let the derived class render the object geometry
395 Render(Target);
396
397 // Restore the previous modelview matrix
398 GLCheck(glMatrixMode(GL_MODELVIEW));
399 GLCheck(glPopMatrix());
400}
401
402} // namespace sf
Color is an utility class for manipulating 32-bits RGBA colors.
Definition Color.hpp:41
void SetScale(float ScaleX, float ScaleY)
Set the scale of the object (take 2 values).
Definition Drawable.cpp:107
void SetScaleX(float FactorX)
Set the X scale factor of the object.
Definition Drawable.cpp:127
void SetCenter(float CenterX, float CenterY)
Set the center of the object, in coordinates relative to the top-left of the object (take 2 values).
Definition Drawable.cpp:157
const Matrix3 & GetMatrix() const
Get the transform matrix of the drawable.
Definition Drawable.cpp:334
virtual ~Drawable()
Virtual destructor.
Definition Drawable.cpp:56
void Scale(float FactorX, float FactorY)
Scale the object (take 2 values).
Definition Drawable.cpp:287
const Vector2f & GetPosition() const
Get the position of the object.
Definition Drawable.cpp:213
const Vector2f & GetScale() const
Get the current scale of the object.
Definition Drawable.cpp:222
void Rotate(float Angle)
Rotate the object.
Definition Drawable.cpp:306
void SetPosition(float X, float Y)
Set the position of the object (take 2 values).
Definition Drawable.cpp:65
const Matrix3 & GetInverseMatrix() const
Get the inverse transform matrix of the drawable.
Definition Drawable.cpp:350
void Move(float OffsetX, float OffsetY)
Move the object of a given offset (take 2 values).
Definition Drawable.cpp:268
void SetRotation(float Rotation)
Set the orientation of the object.
Definition Drawable.cpp:180
sf::Vector2f TransformToGlobal(const sf::Vector2f &Point) const
Transform a point from local coordinates into global coordinates (ie it applies the object's center,...
Definition Drawable.cpp:325
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))
Default constructor.
Definition Drawable.cpp:39
Blend::Mode GetBlendMode() const
Get the current blending mode.
Definition Drawable.cpp:258
const Color & GetColor() const
Get the color of the object.
Definition Drawable.cpp:249
void SetY(float Y)
Set the Y position of the object.
Definition Drawable.cpp:96
void SetScaleY(float FactorY)
Set the Y scale factor of the object.
Definition Drawable.cpp:141
void SetColor(const Color &Col)
Set the color of the object.
Definition Drawable.cpp:194
void SetBlendMode(Blend::Mode Mode)
Set the blending mode for the object.
Definition Drawable.cpp:204
const Vector2f & GetCenter() const
Get the center of the object.
Definition Drawable.cpp:231
void SetX(float X)
Set the X position of the object.
Definition Drawable.cpp:85
sf::Vector2f TransformToLocal(const sf::Vector2f &Point) const
Transform a point from global coordinates into local coordinates (ie it applies the inverse of object...
Definition Drawable.cpp:316
float GetRotation() const
Get the orientation of the object.
Definition Drawable.cpp:240
Utility class to manipulate 3x3 matrices representing 2D transformations.
Definition Matrix3.hpp:43
Matrix3 GetInverse() const
Return the inverse of the matrix.
Definition Matrix3.hpp:88
Vector2f Transform(const Vector2f &Point) const
Transform a point by the matrix.
Definition Matrix3.hpp:78
Base class for all render targets (window, image, ...).
T x
X coordinate of the vector.
Definition Vector2.hpp:59
T y
Y coordinate of the vector.
Definition Vector2.hpp:60
Enumerate the blending modes for drawable objects.
Definition Drawable.hpp:44
@ Alpha
Pixel = Src * a + Dest * (1 - a).
Definition Drawable.hpp:47
@ None
No blending.
Definition Drawable.hpp:50
@ Multiply
Pixel = Src * Dest.
Definition Drawable.hpp:49
@ Add
Pixel = Src + Dest.
Definition Drawable.hpp:48