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 
00035 sfDrawable::sfDrawable(float Left, float Top, float Scale, float Rotation, const sfColor& Color) :
00036 myLeft    (Left),
00037 myTop     (Top),
00038 myScale   (Scale),
00039 myRotation(Rotation),
00040 myCenterX (0.f),
00041 myCenterY (0.f),
00042 myColor   (Color)
00043 {
00044 
00045 }
00046 
00047 
00051 sfDrawable::~sfDrawable()
00052 {
00053     // Nothing to do
00054 }
00055 
00056 
00060 void sfDrawable::SetLeft(float Left)
00061 {
00062     myLeft = Left;
00063 }
00064 
00065 
00069 void sfDrawable::SetTop(float Top)
00070 {
00071     myTop = Top;
00072 }
00073 
00074 
00078 void sfDrawable::SetScale(float Scale)
00079 {
00080     if (Scale > 0.f)
00081         myScale = Scale;
00082 }
00083 
00084 
00088 void sfDrawable::SetRotation(float Rotation)
00089 {
00090     myRotation = Rotation;
00091 }
00092 
00093 
00098 void sfDrawable::SetRotationCenter(float X, float Y)
00099 {
00100     myCenterX = X;
00101     myCenterY = Y;
00102 }
00103 
00104 
00108 void sfDrawable::SetColor(const sfColor& Color)
00109 {
00110     myColor = Color;
00111 }
00112 
00113 
00117 float sfDrawable::GetLeft() const
00118 {
00119     return myLeft;
00120 }
00121 
00122 
00126 float sfDrawable::GetTop() const
00127 {
00128     return myTop;
00129 }
00130 
00131 
00135 float sfDrawable::GetScale() const
00136 {
00137     return myScale;
00138 }
00139 
00140 
00144 float sfDrawable::GetRotation() const
00145 {
00146     return myRotation;
00147 }
00148 
00149 
00153 const sfColor& sfDrawable::GetColor() const
00154 {
00155     return myColor;
00156 }
00157 
00158 
00162 void sfDrawable::Move(float OffsetX, float OffsetY)
00163 {
00164     myLeft += OffsetX;
00165     myTop  += OffsetY;
00166 }
00167 
00168 
00172 void sfDrawable::Scale(float Factor)
00173 {
00174     if (Factor > 0.f)
00175         myScale *= Factor;
00176 }
00177 
00178 
00182 void sfDrawable::Rotate(float Angle)
00183 {
00184     myRotation += Angle;
00185 }
00186 
00187 
00191 void sfDrawable::Draw(sfRenderWindow& Window)
00192 {
00193     // Reset transformations
00194     sfGLCheck(glMatrixMode(GL_MODELVIEW));
00195     sfGLCheck(glLoadIdentity());
00196 
00197     // Setup alpha-blending
00198     sfGLCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
00199 
00200     // Set color
00201     sfGLCheck(glColor4ub(myColor.r, myColor.g, myColor.b, myColor.a));
00202 
00203     // Set transformations
00204     sfGLCheck(glTranslatef(myLeft + myCenterX, myTop + myCenterY, 0));
00205     sfGLCheck(glRotatef(-myRotation, 0, 0, 1));
00206     sfGLCheck(glTranslatef(-myCenterX, -myCenterY, 0));
00207     sfGLCheck(glScalef(myScale, myScale, 1));
00208 
00209     // Let the derived class render the object geometry
00210     Render(Window);
00211 }