Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
Shape.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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  * The returned reference is const, which means that you can't
104  * modify the texture when you retrieve it with this function.
105  *
106  * @return Reference to the shape's texture
107  *
108  * @sa setTexture()
109  */
110  const Texture& getTexture() const {
111  return *m_texture;
112  }
113 
114  /**
115  * @brief Check if a texture is set
116  *
117  * @returns True if a texture is already set
118  *
119  * @sa setTexture(), getTexture()
120  */
121  bool hasTexture() const {
122  return m_texture != nullptr;
123  }
124 
125  /**
126  * @brief Unset the source texture of the shape
127  *
128  * After a call to this function, the shape has no source texture.
129  *
130  * @sa setTexture()
131  */
132  void unsetTexture();
133 
134  /**
135  * @brief Set the sub-rectangle of the texture that the shape will display
136  *
137  * The texture rect is useful when you don't want to display
138  * the whole texture, but rather a part of it.
139  *
140  * By default, the texture rect covers the entire texture.
141  *
142  * @param rect Rectangle defining the region of the texture to display
143  *
144  * @sa getTextureRect(), setTexture()
145  */
146  void setTextureRect(const RectF& rect);
147 
148  /**
149  * @brief Get the sub-rectangle of the texture displayed by the shape
150  *
151  * @return Texture rectangle of the shape
152  *
153  * @sa setTextureRect()
154  */
155  const RectF& getTextureRect() const {
156  return m_textureRect;
157  }
158 
159  /**
160  * @brief Set the fill color of the shape
161  *
162  * This color is modulated (multiplied) with the shape's
163  * texture if any. It can be used to colorize the shape,
164  * or change its global opacity.
165  *
166  * You can use gf::Color::Transparent to make the inside of
167  * the shape transparent, and have the outline alone.
168  *
169  * By default, the shape's fill color is opaque white.
170  *
171  * @param color New color of the shape
172  *
173  * @sa getColor(), setOutlineColor()
174  */
175  void setColor(const Color4f& color);
176 
177  /**
178  * @brief Get the fill color of the shape
179  *
180  * @return Fill color of the shape
181  *
182  * @sa setColor()
183  */
184  const Color4f& getColor() const {
185  return m_color;
186  }
187 
188  /**
189  * @brief Set the outline color of the shape
190  *
191  * By default, the shape's outline color is opaque white.
192  *
193  * @param color New outline color of the shape
194  *
195  * @sa getOutlineColor(), setColor()
196  */
197  void setOutlineColor(const Color4f& color);
198 
199  /**
200  * @brief Get the outline color of the shape
201  *
202  * @return Outline color of the shape
203  *
204  * @sa setOutlineColor()
205  */
206  const Color4f& getOutlineColor() const {
207  return m_outlineColor;
208  }
209 
210  /**
211  * @brief Set the thickness of the shape's outline
212  *
213  * By default, the outline thickness is @f$ 0 @f$.
214  *
215  * @param thickness New outline thickness
216  *
217  * @sa getOutlineThickness()
218  */
219  void setOutlineThickness(float thickness);
220 
221  /**
222  * @brief Get the outline thickness of the shape
223  *
224  * @return Outline thickness of the shape
225  * @sa setOutlineThickness()
226  */
227  float getOutlineThickness() const {
228  return m_outlineThickness;
229  }
230 
231  /**
232  * @brief Get the total number of points of the shape
233  *
234  * @return Number of points of the shape
235  * @sa getPoint()
236  */
237  virtual std::size_t getPointCount() const = 0;
238 
239  /**
240  * @brief Get a point of the shape
241  *
242  * The returned point is in local coordinates, that is,
243  * the shape's transforms (position, rotation, scale) are
244  * not taken into account.
245  *
246  * The result is undefined if `index` is out of the valid range.
247  *
248  * @param index Index of the point to get, in range @f$ [0, n-1] @f$
249  * where @f$ n @f$ is the number of points of the shape.
250  *
251  * @return index-th point of the shape
252  * @sa getPointCount()
253  */
254  virtual Vector2f getPoint(std::size_t index) const = 0;
255 
256  /**
257  * @brief Get the local bounding rectangle of the entity
258  *
259  * The returned rectangle is in local coordinates, which means
260  * that it ignores the transformations (translation, rotation,
261  * scale, ...) that are applied to the entity.
262  * In other words, this function returns the bounds of the
263  * entity in the entity's coordinate system.
264  *
265  * @return Local bounding rectangle of the entity
266  */
267  RectF getLocalBounds() const;
268 
269  /**
270  * @brief Set the anchor origin of the entity
271  *
272  * Compute the origin of the entity based on the local bounds and
273  * the specified anchor. Internally, this function calls
274  * `Transformable::setOrigin()`.
275  *
276  * @param anchor The anchor of the entity
277  * @sa getLocalBounds(), Transformable::setOrigin()
278  */
279  void setAnchor(Anchor anchor);
280 
281 
282  /**
283  * @brief Create a buffer with the current 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 geometry
289  */
291 
292  /**
293  * @brief Create a buffer with the current outline geometry
294  *
295  * The geometry is uploaded in the graphics memory so that it's faster
296  * to draw.
297  *
298  * @return A buffer with the current outline geometry
299  */
301 
302  virtual void draw(RenderTarget& target, RenderStates states) override;
303 
304  protected:
305  /**
306  * @brief Recompute the internal geometry of the shape
307  *
308  * This function must be called by the derived class everytime
309  * the shape's points change (i.e. the result of either
310  * getPointCount() or getPoint() is different).
311  */
312  void updateGeometry();
313 
314  private:
315  void updateColors();
316  void updateTexCoords();
317  void updateOutline();
318  void updateOutlineColors();
319 
320  private:
321  const Texture *m_texture;
322  RectF m_textureRect;
323  Color4f m_color;
324  VertexArray m_vertices;
325  RectF m_bounds;
326 
327  Color4f m_outlineColor;
328  float m_outlineThickness;
329  VertexArray m_outlineVertices;
330  };
331 
332 #ifndef DOXYGEN_SHOULD_SKIP_THIS
333 }
334 #endif
335 }
336 
337 #endif // GF_SHAPE_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
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
const Color4f & getOutlineColor() const
Get the outline color of the shape.
Definition: Shape.h:206
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
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
VertexBuffer commitOutlineGeometry() const
Create a buffer with the current outline geometry.
void updateGeometry()
Recompute the internal geometry of the shape.
virtual Vector2f getPoint(std::size_t index) const =0
Get a point of the shape.
A texture for colored images.
Definition: Texture.h:339
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
float getOutlineThickness() const
Get the outline thickness of the shape.
Definition: Shape.h:227
The namespace for gf classes.
Definition: Action.h:34
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
void setTextureRect(const RectF &rect)
Set the sub-rectangle of the texture that the shape will display.
const RectF & getTextureRect() const
Get the sub-rectangle of the texture displayed by the shape.
Definition: Shape.h:155
const Texture & getTexture() const
Get the source texture of the shape.
Definition: Shape.h:110
void setOutlineThickness(float thickness)
Set the thickness of the shape's outline.
void setOutlineColor(const Color4f &color)
Set the outline color of the shape.
bool hasTexture() const
Check if a texture is set.
Definition: Shape.h:121
Anchor
An anchor of a box.
Definition: Anchor.h:41
void setColor(const Color4f &color)
Set the fill color of the shape.
const Color4f & getColor() const
Get the fill color of the shape.
Definition: Shape.h:184
#define GF_API
Definition: Portability.h:35
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.