Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
TileLayer.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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 "Portability.h"
26 #include "Transformable.h"
27 #include "VertexArray.h"
28 #include "VertexBuffer.h"
29 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
35  class Texture;
36 
37  /**
38  * @ingroup graphics
39  * @brief A tile layer
40  *
41  * A tile layer represents a map made of tiles. gf::TileLayer makes it easy
42  * to draw a tile map.
43  *
44  * A tile layer is associated to a single tileset. A tileset is a texture
45  * that contains all the tiles, ordered in a grid. The tileset has several
46  * parameters that are inspired by the parameters in
47  * [Tiled](http://www.mapeditor.org/):
48  *
49  * - tile size: the size of a tile (`setTileSize()`, `getTileSize()`)
50  * - margin: the margin around the tiles (`setMargin()`, `getMargin()`), default: 0
51  * - spacing: the spacing between the tiles (`setSpacing()`, `getSpacing()`), default: 0
52  *
53  * The tile layer is given with an array of indices. Each index correspond
54  * to a tile in the tileset. Tile 0 correspond to the tile at the top left
55  * in the tileset. Then tile are numbered from left to right, and then from
56  * top to bottom. If a tile is not present in the tile layer, the constant
57  * gf::TileLayer::NoTile can be used.
58  *
59  */
60  class GF_API TileLayer : public Transformable {
61  public:
62  /**
63  * @brief A constant meaning that there is no tile
64  */
65  static constexpr int NoTile = -1;
66 
67  /**
68  * @brief Constructor
69  *
70  * @param layerSize the size of the layer, in number of tiles
71  */
72  TileLayer(Vector2u layerSize);
73 
74  /**
75  * @name Tileset parameters
76  * @{
77  */
78 
79  /**
80  * @brief Change the source texture of the tileset
81  *
82  * The texture must exist as long as the tile layer uses it. Indeed, the
83  * tile layer doesn't store its own copy of the texture, but rather keeps
84  * a pointer to the one that you passed to this function.
85  * If the source texture is destroyed and the tile layer tries to
86  * use it, the behavior is undefined.
87  *
88  * @param texture New texture
89  * @sa getTexture()
90  */
91  void setTexture(const Texture& texture);
92 
93  /**
94  * @brief Get the source texture of the tileset
95  *
96  * If the tile layer has no source texture, a `nullptr` pointer is returned.
97  * The returned pointer is const, which means that you can't
98  * modify the texture when you retrieve it with this function.
99  *
100  * @return Pointer to the tileset's texture
101  * @sa setTexture()
102  */
103  const Texture *getTexture() const {
104  return m_texture;
105  }
106 
107  /**
108  * @brief Set the tile size in the tileset
109  *
110  * @param tileSize The new tile size, in pixels
111  * @sa getTileSize()
112  */
113  void setTileSize(Vector2u tileSize);
114 
115  /**
116  * @brief Get the tile size in the tileset
117  *
118  * @return The tile size, in pixels
119  * @sa setTileSize()
120  */
121  Vector2u getTileSize() const {
122  return m_tileSize;
123  }
124 
125  /**
126  * @brief Set the margin of the tileset
127  *
128  * @param margin The margin, in pixels
129  * @sa getMargin()
130  */
131  void setMargin(unsigned margin) {
132  setMargin({ margin, margin });
133  }
134 
135  /**
136  * @brief Set the margin of the tileset
137  *
138  * @param margin The margin, in pixels
139  * @sa getMargin()
140  */
141  void setMargin(Vector2u margin);
142 
143  /**
144  * @brief Get the margin of the tileset
145  *
146  * @return The margin, in pixels
147  * @sa setMargin()
148  */
149  Vector2u getMargin() const {
150  return m_margin;
151  }
152 
153  /**
154  * @brief Set the spacing of the tileset
155  *
156  * @param spacing The spacing, in pixels
157  * @sa getSpacing()
158  */
159  void setSpacing(unsigned spacing) {
160  setSpacing({ spacing, spacing });
161  }
162 
163  /**
164  * @brief Set the spacing of the tileset
165  *
166  * @param spacing The spacing, in pixels
167  * @sa getSpacing()
168  */
169  void setSpacing(Vector2u spacing);
170 
171  /**
172  * @brief Get the spacing of the tileset
173  *
174  * @return The spacing, in pixels
175  * @sa setSpacing()
176  */
177  Vector2u getSpacing() const {
178  return m_spacing;
179  }
180 
181  /** @} */
182 
183  /**
184  * @name Tile definition
185  * @{
186  */
187 
188  /**
189  * @brief Set the block size
190  *
191  * The block size is the size of tiles in the layer. If not specified,
192  * it is the same as the tile size.
193  *
194  * @param blockSize The new size of the block, in pixels
195  * @sa getBlockSize()
196  */
197  void setBlockSize(Vector2u blockSize);
198 
199  /**
200  * @brief Get the block size
201  *
202  * The block size is the size of tiles in the layer. If not specified,
203  * it is the same as the tile size.
204  *
205  * @return The block size
206  */
207  Vector2u getBlockSize() const;
208 
209  /**
210  * @brief Set a tile
211  *
212  * @param position The position of the tile in the tile layer
213  * @param tile The number of the tile in the tileset or `gf::TileLayer::NoTile`
214  * @sa getTile()
215  */
216  void setTile(Vector2u position, int tile);
217 
218  /**
219  * @brief Get a tile
220  *
221  * @param position The position of the tile in the tile layer
222  * @return The number of the tile in the tileset or `gf::TileLayer::NoTile`
223  * @sa setTile()
224  */
225  int getTile(Vector2u position) const;
226 
227  /**
228  * @brief Remove all the tiles
229  */
230  void clear();
231 
232  /** @} */
233 
234  /**
235  * @brief Create a buffer with the current geometry
236  *
237  * The geometry is uploaded in the graphics memory so that it's faster
238  * to draw.
239  *
240  * @return A buffer with the current geometry
241  */
243 
244  virtual void draw(RenderTarget& target, RenderStates states) override;
245 
246  private:
247  void fillVertexArray(VertexArray& vertices, RectU rect) const;
248  void updateGeometry();
249 
250  private:
251  Vector2u m_layerSize;
252  Vector2u m_blockSize;
253 
254  const Texture *m_texture;
255  Vector2u m_tileSize;
256  Vector2u m_margin;
257  Vector2u m_spacing;
258 
259  Array2D<int> m_tiles;
260 
261  RectU m_rect;
262  VertexArray m_vertices;
263  };
264 
265 #ifndef DOXYGEN_SHOULD_SKIP_THIS
266 }
267 #endif
268 }
269 
270 #endif // GF_TILE_LAYER_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
A two-dimensional array.
Definition: Array2D.h:55
void setBlockSize(Vector2u blockSize)
Set the block size.
A set of primitives.
Definition: VertexArray.h:65
Vector2u getMargin() const
Get the margin of the tileset.
Definition: TileLayer.h:149
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:65
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
void setMargin(Vector2u margin)
Set the margin of the tileset.
constexpr Vector(T x, T y)
Constructor that takes 2 components.
Definition: Vector.h:348
Data in the graphics memory.
Definition: VertexBuffer.h:70
void setSpacing(unsigned spacing)
Set the spacing of the tileset.
Definition: TileLayer.h:159
void setSpacing(Vector2u spacing)
Set the spacing of the tileset.
void setMargin(unsigned margin)
Set the margin of the tileset.
Definition: TileLayer.h:131
A texture for colored images.
Definition: Texture.h:339
static constexpr int NoTile
A constant meaning that there is no tile.
Definition: TileLayer.h:65
const Texture * getTexture() const
Get the source texture of the tileset.
Definition: TileLayer.h:103
Vector2u getSpacing() const
Get the spacing of the tileset.
Definition: TileLayer.h:177
The namespace for gf classes.
Definition: Action.h:34
Vector2u getBlockSize() const
Get the block size.
void setTile(Vector2u position, int tile)
Set a tile.
A tile layer.
Definition: TileLayer.h:60
Vector2u getTileSize() const
Get the tile size in the tileset.
Definition: TileLayer.h:121
TileLayer(Vector2u layerSize)
Constructor.
void clear()
Remove all the tiles.
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
virtual void draw(RenderTarget &target, RenderStates states) override
Draw the object to a render target.
int getTile(Vector2u position) const
Get a tile.
void setTileSize(Vector2u tileSize)
Set the tile size in the tileset.
void setTexture(const Texture &texture)
Change the source texture of the tileset.
#define GF_API
Definition: Portability.h:35