Chapter 6. Data-driven Design

With the previous chapter adding the ability to create and handle game states, our framework has really begun to take shape. In this chapter, we will explore a new way to create our states and objects by removing the need to hardcode the creation of our objects at compile time. To do this we will parse through an external file, in our case an XML file, which lists all of the objects needed for our state. This will make our states generic as they can be completely different simply by loading up an alternate XML file. Taking PlayState as an example, when creating a new level we would need to create a new state with different objects and set up objects we want for that level. If we could instead load the objects from an external file, we could reuse the same PlayState and simply load the correct file depending on the current level we want. Keeping classes generic like this and loading external data to determine their state is called Data-driven Design.

In this chapter we will cover:

  • Loading XML files using the TinyXML library
  • Creating a Distributed Factory
  • Loading objects dynamically using the factory and an XML file
  • Parsing a state from an XML file
  • Fitting everything together into the framework

Loading XML files

I have chosen to use XML files because they are so easy to parse. We are not going to write our own XML parser, rather we will use an open source library called TinyXML. TinyXML was written by Lee Thomason and is available under the zlib license from http://sourceforge.net/projects/tinyxml/.

Once downloaded the only setup we need to do is to include a few of the files in our project:

  • tinyxmlerror.cpp
  • tinyxmlparser.cpp
  • tinystr.cpp
  • tinystr.h
  • tinyxml.cpp
  • tinyxml.h

Also, at the top of tinyxml.h, add this line of code:

#define TIXML_USE_STL

By doing this we ensure that we are using the STL versions of the TinyXML functions. We can now go through a little of how an XML file is structured. It's actually fairly simple and we will only give a brief overview to help you get up to speed with how we will use it.

Basic XML structure

Here is a basic XML file:

<?xml version="1.0" ?>
<ROOT>
    <ELEMENT>
    </ELEMENT>
</ROOT>

The first line of the file defines the format of the XML file. The second line is our Root element; everything else is a child of this element. The third line is the first child of the root element. Now let's look at a slightly more complicated XML file:

<?xml version="1.0" ?>
<ROOT>
    <ELEMENTS>
        <ELEMENT>Hello,</ELEMENT>
        <ELEMENT> World!</ELEMENT>
    </ELEMENTS>
</ROOT>

As you can see we have now added children to the first child element. You can nest as many children as you like. But without a good structure, your XML file may become very hard to read. If we were to parse the above file, here are the steps we would take:

  1. Load the XML file.
  2. Get the root element, <ROOT>.
  3. Get the first child of the root element, <ELEMENTS>.
  4. For each child, <ELEMENT> of <ELEMENTS>, get the content.
  5. Close the file.

Another useful XML feature is the use of attributes. Here is an example:

<ROOT>
    <ELEMENTS>
        <ELEMENT text="Hello,"/>
        <ELEMENT text=" World!"/>
    </ELEMENTS>
</ROOT>

We have now stored the text we want in an attribute named text. When this file is parsed, we would now grab the text attribute for each element and store that instead of the content between the <ELEMENT></ELEMENT> tags. This is especially useful for us as we can use attributes to store lots of different values for our objects. So let's look at something closer to what we will use in our game:

<?xml version="1.0" ?>
<STATES>

<!--The Menu State-->
<MENU>
<TEXTURES>
  <texture filename="button.png" ID="playbutton"/>
  <texture filename="exit.png" ID="exitbutton"/>
</TEXTURES>

<OBJECTS>
  <object type="MenuButton" x="100" y="100" width="400" 
  height="100" textureID="playbutton"/>
  <object type="MenuButton" x="100" y="300" width="400" 
  height="100" textureID="exitbutton"/>
</OBJECTS>
</MENU>

<!--The Play State-->
<PLAY>
</PLAY>

<!-- The Game Over State -->
<GAMEOVER>
</GAMEOVER>
</STATES>

This is slightly more complex. We define each state in its own element and within this element we have objects and textures with various attributes. These attributes can be loaded in to create the state.

With this knowledge of XML you can easily create your own file structures if what we cover within this book is not to your needs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.10.32