D:/Programmation/Cpp/SFML/src/SFML/Graphics/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 #include <SFML/Graphics/RenderWindow.hpp>
00032 #include <cmath>
00033 
00034 
00038 sfSprite::sfSprite(const sfImage* Img, const sfColor& Col, float X, float Y, float Rot, float Sc) :
00039 Image   (Img),
00040 Color   (Col),
00041 Left    (X),
00042 Top     (Y),
00043 Rotation(Rot),
00044 Scale   (Sc),
00045 SubRect (0, 0, Img ? Img->GetWidth() : 0, Img ? Img->GetHeight() : 0)
00046 {
00047 
00048 }
00049 
00050 
00054 void sfSprite::Render(sfRenderWindow& Window)
00055 {
00056     // Check image is valid
00057     if (Image == NULL)
00058         return;
00059 
00060     // Ensure subrect is valid
00061     if ((SubRect.GetWidth() == 0) || (SubRect.GetHeight() == 0))
00062         SubRect = sfIntRect(0, 0, Image->GetWidth(), Image->GetHeight());
00063 
00064     // Calculate vertex coordinates
00065     const float HalfWidth  = GetWidth()  / 2;
00066     const float HalfHeight = GetHeight() / 2;
00067 
00068     // Calculate texture coordinates
00069     sfFloatRect TexCoords = Image->GetTexCoords(SubRect);
00070 
00071     // Setup alpha-blending
00072     sfGLCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
00073 
00074     // Set color
00075     sfGLCheck(glColor4ub(Color.Red, Color.Green, Color.Blue, Color.Alpha));
00076 
00077     // Set transformations
00078     sfGLCheck(glTranslatef(Left + HalfWidth, Top + HalfHeight, 0));
00079     sfGLCheck(glRotatef(-Rotation, 0, 0, 1));
00080 
00081     // Set texture
00082     Image->Bind();
00083 
00084     // Draw the sprite
00085     glBegin(GL_QUADS);
00086         glTexCoord2f(TexCoords.Left,  TexCoords.Top);    glVertex2f(-HalfWidth, -HalfHeight);
00087         glTexCoord2f(TexCoords.Left,  TexCoords.Bottom); glVertex2f(-HalfWidth,  HalfHeight);
00088         glTexCoord2f(TexCoords.Right, TexCoords.Bottom); glVertex2f( HalfWidth,  HalfHeight);
00089         glTexCoord2f(TexCoords.Right, TexCoords.Top);    glVertex2f( HalfWidth, -HalfHeight);
00090     glEnd();
00091 }
00092 
00093 
00097 float sfSprite::GetWidth() const
00098 {
00099     return SubRect.GetWidth() * Scale;
00100 }
00101 
00102 
00106 float sfSprite::GetHeight() const
00107 {
00108     return SubRect.GetHeight() * Scale;
00109 }

Generated for SFML by  doxygen 1.5.2