Skip to content
Wu Jie edited this page Mar 30, 2014 · 1 revision

When define a class, you need to do the things below:

  • declare the class and its inheritance(if exists) in header file
  • define the creator in source file
  • define the properties in source file
  • define the serialize method in source file
  • register in classes_registry.h

Declare Class

There are two MACRO help you declare a class in header file -- EX_DECL_CLASS_BEGIN, EX_DECL_CLASS_SUPER_BEGIN.

EX_DECL_CLASS_BEGIN(your_class)

The macro define a single class directly inherit from ex_class_t.

EX_DECL_CLASS_SUPER_BEGIN(your_class,super_class)

The macro define a class inherit from super_class.

Note: the exsdk assume the super class is define by EX_DEF_CLASS_BEGIN or EX_DEF_CLASS_SUPER_BEGIN, there's no protected so you have to carefully check the super-class by yourself.

To finish the declaration, add EX_DEF_CLASS_END(your_class) or EX_DEF_CLASS_SUPER_END(your_class,super_class) at the end of the code. Here is an example to define a class named foo, and a class named bar that inherit from foo.

EX_DECL_CLASS_BEGIN(foo)
    int id;
    float value;
EX_DECL_CLASS_END(foo)

EX_DECL_CLASS_SUPER_BEGIN(bar,foo)
    float value2;
EX_DECL_CLASS_SUPER_END(bar,foo)

Define Creator

The creator is used for creating the class also give the ex_factor_t the create function so that the factory can create the class by type-id/text from text file. To define a creator, use the macro EX_DEF_CLASS_BEGIN(your_class) and EX_DEF_CLASS_END. So here are the creator of foo and bar.

EX_DEF_CLASS_BEGIN(foo)
    1, // id
    1.0f, // value
EX_DEF_CLASS_END

EX_DEF_CLASS_BEGIN(bar)
    // foo
    1, // id
    1.0f, // value

    // bar
    10.0f // value2
EX_DEF_CLASS_END

Define Properties

The details in writing properties will be introduced in property. Here is the example:

EX_DEF_PROPS_BEGIN(foo)
    EX_PROP( foo, id, "id",  EX_PROP_ATTR_NONE, ex_prop_set_raw_int, ex_prop_get_raw_int )
    EX_PROP( foo, value, "value",  EX_PROP_ATTR_NONE, ex_prop_set_raw_float, ex_prop_get_raw_float )
EX_DEF_PROPS_END(foo)

EX_DEF_PROPS_BEGIN(bar)
    EX_PROP( bar, value2, "value2",  EX_PROP_ATTR_NONE, ex_prop_set_raw_float, ex_prop_get_raw_float )
EX_DEF_PROPS_END(bar)

Define Serializer

EX_SERIALIZE_BEGIN(foo)
    EX_SERIALIZE( int, id );
    EX_SERIALIZE( float, value );
EX_SERIALIZE_END

EX_SERIALIZE_BEGIN(bar)
// TODO: how to serialize base?????
    EX_SERIALIZE( float, value2 );
EX_SERIALIZE_END

Register Class

After all definition is done, open the classes_registry.c file, and fill your register code behind it.

Note: the super class must register before the child.

EX_REGISTER_CLASS(foo,ex.foo);
EX_REGISTER_CLASS(bar,ex.bar);

Using a Class

You MUST instantiate a class by function ex_create_YOUR_CLASS(), for example, suppose you have class foo, to create an instance, write:

foo *inst = ex_create_foo();

Clone this wiki locally