Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Curves.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#ifndef GF_CURVES_H
22#define GF_CURVES_H
23
24#include <vector>
25
26#include "Curve.h"
27#include "GraphicsApi.h"
28#include "Polyline.h"
29
30namespace gf {
31#ifndef DOXYGEN_SHOULD_SKIP_THIS
32inline namespace v1 {
33#endif
34
41 class GF_GRAPHICS_API Line : public Curve {
42 public:
50
51 std::size_t getPointCount() const override;
52
53 Vector2f getPoint(std::size_t index) const override;
54
55 private:
56 Vector2f m_p0;
57 Vector2f m_p1;
58 };
59
66 class GF_GRAPHICS_API QuadraticBezierCurve : public Curve {
67 public:
76 QuadraticBezierCurve(Vector2f p0, Vector2f p1, Vector2f p2, std::size_t pointCount = 20);
77
78 std::size_t getPointCount() const override;
79
80 Vector2f getPoint(std::size_t index) const override;
81
82 private:
83 std::size_t m_pointCount;
84 Vector2f m_p0;
85 Vector2f m_p1;
86 Vector2f m_p2;
87 };
88
95 class GF_GRAPHICS_API CubicBezierCurve : public Curve {
96 public:
106 CubicBezierCurve(Vector2f p0, Vector2f p1, Vector2f p2, Vector2f p3, std::size_t pointCount = 30);
107
108 std::size_t getPointCount() const override;
109
110 Vector2f getPoint(std::size_t index) const override;
111
112 private:
113 std::size_t m_pointCount;
114 Vector2f m_p0;
115 Vector2f m_p1;
116 Vector2f m_p2;
117 Vector2f m_p3;
118 };
119
126 class GF_GRAPHICS_API SplineCurve : public Curve {
127 public:
137 enum Type {
141 };
142
149 SplineCurve(Type type = Centripetal, std::size_t pointCount = 30);
150
156 void setControlPoints(const Polyline& line);
157
158 std::size_t getPointCount() const override;
159
160 Vector2f getPoint(std::size_t index) const override;
161
162 private:
163 Type m_type;
164 std::size_t m_pointCount;
165 std::vector<Vector2f> m_points;
166 };
167
175 class GF_GRAPHICS_API CompoundCurve : public Curve {
176 public:
183
191
198
206 CompoundCurve& quadraticCurveTo(Vector2f p1, Vector2f p2, std::size_t pointCount = 20);
207
216 CompoundCurve& cubicCurveTo(Vector2f p1, Vector2f p2, Vector2f p3, std::size_t pointCount = 30);
217
223 void close();
224
233
234 std::size_t getPointCount() const override;
235
236 Vector2f getPoint(std::size_t index) const override;
237
238 private:
239 std::vector<Vector2f> m_points;
240 };
241
242
243
244#ifndef DOXYGEN_SHOULD_SKIP_THIS
245}
246#endif
247}
248
249#endif // GF_CURVES_H
A compound curve.
Definition: Curves.h:175
CompoundCurve & setOrigin(Vector2f origin)
Set the first point of the curve.
void close()
Close the curve.
Vector2f getPoint(std::size_t index) const override
Get a point of the curve.
CompoundCurve & cubicCurveTo(Vector2f p1, Vector2f p2, Vector2f p3, std::size_t pointCount=30)
Create a cubic Bézier curve from the last point to a new point.
CompoundCurve & quadraticCurveTo(Vector2f p1, Vector2f p2, std::size_t pointCount=20)
Create a quadratic Bézier curve from the last point to a new point.
CompoundCurve & clear(Vector2f origin=Vector2f(0, 0))
Reset the curve to a new origin.
std::size_t getPointCount() const override
Get the total number of points of the curve.
CompoundCurve & lineTo(Vector2f p1)
Create a line from the last point to a new point.
CompoundCurve(Vector2f origin=Vector2f(0, 0))
Constructor.
A cubic Bézier curve.
Definition: Curves.h:95
std::size_t getPointCount() const override
Get the total number of points of the curve.
CubicBezierCurve(Vector2f p0, Vector2f p1, Vector2f p2, Vector2f p3, std::size_t pointCount=30)
Constructor.
Vector2f getPoint(std::size_t index) const override
Get a point of the curve.
A curve is a one dimension object.
Definition: Curve.h:52
A line.
Definition: Curves.h:41
std::size_t getPointCount() const override
Get the total number of points of the curve.
Line(Vector2f p0, Vector2f p1)
Constructor.
Vector2f getPoint(std::size_t index) const override
Get a point of the curve.
A polyline.
Definition: Polyline.h:47
A quadratic Bézier curve.
Definition: Curves.h:66
Vector2f getPoint(std::size_t index) const override
Get a point of the curve.
QuadraticBezierCurve(Vector2f p0, Vector2f p1, Vector2f p2, std::size_t pointCount=20)
Constructor.
std::size_t getPointCount() const override
Get the total number of points of the curve.
A Catmull–Rom spline.
Definition: Curves.h:126
void setControlPoints(const Polyline &line)
Set the control points of the spline.
SplineCurve(Type type=Centripetal, std::size_t pointCount=30)
Constructor.
Vector2f getPoint(std::size_t index) const override
Get a point of the curve.
std::size_t getPointCount() const override
Get the total number of points of the curve.
Type
The type of the spline.
Definition: Curves.h:137
@ Chordal
A uniform spline ( )
Definition: Curves.h:139
@ Uniform
A uniform spline ( )
Definition: Curves.h:138
@ Centripetal
A uniform spline ( )
Definition: Curves.h:140
Vector< float, 2 > Vector2f
A float vector with 2 components.
Definition: Vector.h:1117
The namespace for gf classes.