Gamedev Framework (gf)  0.1.0
A C++11 framework for 2D games
Curve.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 #ifndef GF_CURVE_H
22 #define GF_CURVE_H
23 
24 #include <cstddef>
25 
26 #include "Portability.h"
27 #include "Rect.h"
28 #include "Transformable.h"
29 #include "Vector.h"
30 #include "VertexArray.h"
31 #include "VertexBuffer.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
38  /**
39  * @ingroup graphics
40  * @brief A curve is a one dimension object
41  *
42  * A curve is similar to a shape but for one dimension objects like lines,
43  * [Bézier curves](https://en.wikipedia.org/wiki/B%C3%A9zier_curve), or
44  * compound curves.
45  *
46  * A curve can be simple or outlined. A simple curve has no outline and is
47  * generally thin. An outline curve is more complex and can have an outline
48  * and has no limit on the width. By default, curves are outlined.
49  *
50  * @sa gf::Line, gf::QuadraticBezierCurve, gf::CubicBezierCurve, gf::CompoundCurve
51  */
52  class GF_API Curve : public Transformable {
53  public:
54  /**
55  * @brief The type of the curve
56  */
57  enum Type {
58  Simple, ///< A simple curve with no outline
59  Outlined, ///< An outlined curve
60  };
61 
62  /**
63  * @brief Default constructor
64  */
65  Curve();
66 
67  /**
68  * @brief Set the type of the curve
69  *
70  * By default, a curve is outlined.
71  *
72  * @param type The type of the curve
73  */
74  void setType(Type type);
75 
76  /**
77  * @brief Return the type of the curve
78  *
79  * @return The current type of the curve
80  */
81  Type getType() const noexcept {
82  return m_type;
83  }
84 
85  /**
86  * @brief Set the fill color of the curve
87  *
88  * This color is modulated (multiplied) with the curve's
89  * texture if any. It can be used to colorize the curve,
90  * or change its global opacity.
91  *
92  * By default, the curve's fill color is opaque white.
93  *
94  * @param color New color of the curve
95  *
96  * @sa getColor(), setOutlineColor()
97  */
98  void setColor(const Color4f& color);
99 
100  /**
101  * @brief Get the fill color of the curve
102  *
103  * @return Fill color of the curve
104  *
105  * @sa setColor()
106  */
107  const Color4f& getColor() const {
108  return m_color;
109  }
110 
111  /**
112  * @brief Set the width of the curve
113  *
114  * @param width The width of the curve
115  */
116  void setWidth(float width);
117 
118  /**
119  * @brief Get the width of the curve
120  *
121  * @return The current width of the curve
122  */
123  float getWidth() const {
124  return m_width;
125  }
126 
127  /**
128  * @brief Set the outline color of the curve
129  *
130  * By default, the curve's outline color is opaque white.
131  *
132  * @param color New outline color of the curve
133  *
134  * @sa getOutlineColor(), setColor()
135  */
136  void setOutlineColor(const Color4f& color);
137 
138  /**
139  * @brief Get the outline color of the curve
140  *
141  * @return Outline color of the curve
142  *
143  * @sa setOutlineColor()
144  */
145  const Color4f& getOutlineColor() const {
146  return m_outlineColor;
147  }
148 
149  /**
150  * @brief Set the thickness of the curve's outline
151  *
152  * By default, the outline thickness is @f$ 0 @f$.
153  *
154  * @param thickness New outline thickness
155  *
156  * @sa getOutlineThickness()
157  */
158  void setOutlineThickness(float thickness);
159 
160  /**
161  * @brief Get the outline thickness of the curve
162  *
163  * @return Outline thickness of the curve
164  *
165  * @sa setOutlineThickness()
166  */
167  float getOutlineThickness() const {
168  return m_outlineThickness;
169  }
170 
171  /**
172  * @brief Get the total number of points of the curve
173  *
174  * @return Number of points of the curve
175  * @sa getPoint()
176  */
177  virtual std::size_t getPointCount() const = 0;
178 
179  /**
180  * @brief Get a point of the curve
181  *
182  * The returned point is in local coordinates, that is,
183  * the curve's transforms (position, rotation, scale) are
184  * not taken into account.
185  *
186  * The result is undefined if `index` is out of the valid range.
187  *
188  * @param index Index of the point to get, in range @f$ [0, n-1] @f$
189  * where @f$ n @f$ is the number of points of the curve.
190  *
191  * @return index-th point of the curve
192  * @sa getPointCount()
193  */
194  virtual Vector2f getPoint(std::size_t index) const = 0;
195 
196  /**
197  * @brief Get the local bounding rectangle of the entity
198  *
199  * The returned rectangle is in local coordinates, which means
200  * that it ignores the transformations (translation, rotation,
201  * scale, ...) that are applied to the entity.
202  * In other words, this function returns the bounds of the
203  * entity in the entity's coordinate system.
204  *
205  * @return Local bounding rectangle of the entity
206  */
207  RectF getLocalBounds() const;
208 
209  /**
210  * @brief Set the anchor origin of the entity
211  *
212  * Compute the origin of the entity based on the local bounds and
213  * the specified anchor. Internally, this function calls
214  * `Transformable::setOrigin()`.
215  *
216  * @param anchor The anchor of the entity
217  * @sa getLocalBounds(), Transformable::setOrigin()
218  */
219  void setAnchor(Anchor anchor);
220 
221  /**
222  * @brief Create a buffer with the current geometry
223  *
224  * The geometry is uploaded in the graphics memory so that it's faster
225  * to draw.
226  *
227  * @return A buffer with the current geometry
228  */
230 
231  /**
232  * @brief Create a buffer with the current outline geometry
233  *
234  * The geometry is uploaded in the graphics memory so that it's faster
235  * to draw.
236  *
237  * @return A buffer with the current outline geometry
238  */
240 
241  virtual void draw(RenderTarget& target, RenderStates states);
242 
243  protected:
244  /**
245  * @brief Recompute the internal geometry of the curve
246  *
247  * This function must be called by the derived class everytime
248  * the curve's points change (i.e. the result of either
249  * getPointCount() or getPoint() is different).
250  */
251  void updateGeometry();
252 
253  /**
254  * @brief Set the curve closed
255  *
256  * This function must be called by derived class when the curve must be
257  * closed, i.e. the last point is connected to the first point with a
258  * line.
259  *
260  * @param closed True if the curve is closed
261  */
262  void setClosed(bool closed = true);
263 
264  private:
265  void updateColors();
266  void updateOutline();
267  void updateOutlineColors();
268 
269  void computeVertices(VertexArray& vertices, float halfWidth);
270  private:
271  Type m_type;
272  bool m_closed;
273 
274  Color4f m_color;
275  float m_width;
276  VertexArray m_vertices;
277  RectF m_bounds;
278 
279  Color4f m_outlineColor;
280  float m_outlineThickness;
281  VertexArray m_outlineVertices;
282  };
283 
284 #ifndef DOXYGEN_SHOULD_SKIP_THIS
285 }
286 #endif
287 }
288 
289 #endif // GF_CURVE_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:109
void setColor(const Color4f &color)
Set the fill color of the curve.
void setOutlineThickness(float thickness)
Set the thickness of the curve's outline.
float getWidth() const
Get the width of the curve.
Definition: Curve.h:123
A set of primitives.
Definition: VertexArray.h:65
virtual std::size_t getPointCount() const =0
Get the total number of points of the curve.
void setType(Type type)
Set the type of the curve.
Vector< float, 2 > Vector2f
A float vector with 2 components.
Definition: Vector.h:741
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:65
VertexBuffer commitOutlineGeometry() const
Create a buffer with the current outline geometry.
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:81
Type
The type of the curve.
Definition: Curve.h:57
Data in the graphics memory.
Definition: VertexBuffer.h:70
void updateGeometry()
Recompute the internal geometry of the curve.
Curve()
Default constructor.
A simple curve with no outline.
Definition: Curve.h:58
A curve is a one dimension object.
Definition: Curve.h:52
virtual void draw(RenderTarget &target, RenderStates states)
Draw the object to a render target.
void setWidth(float width)
Set the width of the curve.
Rect< float > RectF
A float rectangle.
Definition: Rect.h:306
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
float getOutlineThickness() const
Get the outline thickness of the curve.
Definition: Curve.h:167
Definition: Action.h:34
Type getType() const noexcept
Return the type of the curve.
Definition: Curve.h:81
An outlined curve.
Definition: Curve.h:59
const Color4f & getColor() const
Get the fill color of the curve.
Definition: Curve.h:107
Vector< float, 4 > Color4f
A float color vector with 4 components.
Definition: Vector.h:855
void setClosed(bool closed=true)
Set the curve closed.
Anchor
The origin anchor of the transformable object.
Definition: Transformable.h:45
virtual Vector2f getPoint(std::size_t index) const =0
Get a point of the curve.
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
#define GF_API
Definition: Portability.h:35
const Color4f & getOutlineColor() const
Get the outline color of the curve.
Definition: Curve.h:145
void setOutlineColor(const Color4f &color)
Set the outline color of the curve.