Gamedev Framework (gf)  0.14.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 
64  class GF_API TileLayer : public Transformable {
65  public:
69  static constexpr int NoTile = -1;
70 
74  enum Type {
77  };
78 
85  TileLayer(Vector2i layerSize, Type type = Orthogonal);
86 
92  Vector2i getMapSize() const {
93  return m_tiles.getSize();
94  }
95 
113  void setTexture(const Texture& texture);
114 
124  const Texture& getTexture() const {
125  return *m_texture;
126  }
127 
135  bool hasTexture() const {
136  return m_texture != nullptr;
137  }
138 
146  void unsetTexture();
147 
154  void setTilesetTileSize(Vector2i tileSize);
155 
163  return m_tilesetTileSize;
164  }
165 
172  void setMargin(int margin) {
173  setMargin({ margin, margin });
174  }
175 
182  void setMargin(Vector2i margin);
183 
190  Vector2i getMargin() const {
191  return m_margin;
192  }
193 
200  void setSpacing(int spacing) {
201  setSpacing({ spacing, spacing });
202  }
203 
210  void setSpacing(Vector2i spacing);
211 
219  return m_spacing;
220  }
221 
228  void setOffset(Vector2i offset);
229 
236  Vector2i getOffset() const {
237  return m_offset;
238  }
239 
253  void setTileSize(Vector2i tileSize);
254 
262  return m_tileSize;
263  }
264 
273  void setTile(Vector2i position, int tile, Flags<Flip> flip = None);
274 
282  int getTile(Vector2i position) const;
283 
284 
292  Flags<Flip> getFlip(Vector2i position) const;
293 
297  void clear();
298 
312  RectF getLocalBounds() const;
313 
324  void setAnchor(Anchor anchor);
325 
334  VertexBuffer commitGeometry() const;
335 
336  virtual void draw(RenderTarget& target, const RenderStates& states) override;
337 
338  private:
339  struct Cell {
340  int tile;
341  Flags<Flip> flip;
342  };
343 
344  private:
345  void fillVertexArray(VertexArray& array, RectI rect) const;
346  void updateGeometry();
347 
348  private:
349  Type m_type;
350  StaggerIndex m_staggerIndex;
351  StaggerAxis m_staggerAxis;
352 
353  Vector2i m_layerSize;
354  Vector2i m_tileSize;
355 
356  const Texture *m_texture;
357  Vector2i m_tilesetTileSize;
358  Vector2i m_margin;
359  Vector2i m_spacing;
360  Vector2i m_offset;
361 
362  Array2D<Cell> m_tiles;
363 
364  RectI m_rect;
365  VertexArray m_vertices;
366  };
367 
368 #ifndef DOXYGEN_SHOULD_SKIP_THIS
369 }
370 #endif
371 }
372 
373 #endif // GF_TILE_LAYER_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
Vector2i getTileSize() const
Get the tile size.
Definition: TileLayer.h:261
A set of primitives.
Definition: VertexArray.h:65
bool hasTexture() const
Check if a texture is set.
Definition: TileLayer.h:135
No alignement.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:73
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
Vector2i getMargin() const
Get the margin of the tileset.
Definition: TileLayer.h:190
Data in the graphics memory.
Definition: VertexBuffer.h:77
StaggerAxis
Stagger axis in a staggered or hexagonal map.
Definition: Stagger.h:48
Type
The type of tile layer.
Definition: TileLayer.h:74
A texture for colored images.
Definition: Texture.h:301
void setMargin(int margin)
Set the margin of the tileset.
Definition: TileLayer.h:172
Vector2i getMapSize() const
Get the size of the layer.
Definition: TileLayer.h:92
The namespace for gf classes.
Definition: Action.h:35
const Texture & getTexture() const
Get the source texture of the tileset.
Definition: TileLayer.h:124
Vector2i getOffset() const
Get the offset of the tileset.
Definition: TileLayer.h:236
void setSpacing(int spacing)
Set the spacing of the tileset.
Definition: TileLayer.h:200
A tile layer.
Definition: TileLayer.h:64
A staggered tile layer.
Definition: TileLayer.h:76
constexpr ArrayRef< T > array(const T *data, std::size_t size)
Create a constant reference to an array.
Definition: ArrayRef.h:203
Anchor
An anchor of a box.
Definition: Anchor.h:38
Vector2i getSpacing() const
Get the spacing of the tileset.
Definition: TileLayer.h:218
Vector2i getTilesetTileSize() const
Get the tile size in the tileset.
Definition: TileLayer.h:162
StaggerIndex
Stagger index in a staggered or hexagonal map.
Definition: Stagger.h:37
An orthogonal tile layer.
Definition: TileLayer.h:75
An orthogonal orientation.