Gamedev Framework (gf)  0.1.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  */
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 
132  /**
133  * @brief Get the margin of the tileset
134  *
135  * @return The margin, in pixels
136  * @sa setMargin()
137  */
138  unsigned getMargin() const {
139  return m_margin;
140  }
141 
142  /**
143  * @brief Set the spacing of the tileset
144  *
145  * @param spacing The spacing, in pixels
146  * @sa getSpacing()
147  */
148  void setSpacing(unsigned spacing);
149 
150  /**
151  * @brief Get the spacing of the tileset
152  *
153  * @return The spacing, in pixels
154  * @sa setSpacing()
155  */
156  unsigned getSpacing() const {
157  return m_spacing;
158  }
159 
160  /** @} */
161 
162  /**
163  * @name Tile definition
164  * @{
165  */
166 
167  /**
168  * @brief Set a tile
169  *
170  * @param position The position of the tile in the tile layer
171  * @param tile The number of the tile in the tileset or `gf::TileLayer::NoTile`
172  * @sa getTile()
173  */
174  void setTile(Vector2u position, int tile);
175 
176  /**
177  * @brief Get a tile
178  *
179  * @param position The position of the tile in the tile layer
180  * @return The number of the tile in the tileset or `gf::TileLayer::NoTile`
181  * @sa setTile()
182  */
183  int getTile(Vector2u position) const;
184 
185  /** @} */
186 
187  virtual void draw(RenderTarget& target, RenderStates states) override;
188 
189  private:
190  void updateGeometry();
191 
192  private:
193  Vector2u m_layerSize;
194  Vector2u m_tileSize;
195 
196  const Texture *m_texture;
197 
198  unsigned m_margin;
199  unsigned m_spacing;
200 
201  Array2D<int> m_tiles;
202 
203  RectU m_rect;
204  VertexArray m_vertices;
205  };
206 
207 #ifndef DOXYGEN_SHOULD_SKIP_THIS
208 }
209 #endif
210 }
211 
212 #endif // GF_TILE_LAYER_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:109
A two-dimensional array.
Definition: Array2D.h:55
Vector< unsigned, 2 > Vector2u
A unsigned vector with 2 components.
Definition: Vector.h:795
A set of primitives.
Definition: VertexArray.h:65
Rect< unsigned > RectU
A unsigned rectangle.
Definition: Rect.h:318
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:65
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:81
unsigned getMargin() const
Get the margin of the tileset.
Definition: TileLayer.h:138
void setSpacing(unsigned spacing)
Set the spacing of the tileset.
void setMargin(unsigned margin)
Set the margin of the tileset.
A texture for colored images.
Definition: Texture.h:317
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
Definition: Action.h:34
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.
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
unsigned getSpacing() const
Get the spacing of the tileset.
Definition: TileLayer.h:156