Gamedev Framework (gf)  0.8.0
A C++14 framework for 2D games
Shape.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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_SHAPE_H
25 #define GF_SHAPE_H
26 
27 #include "Portability.h"
28 #include "Transformable.h"
29 #include "VertexArray.h"
30 #include "VertexBuffer.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
37  class Texture;
38 
73  class GF_API Shape : public Transformable {
74  public:
78  Shape();
79 
98  void setTexture(const Texture& texture, bool resetRect = false);
99 
110  const Texture& getTexture() const {
111  return *m_texture;
112  }
113 
121  bool hasTexture() const {
122  return m_texture != nullptr;
123  }
124 
132  void unsetTexture();
133 
146  void setTextureRect(const RectF& rect);
147 
155  const RectF& getTextureRect() const {
156  return m_textureRect;
157  }
158 
175  void setColor(const Color4f& color);
176 
184  const Color4f& getColor() const {
185  return m_color;
186  }
187 
197  void setOutlineColor(const Color4f& color);
198 
206  const Color4f& getOutlineColor() const {
207  return m_outlineColor;
208  }
209 
219  void setOutlineThickness(float thickness);
220 
227  float getOutlineThickness() const {
228  return m_outlineThickness;
229  }
230 
237  virtual std::size_t getPointCount() const = 0;
238 
254  virtual Vector2f getPoint(std::size_t index) const = 0;
255 
267  RectF getLocalBounds() const;
268 
279  void setAnchor(Anchor anchor);
280 
281 
290  VertexBuffer commitGeometry() const;
291 
300  VertexBuffer commitOutlineGeometry() const;
301 
302  virtual void draw(RenderTarget& target, RenderStates states) override;
303 
304  protected:
312  void updateGeometry();
313 
314  private:
315  void updateColors();
316  void updateTexCoords();
317  void updateOutline();
318  void updateOutlineColors();
319 
320  private:
321  const Texture *m_texture;
322  RectF m_textureRect;
323  Color4f m_color;
324  VertexArray m_vertices;
325  RectF m_bounds;
326 
327  Color4f m_outlineColor;
328  float m_outlineThickness;
329  VertexArray m_outlineVertices;
330  };
331 
332 #ifndef DOXYGEN_SHOULD_SKIP_THIS
333 }
334 #endif
335 }
336 
337 #endif // GF_SHAPE_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
Base class for textured shapes with outline.
Definition: Shape.h:73
A set of primitives.
Definition: VertexArray.h:65
const Color4f & getOutlineColor() const
Get the outline color of the shape.
Definition: Shape.h:206
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
Data in the graphics memory.
Definition: VertexBuffer.h:70
A texture for colored images.
Definition: Texture.h:339
float getOutlineThickness() const
Get the outline thickness of the shape.
Definition: Shape.h:227
The namespace for gf classes.
Definition: Action.h:34
const RectF & getTextureRect() const
Get the sub-rectangle of the texture displayed by the shape.
Definition: Shape.h:155
const Texture & getTexture() const
Get the source texture of the shape.
Definition: Shape.h:110
bool hasTexture() const
Check if a texture is set.
Definition: Shape.h:121
Anchor
An anchor of a box.
Definition: Anchor.h:38
const Color4f & getColor() const
Get the fill color of the shape.
Definition: Shape.h:184