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