Gamedev Framework (gf)  0.1.0
A C++11 framework for 2D games
Shapes.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_SHAPES_H
25 #define GF_SHAPES_H
26 
27 #include <cstddef>
28 #include <vector>
29 
30 #include "Portability.h"
31 #include "Shape.h"
32 #include "Vector.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
39  /**
40  * @ingroup graphics
41  * @brief Specialized shape representing a rectangle
42  *
43  * This class inherits all the functions of gf::Transformable
44  * (position, rotation, scale, bounds, ...) as well as the
45  * functions of gf::Shape (outline, color, texture, ...).
46  *
47  * Usage example:
48  *
49  * ~~~{.cc}
50  * gf::RectangleShape rectangle;
51  * rectangle.setSize({ 100.0f, 50.0f });
52  * rectangle.setOutlineColor(gf::Color::Red);
53  * rectangle.setOutlineThickness(5);
54  * rectangle.setPosition({ 10.0f, 20.0f });
55  * ...
56  * window.draw(rectangle);
57  * ~~~
58  *
59  * @sa gf::Shape
60  */
61  class GF_API RectangleShape : public Shape {
62  public:
63  /**
64  * @brief Default constructor
65  *
66  * @param size Size of the rectangle
67  */
68  RectangleShape(Vector2f size = Vector2f{ 0.0f, 0.0f });
69 
70  /**
71  * @brief Set the size of the rectangle
72  *
73  * @param size New size of the rectangle
74  *
75  * @sa getSize()
76  */
77  void setSize(Vector2f size);
78 
79  /**
80  * @brief Get the size of the rectangle
81  *
82  * @return Size of the rectangle
83  *
84  * @sa setSize()
85  */
86  Vector2f getSize() const {
87  return m_size;
88  }
89 
90  virtual std::size_t getPointCount() const override;
91  virtual Vector2f getPoint(std::size_t index) const override;
92 
93  private:
94  Vector2f m_size;
95  };
96 
97  /**
98  * @ingroup graphics
99  * @brief Specialized shape representing a circle
100  *
101  * This class inherits all the functions of gf::Transformable
102  * (position, rotation, scale, bounds, ...) as well as the
103  * functions of gf::Shape (outline, color, texture, ...).
104  *
105  * Usage example:
106  *
107  * ~~~{.cc}
108  * gf::CircleShape circle;
109  * circle.setRadius(150);
110  * circle.setOutlineColor(gf::Color::Red);
111  * circle.setOutlineThickness(5);
112  * circle.setPosition({ 10.0f, 20.0f });
113  * ...
114  * window.draw(circle);
115  * ~~~
116  *
117  * Since the graphics card can't draw perfect circles, we have to
118  * fake them with multiple triangles connected to each other. The
119  * "points count" property of gf::CircleShape defines how many of these
120  * triangles to use, and therefore defines the quality of the circle.
121  *
122  * The number of points can also be used for another purpose; with
123  * small numbers you can create any regular polygon shape:
124  * equilateral triangle, square, pentagon, hexagon, ...
125  *
126  * @sa gf::Shape
127  */
128  class GF_API CircleShape : public Shape {
129  public:
130  /**
131  * @brief Default constructor
132  *
133  * @param radius Radius of the circle
134  * @param pointCount Number of points composing the circle
135  */
136  CircleShape(float radius = 0, std::size_t pointCount = 30);
137 
138  /**
139  * @brief Set the radius of the circle
140  *
141  * @param radius New radius of the circle
142  *
143  * @sa getRadius()
144  */
145  void setRadius(float radius);
146 
147  /**
148  * @brief Get the radius of the circle
149  *
150  * @return Radius of the circle
151  *
152  * @sa setRadius()
153  */
154  float getRadius() const {
155  return m_radius;
156  }
157 
158  /**
159  * @brief Set the number of points of the circle
160  *
161  * @param pointCount New number of points of the circle
162  *
163  * @sa getPointCount()
164  */
165  void setPointCount(std::size_t pointCount);
166 
167  virtual std::size_t getPointCount() const override;
168  virtual Vector2f getPoint(std::size_t index) const override;
169 
170  private:
171  float m_radius;
172  std::size_t m_pointCount;
173  };
174 
175  /**
176  * @ingroup graphics
177  * @brief Specialized shape representing a convex polygon
178  *
179  * This class inherits all the functions of gf::Transformable
180  * (position, rotation, scale, bounds, ...) as well as the
181  * functions of gf::Shape (outline, color, texture, ...).
182  *
183  * It is important to keep in mind that a convex shape must
184  * always be... convex, otherwise it may not be drawn correctly.
185  * Moreover, the points must be defined in order; using a random
186  * order would result in an incorrect shape.
187  *
188  * Usage example:
189  *
190  * ~~~{.cc}
191  * gf::ConvexShape polygon;
192  * polygon.setPointCount(3);
193  * polygon.setPoint(0, { 0.0f, 0.0f });
194  * polygon.setPoint(1, { 0.0f, 10.0f });
195  * polygon.setPoint(2, { 25.0f, 5.0f });
196  * polygon.setOutlineColor(gf::Color::Red);
197  * polygon.setOutlineThickness(5);
198  * polygon.setPosition({ 10.0f, 20.0f });
199  * ...
200  * window.draw(polygon);
201  * ~~~
202  *
203  * @sa gf::Shape
204  */
205  class GF_API ConvexShape : public Shape {
206  public:
207  /**
208  * @brief Default constructor
209  *
210  * @param pointCount Number of points of the polygon
211  */
212  ConvexShape(std::size_t pointCount);
213 
214  /**
215  * @brief Set the number of points of the polygon
216  *
217  * `pointCount` must be greater than 2 to define a valid shape.
218  *
219  * @param pointCount New number of points of the polygon
220  *
221  * @sa getPointCount()
222  */
223  void setPointCount(std::size_t pointCount);
224 
225  /**
226  * @brief Get the number of points of the polygon
227  *
228  * @return Number of points of the polygon
229  *
230  * @sa setPointCount(), getPoint()
231  */
232  void setPoint(std::size_t index, Vector2f point);
233 
234  virtual std::size_t getPointCount() const override;
235  virtual Vector2f getPoint(std::size_t index) const override;
236 
237  private:
238  std::vector<Vector2f> m_points;
239  };
240 
241 
242  /**
243  * @ingroup graphics
244  * @brief Specialized shape representing a star
245  *
246  * This class inherits all the functions of gf::Transformable
247  * (position, rotation, scale, bounds, ...) as well as the
248  * functions of gf::Shape (outline, color, texture, ...).
249  *
250  * Usage example:
251  *
252  * ~~~{.cc}
253  * gf::StarShape star;
254  * star.setMinRadius(25.0f);
255  * star.setMaxRadius(50.0f);
256  * star.setBranches(5);
257  * star.setOutlineColor(gf::Color::Red);
258  * star.setOutlineThickness(5);
259  * star.setPosition({ 10.0f, 20.0f });
260  * ...
261  * window.draw(star);
262  * ~~~
263  *
264  * @sa gf::Shape
265  */
266  class GF_API StarShape : public Shape {
267  public:
268  /**
269  * @brief Default constructor
270  *
271  * @param minRadius Minimum radius
272  * @param maxRadius Maximum radius
273  * @param branches Number of branches of the star
274  */
275  StarShape(float minRadius = 0, float maxRadius = 0, std::size_t branches = 7);
276 
277  /**
278  * @brief Set the minimum radius
279  *
280  * @param minRadius Minimum radius
281  * @sa getMinRadius()
282  */
283  void setMinRadius(float minRadius);
284 
285  /**
286  * @brief Get the minimum radius
287  *
288  * @return The minimum radius
289  * @sa setMinRadius()
290  */
291  float getMinRadius() const {
292  return m_minRadius;
293  }
294 
295  /**
296  * @brief Set the maximum radius
297  *
298  * @param maxRadius Maximum radius
299  * @sa getMaxRadius()
300  */
301  void setMaxRadius(float maxRadius);
302 
303  /**
304  * @brief Get the maximum radius
305  *
306  * @return The maximum radius
307  * @sa setMaxRadius()
308  */
309  float getMaxRadius() const {
310  return m_maxRadius;
311  }
312 
313  /**
314  * @brief Set the number of branches
315  *
316  * @param branches Number of branches
317  * @sa getBranches()
318  */
319  void setBranches(std::size_t branches);
320 
321  /**
322  * @brief Get the number of branches
323  *
324  * @return The number of branches
325  * @sa setBranches()
326  */
327  std::size_t getBranches() const {
328  return m_branches;
329  }
330 
331  virtual std::size_t getPointCount() const override;
332  virtual Vector2f getPoint(std::size_t index) const override;
333 
334  private:
335  float m_minRadius;
336  float m_maxRadius;
337  std::size_t m_branches;
338  };
339 
340 
341  /**
342  * @ingroup graphics
343  * @brief Specialized shape representing a rounded rectangle
344  *
345  * This class inherits all the functions of gf::Transformable
346  * (position, rotation, scale, bounds, ...) as well as the
347  * functions of gf::Shape (outline, color, texture, ...).
348  *
349  * Usage example:
350  *
351  * ~~~{.cc}
352  * gf::RoundedRectangleShape rectangle;
353  * rectangle.setSize({ 100.0f, 50.0f });
354  * rectangle.setRadius(10);
355  * rectangle.setOutlineColor(gf::Color::Red);
356  * rectangle.setOutlineThickness(5);
357  * rectangle.setPosition({ 10.0f, 20.0f });
358  * ...
359  * window.draw(rect);
360  * ~~~
361  *
362  * @sa gf::Shape
363  */
365  public:
366  /**
367  * @brief Default constructor
368  *
369  * @param size Size of the rectangle
370  * @param radius Radius of the corner
371  * @param cornerPointCount Number of points in the corner
372  */
373  RoundedRectangleShape(Vector2f size = Vector2f{ 0.0f, 0.0f }, float radius = 0.0f, std::size_t cornerPointCount = 8);
374 
375  /**
376  * @brief Set the size of the rectangle
377  *
378  * @param size New size of the rectangle
379  *
380  * @sa getSize()
381  */
382  void setSize(Vector2f size);
383 
384  /**
385  * @brief Get the size of the rectangle
386  *
387  * @return Size of the rectangle
388  *
389  * @sa setSize()
390  */
391  Vector2f getSize() const {
392  return m_size;
393  }
394 
395  /**
396  * @brief Set the radius of the corner
397  *
398  * @param radius New radius of the corner
399  *
400  * @sa getRadius()
401  */
402  void setRadius(float radius);
403 
404  /**
405  * @brief Get the radius of the corner
406  *
407  * @return Radius of the corner
408  *
409  * @sa setRadius()
410  */
411  float getRadius() const {
412  return m_radius;
413  }
414 
415  /**
416  * @brief Set the number of points of a corner
417  *
418  * `cornerPointCount` must be greater than 2 to define a valid shape.
419  *
420  * @param cornerPointCount New number of points of a corner
421  */
422  void setCornerPointCount(std::size_t cornerPointCount);
423 
424  virtual std::size_t getPointCount() const override;
425  virtual Vector2f getPoint(std::size_t index) const override;
426 
427  private:
428  Vector2f m_size;
429  float m_radius;
430  std::size_t m_cornerPointCount;
431  };
432 
433 
434 #ifndef DOXYGEN_SHOULD_SKIP_THIS
435 }
436 #endif
437 }
438 
439 #endif // GF_SHAPES_H
StarShape(float minRadius=0, float maxRadius=0, std::size_t branches=7)
Default constructor.
Base class for textured shapes with outline.
Definition: Shape.h:73
virtual Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
void setSize(Vector2f size)
Set the size of the rectangle.
virtual Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
void setMinRadius(float minRadius)
Set the minimum radius.
Vector< float, 2 > Vector2f
A float vector with 2 components.
Definition: Vector.h:741
float getRadius() const
Get the radius of the circle.
Definition: Shapes.h:154
virtual Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
Specialized shape representing a rounded rectangle.
Definition: Shapes.h:364
void setRadius(float radius)
Set the radius of the circle.
void setRadius(float radius)
Set the radius of the corner.
float getMaxRadius() const
Get the maximum radius.
Definition: Shapes.h:309
virtual std::size_t getPointCount() const override
Get the total number of points of the shape.
virtual std::size_t getPointCount() const override
Get the total number of points of the shape.
void setPoint(std::size_t index, Vector2f point)
Get the number of points of the polygon.
void setSize(Vector2f size)
Set the size of the rectangle.
constexpr Vector(T x, T y)
Constructor that takes 2 components.
Definition: Vector.h:293
Specialized shape representing a rectangle.
Definition: Shapes.h:61
virtual std::size_t getPointCount() const override
Get the total number of points of the shape.
CircleShape(float radius=0, std::size_t pointCount=30)
Default constructor.
void setBranches(std::size_t branches)
Set the number of branches.
void setPointCount(std::size_t pointCount)
Set the number of points of the polygon.
std::size_t getBranches() const
Get the number of branches.
Definition: Shapes.h:327
RectangleShape(Vector2f size=Vector2f{0.0f, 0.0f})
Default constructor.
Vector2f getSize() const
Get the size of the rectangle.
Definition: Shapes.h:86
Definition: Action.h:34
Specialized shape representing a circle.
Definition: Shapes.h:128
virtual std::size_t getPointCount() const override
Get the total number of points of the shape.
virtual std::size_t getPointCount() const override
Get the total number of points of the shape.
ConvexShape(std::size_t pointCount)
Default constructor.
Specialized shape representing a star.
Definition: Shapes.h:266
virtual Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
virtual Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
void setCornerPointCount(std::size_t cornerPointCount)
Set the number of points of a corner.
float getRadius() const
Get the radius of the corner.
Definition: Shapes.h:411
void setMaxRadius(float maxRadius)
Set the maximum radius.
#define GF_API
Definition: Portability.h:35
float getMinRadius() const
Get the minimum radius.
Definition: Shapes.h:291
Specialized shape representing a convex polygon.
Definition: Shapes.h:205
void setPointCount(std::size_t pointCount)
Set the number of points of the circle.
Vector2f getSize() const
Get the size of the rectangle.
Definition: Shapes.h:391
RoundedRectangleShape(Vector2f size=Vector2f{0.0f, 0.0f}, float radius=0.0f, std::size_t cornerPointCount=8)
Default constructor.