Game Development Reference
In-Depth Information
The Marmalade class factory system allows us to add our own classes to those
provided by the SDK itself by registering a unique hash value identifying the class
and a method that creates a new instance of it.
The hash value is normally derived by converting the name of the class into a
number by passing its name as a string to the IwUtil API's function IwHashString .
While this isn't guaranteed to produce a unique number, it is usually good enough
for our purposes and clashes with hash values from other class names are rare.
To add our own custom CIwManaged derived class to the class factory, we just need
to do the following (if you want to see a full example of this and indeed the things
we'll be covering in the next few sections, take a look at the source code for the ITX
project that accompanies this chapter):
1.
Add the IW_MANAGED_DECLARE(CustomClassName) macro to the public
section of the class. This declares a method called GetClassName , which
will return the name of the class as a string, and also adds a couple of type
definitions to allow the class to be used more easily with the CIwArray class,
which is yet another piece of functionality provided by IwUtil.
2.
Add the macro IW_MANAGED_IMPLEMENT_FACTORY(CustomClassName) to
the source file for the class. This macro implements the GetClassName
method and also creates the necessary class factory function that will be
used to create a new instance of our class.
3.
Finally, we have to register our class with the class factory itself by adding
the macro IW_CLASS_REGISTER(CustomClassName) somewhere in our
initialization code.
With this done, we can now include our class in an ITX file. The CIwTextParserITX
class can now create a brand new instance of it with a call to the class factory
function IwClassFactoryCreate("CustomClassName") .
Parsing a class
With the creation of a new instance of our class taken care of, the next step is to allow
CIwTextParserITX to configure that instance by modifying its members. This is
done with the following CIwManaged class' virtual methods:
 
Search WWH ::




Custom Search