an C++11 library for reading TMX files
libtmx provides some useful tools for rendering tiles from a TMX map, even if
libtmx is not itself a rendering library. This tutorial provides some hints on how to render tiles in a neutral way. You will have to adapt to your drawing toolkit. More precisely, we assume you use a visitor and want to render a
tmx::TileLayer. Here is the procedure.
First step, let's get some information about the size of the map and the size of the tiles to compute the size of the resulting surface.
Second step, we visit the tile layer.
First, it's not necessary to render the layer if it's not visible. This visibility property can be set in Tiled. Then, we visit every cell in the layer. Cells are organised in a row-major order, so we can compute the column
i and row
j of the cell, and then the coordinates
y of the tile in the rendering surface. Finally, if the
gid is an actual
gid, then we draw the tile.
Third step, we draw each tile on the rendering surface.
Once the tileset has been computed (according to the documentation of the TMX format), we can compute the id of the tile inside the tileset. Then, in the case of Tiled Qt (the newest version), we get the corresponding image of the tileset and we compute its size (if the size is not present in the TMX file, you must get it from the toolkit). With the id of the tileset and the size of the image, we can compute the rectangular part of the image where the tile is. Finally, we draw the tile on the rendering surface at the
In the case of Tiled Java, the process is a little bit different. We put it here for completeness, in case you have to deal with old TMX files.