Sprite.cpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
26// Headers
28#include <SFML/Graphics/Sprite.hpp>
29#include <SFML/Graphics/Image.hpp>
30#include <SFML/Graphics/GraphicsContext.hpp>
31
32
33namespace sf
34{
39mySubRect (0, 0, 1, 1),
40myIsFlippedX(false),
41myIsFlippedY(false)
42{
43
44}
45
46
50Sprite::Sprite(const Image& Img, const Vector2f& Position, const Vector2f& Scale, float Rotation, const Color& Col) :
51Drawable (Position, Scale, Rotation, Col),
52mySubRect (0, 0, 1, 1),
53myIsFlippedX(false),
54myIsFlippedY(false)
55{
56 SetImage(Img);
57}
58
59
63void Sprite::SetImage(const Image& Img)
64{
65 // If there was no source image before and the new image is valid, adjust the source rectangle
66 if (!myImage && (Img.GetWidth() > 0) && (Img.GetHeight() > 0))
67 {
68 SetSubRect(IntRect(0, 0, Img.GetWidth(), Img.GetHeight()));
69 }
70
71 // Assign the new image
72 myImage = &Img;
73}
74
75
79void Sprite::SetSubRect(const IntRect& SubRect)
80{
81 mySubRect = SubRect;
82}
83
84
89void Sprite::Resize(float Width, float Height)
90{
91 int LocalWidth = mySubRect.GetWidth();
92 int LocalHeight = mySubRect.GetHeight();
93
94 if ((LocalWidth > 0) && (LocalHeight > 0))
95 SetScale(Width / LocalWidth, Height / LocalHeight);
96}
97
98
103void Sprite::Resize(const Vector2f& Size)
104{
105 Resize(Size.x, Size.y);
106}
107
108
112void Sprite::FlipX(bool Flipped)
113{
114 myIsFlippedX = Flipped;
115}
116
117
121void Sprite::FlipY(bool Flipped)
122{
123 myIsFlippedY = Flipped;
124}
125
126
131{
132 return myImage;
133}
134
135
139const IntRect& Sprite::GetSubRect() const
140{
141 return mySubRect;
142}
143
144
148Vector2f Sprite::GetSize() const
149{
150 return Vector2f(mySubRect.GetWidth() * GetScale().x, mySubRect.GetHeight() * GetScale().y);
151}
152
153
158Color Sprite::GetPixel(unsigned int X, unsigned int Y) const
159{
160 if (myImage)
161 {
162 unsigned int ImageX = mySubRect.Left + X;
163 unsigned int ImageY = mySubRect.Top + Y;
164
165 if (myIsFlippedX) ImageX = mySubRect.GetWidth() - ImageX - 1;
166 if (myIsFlippedY) ImageY = mySubRect.GetHeight() - ImageY - 1;
167
168 return myImage->GetPixel(ImageX, ImageY) * GetColor();
169 }
170 else
171 {
172 return GetColor();
173 }
174}
175
176
180void Sprite::Render(RenderTarget&) const
181{
182 // Get the sprite size
183 float Width = static_cast<float>(mySubRect.GetWidth());
184 float Height = static_cast<float>(mySubRect.GetHeight());
185
186 // Check if the image is valid
187 if (myImage && (myImage->GetWidth() > 0) && (myImage->GetHeight() > 0))
188 {
189 // Use the "offset trick" to get pixel-perfect rendering
190 // see http://www.opengl.org/resources/faq/technical/transformations.htm#tran0030
191 GLCheck(glTranslatef(0.375f, 0.375f, 0.f));
192
193 // Bind the texture
194 myImage->Bind();
195
196 // Calculate the texture coordinates
197 FloatRect TexCoords = myImage->GetTexCoords(mySubRect);
198 FloatRect Rect(myIsFlippedX ? TexCoords.Right : TexCoords.Left,
199 myIsFlippedY ? TexCoords.Bottom : TexCoords.Top,
200 myIsFlippedX ? TexCoords.Left : TexCoords.Right,
201 myIsFlippedY ? TexCoords.Top : TexCoords.Bottom);
202
203 // Draw the sprite's triangles
204 glBegin(GL_QUADS);
205 glTexCoord2f(Rect.Left, Rect.Top); glVertex2f(0, 0);
206 glTexCoord2f(Rect.Left, Rect.Bottom); glVertex2f(0, Height);
207 glTexCoord2f(Rect.Right, Rect.Bottom); glVertex2f(Width, Height);
208 glTexCoord2f(Rect.Right, Rect.Top); glVertex2f(Width, 0) ;
209 glEnd();
210 }
211 else
212 {
213 // Disable texturing
214 GLCheck(glDisable(GL_TEXTURE_2D));
215
216 // Draw the sprite's triangles
217 glBegin(GL_QUADS);
218 glVertex2f(0, 0);
219 glVertex2f(0, Height);
220 glVertex2f(Width, Height);
221 glVertex2f(Width, 0);
222 glEnd();
223 }
224}
225
226} // namespace sf
Color is an utility class for manipulating 32-bits RGBA colors.
Definition Color.hpp:41
void SetScale(float ScaleX, float ScaleY)
Set the scale of the object (take 2 values).
Definition Drawable.cpp:107
void Scale(float FactorX, float FactorY)
Scale the object (take 2 values).
Definition Drawable.cpp:287
const Vector2f & GetScale() const
Get the current scale of the object.
Definition Drawable.cpp:222
Drawable(const Vector2f &Position=Vector2f(0, 0), const Vector2f &Scale=Vector2f(1, 1), float Rotation=0.f, const Color &Col=Color(255, 255, 255, 255))
Default constructor.
Definition Drawable.cpp:39
const Color & GetColor() const
Get the color of the object.
Definition Drawable.cpp:249
Image is the low-level class for loading and manipulating images.
Definition Image.hpp:47
unsigned int GetHeight() const
Return the height of the image.
Definition Image.cpp:526
unsigned int GetWidth() const
Return the width of the image.
Definition Image.cpp:517
Rect is an utility class for manipulating rectangles.
Definition Rect.hpp:42
T GetWidth() const
Get the width of the rectangle.
Definition Rect.hpp:59
T Top
Top coordinate of the rectangle.
Definition Rect.hpp:113
T Right
Right coordinate of the rectangle.
Definition Rect.hpp:114
T Left
Left coordinate of the rectangle.
Definition Rect.hpp:112
T Bottom
Bottom coordinate of the rectangle.
Definition Rect.hpp:115
Color GetPixel(unsigned int X, unsigned int Y) const
Get the color of a given pixel in the sprite (point is in local coordinates).
Definition Sprite.cpp:158
void FlipX(bool Flipped)
Flip the sprite horizontally.
Definition Sprite.cpp:112
void SetImage(const Image &Img)
Change the image of the sprite.
Definition Sprite.cpp:63
void Resize(float Width, float Height)
Resize the sprite (by changing its scale factors) (take 2 values).
Definition Sprite.cpp:89
void SetSubRect(const IntRect &SubRect)
Set the sub-rectangle of the sprite inside the source image.
Definition Sprite.cpp:79
const Image * GetImage() const
Get the source image of the sprite.
Definition Sprite.cpp:130
virtual void Render(RenderTarget &Target) const
/see Drawable::Render
Definition Sprite.cpp:180
const IntRect & GetSubRect() const
Get the sub-rectangle of the sprite inside the source image.
Definition Sprite.cpp:139
void FlipY(bool Flipped)
Flip the sprite vertically.
Definition Sprite.cpp:121
Sprite()
Default constructor.
Definition Sprite.cpp:38
Vector2f GetSize() const
Get the sprite size.
Definition Sprite.cpp:148
T x
X coordinate of the vector.
Definition Vector2.hpp:59
T y
Y coordinate of the vector.
Definition Vector2.hpp:60