Tile Map#

The TileMap class serves to implement a specific type of background which consists of a grid of individual block textures. It is a child of the Manager class, since it also handles a list of entites (each individual block).

Note

This class can still be optimized so as to not draw tiles that are off-screen. Actually, this should probably be done in Sprite::render for it to be more general (or not, calculations may be quicker in the TileMap because of symmetry etc.).

Typedefs

using map_shape = std::vector<std::vector<int>>#
class TileMap : public Manager#

Public Functions

inline TileMap(std::string mName, int mTwidth, int mTheight)#

Constructor. This will only set up some basic members.

Parameters:
  • mName – A name to give the TileMap object (mostly for debugging).

  • mTwidth – the tile width in game (will become a destination rectangle’s width).

  • mTheight – analogous to mTwidth but for the height.

void init(std::string texture_id, std::string metadata_path)#

Initialize TileMap with a given texture file and its metadata. This will touch texture related members (texture_id, sprite width and height, nb_ids, ids_per_row and texture_pos).

Parameters:
  • texture_id – the ID under which data is stored in the AssetManager.

  • metadata_path – the path to the text file in which information about the tilemap textures is stored (namely the shape of each texture, and the total number of IDs). The number of IDs per row is calculated automatically according to the size of the image. Note that this is not the map file.

void setup(int x_tiles, int y_tiles)#

Sets up the Entity vectors, according to the number of tiles passed. It will set up information about the position and sprite of the tiles, although their IDs will only be set later (in load_map), by effectively changing where the source rectangle is read from in the base texture file.

Ideally, in the future, this should be set up automatically in load_map.

Parameters:
  • x_tiles – Number of tiles (x axis).

  • y_tiles – Number of tiles (y axis).

void load_map(std::string path)#

Load the tile IDs and collision information from a map text file.

template<std::size_t xtiles, std::size_t ytiles>
void load_map(bool redo_setup, int id_array[xtiles][ytiles], std::string type)#

Eventually we could couple this with the previous function, so that this function is called by the other.

Load the tile IDs or collision information from an array of ints. We use double pointer to avoid having to declare the shape before-hand.

void set_position(int x, int y)#

This can be used to change where the grid starts.

inline int get_x0()#
inline int get_y0()#
inline int get_tile_width()#
inline int get_tile_height()#

Private Members

std::string name#

A name for the tile map (may help debugging):

< We create this as a Manager child class because it acts as one (managing multiple entities, which are the tile blocks).

int x0#
int y0#

Where to start drawing the tile map.

int tile_width#
int tile_height#

Dimensions of the tiles in the game window.

std::string texture_id#

Texture ID of the whole.

int sprite_width#
int sprite_height#

Dimensions of the tiles in the .bmp file.

int nb_ids#
int ids_per_row#

Number of textures (total) and how many per row.

int nb_xtiles#
int nb_ytiles#

Nb of tiles in each dimension (in the screen)

map_shape map#

Map grid information.

std::vector<std::array<int, 2>> texture_pos#

texture_pos[id][0] = x, texture_pos[id][1] = y