Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
100 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Introduction
 Saving Windows
 Loading Windows
 Resource Editors
 Subclassing
 Speeding Up
 GUI Rendering

 Conclusion

 Printable version

 


  The Series

 Part I
 Part II
 Part III
 Part IV

 

Saving Windows

Window serialization (or, saving and loading windows) may or may not be crucial for your project. If your game GUI is minimal, you might be able to get by with just hard-coding the windows into your game. But if your GUI's relatively complex, or will change often over the course of development, you're going to want code that will save a window (and all its children) to a file, and then load it back up again. For starters, having window serialization code allows you to change your game's GUI without recompiling, and is a boon if you're working with more than one person.

My plan of attack was easy – start at the main dialog window, and recursively go through all of its child windows, saving each one to disk. If I were programming in C, the first thing I would have said to myself would have been "OK, so if I have to save these windows, I need a byte for each window that tells me what type of window it is, so I can load it back up correctly. 1 is a button, 2 is a listbox, 3 is an icon, etc."

This kind of problem is specifically what C++'s RTTI (Run Time Type Identification) addresses. RTTI provides two things, a type_info class and a typeid() function, which together allowed me to query an object for its class name – "gui_window", "gui_button", etc. Instead of fiddling with enums and IDs, I simply call typid() for each window I'm going to save, and "write down" the class name of the window.

I saw two minor disadvantages to using RTTI's object identification functions to help save windows. First of all, the RTTI IDs are strings, not integers, which means they'll take up more space on disk (store them Pascal style, that is, the first 4 bytes describe the length of the string, followed by the string data itself). Second, if you change the name of one of your window classes, you'll break any window files that you've previously saved.

For these reasons, you might opt out of using RTTI in this manner – after all, just because a technology is there doesn't mean you have to use it. However, I found RTTI to be a lifesaver in my code. For more information on RTTI and these two functions, search for them in your online help.

Also, if you decide to use RTTI with Visual C++, make sure you turn it on in your project settings, C/C++ tab, C++ language option.


Next : Loading Windows