Game Development Reference
In-Depth Information
else
return CIwManaged::ParseAttribute(apParser, apAttribute);
return true;
}
Serializing a class
Serializing an object instance is the process of converting the current state of the
object into (or from) a binary format.
While not strictly necessary when parsing an ITX file, it is still very much a useful
part of the functionality provided by
CIwManaged
, and forms an integral part of the
resource handling process that we will be seeing later in this chapter.
The serialization functionality can also be useful when it comes to saving out things
such as current game progress or high score tables, though of course we can still use
normal file handling operations to do this if we prefer.
Serialization of our class is handled by overriding the virtual method
Serialise
.
This method can then use the serialization functions provided by IwUtil, which all
start with the prefix
IwSerialise
.
For example,
IwSerialiseInt32
will serialize an
int32
value. All these functions
make use of the Marmalade type definitions for the basic variable types, as these
are far more explicit when it comes to the memory footprint of a variable. Take a
look at the header files
IwSerialise.h
and
s3eTypes.h
in the Marmalade SDK
installation for more information on the
IwSerialise
functions and the variable
types respectively.
We must make sure to call our superclass implementation of
Serialise
as well to
ensure every part of the object is serialized. Normally this would be the first thing we
do in our implementation of
Serialise
, but it does not have to be so as long as it is
called at some point.
We can serialize our objects to a file of our choosing by calling
IwSerialiseOpen
.
This allows us to specify the filename and a Boolean flag that indicates whether we
are reading or writing the file. We then call the
Serialise
method of each object we
want to serialize, and finally call
IwSerialiseClose
to finish the process.
One nice feature of the
IwSerialise
functions is that, in most cases, we do not have
to worry about whether the
Serialise
method has been called to write data to a file
or if it has been called to read data from a file. We just call the function and it will
read or write the value, as appropriate.
Search WWH ::
Custom Search