• entries
    743
  • comments
    1924
  • views
    580387

Wheel reinvention

Sign in to follow this  

74 views

Taken a break from the game tonight to do some reinvention of the wheel.

As a lighter alternative to XML, I introduce Extensible Configuration Script.

This is an example of the script:


orb
{
screen
{
width=1024
height=768
}

workingdir="c:\\orb\\resources"

levels
{
level1
somelevel
mylevel
}
}


Nice and simple, and will nest as deeply as you want.

It is the usage in code that appeals to me. There are two situations - one where your configuration file must contain a certain value in a certain format or you have an error (the screen resolutions above for example), or another where you need to dynamically examine the contents (the list of levels above).

So for the first example, you can use the following syntax:


#include
#include "xcs.h"

int main(int,const char **av)
{
std::cout << "XcsDriver 1.0 Copyright(C)2007 All Rights Reserved\n";

try
{
xcs::tree t("sample.xcs");

std::cout << t["orb"]["screen"]["width"].as() << "\n";
std::cout << t["orb"]["screen"]["height"].as() << "\n";

std::cout << t["orb"]["workingdir"].as() << "\n";
}

catch(xcs::exception &e)
{
std::cout << e.what() << std::endl;
}
}


Obviously different exceptions are thrown depending on what the problem is - a name missing in a scope, a value not being converted etc.

Alternatively, to dynamically look at the list of levels:


#include
#include "xcs.h"

int main(int,const char **av)
{
std::cout << "XcsDriver 1.0 Copyright(C)2007 All Rights Reserved\n";

try
{
xcs::tree t("sample.xcs");

xcs::node &n=t["orb"]["levels"];

for(xcs::iterator i=n.begin();i!=n.end();++i) std::cout << i->as() << "\n";
}

catch(xcs::exception &e)
{
std::cout << e.what() << std::endl;
}
}


Or to use a recursive function to traverse the entire contents of the tree:


#include
#include "xcs.h"

void traverse(xcs::node &n)
{
std::cout << n.full_name() << "\n";

for(xcs::iterator i=n.begin();i!=n.end();++i) traverse(*i);
}

int main(int,const char **av)
{
std::cout << "XcsDriver 1.0 Copyright(C)2007 All Rights Reserved\n";

try
{
xcs::tree t("sample.xcs");

traverse(t.root());
}

catch(xcs::exception &e)
{
std::cout << e.what() << std::endl;
}
}


You can concatenate quoted strings in the script using the + operator, so you can split lengthy text over several lines, and the C system of forward-slash escape sequences is working for strings as well.

I plan to start using this quite extensively in Orb and future projects. When I've tested it a bit more thoroughly, I might stick up the source code somewhere.

[EDIT] Oh, and I bought The Orange Box. Portal rocks as much as I thought it would, then a little bit more.
Sign in to follow this  


2 Comments


Recommended Comments

Wow, that seems like a nifty little system you've got going on there. I'm going to have to come up with something like that for our latest project, and I hope you don't mind if I "borrow" a few key concepts[grin]

Orb is looking pretty sweet too, have you told us what the game is going to be about yet?

Share this comment


Link to comment
Quote:
Original post by Sir Sapo
Wow, that seems like a nifty little system you've got going on there. I'm going to have to come up with something like that for our latest project, and I hope you don't mind if I "borrow" a few key concepts[grin]


Of course not. I'd be honoured if my xcs library could even be used in 2X, although I appreciate that is asking a lot. [smile]

Quote:
Original post by Sir Sapo
Orb is looking pretty sweet too, have you told us what the game is going to be about yet?


No, because I haven't really decided yet, but the current vague idea is a puzzle sort of game based on playing pool with objects and your ship's cannons. Expect to see gravity wells, remotely detonated missiles and warp holes.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now