Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Curves.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 #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 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
41  class GF_GRAPHICS_API Line : public Curve {
42  public:
49  Line(Vector2f p0, Vector2f p1);
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:
182  CompoundCurve(Vector2f origin = Vector2f(0, 0));
183 
190  CompoundCurve& setOrigin(Vector2f origin);
191 
197  CompoundCurve& lineTo(Vector2f p1);
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 
232  CompoundCurve& clear(Vector2f origin = Vector2f(0, 0));
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
Type
The type of the spline.
Definition: Curves.h:137
A uniform spline ( )
Definition: Curves.h:138
A polyline.
Definition: Polyline.h:46
A uniform spline ( )
Definition: Curves.h:139
A line.
Definition: Curves.h:41
A Catmull–Rom spline.
Definition: Curves.h:126
A quadratic Bézier curve.
Definition: Curves.h:66
A curve is a one dimension object.
Definition: Curve.h:52
The namespace for gf classes.
Definition: Action.h:35
A cubic Bézier curve.
Definition: Curves.h:95
Vector< float, 2 > Vector2f
A float vector with 2 components.
Definition: Vector.h:1117
A uniform spline ( )
Definition: Curves.h:140
General purpose math vector.
Definition: Vector.h:61