Skip to content

Instantly share code, notes, and snippets.

@jls
Created July 3, 2012 18:25
Show Gist options
  • Save jls/3041591 to your computer and use it in GitHub Desktop.
Save jls/3041591 to your computer and use it in GitHub Desktop.
Cocos2d-x Static Constructor Macro
#define STATIC_CONSTRUCTOR(className, methodPostfix, methodArgs, initArgs, constructorArgs) \
static className* create ## methodPostfix methodArgs \
{ \
className *instance = new className constructorArgs; \
if (instance && instance->init ## methodPostfix initArgs) \
{ \
instance->autorelease(); \
} \
else \
{ \
CC_SAFE_DELETE(instance); \
} \
return instance; \
};
// This example call:
// STATIC_CONSTRUCTOR(MyScene, WithTitle, (const char *title), (title), ())
//
// generates:
//static MyScene* createWithTitle(const char *title){
// MyScene *instance = new MyScene();
// if(instance && instance->initWithTitle(title)){
// instance->autorelease();
// }else{
// CC_SAFE_DELETE(instance);
// }
// return instance;
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment