SFML logo
  • Main Page
  • Namespaces
  • Classes
  • Files
  • File List

Drawable.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/Graphics/Drawable.hpp>
00029 #include <SFML/Graphics/Renderer.hpp>
00030 #include <SFML/Graphics/RenderTarget.hpp>
00031 #include <math.h>
00032 
00033 
00034 namespace sf
00035 {
00039 Drawable::Drawable(const Vector2f& position, const Vector2f& scale, float rotation, const Color& color) :
00040 myPosition        (position),
00041 myScale           (scale),
00042 myOrigin          (0, 0),
00043 myRotation        (rotation),
00044 myColor           (color),
00045 myBlendMode       (Blend::Alpha),
00046 myMatrixUpdated   (false),
00047 myInvMatrixUpdated(false)
00048 {
00049 }
00050 
00051 
00055 Drawable::~Drawable()
00056 {
00057     // Nothing to do
00058 }
00059 
00060 
00064 void Drawable::SetPosition(float x, float y)
00065 {
00066     SetX(x);
00067     SetY(y);
00068 }
00069 
00070 
00074 void Drawable::SetPosition(const Vector2f& position)
00075 {
00076     SetX(position.x);
00077     SetY(position.y);
00078 }
00079 
00080 
00084 void Drawable::SetX(float x)
00085 {
00086     myPosition.x = x;
00087 
00088     myMatrixUpdated    = false;
00089     myInvMatrixUpdated = false;
00090 }
00091 
00092 
00096 void Drawable::SetY(float y)
00097 {
00098     myPosition.y = y;
00099 
00100     myMatrixUpdated    = false;
00101     myInvMatrixUpdated = false;
00102 }
00103 
00104 
00108 void Drawable::SetScale(float factorX, float factorY)
00109 {
00110     SetScaleX(factorX);
00111     SetScaleY(factorY);
00112 }
00113 
00114 
00118 void Drawable::SetScale(const Vector2f& scale)
00119 {
00120     SetScaleX(scale.x);
00121     SetScaleY(scale.y);
00122 }
00123 
00124 
00128 void Drawable::SetScaleX(float factor)
00129 {
00130     if (factor > 0)
00131     {
00132         myScale.x = factor;
00133 
00134         myMatrixUpdated    = false;
00135         myInvMatrixUpdated = false;
00136     }
00137 }
00138 
00139 
00143 void Drawable::SetScaleY(float factor)
00144 {
00145     if (factor > 0)
00146     {
00147         myScale.y = factor;
00148 
00149         myMatrixUpdated    = false;
00150         myInvMatrixUpdated = false;
00151     }
00152 }
00153 
00154 
00160 void Drawable::SetOrigin(float x, float y)
00161 {
00162     myOrigin.x = x;
00163     myOrigin.y = y;
00164 
00165     myMatrixUpdated    = false;
00166     myInvMatrixUpdated = false;
00167 }
00168 
00169 
00175 void Drawable::SetOrigin(const Vector2f& origin)
00176 {
00177     SetOrigin(origin.x, origin.y);
00178 }
00179 
00180 
00184 void Drawable::SetRotation(float angle)
00185 {
00186     myRotation = static_cast<float>(fmod(angle, 360));
00187     if (myRotation < 0)
00188         myRotation += 360.f;
00189 
00190     myMatrixUpdated    = false;
00191     myInvMatrixUpdated = false;
00192 }
00193 
00194 
00199 void Drawable::SetColor(const Color& color)
00200 {
00201     myColor = color;
00202 }
00203 
00204 
00209 void Drawable::SetBlendMode(Blend::Mode mode)
00210 {
00211     myBlendMode = mode;
00212 }
00213 
00214 
00218 const Vector2f& Drawable::GetPosition() const
00219 {
00220     return myPosition;
00221 }
00222 
00223 
00227 const Vector2f& Drawable::GetScale() const
00228 {
00229     return myScale;
00230 }
00231 
00232 
00236 const Vector2f& Drawable::GetOrigin() const
00237 {
00238     return myOrigin;
00239 }
00240 
00241 
00245 float Drawable::GetRotation() const
00246 {
00247     return myRotation;
00248 }
00249 
00250 
00254 const Color& Drawable::GetColor() const
00255 {
00256     return myColor;
00257 }
00258 
00259 
00263 Blend::Mode Drawable::GetBlendMode() const
00264 {
00265     return myBlendMode;
00266 }
00267 
00268 
00273 void Drawable::Move(float offsetX, float offsetY)
00274 {
00275     SetPosition(myPosition.x + offsetX, myPosition.y + offsetY);
00276 }
00277 
00278 
00282 void Drawable::Move(const Vector2f& offset)
00283 {
00284     SetPosition(myPosition + offset);
00285 }
00286 
00287 
00291 void Drawable::Scale(float factorX, float factorY)
00292 {
00293     SetScale(myScale.x * factorX, myScale.y * factorY);
00294 }
00295 
00296 
00300 void Drawable::Scale(const Vector2f& factor)
00301 {
00302     SetScale(myScale.x * factor.x, myScale.y * factor.y);
00303 }
00304 
00305 
00309 void Drawable::Rotate(float angle)
00310 {
00311     SetRotation(myRotation + angle);
00312 }
00313 
00314 
00319 Vector2f Drawable::TransformToLocal(const Vector2f& point) const
00320 {
00321     return GetInverseMatrix().Transform(point);
00322 }
00323 
00328 Vector2f Drawable::TransformToGlobal(const Vector2f& point) const
00329 {
00330     return GetMatrix().Transform(point);
00331 }
00332 
00333 
00337 const Matrix3& Drawable::GetMatrix() const
00338 {
00339     // First recompute it if needed
00340     if (!myMatrixUpdated)
00341     {
00342         myMatrix.SetFromTransformations(myOrigin, myPosition, myRotation, myScale);
00343         myMatrixUpdated = true;
00344     }
00345 
00346     return myMatrix;
00347 }
00348 
00349 
00353 const Matrix3& Drawable::GetInverseMatrix() const
00354 {
00355     // First recompute it if needed
00356     if (!myInvMatrixUpdated)
00357     {
00358         myInvMatrix = GetMatrix().GetInverse();
00359         myInvMatrixUpdated = true;
00360     }
00361 
00362     return myInvMatrix;
00363 }
00364 
00365 
00369 void Drawable::Draw(RenderTarget& target, Renderer& renderer) const
00370 {
00371     // Set the current model-view matrix
00372     renderer.ApplyModelView(GetMatrix());
00373 
00374     // Set the current global color
00375     renderer.ApplyColor(myColor);
00376 
00377     // Set the current alpha-blending mode
00378     renderer.SetBlendMode(myBlendMode);
00379 
00380     // Let the derived class render the object geometry
00381     Render(target, renderer);
00382 }
00383 
00384 } // namespace sf

 ::  Copyright © 2007-2008 Laurent Gomila, all rights reserved  ::  Documentation generated by doxygen 1.5.2  ::