Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:14

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_SPRITE_HPP
0026 #define SFML_SPRITE_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Graphics/Export.hpp>
0032 #include <SFML/Graphics/Drawable.hpp>
0033 #include <SFML/Graphics/Transformable.hpp>
0034 #include <SFML/Graphics/Vertex.hpp>
0035 #include <SFML/Graphics/Rect.hpp>
0036 
0037 
0038 namespace sf
0039 {
0040 class Texture;
0041 
0042 ////////////////////////////////////////////////////////////
0043 /// \brief Drawable representation of a texture, with its
0044 ///        own transformations, color, etc.
0045 ///
0046 ////////////////////////////////////////////////////////////
0047 class SFML_GRAPHICS_API Sprite : public Drawable, public Transformable
0048 {
0049 public:
0050 
0051     ////////////////////////////////////////////////////////////
0052     /// \brief Default constructor
0053     ///
0054     /// Creates an empty sprite with no source texture.
0055     ///
0056     ////////////////////////////////////////////////////////////
0057     Sprite();
0058 
0059     ////////////////////////////////////////////////////////////
0060     /// \brief Construct the sprite from a source texture
0061     ///
0062     /// \param texture Source texture
0063     ///
0064     /// \see setTexture
0065     ///
0066     ////////////////////////////////////////////////////////////
0067     explicit Sprite(const Texture& texture);
0068 
0069     ////////////////////////////////////////////////////////////
0070     /// \brief Construct the sprite from a sub-rectangle of a source texture
0071     ///
0072     /// \param texture   Source texture
0073     /// \param rectangle Sub-rectangle of the texture to assign to the sprite
0074     ///
0075     /// \see setTexture, setTextureRect
0076     ///
0077     ////////////////////////////////////////////////////////////
0078     Sprite(const Texture& texture, const IntRect& rectangle);
0079 
0080     ////////////////////////////////////////////////////////////
0081     /// \brief Change the source texture of the sprite
0082     ///
0083     /// The \a texture argument refers to a texture that must
0084     /// exist as long as the sprite uses it. Indeed, the sprite
0085     /// doesn't store its own copy of the texture, but rather keeps
0086     /// a pointer to the one that you passed to this function.
0087     /// If the source texture is destroyed and the sprite tries to
0088     /// use it, the behavior is undefined.
0089     /// If \a resetRect is true, the TextureRect property of
0090     /// the sprite is automatically adjusted to the size of the new
0091     /// texture. If it is false, the texture rect is left unchanged.
0092     ///
0093     /// \param texture   New texture
0094     /// \param resetRect Should the texture rect be reset to the size of the new texture?
0095     ///
0096     /// \see getTexture, setTextureRect
0097     ///
0098     ////////////////////////////////////////////////////////////
0099     void setTexture(const Texture& texture, bool resetRect = false);
0100 
0101     ////////////////////////////////////////////////////////////
0102     /// \brief Set the sub-rectangle of the texture that the sprite will display
0103     ///
0104     /// The texture rect is useful when you don't want to display
0105     /// the whole texture, but rather a part of it.
0106     /// By default, the texture rect covers the entire texture.
0107     ///
0108     /// \param rectangle Rectangle defining the region of the texture to display
0109     ///
0110     /// \see getTextureRect, setTexture
0111     ///
0112     ////////////////////////////////////////////////////////////
0113     void setTextureRect(const IntRect& rectangle);
0114 
0115     ////////////////////////////////////////////////////////////
0116     /// \brief Set the global color of the sprite
0117     ///
0118     /// This color is modulated (multiplied) with the sprite's
0119     /// texture. It can be used to colorize the sprite, or change
0120     /// its global opacity.
0121     /// By default, the sprite's color is opaque white.
0122     ///
0123     /// \param color New color of the sprite
0124     ///
0125     /// \see getColor
0126     ///
0127     ////////////////////////////////////////////////////////////
0128     void setColor(const Color& color);
0129 
0130     ////////////////////////////////////////////////////////////
0131     /// \brief Get the source texture of the sprite
0132     ///
0133     /// If the sprite has no source texture, a NULL pointer is returned.
0134     /// The returned pointer is const, which means that you can't
0135     /// modify the texture when you retrieve it with this function.
0136     ///
0137     /// \return Pointer to the sprite's texture
0138     ///
0139     /// \see setTexture
0140     ///
0141     ////////////////////////////////////////////////////////////
0142     const Texture* getTexture() const;
0143 
0144     ////////////////////////////////////////////////////////////
0145     /// \brief Get the sub-rectangle of the texture displayed by the sprite
0146     ///
0147     /// \return Texture rectangle of the sprite
0148     ///
0149     /// \see setTextureRect
0150     ///
0151     ////////////////////////////////////////////////////////////
0152     const IntRect& getTextureRect() const;
0153 
0154     ////////////////////////////////////////////////////////////
0155     /// \brief Get the global color of the sprite
0156     ///
0157     /// \return Global color of the sprite
0158     ///
0159     /// \see setColor
0160     ///
0161     ////////////////////////////////////////////////////////////
0162     const Color& getColor() const;
0163 
0164     ////////////////////////////////////////////////////////////
0165     /// \brief Get the local bounding rectangle of the entity
0166     ///
0167     /// The returned rectangle is in local coordinates, which means
0168     /// that it ignores the transformations (translation, rotation,
0169     /// scale, ...) that are applied to the entity.
0170     /// In other words, this function returns the bounds of the
0171     /// entity in the entity's coordinate system.
0172     ///
0173     /// \return Local bounding rectangle of the entity
0174     ///
0175     ////////////////////////////////////////////////////////////
0176     FloatRect getLocalBounds() const;
0177 
0178     ////////////////////////////////////////////////////////////
0179     /// \brief Get the global bounding rectangle of the entity
0180     ///
0181     /// The returned rectangle is in global coordinates, which means
0182     /// that it takes into account the transformations (translation,
0183     /// rotation, scale, ...) that are applied to the entity.
0184     /// In other words, this function returns the bounds of the
0185     /// sprite in the global 2D world's coordinate system.
0186     ///
0187     /// \return Global bounding rectangle of the entity
0188     ///
0189     ////////////////////////////////////////////////////////////
0190     FloatRect getGlobalBounds() const;
0191 
0192 private:
0193 
0194     ////////////////////////////////////////////////////////////
0195     /// \brief Draw the sprite to a render target
0196     ///
0197     /// \param target Render target to draw to
0198     /// \param states Current render states
0199     ///
0200     ////////////////////////////////////////////////////////////
0201     virtual void draw(RenderTarget& target, RenderStates states) const;
0202 
0203     ////////////////////////////////////////////////////////////
0204     /// \brief Update the vertices' positions
0205     ///
0206     ////////////////////////////////////////////////////////////
0207     void updatePositions();
0208 
0209     ////////////////////////////////////////////////////////////
0210     /// \brief Update the vertices' texture coordinates
0211     ///
0212     ////////////////////////////////////////////////////////////
0213     void updateTexCoords();
0214 
0215     ////////////////////////////////////////////////////////////
0216     // Member data
0217     ////////////////////////////////////////////////////////////
0218     Vertex         m_vertices[4]; //!< Vertices defining the sprite's geometry
0219     const Texture* m_texture;     //!< Texture of the sprite
0220     IntRect        m_textureRect; //!< Rectangle defining the area of the source texture to display
0221 };
0222 
0223 } // namespace sf
0224 
0225 
0226 #endif // SFML_SPRITE_HPP
0227 
0228 
0229 ////////////////////////////////////////////////////////////
0230 /// \class sf::Sprite
0231 /// \ingroup graphics
0232 ///
0233 /// sf::Sprite is a drawable class that allows to easily display
0234 /// a texture (or a part of it) on a render target.
0235 ///
0236 /// It inherits all the functions from sf::Transformable:
0237 /// position, rotation, scale, origin. It also adds sprite-specific
0238 /// properties such as the texture to use, the part of it to display,
0239 /// and some convenience functions to change the overall color of the
0240 /// sprite, or to get its bounding rectangle.
0241 ///
0242 /// sf::Sprite works in combination with the sf::Texture class, which
0243 /// loads and provides the pixel data of a given texture.
0244 ///
0245 /// The separation of sf::Sprite and sf::Texture allows more flexibility
0246 /// and better performances: indeed a sf::Texture is a heavy resource,
0247 /// and any operation on it is slow (often too slow for real-time
0248 /// applications). On the other side, a sf::Sprite is a lightweight
0249 /// object which can use the pixel data of a sf::Texture and draw
0250 /// it with its own transformation/color/blending attributes.
0251 ///
0252 /// It is important to note that the sf::Sprite instance doesn't
0253 /// copy the texture that it uses, it only keeps a reference to it.
0254 /// Thus, a sf::Texture must not be destroyed while it is
0255 /// used by a sf::Sprite (i.e. never write a function that
0256 /// uses a local sf::Texture instance for creating a sprite).
0257 ///
0258 /// See also the note on coordinates and undistorted rendering in sf::Transformable.
0259 ///
0260 /// Usage example:
0261 /// \code
0262 /// // Declare and load a texture
0263 /// sf::Texture texture;
0264 /// texture.loadFromFile("texture.png");
0265 ///
0266 /// // Create a sprite
0267 /// sf::Sprite sprite;
0268 /// sprite.setTexture(texture);
0269 /// sprite.setTextureRect(sf::IntRect(10, 10, 50, 30));
0270 /// sprite.setColor(sf::Color(255, 255, 255, 200));
0271 /// sprite.setPosition(100, 25);
0272 ///
0273 /// // Draw it
0274 /// window.draw(sprite);
0275 /// \endcode
0276 ///
0277 /// \see sf::Texture, sf::Transformable
0278 ///
0279 ////////////////////////////////////////////////////////////