Gamedev Framework (gf)  0.6.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  * The returned reference is const, which means that you can't
97  * modify the texture when you retrieve it with this function.
98  *
99  * @return Reference to the tileset's texture
100  * @sa setTexture()
101  */
102  const Texture& getTexture() const {
103  return *m_texture;
104  }
105 
106  /**
107  * @brief Check if a texture is set
108  *
109  * @returns True if a texture is already set
110  *
111  * @sa setTexture(), getTexture()
112  */
113  bool hasTexture() const {
114  return m_texture != nullptr;
115  }
116 
117  /**
118  * @brief Unset the source texture of the tile layer
119  *
120  * After a call to this function, the tile layer has no source texture.
121  *
122  * @sa setTexture()
123  */
124  void unsetTexture();
125 
126  /**
127  * @brief Set the tile size in the tileset
128  *
129  * @param tileSize The new tile size, in pixels
130  * @sa getTileSize()
131  */
132  void setTileSize(Vector2u tileSize);
133 
134  /**
135  * @brief Get the tile size in the tileset
136  *
137  * @return The tile size, in pixels
138  * @sa setTileSize()
139  */
140  Vector2u getTileSize() const {
141  return m_tileSize;
142  }
143 
144  /**
145  * @brief Set the margin of the tileset
146  *
147  * @param margin The margin, in pixels
148  * @sa getMargin()
149  */
150  void setMargin(unsigned margin) {
151  setMargin({ margin, margin });
152  }
153 
154  /**
155  * @brief Set the margin of the tileset
156  *
157  * @param margin The margin, in pixels
158  * @sa getMargin()
159  */
160  void setMargin(Vector2u margin);
161 
162  /**
163  * @brief Get the margin of the tileset
164  *
165  * @return The margin, in pixels
166  * @sa setMargin()
167  */
168  Vector2u getMargin() const {
169  return m_margin;
170  }
171 
172  /**
173  * @brief Set the spacing of the tileset
174  *
175  * @param spacing The spacing, in pixels
176  * @sa getSpacing()
177  */
178  void setSpacing(unsigned spacing) {
179  setSpacing({ spacing, spacing });
180  }
181 
182  /**
183  * @brief Set the spacing of the tileset
184  *
185  * @param spacing The spacing, in pixels
186  * @sa getSpacing()
187  */
188  void setSpacing(Vector2u spacing);
189 
190  /**
191  * @brief Get the spacing of the tileset
192  *
193  * @return The spacing, in pixels
194  * @sa setSpacing()
195  */
196  Vector2u getSpacing() const {
197  return m_spacing;
198  }
199 
200  /** @} */
201 
202  /**
203  * @name Tile definition
204  * @{
205  */
206 
207  /**
208  * @brief Set the block size
209  *
210  * The block size is the size of tiles in the layer. If not specified,
211  * it is the same as the tile size.
212  *
213  * @param blockSize The new size of the block, in pixels
214  * @sa getBlockSize()
215  */
216  void setBlockSize(Vector2u blockSize);
217 
218  /**
219  * @brief Get the block size
220  *
221  * The block size is the size of tiles in the layer. If not specified,
222  * it is the same as the tile size.
223  *
224  * @return The block size
225  */
226  Vector2u getBlockSize() const;
227 
228  /**
229  * @brief Set a tile
230  *
231  * @param position The position of the tile in the tile layer
232  * @param tile The number of the tile in the tileset or `gf::TileLayer::NoTile`
233  * @sa getTile()
234  */
235  void setTile(Vector2u position, int tile);
236 
237  /**
238  * @brief Get a tile
239  *
240  * @param position The position of the tile in the tile layer
241  * @return The number of the tile in the tileset or `gf::TileLayer::NoTile`
242  * @sa setTile()
243  */
244  int getTile(Vector2u position) const;
245 
246  /**
247  * @brief Remove all the tiles
248  */
249  void clear();
250 
251  /** @} */
252 
253  /**
254  * @brief Create a buffer with the current geometry
255  *
256  * The geometry is uploaded in the graphics memory so that it's faster
257  * to draw.
258  *
259  * @return A buffer with the current geometry
260  */
262 
263  virtual void draw(RenderTarget& target, RenderStates states) override;
264 
265  private:
266  void fillVertexArray(VertexArray& vertices, RectU rect) const;
267  void updateGeometry();
268 
269  private:
270  Vector2u m_layerSize;
271  Vector2u m_blockSize;
272 
273  const Texture *m_texture;
274  Vector2u m_tileSize;
275  Vector2u m_margin;
276  Vector2u m_spacing;
277 
278  Array2D<int> m_tiles;
279 
280  RectU m_rect;
281  VertexArray m_vertices;
282  };
283 
284 #ifndef DOXYGEN_SHOULD_SKIP_THIS
285 }
286 #endif
287 }
288 
289 #endif // GF_TILE_LAYER_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
void setBlockSize(Vector2u blockSize)
Set the block size.
A set of primitives.
Definition: VertexArray.h:65
Vector2u getBlockSize() const
Get the block size.
bool hasTexture() const
Check if a texture is set.
Definition: TileLayer.h:113
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
void setMargin(Vector2u margin)
Set the margin of the tileset.
Data in the graphics memory.
Definition: VertexBuffer.h:70
int getTile(Vector2u position) const
Get a tile.
void setSpacing(unsigned spacing)
Set the spacing of the tileset.
Definition: TileLayer.h:178
void setSpacing(Vector2u spacing)
Set the spacing of the tileset.
void setMargin(unsigned margin)
Set the margin of the tileset.
Definition: TileLayer.h:150
A texture for colored images.
Definition: Texture.h:339
Vector2u getTileSize() const
Get the tile size in the tileset.
Definition: TileLayer.h:140
Vector2u getSpacing() const
Get the spacing of the tileset.
Definition: TileLayer.h:196
static constexpr int NoTile
A constant meaning that there is no tile.
Definition: TileLayer.h:65
The namespace for gf classes.
Definition: Action.h:34
const Texture & getTexture() const
Get the source texture of the tileset.
Definition: TileLayer.h:102
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
void setTile(Vector2u position, int tile)
Set a tile.
Vector2u getMargin() const
Get the margin of the tileset.
Definition: TileLayer.h:168
A tile layer.
Definition: TileLayer.h:60
void unsetTexture()
Unset the source texture of the tile layer.
TileLayer(Vector2u layerSize)
Constructor.
void clear()
Remove all the tiles.
virtual void draw(RenderTarget &target, RenderStates states) override
Draw the object to a render target.
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