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
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
00064 myImage = &Img;
00065
00066
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
00147 float Width = static_cast<float>(mySubRect.GetWidth());
00148 float Height = static_cast<float>(mySubRect.GetHeight());
00149
00150
00151 if (myImage)
00152 {
00153
00154 FloatRect TexCoords = myImage->GetTexCoords(mySubRect);
00155
00156
00157 myImage->Bind();
00158
00159
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
00170 glDisable(GL_TEXTURE_2D);
00171
00172
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 }