If you’re enjoying this tutorial, please consider supporting it by purchasing a book through one of the links on my site, such as this one ->
- textureID: the id of the texture that contains the tiles sprite. This will be returned by our texture allocator.
- tileID: the id of the tile as defined by our Tiled application (more on this in a bit).
- textureRect: the location of the tile in the sprite sheet.
- BuildTileSheetData: reads the XML file and loads in the tileset used. Currently, we only have support for one tileset but this will change shortly.
- BuildMapTiles: this returns the data for each tile in each layer. We need this data to create our final Objects.
- BuildLayer: this creates and returns the data for one layer within the time map.
- rapid XML parser requires the file location to be in the c string format (char*) rather than the C++ string format. There are pros and cons to each but for now, all we need to be concerned with is that before we can load the XML file we need to convert between the two. Mac and windows handle this in slightly different ways unfortunately so, for now, we’ll add some branching logic for the compiler. We’ll change this in future when we write a multi-format string copy.
- Once we’ve created the tilemap we can iterate over each layer, and each tile in the layer and create an Object to represent the tile. We use its shared properties for information on the tiles sprite and we use the tilemap data (tile size and scale) and the tiles position in the grid to work out where to place the tile in the world space. When we create a tile Object we are only concerned about what it looks like (sprite) and where it is in the world (transform position) for the moment.
For the tileset, we retrieve specific data from the XML document. We store the:
- Width of tile sprite
- Height of tile sprite
- Number of tiles in the tileset
- Number of columns in the tileset
- Number of rows in tileset (calculated by dividing the number of tiles by columns)
- The texture id for the tileset (retrieved using our texture allocator)
- Width of the tileset
- Height of the tileset
The steps to read a tile are:
- Get the next integer. We split at each comma.
- Check if the value can be parsed as an integer using a function we will write shortly. If it cannot be parsed, we remove special characters that are used for carriage returns and new lines.
- Parse the value to an int.
- Check if the int is not 0, which is represents a space with no tile.
- Try to find the tiles id in our tileset.
- Create new tile data if the tile was not found in the tileset.
- Create a new tile and set its shared properties using the value from the tileset.
- Add the new tile to our collection.
This is repeated for each value in the dataset.