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