00001 00002 // 00003 // SFML - Simple and Fast Multimedia Library 00004 // Copyright (C) 2007-2009 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/Renderer.hpp> 00031 #include <utility> 00032 00033 00034 namespace sf 00035 { 00039 Sprite::Sprite() : 00040 mySubRect (0, 0, 1, 1), 00041 myIsFlippedX(false), 00042 myIsFlippedY(false) 00043 { 00044 00045 } 00046 00047 00051 Sprite::Sprite(const Image& image, const Vector2f& position, const Vector2f& scale, float rotation, const Color& color) : 00052 Drawable (position, scale, rotation, color), 00053 mySubRect (0, 0, 1, 1), 00054 myIsFlippedX(false), 00055 myIsFlippedY(false) 00056 { 00057 SetImage(image); 00058 } 00059 00060 00064 void Sprite::SetImage(const Image& image, bool adjustToNewSize) 00065 { 00066 // If there was no valid image before, force adjusting to the new image size 00067 if (!myImage) 00068 adjustToNewSize = true; 00069 00070 // If we want to adjust the size and the new image is valid, we adjust the source rectangle 00071 if (adjustToNewSize && (image.GetWidth() > 0) && (image.GetHeight() > 0)) 00072 { 00073 SetSubRect(IntRect(0, 0, image.GetWidth(), image.GetHeight())); 00074 } 00075 00076 // Assign the new image 00077 myImage = ℑ 00078 } 00079 00080 00084 void Sprite::SetSubRect(const IntRect& rectangle) 00085 { 00086 mySubRect = rectangle; 00087 } 00088 00089 00094 void Sprite::Resize(float width, float height) 00095 { 00096 int localWidth = mySubRect.GetSize().x; 00097 int localHeight = mySubRect.GetSize().y; 00098 00099 if ((localWidth > 0) && (localHeight > 0)) 00100 SetScale(width / localWidth, height / localHeight); 00101 } 00102 00103 00108 void Sprite::Resize(const Vector2f& size) 00109 { 00110 Resize(size.x, size.y); 00111 } 00112 00113 00117 void Sprite::FlipX(bool flipped) 00118 { 00119 myIsFlippedX = flipped; 00120 } 00121 00122 00126 void Sprite::FlipY(bool flipped) 00127 { 00128 myIsFlippedY = flipped; 00129 } 00130 00131 00135 const Image* Sprite::GetImage() const 00136 { 00137 return myImage; 00138 } 00139 00140 00144 const IntRect& Sprite::GetSubRect() const 00145 { 00146 return mySubRect; 00147 } 00148 00149 00153 Vector2f Sprite::GetSize() const 00154 { 00155 return Vector2f(mySubRect.GetSize().x * GetScale().x, mySubRect.GetSize().y * GetScale().y); 00156 } 00157 00158 00163 Color Sprite::GetPixel(unsigned int x, unsigned int y) const 00164 { 00165 if (myImage) 00166 { 00167 unsigned int imageX = mySubRect.Left + x; 00168 unsigned int imageY = mySubRect.Top + y; 00169 00170 if (myIsFlippedX) imageX = mySubRect.GetSize().x - imageX - 1; 00171 if (myIsFlippedY) imageY = mySubRect.GetSize().y - imageY - 1; 00172 00173 return myImage->GetPixel(imageX, imageY) * GetColor(); 00174 } 00175 else 00176 { 00177 return GetColor(); 00178 } 00179 } 00180 00181 00185 void Sprite::Render(RenderTarget&, Renderer& renderer) const 00186 { 00187 // Get the sprite size 00188 float width = static_cast<float>(mySubRect.GetSize().x); 00189 float height = static_cast<float>(mySubRect.GetSize().y); 00190 00191 // Check if the image is valid, and calculate the texture coordinates 00192 FloatRect coords; 00193 if (myImage) 00194 { 00195 coords = myImage->GetTexCoords(mySubRect); 00196 if (myIsFlippedX) std::swap(coords.Left, coords.Right); 00197 if (myIsFlippedY) std::swap(coords.Top, coords.Bottom); 00198 } 00199 00200 // Bind the texture 00201 renderer.SetTexture(myImage); 00202 00203 // Draw the sprite's geometry 00204 renderer.Begin(Renderer::TriangleStrip); 00205 renderer.AddVertex(0, 0, coords.Left, coords.Top); 00206 renderer.AddVertex(width, 0, coords.Right, coords.Top); 00207 renderer.AddVertex(0, height, coords.Left, coords.Bottom); 00208 renderer.AddVertex(width, height, coords.Right, coords.Bottom); 00209 renderer.End(); 00210 } 00211 00212 } // namespace sf
:: Copyright © 2007-2008 Laurent Gomila, all rights reserved :: Documentation generated by doxygen 1.5.2 ::