Sprite.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/Sprite.hpp>
00029 #include <SFML/Graphics/Image.hpp>
00030 #include <SFML/Graphics/OpenGL.hpp>
00031 
00032 
00033 namespace sf
00034 {
00038 Sprite::Sprite() :
00039 myImage  (NULL),
00040 mySubRect(0, 0, 0, 0)
00041 {
00042 
00043 }
00044 
00045 
00049 Sprite::Sprite(const Image& Img, float Left, float Top, float ScaleX, float ScaleY, float Rotation, const Color& Col) :
00050 Drawable (Left, Top, ScaleX, ScaleY, Rotation, Col),
00051 myImage  (&Img),
00052 mySubRect(0, 0, Img.GetWidth(), Img.GetHeight())
00053 {
00054 
00055 }
00056 
00057 
00061 void Sprite::SetImage(const Image& Img)
00062 {
00063     // Assign the new image
00064     myImage = &Img;
00065 
00066     // If the sub-rectangle was null, we define it as the whole image
00067     if ((mySubRect.GetWidth() == 0) || (mySubRect.GetHeight() == 0))
00068         SetSubRect(IntRect(0, 0, Img.GetWidth(), Img.GetHeight()));
00069 }
00070 
00071 
00075 void Sprite::SetSubRect(const IntRect& SubRect)
00076 {
00077     mySubRect = SubRect;
00078 }
00079 
00080 
00084 void Sprite::Resize(float Width, float Height)
00085 {
00086     if ((mySubRect.GetWidth() > 0) && (mySubRect.GetHeight() > 0))
00087     {
00088         float ScaleX = (Width  > 0.f ? Width  / mySubRect.GetWidth()  : GetScaleX());
00089         float ScaleY = (Height > 0.f ? Height / mySubRect.GetHeight() : GetScaleY());
00090 
00091         SetScale(ScaleX, ScaleY);
00092     }
00093 }
00094 
00095 
00099 const Image* Sprite::GetImage() const
00100 {
00101     return myImage;
00102 }
00103 
00104 
00108 const IntRect& Sprite::GetSubRect() const
00109 {
00110     return mySubRect;
00111 }
00112 
00113 
00117 float Sprite::GetWidth() const
00118 {
00119     return mySubRect.GetWidth() * GetScaleX();
00120 }
00121 
00122 
00126 float Sprite::GetHeight() const
00127 {
00128     return mySubRect.GetHeight() * GetScaleY();
00129 }
00130 
00131 
00135 Color Sprite::GetPixel(unsigned int X, unsigned int Y) const
00136 {
00137     return myImage ? myImage->GetPixel(X + mySubRect.Left, Y + mySubRect.Top) : Color(0, 0, 0, 0);
00138 }
00139 
00140 
00144 void Sprite::Render(RenderWindow&)
00145 {
00146     // Get the sprite size
00147     float Width  = static_cast<float>(mySubRect.GetWidth());
00148     float Height = static_cast<float>(mySubRect.GetHeight());
00149 
00150     // Check if the image is valid
00151     if (myImage)
00152     {
00153         // Calculate the texture coordinates
00154         FloatRect TexCoords = myImage->GetTexCoords(mySubRect);
00155 
00156         // Set the texture
00157         myImage->Bind();
00158 
00159         // Draw the textured sprite
00160         glBegin(GL_QUADS);
00161             glTexCoord2f(TexCoords.Left,  TexCoords.Top);    glVertex2f(0,     0);
00162             glTexCoord2f(TexCoords.Left,  TexCoords.Bottom); glVertex2f(0,     Height);
00163             glTexCoord2f(TexCoords.Right, TexCoords.Bottom); glVertex2f(Width, Height);
00164             glTexCoord2f(TexCoords.Right, TexCoords.Top);    glVertex2f(Width, 0);
00165         glEnd();
00166     }
00167     else
00168     {
00169         // Disable texturing
00170         glDisable(GL_TEXTURE_2D);
00171 
00172         // Draw the colored rectangle
00173         glBegin(GL_QUADS);
00174             glVertex2f(0,     0);
00175             glVertex2f(0,     Height);
00176             glVertex2f(Width, Height);
00177             glVertex2f(Width, 0);
00178         glEnd();
00179     }
00180 }
00181 
00182 } // namespace sf