Game Development

Oracle’s Eye Development: Configuration Parser and Loader

Yesterday I finished by saying that I was working on adding a configuration loader . I added some stub code so that I would be ready to start working right away.

I realized that while I have coded a text parser before, I never had to worry about getting configuration data out of a file. What would I actually need? Before agonizing too much over it, I thought that it was more than likely that a ready-made, well-tested solution already exists. I spent some time looking through books, notes, and before finding someone asking the same question I was: isn’t there a an easy to use tool so I don’t have to reinvent this obviously already solved problem?

One suggestion was boost::program_options. I looked into it for a little bit, but another suggestion was to use TinyXML. At first, I thought that using XML was a bit overkill for configuration files. At the same time, it would be ideal for holding the levels, and if I could kill two birds with one stone, all the better. The best part? TinyXML is already in my project! It comes with the Kyra Sprite Engine.

I spent the day reading the tutorial and trying out some test code. It took me only a few moments to get an XML configuration file to load successfully, and a bit more code allowed me to view a few of the elements. I spent the evening learning how the library works, and it seems easy so far. I wish there was some better documentation out for it, though.

My configuration file used to look like:

TimerInterval 80
SpriteSize 64
LeftEdge 64
UpperEdge 64
Ball velocity 5
Player velocity 5

and I wrote it before I wrote more than a single function to help parse it.


<?xml version=”1.0″ ?>
<!– Configuration for codename: Oracle’s Eye –>
<Timerinterval interval = “80”/>
<Spritesize dimension = “64”/>
<Border leftEdge = “64” upperEdge= “64”/>
<Ball velocity = “5” />
<Player velocity = “5” />

Unfortunately, I haven’t been able to do more than grab the root element, but I’m still learning how this library works. I’ll hope to be able to finish the configuration loader within the week. Afterwards, it shouldn’t be too much more work to write a level loader with TinyXML as well.

7 replies on “Oracle’s Eye Development: Configuration Parser and Loader”

I recommend LibXML2 if you like a more procedural approach to parsing XML. It’s dirt simple to use once you figure out which of the several interface styles to use.

I’ll look into it, but for the moment I think TinyXML will be preferrable just because it is already part of my project. I’d feel uncomfortable having two XML parsers in one project. B-)

Hey Franco,
I’m an XML expert and actually XML valid for configuration files and not as heavy as many may think. You will only need to get used to doing XPath querries and node traversal.

Send me an e-mail if you need some help with that stuff.

Having used LibXML2 before, I can say that it’s indeed easy to use. However, it’s also huge. TinyXML should be fine unless you want to parse DTD’s and encodings other than utf-8.

I’m currently concerned with getting data out of and into my configuration files. Afterwards, I can worry about level files. My game is fairly simple, but perhaps a future game might need more heavy-duty data manipulation and storage.

Comments are closed.