Gamedev Framework (gf)  0.17.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 "Portability.h"
27 #include "Stagger.h"
28 #include "Tileset.h"
29 #include "TileTypes.h"
30 #include "Transformable.h"
31 #include "VertexArray.h"
32 #include "VertexBuffer.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
39  class Texture;
40 
65  class GF_API TileLayer : public Transformable {
66  public:
70  static constexpr int NoTile = -1;
71 
75  TileLayer();
76 
84 
90  Vector2i getMapSize() const {
91  return m_tiles.getSize();
92  }
93 
111  void setTexture(const Texture& texture) {
112  m_tileset.setTexture(texture);
113  }
114 
124  const Texture& getTexture() const {
125  return m_tileset.getTexture();
126  }
127 
135  bool hasTexture() const {
136  return m_tileset.hasTexture();
137  }
138 
146  void unsetTexture() {
147  m_tileset.unsetTexture();
148  }
149 
156  void setTilesetTileSize(Vector2i tileSize) {
157  m_tileset.setTileSize(tileSize);
158  }
159 
167  return m_tileset.getTileSize();
168  }
169 
176  void setMargin(int margin) {
177  m_tileset.setMargin(margin);
178  }
179 
186  void setMargin(Vector2i margin) {
187  m_tileset.setMargin(margin);
188  }
189 
196  Vector2i getMargin() const {
197  return m_tileset.getMargin();
198  }
199 
206  void setSpacing(int spacing) {
207  m_tileset.setSpacing(spacing);
208  }
209 
216  void setSpacing(Vector2i spacing) {
217  m_tileset.setSpacing(spacing);
218  }
219 
227  return m_tileset.getSpacing();
228  }
229 
236  void setOffset(Vector2i offset) {
237  m_tileset.setOffset(offset);
238  }
239 
246  Vector2i getOffset() const {
247  return m_tileset.getOffset();
248  }
249 
263  void setTileSize(Vector2i tileSize);
264 
272  return m_tileSize;
273  }
274 
283  void setTile(Vector2i position, int tile, Flags<Flip> flip = None);
284 
292  int getTile(Vector2i position) const;
293 
294 
302  Flags<Flip> getFlip(Vector2i position) const;
303 
307  void clear();
308 
322  RectF getLocalBounds() const;
323 
334  void setAnchor(Anchor anchor);
335 
344  VertexBuffer commitGeometry() const;
345 
346  virtual void draw(RenderTarget& target, const RenderStates& states) override;
347 
348  private:
349  struct Cell {
350  int tile;
351  Flags<Flip> flip;
352  };
353 
354  private:
355  void fillVertexArray(VertexArray& array, RectI rect) const;
356  void updateGeometry();
357 
358  private:
359  TileOrientation m_orientation;
360  MapCellIndex m_mapCellIndex;
361  MapCellAxis m_mapCellAxis;
362 
363  Vector2i m_layerSize;
364  Vector2i m_tileSize;
365 
366  Tileset m_tileset;
367 
368  Array2D<Cell> m_tiles;
369 
370  RectI m_rect;
371  VertexArray m_vertices;
372  };
373 
374 #ifndef DOXYGEN_SHOULD_SKIP_THIS
375 }
376 #endif
377 }
378 
379 #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:271
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:90
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:196
void setTilesetTileSize(Vector2i tileSize)
Set the tile size in the tileset.
Definition: TileLayer.h:156
Data in the graphics memory.
Definition: VertexBuffer.h:77
A tileset.
Definition: Tileset.h:39
Orientation orientation(float angle)
Get an orientation from an angle.
A texture for colored images.
Definition: Texture.h:309
void setSpacing(Vector2i spacing)
Set the spacing of the tileset.
Definition: TileLayer.h:216
void setMargin(int margin)
Set the margin of the tileset.
Definition: TileLayer.h:176
void setMargin(Vector2i margin)
Set the margin of the tileset.
Definition: TileLayer.h:186
Vector2i getMapSize() const
Get the size of the layer.
Definition: TileLayer.h:90
void setOffset(Vector2i offset)
Set the offset of the tileset.
Definition: TileLayer.h:236
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:246
MapCellIndex
Map cell index in a map celled or hexagonal map.
Definition: MapCell.h:35
void setSpacing(int spacing)
Set the spacing of the tileset.
Definition: TileLayer.h:206
A tile layer.
Definition: TileLayer.h:65
void unsetTexture()
Unset the source texture of the tile layer.
Definition: TileLayer.h:146
constexpr ArrayRef< T > array(const T *data, std::size_t size)
Create a constant reference to an array.
Definition: ArrayRef.h:204
Anchor
An anchor of a box.
Definition: Anchor.h:38
Vector2i getSpacing() const
Get the spacing of the tileset.
Definition: TileLayer.h:226
Vector2i getTilesetTileSize() const
Get the tile size in the tileset.
Definition: TileLayer.h:166
void setTexture(const Texture &texture)
Change the source texture of the tileset.
Definition: TileLayer.h:111
An orthogonal orientation.
MapCellAxis
Map cell axis in a map celled.
Definition: MapCell.h:46
TileOrientation
The orientation of the tile.
Definition: TileTypes.h:37