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
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
00062 myImage = &Image;
00063
00064
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
00130 if (myImage)
00131 {
00132
00133 float Width = static_cast<float>(mySubRect.GetWidth());
00134 float Height = static_cast<float>(mySubRect.GetHeight());
00135
00136
00137 sfFloatRect TexCoords = myImage->GetTexCoords(mySubRect);
00138
00139
00140 myImage->Bind();
00141
00142
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 }