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