Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Tileset.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 #ifndef GF_TILESET_H
22 #define GF_TILESET_H
23 
24 #include "GraphicsApi.h"
25 #include "Texture.h"
26 #include "Vector.h"
27 
28 namespace gf {
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 inline namespace v1 {
31 #endif
32 
48  class GF_GRAPHICS_API Tileset {
49  public:
50  Tileset();
51 
64  void setTexture(const Texture& texture);
65 
75  const Texture& getTexture() const {
76  return *m_texture;
77  }
78 
86  bool hasTexture() const {
87  return m_texture != nullptr;
88  }
89 
97  void unsetTexture();
98 
105  void setTileSize(Vector2i tileSize);
106 
114  return m_tileSize;
115  }
116 
123  void setMargin(int margin) {
124  setMargin({ margin, margin });
125  }
126 
133  void setMargin(Vector2i margin);
134 
141  Vector2i getMargin() const {
142  return m_margin;
143  }
144 
151  void setSpacing(int spacing) {
152  setSpacing({ spacing, spacing });
153  }
154 
161  void setSpacing(Vector2i spacing);
162 
170  return m_spacing;
171  }
172 
179  void setOffset(Vector2i offset);
180 
187  Vector2i getOffset() const {
188  return m_offset;
189  }
190 
196  Vector2i getSize() const {
197  return m_size;
198  }
199 
206  RectF computeTextureCoords(int tile) const;
207 
214  RectF computeTextureCoords(Vector2i coords) const;
215 
216  private:
217  void updateSize();
218 
219  private:
220  const Texture *m_texture;
221  Vector2i m_tileSize;
222  Vector2i m_margin;
223  Vector2i m_spacing;
224  Vector2i m_offset;
225  Vector2i m_size;
226  };
227 
228 #ifndef DOXYGEN_SHOULD_SKIP_THIS
229 }
230 #endif
231 }
232 
233 #endif // GF_TILESET_H
void setMargin(int margin)
Set the margin of the tileset.
Definition: Tileset.h:123
Vector2i getMargin() const
Get the margin of the tileset.
Definition: Tileset.h:141
void setSpacing(int spacing)
Set the spacing of the tileset.
Definition: Tileset.h:151
A tileset.
Definition: Tileset.h:48
bool hasTexture() const
Check if a texture is set.
Definition: Tileset.h:86
Vector2i getTileSize() const
Get the tile size in the tileset.
Definition: Tileset.h:113
A texture for colored images.
Definition: Texture.h:313
const Texture & getTexture() const
Get the source texture of the tileset.
Definition: Tileset.h:75
Vector2i getSize() const
Get the size of the tileset.
Definition: Tileset.h:196
The namespace for gf classes.
Definition: Action.h:35
Vector2i getOffset() const
Get the offset of the tileset.
Definition: Tileset.h:187
Vector2i getSpacing() const
Get the spacing of the tileset.
Definition: Tileset.h:169