# Tiled/TMX C++ Library/Parser [closed]

Where can I find an easy to use and up to date C++ parser/library for the .tmx map format (used by the Tiled Map Editor) ?

EDIT: David's comment, 'Unless you want to build your game around the format of the parser..', got me thinking... So I have downloaded pugixml, which is an easy to use xml-parser with very straightforward documentation. Together with the spec for the TMX Map Format, I think I'll give it a try myself. I'll probably compare with Cocos2d-x's CCTMXTiledMap at some point.

General purpose tile map editor with XML-based map format

So you could just use a good, general purpose XML library such as:

http://rapidxml.sourceforge.net/

After that it's just a matter of following the TMX specification. In other words, you will simply need to ask the XML library to fetch you the nodes and attributes you need from that list, which is usually a trivial task.

PS: It also seems to be capable of exporting to JSON format, in which case you could use a JSON library instead (e.g. this).

Edit

But yes there seems to be a parser for this format available. A quick search returned this:

I have never tested it, but give it a try.

Edit 2

If you don't want to install SVN just to download it, you can use this application (I've tested it, and it works - no need to install anything):

And since you need an example of how to use it, here it is (based on this link):

1. Create a new instance of type Tmx::Map.
2. Call Map::ParseFile() passing it the path to your map file.
3. Check for errors using Map::HasError().
4. Iterate through tilesets using Map::GetTileset() and Map::GetNumTilesets(). Get individual tile information inside the tileset using Tileset::GetTile().
5. Iterate through layers using Map::GetLayer() and Map::GetNumLayers(). Use Layer::GetTileGid(y,x) to get the ID of a specific tile in the layer.
6. Iterate through object groups using Map::GetObjectGroup() and Map::GetNumObjectGroups(). Iterate through objects inside each group using ObjectGroup::GetObject() and ObjectGroup::GetNumObjects().

For individual properties on each of these objects, check the link.

• Yes I am talking about the editor you linked to. Yes, it is xml-based, but I am sure that I am not the first person who wants to use this particular format, so I'm thinking somebody must have done it before... – Ben Dec 18 '11 at 12:30
• Even if there happens to be a parser, it's just going to output some intermediate data structure. And you're still going to need to map all of that into your game's own structures for it to do anything. I think they chose a XML-based format for some reason. – David Gouveia Dec 18 '11 at 12:37
• Unless you want to build your game around the format of the parser... Anyway, check my edit then. – David Gouveia Dec 18 '11 at 12:40
• Thx for the link. Could you explain how I can download the sourcefiles? Caus there are a whole bunch and I'm guessing there must be a smarter way then clicking "Save files as..." on each individual file? The "Downloads" tab isn't really what I'd expect!? – Ben Dec 18 '11 at 13:42
• @Ben I can't seem to find an easy way either... Unlike github I'm not seeing any link to download a full archive of the repository :\ So I guess you'll either have to download SVN and run the checkout command, or browse the source and download each file individually... – David Gouveia Dec 18 '11 at 14:09

you can use a part of cocos2d-x library, it's a gameengine developed in c++ for 2d games, but you can just use the CCTMXTiledMap class from that engine to parse tmx file for you.

• Well, I guess the CCTMXTiledMap class is rather tightly integrated with the rest of cocos2D, i.e. it uses some of the other classes provided!? I'm looking for something more general, like the link David posted, but with an explanation to get started, cause just a bunch of sourcefiles isn't really helpful. – Ben Dec 18 '11 at 13:50
• yeah it uses some of them but you can extract all the data after it's loaded – Ali1S232 Dec 18 '11 at 14:27
• @Ben Then you didn't look close enough. I'll edit that in my post anyway. – David Gouveia Dec 18 '11 at 15:33

Here's a small one I wrote a while ago. It uses Boost property tree for parsing the XML. It's probably not feature complete - it will only parse the features I needed for my own game. Should be easy to extend though.

Here is one I quickly wrote: https://github.com/halsafar/libtmx-parser

C++ but at the moment requires C++11x. However it is very easy to add to a project. TinyXml2 is used which is also very easy to a project.

It simply parses the XML into simple structs you can manage. Nothing fancy here, coded in haste to do one job.