Gamedev Framework (gf)  0.20.0
A C++17 framework for 2D games
BasicSprite.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  * 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_BASIC_SPRITE_H
25 #define GF_BASIC_SPRITE_H
26 
27 #include "GraphicsApi.h"
28 #include "Rect.h"
29 #include "Span.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35  class Texture;
36  struct Vertex;
37 
46  class GF_GRAPHICS_API BasicSprite {
47  public:
53  BasicSprite();
54 
62  BasicSprite(const Texture& texture);
63 
72  BasicSprite(const Texture& texture, const RectF& textureRect);
73 
92  [[deprecated("You should use setTexture(const Texture&, const RectF&) instead")]]
93  void setTexture(const Texture& texture, bool resetRect);
94 
111  void setTexture(const Texture& texture, const RectF& textureRect = RectF::fromSize({ 1.0f, 1.0f }));
112 
123  const Texture& getTexture() const {
124  return *m_texture;
125  }
126 
134  bool hasTexture() const {
135  return m_texture != nullptr;
136  }
137 
145  void unsetTexture();
146 
162  void setTextureRect(const RectF& rect);
163 
171  const RectF& getTextureRect() const {
172  return m_textureRect;
173  }
174 
186  RectF getLocalBounds() const;
187 
188 
189  void updateGeometry(Span<Vertex> vertices);
190 
191  private:
192  const Texture *m_texture;
193  RectF m_textureRect;
194  RectF m_bounds;
195  };
196 
197 #ifndef DOXYGEN_SHOULD_SKIP_THIS
198 }
199 #endif
200 }
201 
202 #endif // GF_BASIC_SPRITE_H
bool hasTexture() const
Check if a texture is set.
Definition: BasicSprite.h:134
static constexpr Rect< float > fromSize(Vector< float, 2 > size) noexcept
Create a rectangle from a size.
Definition: Rect.h:129
A span.
Definition: Span.h:36
A texture for colored images.
Definition: Texture.h:313
const RectF & getTextureRect() const
Get the sub-rectangle of the texture displayed by the sprite.
Definition: BasicSprite.h:171
The namespace for gf classes.
Definition: Action.h:35
const Texture & getTexture() const
Get the source texture of the sprite.
Definition: BasicSprite.h:123
A basic sprite.
Definition: BasicSprite.h:46