00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
00026
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
00057 if (Image == NULL)
00058 return;
00059
00060
00061 if ((SubRect.GetWidth() == 0) || (SubRect.GetHeight() == 0))
00062 SubRect = sfIntRect(0, 0, Image->GetWidth(), Image->GetHeight());
00063
00064
00065 const float HalfWidth = GetWidth() / 2;
00066 const float HalfHeight = GetHeight() / 2;
00067
00068
00069 sfFloatRect TexCoords = Image->GetTexCoords(SubRect);
00070
00071
00072 sfGLCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
00073
00074
00075 sfGLCheck(glColor4ub(Color.Red, Color.Green, Color.Blue, Color.Alpha));
00076
00077
00078 sfGLCheck(glTranslatef(Left + HalfWidth, Top + HalfHeight, 0));
00079 sfGLCheck(glRotatef(-Rotation, 0, 0, 1));
00080
00081
00082 Image->Bind();
00083
00084
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 }