Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Sprite.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_SPRITE_H
25#define GF_SPRITE_H
26
27#include "BasicSprite.h"
28#include "GraphicsApi.h"
29#include "Transformable.h"
30#include "Vertex.h"
31#include "VertexBuffer.h"
32
33namespace gf {
34#ifndef DOXYGEN_SHOULD_SKIP_THIS
35inline namespace v1 {
36#endif
37 class Texture;
38
75 class GF_GRAPHICS_API Sprite : public Transformable {
76 public:
83
91 Sprite(const Texture& texture);
92
101 Sprite(const Texture& texture, const RectF& textureRect);
102
121 [[deprecated("You should use setTexture(const Texture&, const RectF&) instead")]]
122 void setTexture(const Texture& texture, bool resetRect);
123
140 void setTexture(const Texture& texture, const RectF& textureRect = RectF::fromSize({ 1.0f, 1.0f }));
141
152 const Texture& getTexture() const {
153 return m_basic.getTexture();
154 }
155
163 bool hasTexture() const {
164 return m_basic.hasTexture();
165 }
166
175
191 void setTextureRect(const RectF& rect);
192
200 const RectF& getTextureRect() const {
201 return m_basic.getTextureRect();
202 }
203
217 void setColor(const Color4f& color);
218
226 const Color4f& getColor() const;
227
228
241
252 void setAnchor(Anchor anchor);
253
263
264 virtual void draw(RenderTarget& target, const RenderStates& states) override;
265
266 private:
267 BasicSprite m_basic;
268 Vertex m_vertices[4];
269 };
270
271#ifndef DOXYGEN_SHOULD_SKIP_THIS
272}
273#endif
274}
275
276#endif // GF_SPRITE_H
A basic sprite.
Definition: BasicSprite.h:46
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
A drawable representation of a texture, with its own transformations, color, etc.
Definition: Sprite.h:75
void setTexture(const Texture &texture, const RectF &textureRect=RectF::fromSize({ 1.0f, 1.0f }))
Change the source texture of the sprite.
const Texture & getTexture() const
Get the source texture of the sprite.
Definition: Sprite.h:152
void setTexture(const Texture &texture, bool resetRect)
Change the source texture of the sprite.
Sprite(const Texture &texture)
Construct the sprite from a source texture.
Sprite(const Texture &texture, const RectF &textureRect)
Construct the sprite from a sub-rectangle of a source texture.
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
void unsetTexture()
Unset the source texture of the sprite.
const Color4f & getColor() const
Get the global color of the sprite.
virtual void draw(RenderTarget &target, const RenderStates &states) override
Draw the object to a render target.
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
void setColor(const Color4f &color)
Set the global color of the sprite.
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
void setTextureRect(const RectF &rect)
Set the sub-rectangle of the texture that the sprite will display.
const RectF & getTextureRect() const
Get the sub-rectangle of the texture displayed by the sprite.
Definition: Sprite.h:200
Sprite()
Default constructor.
bool hasTexture() const
Check if a texture is set.
Definition: Sprite.h:163
A texture for colored images.
Definition: Texture.h:313
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
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
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75