If you’re making a game that uses levels or maps of some sort, why bother reinventing the wheel? Making your own map editor and custom file format might take a while and be troublesome at the end.

Tiled is this great map editor that takes an image (tileset), a scheme you draw (tilemap) and saves it a specific compressed format (.tmx). The editor has a nice interface, making anyone capable of creating maps and customizing them. Look up more features on the site.

This post will help you parse Tiled maps on your C++ application with tmx-parser.


tmx-parser requires two libs – tinyxml and zlib.

You can install them yourself (and ask your users to do the same) on Debian-based distros like this:

$ sudo apt-get install libtinyxml-dev libzlib-dev

As an alternative, you can include their source codes on your application. This way, it’ll be more portable. To do that, download them at the links above and place them, say, inside a lib folder on your project.

Then, follow the same instructions as below with tmx-parser.


To use tmx-parser you’ll have to download it and use it along with your project.

Place it on a lib folder and adjust your Makefile to make sure they’re all compiled together – perhaps like this:

CFILES   = $(shell find lib/ -type f -name '*.c')
CXXFILES = $(shell find lib/ -type f -name '*.cpp')
OBJECTS = $(CFILES:.c=.o) $(CXXFILES:.cpp=.o)
# Keep your normal Makefile with $(OBJECTS) as the target


First, start the Tiled editor. You can get any tileset image around there and draw a map. After saving it as test.tmx, let’s do the programming.

#include "lib/Tmx.h"
int main()
    Tmx::Map map;
    if (map.HasError())
        printf("Error loading map: %s", map.GetErrorText());
        return 1;
    // How many layers the map has
    int layers = map.GetNumLayers();
    // Will get first layer anyway
    const Tmx::Layer* current_layer = map.GetLayer(0);
    int width = current_layer->GetWidth();
    int height = current_layer->GetHeight();
    // Now we'll get the actual map
    int map[width][height];
    // Retrieving each tile index
    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++)
            map[i][j] = current_layer->GetTileId(i, j);
    // Do whatever you want with the map
    return 0;

As you can see, the actual map is a matrix of int . They contain the index of the tile. That index is the number of the image on your tileset.

Of course, this is just a test. You really should abstract that into a class. Since I’ve already done it, you might as well take a quick look.

Hope you enjoyed, any comments and critics are welcome!

4 thoughts on “Quickly Parsing Tiled’s TMX Maps in C/C++

  1. I was able to parse a map throuugh your tutorial , but on printing the map’s data id like a 2 d matrix, i observed that the map is roatated/flipped by 180and inverted by 90,

    How can i revert it back to original position ?

    • Hello!
      Sorry for taking so long to answer you.

      Did you manage to figure out the problem?
      As far as I can see it seems your i and j indexes could be reversed.

      So on your printing code do you have [i][j] or [j][i]?

    • Strange, I think it used to work.

      I can compile and run the following program with both gcc and g++.
      I understand the compiler can optimize to the point where it runs ok, but if I’m not mistaken, wasn’t the const a problem only on ANSI C?

      void function(int width, int height)
      	int array[width][height];
      int main(int argc, char *argv[])
      	function(10, 20);
      	int a = 10;
      	int b = 20;
      	function(a, b);
      	return 0;

Leave a reply


<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">