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 
00036 sfSprite::sfSprite() :
00037 myImage  (NULL),
00038 mySubRect(0, 0, 0, 0)
00039 {
00040 
00041 }
00042 
00043 
00047 sfSprite::sfSprite(const sfImage& Image, float Left, float Top, float Scale, float Rotation, const sfColor& Color) :
00048 sfDrawable(Left, Top, Scale, Rotation, Color),
00049 myImage   (&Image),
00050 mySubRect (0, 0, Image.GetWidth(), Image.GetHeight())
00051 {
00052 
00053 }
00054 
00055 
00059 void sfSprite::SetImage(const sfImage& Image)
00060 {
00061     // Assign the new image
00062     myImage = &Image;
00063 
00064     // If the sub-rectangle was null, we define it as the whole image
00065     if ((mySubRect.GetWidth() == 0) || (mySubRect.GetHeight() == 0))
00066         SetSubRect(sfIntRect(0, 0, Image.GetWidth(), Image.GetHeight()));
00067 }
00068 
00069 
00073 void sfSprite::SetSubRect(const sfIntRect& SubRect)
00074 {
00075     mySubRect = SubRect;
00076 }
00077 
00078 
00082 const sfImage* sfSprite::GetImage() const
00083 {
00084     return myImage;
00085 }
00086 
00087 
00091 const sfIntRect& sfSprite::GetSubRect() const
00092 {
00093     return mySubRect;
00094 }
00095 
00096 
00100 float sfSprite::GetWidth() const
00101 {
00102     return mySubRect.GetWidth() * GetScale();
00103 }
00104 
00105 
00109 float sfSprite::GetHeight() const
00110 {
00111     return mySubRect.GetHeight() * GetScale();
00112 }
00113 
00114 
00118 sfColor sfSprite::GetPixel(unsigned int X, unsigned int Y) const
00119 {
00120     return myImage ? myImage->GetPixel(X + mySubRect.Left, Y + mySubRect.Top) : sfColor(0, 0, 0, 0);
00121 }
00122 
00123 
00127 void sfSprite::Render(sfRenderWindow&)
00128 {
00129     // Check if the image is valid
00130     if (myImage)
00131     {
00132         // Get the sprite size
00133         float Width  = static_cast<float>(mySubRect.GetWidth());
00134         float Height = static_cast<float>(mySubRect.GetHeight());
00135 
00136         // Calculate the texture coordinates
00137         sfFloatRect TexCoords = myImage->GetTexCoords(mySubRect);
00138 
00139         // Set the texture
00140         myImage->Bind();
00141 
00142         // Draw the sprite
00143         glBegin(GL_QUADS);
00144             glTexCoord2f(TexCoords.Left,  TexCoords.Top);    glVertex2f(0,     0);
00145             glTexCoord2f(TexCoords.Left,  TexCoords.Bottom); glVertex2f(0,     Height);
00146             glTexCoord2f(TexCoords.Right, TexCoords.Bottom); glVertex2f(Width, Height);
00147             glTexCoord2f(TexCoords.Right, TexCoords.Top);    glVertex2f(Width, 0);
00148         glEnd();
00149     }
00150 }