Gamedev Framework (gf)  0.12.0
A C++14 framework for 2D games
TileLayer.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "Stagger.h"
29 #include "Transformable.h"
30 #include "VertexArray.h"
31 #include "VertexBuffer.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
38  class Texture;
39 
63  class GF_API TileLayer : public Transformable {
64  public:
68  static constexpr int NoTile = -1;
69 
73  enum Type {
76  };
77 
84  TileLayer(Vector2u layerSize, Type type = Orthogonal);
85 
91  Vector2u getMapSize() const {
92  return m_tiles.getSize();
93  }
94 
112  void setTexture(const Texture& texture);
113 
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 
153  void setTileSize(Vector2u tileSize);
154 
162  return m_tileSize;
163  }
164 
171  void setMargin(unsigned margin) {
172  setMargin({ margin, margin });
173  }
174 
181  void setMargin(Vector2u margin);
182 
189  Vector2u getMargin() const {
190  return m_margin;
191  }
192 
199  void setSpacing(unsigned spacing) {
200  setSpacing({ spacing, spacing });
201  }
202 
209  void setSpacing(Vector2u spacing);
210 
218  return m_spacing;
219  }
220 
237  void setBlockSize(Vector2u blockSize);
238 
247  Vector2u getBlockSize() const;
248 
257  void setTile(Vector2u position, int tile, Flags<Flip> flip = None);
258 
266  int getTile(Vector2u position) const;
267 
268 
276  Flags<Flip> getFlip(Vector2u position) const;
277 
281  void clear();
282 
296  RectF getLocalBounds() const;
297 
308  void setAnchor(Anchor anchor);
309 
318  VertexBuffer commitGeometry() const;
319 
320  virtual void draw(RenderTarget& target, const RenderStates& states) override;
321 
322  private:
323  struct Cell {
324  int tile;
325  Flags<Flip> flip;
326  };
327 
328  private:
329  void fillVertexArray(VertexArray& array, RectU rect) const;
330  void updateGeometry();
331 
332  private:
333  Type m_type;
334  StaggerIndex m_staggerIndex;
335  StaggerAxis m_staggerAxis;
336 
337  Vector2u m_layerSize;
338  Vector2u m_blockSize;
339 
340  const Texture *m_texture;
341  Vector2u m_tileSize;
342  Vector2u m_margin;
343  Vector2u m_spacing;
344 
345  Array2D<Cell> m_tiles;
346 
347  RectU m_rect;
348  VertexArray m_vertices;
349  };
350 
351 #ifndef DOXYGEN_SHOULD_SKIP_THIS
352 }
353 #endif
354 }
355 
356 #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:134
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
StaggerAxis
Stagger axis in a staggered or hexagonal map.
Definition: Stagger.h:48
void setSpacing(unsigned spacing)
Set the spacing of the tileset.
Definition: TileLayer.h:199
Type
The type of tile layer.
Definition: TileLayer.h:73
void setMargin(unsigned margin)
Set the margin of the tileset.
Definition: TileLayer.h:171
Vector2u getMapSize() const
Get the size of the layer.
Definition: TileLayer.h:91
A texture for colored images.
Definition: Texture.h:339
Vector2u getTileSize() const
Get the tile size in the tileset.
Definition: TileLayer.h:161
Vector2u getSpacing() const
Get the spacing of the tileset.
Definition: TileLayer.h:217
The namespace for gf classes.
Definition: Action.h:35
const Texture & getTexture() const
Get the source texture of the tileset.
Definition: TileLayer.h:123
Vector2u getMargin() const
Get the margin of the tileset.
Definition: TileLayer.h:189
A tile layer.
Definition: TileLayer.h:63
A staggered tile layer.
Definition: TileLayer.h:75
Anchor
An anchor of a box.
Definition: Anchor.h:38
StaggerIndex
Stagger index in a staggered or hexagonal map.
Definition: Stagger.h:37
An orthogonal tile layer.
Definition: TileLayer.h:74
An orthogonal orientation.