Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Parsing XML files into C++ (Tiled Qt)

This thread is locked; no one can reply to it. rss feed Print
Parsing XML files into C++ (Tiled Qt)
bananafishtoday
Member #14,586
September 2012
avatar

I've made a couple RPG-style "Hello World" games in Allegro, and I'm looking to apply what I learned from those to make something a bit bigger now. I'd like to use the Tiled Map Editor, and I made a test map, but I'm unsure how to use data from the XML file it outputs in my game.

I've read tutorials for XML itself and for TinyXML (if there's a better tool or this won't do what I need, please let me know) but I'm still at a loss on how to proceed. I don't have prior experience with XML, zlib, or with using another language in conjunction with C++.

This is what the output file looks like:

#SelectExpand
1<?xml version="1.0" encoding="UTF-8"?> 2<map version="1.0" orientation="orthogonal" width="32" height="27" tilewidth="32" tileheight="32"> 3 <tileset firstgid="1" name="wall-floor-snow" tilewidth="32" tileheight="32"> 4 <image source="wall-floor-snow.png" width="96" height="32"/> 5 </tileset> 6 <tileset firstgid="4" name="meta" tilewidth="32" tileheight="32"> 7 <image source="meta.png" width="64" height="32"/> 8 <tile id="0"> 9 <properties> 10 <property name="bound" value="True"/> 11 </properties> 12 </tile> 13 </tileset> 14 <layer name="ground" width="32" height="27"> 15 <data encoding="base64" compression="zlib"> 16 eJxjYmBgYBpgzDhAeNT+UftH7cctTus8OtjtZx6h9tML08p+YtPPcLefUPoZrvaT6s5R+4lTR+3yeKjZT+3yeKjYTys8WOwfSAwAVaEEFQ== 17 </data> 18 </layer> 19 <layer name="meta" width="32" height="27"> 20 <data encoding="base64" compression="zlib"> 21 eJxjYWBgYBlgPFBg1P5R+0ftxy1O6zw62O3Hp2Y4208vQCv7iU0/w91+QnYMV/uJBaP2E2c/rcrjoWY/KXqGk/20AoPF/oHEAPPgAqE= 22 </data> 23 </layer> 24 <objectgroup name="objects" width="32" height="27"> 25 <object name="spawn" x="640" y="640" width="32" height="32"/> 26 </objectgroup> 27 <layer name="buildings" width="32" height="27"> 28 <data encoding="base64" compression="zlib"> 29 eJxjYBgF6IAJCx5K9o/qH9U/lNPvqP2j9g82MJB+GejwHLV/ZNs/CkbBKKANAABsyQDZ 30 </data> 31 </layer> 32</map>

The map consists of the tiles, a meta layer that defines which tiles should be impassible, and an object layer that will define map transitions like doors. How can I read and use this data in C++ (presumably as a class), or where can I learn how to do this? Thanks.

bamccaig
Member #7,536
July 2006
avatar

james_lohr
Member #1,947
February 2002

If the "Tile Map Editor" is a library, then it should come with the tools to parsing the map files and instantiating objects from them.

Ashteth
Member #3,310
March 2003
avatar

I think this is what you want:
Tiled Map Support for A5

I haven't tried it myself though as I use my own custom XML format.

bananafishtoday
Member #14,586
September 2012
avatar

@bamccaig: Thanks! I'm pretty new to file I/O in general, and your code is very helpful.

@James Lohr: Tiled is just a GUI app that lets one "paint" maps with tilesets and define properties for tiles, then exports the data as XML.

@Ashteth: Oh this is cool. (Their link is broken, this is the proper one: https://github.com/dradtke/allegro_tiled.)

I've been messing around all night and managed to write functions to printf the data I need, so I should be able to modify them to draw instead without too much hassle. :) Luckily the rest of what I have is written in such a way that it shouldn't care how the map drawing is done.

Go to: