Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
TileLayer.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 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 <memory>
25 #include <vector>
26 
27 #include "Array2D.h"
28 #include "Flags.h"
29 #include "GraphicsApi.h"
30 #include "MapCell.h"
31 #include "TileProperties.h"
32 #include "Tileset.h"
33 #include "TileTypes.h"
34 #include "Transformable.h"
35 #include "VertexArray.h"
36 #include "VertexBuffer.h"
37 
38 namespace gf {
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 inline namespace v1 {
41 #endif
42 
43  class Texture;
44 
59  class GF_GRAPHICS_API TileLayer : public Transformable {
60  public:
64  static constexpr int NoTile = -1;
65 
69  TileLayer();
70 
71  static TileLayer createOrthogonal(Vector2i layerSize);
72 
73  static TileLayer createStaggered(Vector2i layerSize, MapCellAxis axis, MapCellIndex index);
74 
80  Vector2i getMapSize() const {
81  return m_tiles.getSize();
82  }
83 
94  std::size_t createTilesetId();
95 
102  Tileset& getTileset(std::size_t id);
103 
110  const Tileset& getTileset(std::size_t id) const;
111 
112 
126  void setTileSize(Vector2i tileSize);
127 
135  return m_tileSize;
136  }
137 
147  void setTile(Vector2i position, std::size_t tileset, int tile, Flags<Flip> flip = None);
148 
156  int getTile(Vector2i position) const;
157 
165  Flags<Flip> getFlip(Vector2i position) const;
166 
174  std::size_t getTileTileset(Vector2i position) const;
175 
179  void clear();
180 
194  RectF getLocalBounds() const;
195 
206  void setAnchor(Anchor anchor);
207 
216  VertexBuffer commitGeometry() const;
217 
218  virtual void draw(RenderTarget& target, const RenderStates& states) override;
219 
220  private:
221  TileLayer(Vector2i layerSize, TileOrientation orientation, std::unique_ptr<TileProperties> properties);
222 
223  private:
224  struct Cell {
225  std::size_t tileset = -1;
226  int tile = NoTile;
227  Flags<Flip> flip = gf::None;
228  };
229 
230  struct Sheet {
231  Tileset tileset;
232  VertexArray vertices;
233  };
234 
235  private:
236  void fillVertexArray(std::vector<Sheet>& sheets, RectI rect) const;
237  void updateGeometry();
238  RectI computeOffsets() const;
239 
240  private:
241  TileOrientation m_orientation;
242  std::unique_ptr<TileProperties> m_properties;
243 
244  Vector2i m_layerSize;
245  Vector2i m_tileSize;
246 
247  std::vector<Sheet> m_sheets;
248  RectI m_rect;
249 
250  Array2D<Cell> m_tiles;
251  };
252 
253 #ifndef DOXYGEN_SHOULD_SKIP_THIS
254 }
255 #endif
256 }
257 
258 #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:134
A set of primitives.
Definition: VertexArray.h:65
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
TileOrientation
The orientation of the tile.
Definition: TileTypes.h:37
Data in the graphics memory.
Definition: VertexBuffer.h:81
A tileset.
Definition: Tileset.h:48
GF_CORE_API Orientation orientation(float angle)
Get an orientation from an angle.
Vector2i getMapSize() const
Get the size of the layer.
Definition: TileLayer.h:80
The namespace for gf classes.
Definition: Action.h:35
A tile layer.
Definition: TileLayer.h:59
MapCellIndex
Map cell index in a map celled or hexagonal map.
Definition: MapCell.h:35
Anchor
An anchor of a box.
Definition: Anchor.h:38
MapCellAxis
Map cell axis in a map celled.
Definition: MapCell.h:46
No alignement.