Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Shape.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * 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
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_SHAPE_H
25 #define GF_SHAPE_H
26 
27 #include "Portability.h"
28 #include "Transformable.h"
29 #include "VertexArray.h"
30 #include "VertexBuffer.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
37  class Texture;
38 
39  /**
40  * @ingroup graphics
41  * @brief Base class for textured shapes with outline
42  *
43  * gf::Shape is a drawable class that allows to define and
44  * display a custom convex shape on a render target.
45  * It's only an abstract base, it needs to be specialized for
46  * concrete types of shapes (circle, rectangle, convex polygon,
47  * star, ...).
48  *
49  * In addition to the attributes provided by the specialized
50  * shape classes, a shape always has the following attributes:
51  *
52  * - a texture
53  * - a texture rectangle
54  * - a fill color
55  * - an outline color
56  * - an outline thickness
57  *
58  * Each feature is optional, and can be disabled easily:
59  *
60  * - the texture can be null
61  * - the fill/outline colors can be transparent
62  * - the outline thickness can be zero
63  *
64  * You can write your own derived shape class, there are only
65  * two virtual functions to override:
66  *
67  * - `getPointCount()` must return the number of points of the shape
68  * - `getPoint()` must return the points of the shape
69  *
70  * @sa gf::RectangleShape, gf::CircleShape, gf::ConvexShape
71  * @sa gf::Transformable
72  */
73  class GF_API Shape : public Transformable {
74  public:
75  /**
76  * @brief Default constructor
77  */
78  Shape();
79 
80  /**
81  * @brief Change the source texture of the shape
82  *
83  * The texture must exist as long as the shape uses it. Indeed, the
84  * shape doesn't store its own copy of the texture, but rather keeps
85  * a pointer to the one that you passed to this function.
86  * If the source texture is destroyed and the shape tries to
87  * use it, the behavior is undefined.
88  *
89  * If `resetRect` is true, the texture rect property of
90  * the shape is automatically adjusted to the size of the new
91  * texture. If it is false, the texture rect is left unchanged.
92  *
93  * @param texture New texture
94  * @param resetRect Should the texture rect be reset to the size of the new texture?
95  *
96  * @sa getTexture(), setTextureRect()
97  */
98  void setTexture(const Texture& texture, bool resetRect = false);
99 
100  /**
101  * @brief Get the source texture of the shape
102  *
103  * If the shape has no source texture, a `nullptr` pointer is returned.
104  * The returned pointer is const, which means that you can't
105  * modify the texture when you retrieve it with this function.
106  *
107  * @return Pointer to the shape's texture
108  *
109  * @sa setTexture()
110  */
111  const Texture *getTexture() const {
112  return m_texture;
113  }
114 
115  /**
116  * @brief Unset the source texture of the shape
117  *
118  * After a call to this function, the shape has no source texture.
119  *
120  * @sa setTexture()
121  */
122  void unsetTexture();
123 
124  /**
125  * @brief Set the sub-rectangle of the texture that the shape will display
126  *
127  * The texture rect is useful when you don't want to display
128  * the whole texture, but rather a part of it.
129  *
130  * By default, the texture rect covers the entire texture.
131  *
132  * @param rect Rectangle defining the region of the texture to display
133  *
134  * @sa getTextureRect(), setTexture()
135  */
136  void setTextureRect(const RectF& rect);
137 
138  /**
139  * @brief Get the sub-rectangle of the texture displayed by the shape
140  *
141  * @return Texture rectangle of the shape
142  *
143  * @sa setTextureRect()
144  */
145  const RectF& getTextureRect() const {
146  return m_textureRect;
147  }
148 
149  /**
150  * @brief Set the fill color of the shape
151  *
152  * This color is modulated (multiplied) with the shape's
153  * texture if any. It can be used to colorize the shape,
154  * or change its global opacity.
155  *
156  * You can use gf::Color::Transparent to make the inside of
157  * the shape transparent, and have the outline alone.
158  *
159  * By default, the shape's fill color is opaque white.
160  *
161  * @param color New color of the shape
162  *
163  * @sa getColor(), setOutlineColor()
164  */
165  void setColor(const Color4f& color);
166 
167  /**
168  * @brief Get the fill color of the shape
169  *
170  * @return Fill color of the shape
171  *
172  * @sa setColor()
173  */
174  const Color4f& getColor() const {
175  return m_color;
176  }
177 
178  /**
179  * @brief Set the outline color of the shape
180  *
181  * By default, the shape's outline color is opaque white.
182  *
183  * @param color New outline color of the shape
184  *
185  * @sa getOutlineColor(), setColor()
186  */
187  void setOutlineColor(const Color4f& color);
188 
189  /**
190  * @brief Get the outline color of the shape
191  *
192  * @return Outline color of the shape
193  *
194  * @sa setOutlineColor()
195  */
196  const Color4f& getOutlineColor() const {
197  return m_outlineColor;
198  }
199 
200  /**
201  * @brief Set the thickness of the shape's outline
202  *
203  * By default, the outline thickness is @f$ 0 @f$.
204  *
205  * @param thickness New outline thickness
206  *
207  * @sa getOutlineThickness()
208  */
209  void setOutlineThickness(float thickness);
210 
211  /**
212  * @brief Get the outline thickness of the shape
213  *
214  * @return Outline thickness of the shape
215  * @sa setOutlineThickness()
216  */
217  float getOutlineThickness() const {
218  return m_outlineThickness;
219  }
220 
221  /**
222  * @brief Get the total number of points of the shape
223  *
224  * @return Number of points of the shape
225  * @sa getPoint()
226  */
227  virtual std::size_t getPointCount() const = 0;
228 
229  /**
230  * @brief Get a point of the shape
231  *
232  * The returned point is in local coordinates, that is,
233  * the shape's transforms (position, rotation, scale) are
234  * not taken into account.
235  *
236  * The result is undefined if `index` is out of the valid range.
237  *
238  * @param index Index of the point to get, in range @f$ [0, n-1] @f$
239  * where @f$ n @f$ is the number of points of the shape.
240  *
241  * @return index-th point of the shape
242  * @sa getPointCount()
243  */
244  virtual Vector2f getPoint(std::size_t index) const = 0;
245 
246  /**
247  * @brief Get the local bounding rectangle of the entity
248  *
249  * The returned rectangle is in local coordinates, which means
250  * that it ignores the transformations (translation, rotation,
251  * scale, ...) that are applied to the entity.
252  * In other words, this function returns the bounds of the
253  * entity in the entity's coordinate system.
254  *
255  * @return Local bounding rectangle of the entity
256  */
257  RectF getLocalBounds() const;
258 
259  /**
260  * @brief Set the anchor origin of the entity
261  *
262  * Compute the origin of the entity based on the local bounds and
263  * the specified anchor. Internally, this function calls
264  * `Transformable::setOrigin()`.
265  *
266  * @param anchor The anchor of the entity
267  * @sa getLocalBounds(), Transformable::setOrigin()
268  */
269  void setAnchor(Anchor anchor);
270 
271 
272  /**
273  * @brief Create a buffer with the current geometry
274  *
275  * The geometry is uploaded in the graphics memory so that it's faster
276  * to draw.
277  *
278  * @return A buffer with the current geometry
279  */
281 
282  /**
283  * @brief Create a buffer with the current outline geometry
284  *
285  * The geometry is uploaded in the graphics memory so that it's faster
286  * to draw.
287  *
288  * @return A buffer with the current outline geometry
289  */
291 
292  virtual void draw(RenderTarget& target, RenderStates states) override;
293 
294  protected:
295  /**
296  * @brief Recompute the internal geometry of the shape
297  *
298  * This function must be called by the derived class everytime
299  * the shape's points change (i.e. the result of either
300  * getPointCount() or getPoint() is different).
301  */
302  void updateGeometry();
303 
304  private:
305  void updateColors();
306  void updateTexCoords();
307  void updateOutline();
308  void updateOutlineColors();
309 
310  private:
311  const Texture *m_texture;
312  RectF m_textureRect;
313  Color4f m_color;
314  VertexArray m_vertices;
315  RectF m_bounds;
316 
317  Color4f m_outlineColor;
318  float m_outlineThickness;
319  VertexArray m_outlineVertices;
320  };
321 
322 #ifndef DOXYGEN_SHOULD_SKIP_THIS
323 }
324 #endif
325 }
326 
327 #endif // GF_SHAPE_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:113
Base class for textured shapes with outline.
Definition: Shape.h:73
virtual void draw(RenderTarget &target, RenderStates states) override
Draw the object to a render target.
A set of primitives.
Definition: VertexArray.h:65
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:65
void setTexture(const Texture &texture, bool resetRect=false)
Change the source texture of the shape.
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
Shape()
Default constructor.
Data in the graphics memory.
Definition: VertexBuffer.h:70
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
void updateGeometry()
Recompute the internal geometry of the shape.
virtual Vector2f getPoint(std::size_t index) const =0
Get a point of the shape.
const Color4f & getOutlineColor() const
Get the outline color of the shape.
Definition: Shape.h:196
A texture for colored images.
Definition: Texture.h:339
const Color4f & getColor() const
Get the fill color of the shape.
Definition: Shape.h:174
Definition: Action.h:34
void setTextureRect(const RectF &rect)
Set the sub-rectangle of the texture that the shape will display.
void setOutlineThickness(float thickness)
Set the thickness of the shape's outline.
const RectF & getTextureRect() const
Get the sub-rectangle of the texture displayed by the shape.
Definition: Shape.h:145
void setOutlineColor(const Color4f &color)
Set the outline color of the shape.
VertexBuffer commitOutlineGeometry() const
Create a buffer with the current outline geometry.
Anchor
The origin anchor of the transformable object.
Definition: Transformable.h:45
void setColor(const Color4f &color)
Set the fill color of the shape.
#define GF_API
Definition: Portability.h:35
float getOutlineThickness() const
Get the outline thickness of the shape.
Definition: Shape.h:217
const Texture * getTexture() const
Get the source texture of the shape.
Definition: Shape.h:111
void unsetTexture()
Unset the source texture of the shape.
virtual std::size_t getPointCount() const =0
Get the total number of points of the shape.