Gamedev Framework (gf) 0.21.0
A C++17 framework for 2D games
Shapes.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2021 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 "GraphicsApi.h"
32#include "Polygon.h"
33#include "Rect.h"
34#include "Shape.h"
35#include "Vector.h"
36
37namespace gf {
38#ifndef DOXYGEN_SHOULD_SKIP_THIS
39inline namespace v1 {
40#endif
41
64 class GF_GRAPHICS_API RectangleShape : public Shape {
65 public:
71 RectangleShape(Vector2f size = Vector2f{ 0.0f, 0.0f });
72
81 explicit RectangleShape(const RectF& rect);
82
90 void setSize(Vector2f size);
91
99 Vector2f getSize() const {
100 return m_size;
101 }
102
103 std::size_t getPointCount() const override;
104 Vector2f getPoint(std::size_t index) const override;
105
106 private:
107 Vector2f m_size;
108 };
109
141 class GF_GRAPHICS_API CircleShape : public Shape {
142 public:
149 CircleShape(float radius = 0, std::size_t pointCount = 30);
150
160 explicit CircleShape(const CircF& circ, std::size_t pointCount = 30);
161
169 void setRadius(float radius);
170
178 float getRadius() const {
179 return m_radius;
180 }
181
189 void setPointCount(std::size_t pointCount);
190
191 std::size_t getPointCount() const override;
192 Vector2f getPoint(std::size_t index) const override;
193
194 private:
195 float m_radius;
196 std::size_t m_pointCount;
197 };
198
229 class GF_GRAPHICS_API ConvexShape : public Shape {
230 public:
236 ConvexShape(std::size_t pointCount);
237
243 ConvexShape(const Polygon& polygon);
244
254 void setPointCount(std::size_t pointCount);
255
263 void setPoint(std::size_t index, Vector2f point);
264
265 std::size_t getPointCount() const override;
266 Vector2f getPoint(std::size_t index) const override;
267
268 private:
269 std::vector<Vector2f> m_points;
270 };
271
272
297 class GF_GRAPHICS_API StarShape : public Shape {
298 public:
306 StarShape(float minRadius = 0, float maxRadius = 0, std::size_t branches = 7);
307
314 void setMinRadius(float minRadius);
315
322 float getMinRadius() const {
323 return m_minRadius;
324 }
325
332 void setMaxRadius(float maxRadius);
333
340 float getMaxRadius() const {
341 return m_maxRadius;
342 }
343
350 void setBranches(std::size_t branches);
351
358 std::size_t getBranches() const {
359 return m_branches;
360 }
361
362 std::size_t getPointCount() const override;
363 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
395 class GF_GRAPHICS_API RoundedRectangleShape : public Shape {
396 public:
404 RoundedRectangleShape(Vector2f size = Vector2f{ 0.0f, 0.0f }, float radius = 0.0f, std::size_t cornerPointCount = 8);
405
416 explicit RoundedRectangleShape(const RectF& rect, float radius = 0.0f, std::size_t cornerPointCount = 8);
417
425 void setSize(Vector2f size);
426
435 return m_size;
436 }
437
445 void setRadius(float radius);
446
454 float getRadius() const {
455 return m_radius;
456 }
457
465 void setCornerPointCount(std::size_t cornerPointCount);
466
467 std::size_t getPointCount() const override;
468 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 class GF_GRAPHICS_API Pie : public Shape {
478 public:
482 };
483
484 Pie(float radius, float angle0, float angle1, Variation variation = Positive, std::size_t pointCount = 30);
485
486 void setRadius(float radius);
487
488 float getRadius() const {
489 return m_radius;
490 }
491
492 void setAngleRange(float angle0, float angle1, Variation variation);
493
494 void setPointCount(std::size_t pointCount);
495
496 std::size_t getPointCount() const override;
497 Vector2f getPoint(std::size_t index) const override;
498
499 private:
500 void adjustAngles(Variation variation);
501
502 private:
503 float m_radius;
504 float m_angle0;
505 float m_angle1;
506 std::size_t m_pointCount;
507 };
508
509#ifndef DOXYGEN_SHOULD_SKIP_THIS
510}
511#endif
512}
513
514#endif // GF_SHAPES_H
Specialized shape representing a circle.
Definition: Shapes.h:141
CircleShape(const CircF &circ, std::size_t pointCount=30)
Constructor with a circle.
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.
float getRadius() const
Get the radius of the circle.
Definition: Shapes.h:178
Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
void setPointCount(std::size_t pointCount)
Set the number of points of the circle.
void setRadius(float radius)
Set the radius of the circle.
Specialized shape representing a convex polygon.
Definition: Shapes.h:229
ConvexShape(std::size_t pointCount)
Default constructor.
ConvexShape(const Polygon &polygon)
Constructor with a polygon.
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 setPointCount(std::size_t pointCount)
Set the number of points of the polygon.
Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
Definition: Shapes.h:477
Pie(float radius, float angle0, float angle1, Variation variation=Positive, std::size_t pointCount=30)
void setRadius(float radius)
void setPointCount(std::size_t pointCount)
Variation
Definition: Shapes.h:479
@ Negative
Definition: Shapes.h:481
@ Positive
Definition: Shapes.h:480
float getRadius() const
Definition: Shapes.h:488
Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
void setAngleRange(float angle0, float angle1, Variation variation)
std::size_t getPointCount() const override
Get the total number of points of the shape.
A convex polygon.
Definition: Polygon.h:47
Specialized shape representing a rectangle.
Definition: Shapes.h:64
void setSize(Vector2f size)
Set the size of the rectangle.
std::size_t getPointCount() const override
Get the total number of points of the shape.
Vector2f getSize() const
Get the size of the rectangle.
Definition: Shapes.h:99
RectangleShape(Vector2f size=Vector2f{ 0.0f, 0.0f })
Default constructor.
Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
RectangleShape(const RectF &rect)
Constructor with a rectangle.
Specialized shape representing a rounded rectangle.
Definition: Shapes.h:395
RoundedRectangleShape(const RectF &rect, float radius=0.0f, std::size_t cornerPointCount=8)
Constructor with a rectangle.
RoundedRectangleShape(Vector2f size=Vector2f{ 0.0f, 0.0f }, float radius=0.0f, std::size_t cornerPointCount=8)
Default constructor.
float getRadius() const
Get the radius of the corner.
Definition: Shapes.h:454
Vector2f getSize() const
Get the size of the rectangle.
Definition: Shapes.h:434
std::size_t getPointCount() const override
Get the total number of points of the shape.
void setCornerPointCount(std::size_t cornerPointCount)
Set the number of points of a corner.
void setRadius(float radius)
Set the radius of the corner.
Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
void setSize(Vector2f size)
Set the size of the rectangle.
Base class for textured shapes with outline.
Definition: Shape.h:73
Specialized shape representing a star.
Definition: Shapes.h:297
StarShape(float minRadius=0, float maxRadius=0, std::size_t branches=7)
Default constructor.
float getMinRadius() const
Get the minimum radius.
Definition: Shapes.h:322
std::size_t getPointCount() const override
Get the total number of points of the shape.
std::size_t getBranches() const
Get the number of branches.
Definition: Shapes.h:358
Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
float getMaxRadius() const
Get the maximum radius.
Definition: Shapes.h:340
void setBranches(std::size_t branches)
Set the number of branches.
void setMaxRadius(float maxRadius)
Set the maximum radius.
void setMinRadius(float minRadius)
Set the minimum radius.
The namespace for gf classes.
Definition: Action.h:35
Utility class for manipulating circles.
Definition: Circ.h:61