Drawable.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 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/OpenGL.hpp>
00030 
00031 
00032 namespace sf
00033 {
00037 Drawable::Drawable(float Left, float Top, float ScaleX, float ScaleY, float Rotation, const Color& Col) :
00038 myLeft    (Left),
00039 myTop     (Top),
00040 myScaleX  (ScaleX),
00041 myScaleY  (ScaleY),
00042 myRotation(Rotation),
00043 myCenterX (0.f),
00044 myCenterY (0.f),
00045 myColor   (Col)
00046 {
00047 
00048 }
00049 
00050 
00054 Drawable::~Drawable()
00055 {
00056     // Nothing to do
00057 }
00058 
00059 
00063 void Drawable::SetLeft(float Left)
00064 {
00065     myLeft = Left;
00066 }
00067 
00068 
00072 void Drawable::SetTop(float Top)
00073 {
00074     myTop = Top;
00075 }
00076 
00077 
00081 void Drawable::SetScale(float ScaleX, float ScaleY)
00082 {
00083     if (ScaleX > 0.f) myScaleX = ScaleX;
00084     if (ScaleY > 0.f) myScaleY = ScaleY;
00085 }
00086 
00087 
00091 void Drawable::SetRotation(float Rotation)
00092 {
00093     myRotation = Rotation;
00094 }
00095 
00096 
00101 void Drawable::SetRotationCenter(float X, float Y)
00102 {
00103     myCenterX = X;
00104     myCenterY = Y;
00105 }
00106 
00107 
00111 void Drawable::SetColor(const Color& Col)
00112 {
00113     myColor = Col;
00114 }
00115 
00116 
00120 float Drawable::GetLeft() const
00121 {
00122     return myLeft;
00123 }
00124 
00125 
00129 float Drawable::GetTop() const
00130 {
00131     return myTop;
00132 }
00133 
00134 
00138 float Drawable::GetScaleX() const
00139 {
00140     return myScaleX;
00141 }
00142 
00143 
00147 float Drawable::GetScaleY() const
00148 {
00149     return myScaleY;
00150 }
00151 
00152 
00156 float Drawable::GetRotation() const
00157 {
00158     return myRotation;
00159 }
00160 
00161 
00165 const Color& Drawable::GetColor() const
00166 {
00167     return myColor;
00168 }
00169 
00170 
00174 void Drawable::Move(float OffsetX, float OffsetY)
00175 {
00176     myLeft += OffsetX;
00177     myTop  += OffsetY;
00178 }
00179 
00180 
00184 void Drawable::Scale(float FactorX, float FactorY)
00185 {
00186     if (FactorX > 0.f) myScaleX *= FactorX;
00187     if (FactorY > 0.f) myScaleY *= FactorY;
00188 }
00189 
00190 
00194 void Drawable::Rotate(float Angle)
00195 {
00196     myRotation += Angle;
00197 }
00198 
00199 
00203 void Drawable::Draw(RenderWindow& Window)
00204 {
00205     // Save the current modelview matrix
00206     GLCheck(glMatrixMode(GL_MODELVIEW));
00207     GLCheck(glPushMatrix());
00208 
00209     // Setup alpha-blending
00210     GLCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
00211 
00212     // Set color
00213     GLCheck(glColor4ub(myColor.r, myColor.g, myColor.b, myColor.a));
00214 
00215     // Set transformations
00216     GLCheck(glTranslatef(myLeft + myCenterX, myTop + myCenterY, 0));
00217     GLCheck(glRotatef(-myRotation, 0, 0, 1));
00218     GLCheck(glTranslatef(-myCenterX, -myCenterY, 0));
00219     GLCheck(glScalef(myScaleX, myScaleY, 1));
00220 
00221     // Let the derived class render the object geometry
00222     Render(Window);
00223 
00224     // Restore the previous modelview matrix
00225     GLCheck(glPopMatrix());
00226 }
00227 
00228 } // namespace sf