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_SHAPE_HPP
0026 #define SFML_SHAPE_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/VertexArray.hpp>
0035 #include <SFML/System/Vector2.hpp>
0036 
0037 
0038 namespace sf
0039 {
0040 ////////////////////////////////////////////////////////////
0041 /// \brief Base class for textured shapes with outline
0042 ///
0043 ////////////////////////////////////////////////////////////
0044 class SFML_GRAPHICS_API Shape : public Drawable, public Transformable
0045 {
0046 public:
0047 
0048     ////////////////////////////////////////////////////////////
0049     /// \brief Virtual destructor
0050     ///
0051     ////////////////////////////////////////////////////////////
0052     virtual ~Shape();
0053 
0054     ////////////////////////////////////////////////////////////
0055     /// \brief Change the source texture of the shape
0056     ///
0057     /// The \a texture argument refers to a texture that must
0058     /// exist as long as the shape uses it. Indeed, the shape
0059     /// doesn't store its own copy of the texture, but rather keeps
0060     /// a pointer to the one that you passed to this function.
0061     /// If the source texture is destroyed and the shape tries to
0062     /// use it, the behavior is undefined.
0063     /// \a texture can be NULL to disable texturing.
0064     /// If \a resetRect is true, the TextureRect property of
0065     /// the shape is automatically adjusted to the size of the new
0066     /// texture. If it is false, the texture rect is left unchanged.
0067     ///
0068     /// \param texture   New texture
0069     /// \param resetRect Should the texture rect be reset to the size of the new texture?
0070     ///
0071     /// \see getTexture, setTextureRect
0072     ///
0073     ////////////////////////////////////////////////////////////
0074     void setTexture(const Texture* texture, bool resetRect = false);
0075 
0076     ////////////////////////////////////////////////////////////
0077     /// \brief Set the sub-rectangle of the texture that the shape will display
0078     ///
0079     /// The texture rect is useful when you don't want to display
0080     /// the whole texture, but rather a part of it.
0081     /// By default, the texture rect covers the entire texture.
0082     ///
0083     /// \param rect Rectangle defining the region of the texture to display
0084     ///
0085     /// \see getTextureRect, setTexture
0086     ///
0087     ////////////////////////////////////////////////////////////
0088     void setTextureRect(const IntRect& rect);
0089 
0090     ////////////////////////////////////////////////////////////
0091     /// \brief Set the fill color of the shape
0092     ///
0093     /// This color is modulated (multiplied) with the shape's
0094     /// texture if any. It can be used to colorize the shape,
0095     /// or change its global opacity.
0096     /// You can use sf::Color::Transparent to make the inside of
0097     /// the shape transparent, and have the outline alone.
0098     /// By default, the shape's fill color is opaque white.
0099     ///
0100     /// \param color New color of the shape
0101     ///
0102     /// \see getFillColor, setOutlineColor
0103     ///
0104     ////////////////////////////////////////////////////////////
0105     void setFillColor(const Color& color);
0106 
0107     ////////////////////////////////////////////////////////////
0108     /// \brief Set the outline color of the shape
0109     ///
0110     /// By default, the shape's outline color is opaque white.
0111     ///
0112     /// \param color New outline color of the shape
0113     ///
0114     /// \see getOutlineColor, setFillColor
0115     ///
0116     ////////////////////////////////////////////////////////////
0117     void setOutlineColor(const Color& color);
0118 
0119     ////////////////////////////////////////////////////////////
0120     /// \brief Set the thickness of the shape's outline
0121     ///
0122     /// Note that negative values are allowed (so that the outline
0123     /// expands towards the center of the shape), and using zero
0124     /// disables the outline.
0125     /// By default, the outline thickness is 0.
0126     ///
0127     /// \param thickness New outline thickness
0128     ///
0129     /// \see getOutlineThickness
0130     ///
0131     ////////////////////////////////////////////////////////////
0132     void setOutlineThickness(float thickness);
0133 
0134     ////////////////////////////////////////////////////////////
0135     /// \brief Get the source texture of the shape
0136     ///
0137     /// If the shape has no source texture, a NULL pointer is returned.
0138     /// The returned pointer is const, which means that you can't
0139     /// modify the texture when you retrieve it with this function.
0140     ///
0141     /// \return Pointer to the shape's texture
0142     ///
0143     /// \see setTexture
0144     ///
0145     ////////////////////////////////////////////////////////////
0146     const Texture* getTexture() const;
0147 
0148     ////////////////////////////////////////////////////////////
0149     /// \brief Get the sub-rectangle of the texture displayed by the shape
0150     ///
0151     /// \return Texture rectangle of the shape
0152     ///
0153     /// \see setTextureRect
0154     ///
0155     ////////////////////////////////////////////////////////////
0156     const IntRect& getTextureRect() const;
0157 
0158     ////////////////////////////////////////////////////////////
0159     /// \brief Get the fill color of the shape
0160     ///
0161     /// \return Fill color of the shape
0162     ///
0163     /// \see setFillColor
0164     ///
0165     ////////////////////////////////////////////////////////////
0166     const Color& getFillColor() const;
0167 
0168     ////////////////////////////////////////////////////////////
0169     /// \brief Get the outline color of the shape
0170     ///
0171     /// \return Outline color of the shape
0172     ///
0173     /// \see setOutlineColor
0174     ///
0175     ////////////////////////////////////////////////////////////
0176     const Color& getOutlineColor() const;
0177 
0178     ////////////////////////////////////////////////////////////
0179     /// \brief Get the outline thickness of the shape
0180     ///
0181     /// \return Outline thickness of the shape
0182     ///
0183     /// \see setOutlineThickness
0184     ///
0185     ////////////////////////////////////////////////////////////
0186     float getOutlineThickness() const;
0187 
0188     ////////////////////////////////////////////////////////////
0189     /// \brief Get the total number of points of the shape
0190     ///
0191     /// \return Number of points of the shape
0192     ///
0193     /// \see getPoint
0194     ///
0195     ////////////////////////////////////////////////////////////
0196     virtual std::size_t getPointCount() const = 0;
0197 
0198     ////////////////////////////////////////////////////////////
0199     /// \brief Get a point of the shape
0200     ///
0201     /// The returned point is in local coordinates, that is,
0202     /// the shape's transforms (position, rotation, scale) are
0203     /// not taken into account.
0204     /// The result is undefined if \a index is out of the valid range.
0205     ///
0206     /// \param index Index of the point to get, in range [0 .. getPointCount() - 1]
0207     ///
0208     /// \return index-th point of the shape
0209     ///
0210     /// \see getPointCount
0211     ///
0212     ////////////////////////////////////////////////////////////
0213     virtual Vector2f getPoint(std::size_t index) const = 0;
0214 
0215     ////////////////////////////////////////////////////////////
0216     /// \brief Get the local bounding rectangle of the entity
0217     ///
0218     /// The returned rectangle is in local coordinates, which means
0219     /// that it ignores the transformations (translation, rotation,
0220     /// scale, ...) that are applied to the entity.
0221     /// In other words, this function returns the bounds of the
0222     /// entity in the entity's coordinate system.
0223     ///
0224     /// \return Local bounding rectangle of the entity
0225     ///
0226     ////////////////////////////////////////////////////////////
0227     FloatRect getLocalBounds() const;
0228 
0229     ////////////////////////////////////////////////////////////
0230     /// \brief Get the global (non-minimal) bounding rectangle of the entity
0231     ///
0232     /// The returned rectangle is in global coordinates, which means
0233     /// that it takes into account the transformations (translation,
0234     /// rotation, scale, ...) that are applied to the entity.
0235     /// In other words, this function returns the bounds of the
0236     /// shape in the global 2D world's coordinate system.
0237     ///
0238     /// This function does not necessarily return the \a minimal
0239     /// bounding rectangle. It merely ensures that the returned
0240     /// rectangle covers all the vertices (but possibly more).
0241     /// This allows for a fast approximation of the bounds as a
0242     /// first check; you may want to use more precise checks
0243     /// on top of that.
0244     ///
0245     /// \return Global bounding rectangle of the entity
0246     ///
0247     ////////////////////////////////////////////////////////////
0248     FloatRect getGlobalBounds() const;
0249 
0250 protected:
0251 
0252     ////////////////////////////////////////////////////////////
0253     /// \brief Default constructor
0254     ///
0255     ////////////////////////////////////////////////////////////
0256     Shape();
0257 
0258     ////////////////////////////////////////////////////////////
0259     /// \brief Recompute the internal geometry of the shape
0260     ///
0261     /// This function must be called by the derived class everytime
0262     /// the shape's points change (i.e. the result of either
0263     /// getPointCount or getPoint is different).
0264     ///
0265     ////////////////////////////////////////////////////////////
0266     void update();
0267 
0268 private:
0269 
0270     ////////////////////////////////////////////////////////////
0271     /// \brief Draw the shape to a render target
0272     ///
0273     /// \param target Render target to draw to
0274     /// \param states Current render states
0275     ///
0276     ////////////////////////////////////////////////////////////
0277     virtual void draw(RenderTarget& target, RenderStates states) const;
0278 
0279     ////////////////////////////////////////////////////////////
0280     /// \brief Update the fill vertices' color
0281     ///
0282     ////////////////////////////////////////////////////////////
0283     void updateFillColors();
0284 
0285     ////////////////////////////////////////////////////////////
0286     /// \brief Update the fill vertices' texture coordinates
0287     ///
0288     ////////////////////////////////////////////////////////////
0289     void updateTexCoords();
0290 
0291     ////////////////////////////////////////////////////////////
0292     /// \brief Update the outline vertices' position
0293     ///
0294     ////////////////////////////////////////////////////////////
0295     void updateOutline();
0296 
0297     ////////////////////////////////////////////////////////////
0298     /// \brief Update the outline vertices' color
0299     ///
0300     ////////////////////////////////////////////////////////////
0301     void updateOutlineColors();
0302 
0303 private:
0304 
0305     ////////////////////////////////////////////////////////////
0306     // Member data
0307     ////////////////////////////////////////////////////////////
0308     const Texture* m_texture;          //!< Texture of the shape
0309     IntRect        m_textureRect;      //!< Rectangle defining the area of the source texture to display
0310     Color          m_fillColor;        //!< Fill color
0311     Color          m_outlineColor;     //!< Outline color
0312     float          m_outlineThickness; //!< Thickness of the shape's outline
0313     VertexArray    m_vertices;         //!< Vertex array containing the fill geometry
0314     VertexArray    m_outlineVertices;  //!< Vertex array containing the outline geometry
0315     FloatRect      m_insideBounds;     //!< Bounding rectangle of the inside (fill)
0316     FloatRect      m_bounds;           //!< Bounding rectangle of the whole shape (outline + fill)
0317 };
0318 
0319 } // namespace sf
0320 
0321 
0322 #endif // SFML_SHAPE_HPP
0323 
0324 
0325 ////////////////////////////////////////////////////////////
0326 /// \class sf::Shape
0327 /// \ingroup graphics
0328 ///
0329 /// sf::Shape is a drawable class that allows to define and
0330 /// display a custom convex shape on a render target.
0331 /// It's only an abstract base, it needs to be specialized for
0332 /// concrete types of shapes (circle, rectangle, convex polygon,
0333 /// star, ...).
0334 ///
0335 /// In addition to the attributes provided by the specialized
0336 /// shape classes, a shape always has the following attributes:
0337 /// \li a texture
0338 /// \li a texture rectangle
0339 /// \li a fill color
0340 /// \li an outline color
0341 /// \li an outline thickness
0342 ///
0343 /// Each feature is optional, and can be disabled easily:
0344 /// \li the texture can be null
0345 /// \li the fill/outline colors can be sf::Color::Transparent
0346 /// \li the outline thickness can be zero
0347 ///
0348 /// You can write your own derived shape class, there are only
0349 /// two virtual functions to override:
0350 /// \li getPointCount must return the number of points of the shape
0351 /// \li getPoint must return the points of the shape
0352 ///
0353 /// \see sf::RectangleShape, sf::CircleShape, sf::ConvexShape, sf::Transformable
0354 ///
0355 ////////////////////////////////////////////////////////////