Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Shapes.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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
264 void setPoint(std::size_t index, Vector2f point);
265
266 std::size_t getPointCount() const override;
267 Vector2f getPoint(std::size_t index) const override;
268
269 private:
270 std::vector<Vector2f> m_points;
271 };
272
273
298 class GF_GRAPHICS_API StarShape : public Shape {
299 public:
307 StarShape(float minRadius = 0, float maxRadius = 0, std::size_t branches = 7);
308
315 void setMinRadius(float minRadius);
316
323 float getMinRadius() const {
324 return m_minRadius;
325 }
326
333 void setMaxRadius(float maxRadius);
334
341 float getMaxRadius() const {
342 return m_maxRadius;
343 }
344
351 void setBranches(std::size_t branches);
352
359 std::size_t getBranches() const {
360 return m_branches;
361 }
362
363 std::size_t getPointCount() const override;
364 Vector2f getPoint(std::size_t index) const override;
365
366 private:
367 float m_minRadius;
368 float m_maxRadius;
369 std::size_t m_branches;
370 };
371
372
396 class GF_GRAPHICS_API RoundedRectangleShape : public Shape {
397 public:
405 RoundedRectangleShape(Vector2f size = Vector2f{ 0.0f, 0.0f }, float radius = 0.0f, std::size_t cornerPointCount = 8);
406
417 explicit RoundedRectangleShape(const RectF& rect, float radius = 0.0f, std::size_t cornerPointCount = 8);
418
426 void setSize(Vector2f size);
427
436 return m_size;
437 }
438
446 void setRadius(float radius);
447
455 float getRadius() const {
456 return m_radius;
457 }
458
466 void setCornerPointCount(std::size_t cornerPointCount);
467
468 std::size_t getPointCount() const override;
469 Vector2f getPoint(std::size_t index) const override;
470
471 private:
472 Vector2f m_size;
473 float m_radius;
474 std::size_t m_cornerPointCount;
475 };
476
477
478 class GF_GRAPHICS_API Pie : public Shape {
479 public:
483 };
484
485 Pie(float radius, float angle0, float angle1, Variation variation = Positive, std::size_t pointCount = 30);
486
487 void setRadius(float radius);
488
489 float getRadius() const {
490 return m_radius;
491 }
492
493 void setAngleRange(float angle0, float angle1, Variation variation);
494
495 void setPointCount(std::size_t pointCount);
496
497 std::size_t getPointCount() const override;
498 Vector2f getPoint(std::size_t index) const override;
499
500 private:
501 void adjustAngles(Variation variation);
502
503 private:
504 float m_radius;
505 float m_angle0;
506 float m_angle1;
507 std::size_t m_pointCount;
508 };
509
510#ifndef DOXYGEN_SHOULD_SKIP_THIS
511}
512#endif
513}
514
515#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)
Set a point in the shape.
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:478
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:480
@ Negative
Definition: Shapes.h:482
@ Positive
Definition: Shapes.h:481
float getRadius() const
Definition: Shapes.h:489
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:396
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:455
Vector2f getSize() const
Get the size of the rectangle.
Definition: Shapes.h:435
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:298
StarShape(float minRadius=0, float maxRadius=0, std::size_t branches=7)
Default constructor.
float getMinRadius() const
Get the minimum radius.
Definition: Shapes.h:323
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:359
Vector2f getPoint(std::size_t index) const override
Get a point of the shape.
float getMaxRadius() const
Get the maximum radius.
Definition: Shapes.h:341
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.
Utility class for manipulating circles.
Definition: Circ.h:61