1 ////////////////////////////////////////////////////////////
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
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.
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:
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.
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
21 // 3. This notice may not be removed or altered from any source distribution.
23 ////////////////////////////////////////////////////////////
25 #ifndef SFML_SHAPE_HPP
26 #define SFML_SHAPE_HPP
28 ////////////////////////////////////////////////////////////
30 ////////////////////////////////////////////////////////////
31 #include <SFML/Graphics/Export.hpp>
32 #include <SFML/Graphics/Drawable.hpp>
33 #include <SFML/Graphics/Transformable.hpp>
34 #include <SFML/Graphics/VertexArray.hpp>
35 #include <SFML/System/Vector2.hpp>
40 ////////////////////////////////////////////////////////////
41 /// \brief Base class for textured shapes with outline
43 ////////////////////////////////////////////////////////////
44 class SFML_GRAPHICS_API Shape : public Drawable, public Transformable
48 ////////////////////////////////////////////////////////////
49 /// \brief Virtual destructor
51 ////////////////////////////////////////////////////////////
54 ////////////////////////////////////////////////////////////
55 /// \brief Change the source texture of the shape
57 /// The \a texture argument refers to a texture that must
58 /// exist as long as the shape uses it. Indeed, the shape
59 /// doesn't store its own copy of the texture, but rather keeps
60 /// a pointer to the one that you passed to this function.
61 /// If the source texture is destroyed and the shape tries to
62 /// use it, the behavior is undefined.
63 /// \a texture can be NULL to disable texturing.
64 /// If \a resetRect is true, the TextureRect property of
65 /// the shape is automatically adjusted to the size of the new
66 /// texture. If it is false, the texture rect is left unchanged.
68 /// \param texture New texture
69 /// \param resetRect Should the texture rect be reset to the size of the new texture?
71 /// \see getTexture, setTextureRect
73 ////////////////////////////////////////////////////////////
74 void setTexture(const Texture* texture, bool resetRect = false);
76 ////////////////////////////////////////////////////////////
77 /// \brief Set the sub-rectangle of the texture that the shape will display
79 /// The texture rect is useful when you don't want to display
80 /// the whole texture, but rather a part of it.
81 /// By default, the texture rect covers the entire texture.
83 /// \param rect Rectangle defining the region of the texture to display
85 /// \see getTextureRect, setTexture
87 ////////////////////////////////////////////////////////////
88 void setTextureRect(const IntRect& rect);
90 ////////////////////////////////////////////////////////////
91 /// \brief Set the fill color of the shape
93 /// This color is modulated (multiplied) with the shape's
94 /// texture if any. It can be used to colorize the shape,
95 /// or change its global opacity.
96 /// You can use sf::Color::Transparent to make the inside of
97 /// the shape transparent, and have the outline alone.
98 /// By default, the shape's fill color is opaque white.
100 /// \param color New color of the shape
102 /// \see getFillColor, setOutlineColor
104 ////////////////////////////////////////////////////////////
105 void setFillColor(const Color& color);
107 ////////////////////////////////////////////////////////////
108 /// \brief Set the outline color of the shape
110 /// By default, the shape's outline color is opaque white.
112 /// \param color New outline color of the shape
114 /// \see getOutlineColor, setFillColor
116 ////////////////////////////////////////////////////////////
117 void setOutlineColor(const Color& color);
119 ////////////////////////////////////////////////////////////
120 /// \brief Set the thickness of the shape's outline
122 /// Note that negative values are allowed (so that the outline
123 /// expands towards the center of the shape), and using zero
124 /// disables the outline.
125 /// By default, the outline thickness is 0.
127 /// \param thickness New outline thickness
129 /// \see getOutlineThickness
131 ////////////////////////////////////////////////////////////
132 void setOutlineThickness(float thickness);
134 ////////////////////////////////////////////////////////////
135 /// \brief Get the source texture of the shape
137 /// If the shape has no source texture, a NULL pointer is returned.
138 /// The returned pointer is const, which means that you can't
139 /// modify the texture when you retrieve it with this function.
141 /// \return Pointer to the shape's texture
145 ////////////////////////////////////////////////////////////
146 const Texture* getTexture() const;
148 ////////////////////////////////////////////////////////////
149 /// \brief Get the sub-rectangle of the texture displayed by the shape
151 /// \return Texture rectangle of the shape
153 /// \see setTextureRect
155 ////////////////////////////////////////////////////////////
156 const IntRect& getTextureRect() const;
158 ////////////////////////////////////////////////////////////
159 /// \brief Get the fill color of the shape
161 /// \return Fill color of the shape
163 /// \see setFillColor
165 ////////////////////////////////////////////////////////////
166 const Color& getFillColor() const;
168 ////////////////////////////////////////////////////////////
169 /// \brief Get the outline color of the shape
171 /// \return Outline color of the shape
173 /// \see setOutlineColor
175 ////////////////////////////////////////////////////////////
176 const Color& getOutlineColor() const;
178 ////////////////////////////////////////////////////////////
179 /// \brief Get the outline thickness of the shape
181 /// \return Outline thickness of the shape
183 /// \see setOutlineThickness
185 ////////////////////////////////////////////////////////////
186 float getOutlineThickness() const;
188 ////////////////////////////////////////////////////////////
189 /// \brief Get the total number of points of the shape
191 /// \return Number of points of the shape
195 ////////////////////////////////////////////////////////////
196 virtual unsigned int getPointCount() const = 0;
198 ////////////////////////////////////////////////////////////
199 /// \brief Get a point of the shape
201 /// The returned point is in local coordinates, that is,
202 /// the shape's transforms (position, rotation, scale) are
203 /// not taken into account.
204 /// The result is undefined if \a index is out of the valid range.
206 /// \param index Index of the point to get, in range [0 .. getPointCount() - 1]
208 /// \return index-th point of the shape
210 /// \see getPointCount
212 ////////////////////////////////////////////////////////////
213 virtual Vector2f getPoint(unsigned int index) const = 0;
215 ////////////////////////////////////////////////////////////
216 /// \brief Get the local bounding rectangle of the entity
218 /// The returned rectangle is in local coordinates, which means
219 /// that it ignores the transformations (translation, rotation,
220 /// scale, ...) that are applied to the entity.
221 /// In other words, this function returns the bounds of the
222 /// entity in the entity's coordinate system.
224 /// \return Local bounding rectangle of the entity
226 ////////////////////////////////////////////////////////////
227 FloatRect getLocalBounds() const;
229 ////////////////////////////////////////////////////////////
230 /// \brief Get the global bounding rectangle of the entity
232 /// The returned rectangle is in global coordinates, which means
233 /// that it takes in account the transformations (translation,
234 /// rotation, scale, ...) that are applied to the entity.
235 /// In other words, this function returns the bounds of the
236 /// sprite in the global 2D world's coordinate system.
238 /// \return Global bounding rectangle of the entity
240 ////////////////////////////////////////////////////////////
241 FloatRect getGlobalBounds() const;
245 ////////////////////////////////////////////////////////////
246 /// \brief Default constructor
248 ////////////////////////////////////////////////////////////
251 ////////////////////////////////////////////////////////////
252 /// \brief Recompute the internal geometry of the shape
254 /// This function must be called by the derived class everytime
255 /// the shape's points change (i.e. the result of either
256 /// getPointCount or getPoint is different).
258 ////////////////////////////////////////////////////////////
263 ////////////////////////////////////////////////////////////
264 /// \brief Draw the shape to a render target
266 /// \param target Render target to draw to
267 /// \param states Current render states
269 ////////////////////////////////////////////////////////////
270 virtual void draw(RenderTarget& target, RenderStates states) const;
272 ////////////////////////////////////////////////////////////
273 /// \brief Update the fill vertices' color
275 ////////////////////////////////////////////////////////////
276 void updateFillColors();
278 ////////////////////////////////////////////////////////////
279 /// \brief Update the fill vertices' texture coordinates
281 ////////////////////////////////////////////////////////////
282 void updateTexCoords();
284 ////////////////////////////////////////////////////////////
285 /// \brief Update the outline vertices' position
287 ////////////////////////////////////////////////////////////
288 void updateOutline();
290 ////////////////////////////////////////////////////////////
291 /// \brief Update the outline vertices' color
293 ////////////////////////////////////////////////////////////
294 void updateOutlineColors();
298 ////////////////////////////////////////////////////////////
300 ////////////////////////////////////////////////////////////
301 const Texture* m_texture; ///< Texture of the shape
302 IntRect m_textureRect; ///< Rectangle defining the area of the source texture to display
303 Color m_fillColor; ///< Fill color
304 Color m_outlineColor; ///< Outline color
305 float m_outlineThickness; ///< Thickness of the shape's outline
306 VertexArray m_vertices; ///< Vertex array containing the fill geometry
307 VertexArray m_outlineVertices; ///< Vertex array containing the outline geometry
308 FloatRect m_insideBounds; ///< Bounding rectangle of the inside (fill)
309 FloatRect m_bounds; ///< Bounding rectangle of the whole shape (outline + fill)
315 #endif // SFML_SHAPE_HPP
318 ////////////////////////////////////////////////////////////
320 /// \ingroup graphics
322 /// sf::Shape is a drawable class that allows to define and
323 /// display a custom convex shape on a render target.
324 /// It's only an abstract base, it needs to be specialized for
325 /// concrete types of shapes (circle, rectangle, convex polygon,
328 /// In addition to the attributes provided by the specialized
329 /// shape classes, a shape always has the following attributes:
331 /// \li a texture rectangle
333 /// \li an outline color
334 /// \li an outline thickness
336 /// Each feature is optional, and can be disabled easily:
337 /// \li the texture can be null
338 /// \li the fill/outline colors can be sf::Color::Transparent
339 /// \li the outline thickness can be zero
341 /// You can write your own derived shape class, there are only
342 /// two virtual functions to override:
343 /// \li getPointCount must return the number of points of the shape
344 /// \li getPoint must return the points of the shape
346 /// \see sf::RectangleShape, sf::CircleShape, sf::ConvexShape, sf::Transformable
348 ////////////////////////////////////////////////////////////