Gamedev Framework (gf)  0.11.0
A C++14 framework for 2D games
TileLayer.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 "Flags.h"
26 #include "Flip.h"
27 #include "Portability.h"
28 #include "Transformable.h"
29 #include "VertexArray.h"
30 #include "VertexBuffer.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
37  class Texture;
38 
62  class GF_API TileLayer : public Transformable {
63  public:
67  static constexpr int NoTile = -1;
68 
74  TileLayer(Vector2u layerSize);
75 
93  void setTexture(const Texture& texture);
94 
104  const Texture& getTexture() const {
105  return *m_texture;
106  }
107 
115  bool hasTexture() const {
116  return m_texture != nullptr;
117  }
118 
126  void unsetTexture();
127 
134  void setTileSize(Vector2u tileSize);
135 
143  return m_tileSize;
144  }
145 
152  void setMargin(unsigned margin) {
153  setMargin({ margin, margin });
154  }
155 
162  void setMargin(Vector2u margin);
163 
170  Vector2u getMargin() const {
171  return m_margin;
172  }
173 
180  void setSpacing(unsigned spacing) {
181  setSpacing({ spacing, spacing });
182  }
183 
190  void setSpacing(Vector2u spacing);
191 
199  return m_spacing;
200  }
201 
218  void setBlockSize(Vector2u blockSize);
219 
228  Vector2u getBlockSize() const;
229 
238  void setTile(Vector2u position, int tile, Flags<Flip> flip = None);
239 
247  int getTile(Vector2u position) const;
248 
249 
257  Flags<Flip> getFlip(Vector2u position) const;
258 
262  void clear();
263 
277  RectF getLocalBounds() const;
278 
289  void setAnchor(Anchor anchor);
290 
299  VertexBuffer commitGeometry() const;
300 
301  virtual void draw(RenderTarget& target, const RenderStates& states) override;
302 
303  private:
304  struct Cell {
305  int tile;
306  Flags<Flip> flip;
307  };
308 
309  private:
310  void fillVertexArray(VertexArray& array, RectU rect) const;
311  void updateGeometry();
312 
313  private:
314  Vector2u m_layerSize;
315  Vector2u m_blockSize;
316 
317  const Texture *m_texture;
318  Vector2u m_tileSize;
319  Vector2u m_margin;
320  Vector2u m_spacing;
321 
322  Array2D<Cell> m_tiles;
323 
324  RectU m_rect;
325  VertexArray m_vertices;
326  };
327 
328 #ifndef DOXYGEN_SHOULD_SKIP_THIS
329 }
330 #endif
331 }
332 
333 #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:115
No alignement.
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:180
void setMargin(unsigned margin)
Set the margin of the tileset.
Definition: TileLayer.h:152
A texture for colored images.
Definition: Texture.h:339
Vector2u getTileSize() const
Get the tile size in the tileset.
Definition: TileLayer.h:142
Vector2u getSpacing() const
Get the spacing of the tileset.
Definition: TileLayer.h:198
The namespace for gf classes.
Definition: Action.h:35
const Texture & getTexture() const
Get the source texture of the tileset.
Definition: TileLayer.h:104
Vector2u getMargin() const
Get the margin of the tileset.
Definition: TileLayer.h:170
A tile layer.
Definition: TileLayer.h:62
Anchor
An anchor of a box.
Definition: Anchor.h:38