Gamedev Framework (gf)  0.16.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 "TileTypes.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  TileLayer();
75 
83 
89  Vector2i getMapSize() const {
90  return m_tiles.getSize();
91  }
92 
110  void setTexture(const Texture& texture);
111 
121  const Texture& getTexture() const {
122  return *m_texture;
123  }
124 
132  bool hasTexture() const {
133  return m_texture != nullptr;
134  }
135 
143  void unsetTexture();
144 
151  void setTilesetTileSize(Vector2i tileSize);
152 
160  return m_tilesetTileSize;
161  }
162 
169  void setMargin(int margin) {
170  setMargin({ margin, margin });
171  }
172 
179  void setMargin(Vector2i margin);
180 
187  Vector2i getMargin() const {
188  return m_margin;
189  }
190 
197  void setSpacing(int spacing) {
198  setSpacing({ spacing, spacing });
199  }
200 
207  void setSpacing(Vector2i spacing);
208 
216  return m_spacing;
217  }
218 
225  void setOffset(Vector2i offset);
226 
233  Vector2i getOffset() const {
234  return m_offset;
235  }
236 
250  void setTileSize(Vector2i tileSize);
251 
259  return m_tileSize;
260  }
261 
270  void setTile(Vector2i position, int tile, Flags<Flip> flip = None);
271 
279  int getTile(Vector2i position) const;
280 
281 
289  Flags<Flip> getFlip(Vector2i position) const;
290 
294  void clear();
295 
309  RectF getLocalBounds() const;
310 
321  void setAnchor(Anchor anchor);
322 
331  VertexBuffer commitGeometry() const;
332 
333  virtual void draw(RenderTarget& target, const RenderStates& states) override;
334 
335  private:
336  struct Cell {
337  int tile;
338  Flags<Flip> flip;
339  };
340 
341  private:
342  void fillVertexArray(VertexArray& array, RectI rect) const;
343  void updateGeometry();
344 
345  private:
346  TileOrientation m_orientation;
347  MapCellIndex m_mapCellIndex;
348  MapCellAxis m_mapCellAxis;
349 
350  Vector2i m_layerSize;
351  Vector2i m_tileSize;
352 
353  const Texture *m_texture;
354  Vector2i m_tilesetTileSize;
355  Vector2i m_margin;
356  Vector2i m_spacing;
357  Vector2i m_offset;
358 
359  Array2D<Cell> m_tiles;
360 
361  RectI m_rect;
362  VertexArray m_vertices;
363  };
364 
365 #ifndef DOXYGEN_SHOULD_SKIP_THIS
366 }
367 #endif
368 }
369 
370 #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:258
A set of primitives.
Definition: VertexArray.h:65
bool hasTexture() const
Check if a texture is set.
Definition: TileLayer.h:132
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:187
Data in the graphics memory.
Definition: VertexBuffer.h:77
Orientation orientation(float angle)
Get an orientation from an angle.
A texture for colored images.
Definition: Texture.h:309
void setMargin(int margin)
Set the margin of the tileset.
Definition: TileLayer.h:169
Vector2i getMapSize() const
Get the size of the layer.
Definition: TileLayer.h:89
The namespace for gf classes.
Definition: Action.h:35
const Texture & getTexture() const
Get the source texture of the tileset.
Definition: TileLayer.h:121
Vector2i getOffset() const
Get the offset of the tileset.
Definition: TileLayer.h:233
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:197
A tile layer.
Definition: TileLayer.h:64
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:215
Vector2i getTilesetTileSize() const
Get the tile size in the tileset.
Definition: TileLayer.h:159
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