Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Shape.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_SHAPE_H
25#define GF_SHAPE_H
26
27#include "GraphicsApi.h"
28#include "Transformable.h"
29#include "VertexArray.h"
30#include "VertexBuffer.h"
31
32namespace gf {
33#ifndef DOXYGEN_SHOULD_SKIP_THIS
34inline namespace v1 {
35#endif
36
37 class Texture;
38
73 class GF_GRAPHICS_API Shape : public Transformable {
74 public:
79
98 [[deprecated("You should use setTexture(const Texture&, const RectF&) instead")]]
99 void setTexture(const Texture& texture, bool resetRect);
100
117 void setTexture(const Texture& texture, const RectF& textureRect = RectF::fromSize({ 1.0f, 1.0f }));
118
129 const Texture& getTexture() const {
130 return *m_texture;
131 }
132
140 bool hasTexture() const {
141 return m_texture != nullptr;
142 }
143
152
165 void setTextureRect(const RectF& rect);
166
174 const RectF& getTextureRect() const {
175 return m_textureRect;
176 }
177
194 void setColor(const Color4f& color);
195
203 const Color4f& getColor() const {
204 return m_color;
205 }
206
216 void setOutlineColor(const Color4f& color);
217
225 const Color4f& getOutlineColor() const {
226 return m_outlineColor;
227 }
228
238 void setOutlineThickness(float thickness);
239
246 float getOutlineThickness() const {
247 return m_outlineThickness;
248 }
249
256 virtual std::size_t getPointCount() const = 0;
257
273 virtual Vector2f getPoint(std::size_t index) const = 0;
274
287
298 void setAnchor(Anchor anchor);
299
300
310
320
321 virtual void draw(RenderTarget& target, const RenderStates& states) override;
322
323 protected:
332
338 void updateBounds(RectF bounds);
339
344
345 private:
346 void updateColors();
347 void updateTexCoords();
348 void updateOutline();
349 void updateOutlineColors();
350
351 private:
352 const Texture *m_texture;
353 RectF m_textureRect;
354 Color4f m_color;
355 VertexArray m_vertices;
356 RectF m_bounds;
357
358 Color4f m_outlineColor;
359 float m_outlineThickness;
360 VertexArray m_outlineVertices;
361 };
362
363#ifndef DOXYGEN_SHOULD_SKIP_THIS
364}
365#endif
366}
367
368#endif // GF_SHAPE_H
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
Base class for textured shapes with outline.
Definition: Shape.h:73
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
void updateGeometry()
Recompute the internal geometry of the shape.
virtual std::size_t getPointCount() const =0
Get the total number of points of the shape.
void updateAutoBounds()
Compute the bounds of the shape automatically.
void setTexture(const Texture &texture, bool resetRect)
Change the source texture of the shape.
void unsetTexture()
Unset the source texture of the shape.
void setOutlineColor(const Color4f &color)
Set the outline color of the shape.
bool hasTexture() const
Check if a texture is set.
Definition: Shape.h:140
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
virtual void draw(RenderTarget &target, const RenderStates &states) override
Draw the object to a render target.
float getOutlineThickness() const
Get the outline thickness of the shape.
Definition: Shape.h:246
Shape()
Default constructor.
void updateBounds(RectF bounds)
Set the bounds of the shape directly.
const Color4f & getOutlineColor() const
Get the outline color of the shape.
Definition: Shape.h:225
virtual Vector2f getPoint(std::size_t index) const =0
Get a point of the shape.
void setOutlineThickness(float thickness)
Set the thickness of the shape's outline.
VertexBuffer commitOutlineGeometry() const
Create a buffer with the current outline geometry.
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
void setColor(const Color4f &color)
Set the fill color of the shape.
void setTexture(const Texture &texture, const RectF &textureRect=RectF::fromSize({ 1.0f, 1.0f }))
Change the source texture of the shape.
const Color4f & getColor() const
Get the fill color of the shape.
Definition: Shape.h:203
const RectF & getTextureRect() const
Get the sub-rectangle of the texture displayed by the shape.
Definition: Shape.h:174
const Texture & getTexture() const
Get the source texture of the shape.
Definition: Shape.h:129
void setTextureRect(const RectF &rect)
Set the sub-rectangle of the texture that the shape will display.
A texture for colored images.
Definition: Texture.h:313
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
A set of primitives.
Definition: VertexArray.h:65
Data in the graphics memory.
Definition: VertexBuffer.h:81
Anchor
An anchor of a box.
Definition: Anchor.h:38
@ Texture
A GPU texture.
The namespace for gf classes.
static constexpr Rect< float > fromSize(Vector< float, 2 > size) noexcept
Create a rectangle from a size.
Definition: Rect.h:114
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A 4D vector.
Definition: Vector.h:852