Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
TileLayer.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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_TILE_LAYER_H
22 #define GF_TILE_LAYER_H
23 
24 #include "Array2D.h"
25 #include "Portability.h"
26 #include "Transformable.h"
27 #include "VertexArray.h"
28 #include "VertexBuffer.h"
29 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
35  class Texture;
36 
60  class GF_API TileLayer : public Transformable {
61  public:
65  static constexpr int NoTile = -1;
66 
72  TileLayer(Vector2u layerSize);
73 
91  void setTexture(const Texture& texture);
92 
102  const Texture& getTexture() const {
103  return *m_texture;
104  }
105 
113  bool hasTexture() const {
114  return m_texture != nullptr;
115  }
116 
124  void unsetTexture();
125 
132  void setTileSize(Vector2u tileSize);
133 
141  return m_tileSize;
142  }
143 
150  void setMargin(unsigned margin) {
151  setMargin({ margin, margin });
152  }
153 
160  void setMargin(Vector2u margin);
161 
168  Vector2u getMargin() const {
169  return m_margin;
170  }
171 
178  void setSpacing(unsigned spacing) {
179  setSpacing({ spacing, spacing });
180  }
181 
188  void setSpacing(Vector2u spacing);
189 
197  return m_spacing;
198  }
199 
216  void setBlockSize(Vector2u blockSize);
217 
226  Vector2u getBlockSize() const;
227 
235  void setTile(Vector2u position, int tile);
236 
244  int getTile(Vector2u position) const;
245 
249  void clear();
250 
261  VertexBuffer commitGeometry() const;
262 
263  virtual void draw(RenderTarget& target, RenderStates states) override;
264 
265  private:
266  void fillVertexArray(VertexArray& vertices, RectU rect) const;
267  void updateGeometry();
268 
269  private:
270  Vector2u m_layerSize;
271  Vector2u m_blockSize;
272 
273  const Texture *m_texture;
274  Vector2u m_tileSize;
275  Vector2u m_margin;
276  Vector2u m_spacing;
277 
278  Array2D<int> m_tiles;
279 
280  RectU m_rect;
281  VertexArray m_vertices;
282  };
283 
284 #ifndef DOXYGEN_SHOULD_SKIP_THIS
285 }
286 #endif
287 }
288 
289 #endif // GF_TILE_LAYER_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
A set of primitives.
Definition: VertexArray.h:65
bool hasTexture() const
Check if a texture is set.
Definition: TileLayer.h:113
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
void setSpacing(unsigned spacing)
Set the spacing of the tileset.
Definition: TileLayer.h:178
void setMargin(unsigned margin)
Set the margin of the tileset.
Definition: TileLayer.h:150
A texture for colored images.
Definition: Texture.h:339
Vector2u getTileSize() const
Get the tile size in the tileset.
Definition: TileLayer.h:140
Vector2u getSpacing() const
Get the spacing of the tileset.
Definition: TileLayer.h:196
The namespace for gf classes.
Definition: Action.h:34
const Texture & getTexture() const
Get the source texture of the tileset.
Definition: TileLayer.h:102
Vector2u getMargin() const
Get the margin of the tileset.
Definition: TileLayer.h:168
A tile layer.
Definition: TileLayer.h:60