Please note that I don't update this web site anymore. You can visit my new personal blog at www.williamwilling.com.

GUI Definition Language

Tuesday, January 4, 2005

It didn't take long for me to decide that my game engine needs a GUI system. Of course, the idea had already crossed my mind, but I thought I could hold off until after I finished Trichromix. I was wrong. It took about thirty minutes of adjusting hard-coded button coordinates to realise that.

I designed a simple language in which I can define GUIs. I call the language the GUI Definition Language, or GDL for short. Obviously, you won't have to expect a blog entry about how I came up with that name. The language looks like this.

bitmap "mainmenu.png"
{
  sprite background
  {
    offset = (0, 0);
    size = (800, 600);
  }

  sprite quit
  {
    offset = (120, 15);
    size = (50, 50);
  }

  sprite quit_hovered
  {
    offset = (40, 600);
    size = (50, 50);
  }

  sprite quit_pressed
  {
    offset = (50, 600);
    size = (50, 50);
  }
}

control
{
  sprite = background;
  position = (0, 0);
  size = (800, 600);

  button
  {
    position = (120, 15);
    size = (50, 50);
    sprite = quit;
    hovered = quit_hovered;
    pressed = quit_pressed;
    onclick = "quit";
  }
}

I think you can figure out what's happening from the above example, so I won't go into it.

Parsing the language was a bit of a challenge, since I had never written a language parser before. Fortunately, I have in my possession the excellent book Game Scripting Mastery by Alex Varanese. Alex shows that it's perfectly possible to implement your own scripting language. He does, however, take 1200 pages to explain all the details, which indicates that this is not something you'll finish over the weekend. Could it be that I'm biting of more than I can chew?

And then I ran into Spirit. Spirit is a parser framework for C++ that allows you to define the grammar of your language inside your C++ code with a syntax resembling the Enhanced Backus-Naur Form. Spirit is part of the ever-invaluable Boost library and it's free to use. It uses some pretty advanced template magic to accomplish its task, so you need an up-to-date compiler to use it. I got it to work just fine with Microsoft's Visual C++ 7.1 compiler.

It still took me about a week to implement the GUI system, but it was worth it. Defining GUIs is still rather boring, but at least it only takes me a couple of minutes now.

Back to blog index

Comments

Tell me what you think

Since I'm not updating this site anymore, I disabled comments. You can visit me at my new site: www.williamwilling.com.