Skip to content

cocos2d x 3.3 xxx coco里的宏

David edited this page Jan 8, 2015 · 1 revision

coco里的宏

  • 太多了且定义的地方分散(或者说由于我是对代码不熟悉), 故我在学习过程中逐步罗列下coco里经常用到的。 将本文当字典使吧。 查找时在浏览器ctl + f就好。

  • find . -name "*.h" |xargs grep "CREATE_FUNC"
  • ack CREATE_FUNC

鱼 (持续更新...)

  • CL CLN在test-cpp里用的比较多
./cpp-tests/Classes/testBasic.h:#define CL(__className__) [](){ return __className__::create();}
./cpp-tests/Classes/testBasic.h:#define CLN(__className__) [](){ auto obj = new __className__(); obj->autorelease(); return obj; }
  • CREATE_FUNC
./platform/CCPlatformMacros.h

/**
 * define a create function for a specific type, such as Layer
 * @param \__TYPE__  class type to add create(), such as Layer
 */
#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
    __TYPE__ *pRet = new __TYPE__(); \
    if (pRet && pRet->init()) \
    { \
        pRet->autorelease(); \
        return pRet; \
    } \
    else \
    { \
        delete pRet; \
        pRet = NULL; \
        return NULL; \
    } \
}

  • log
./platform/CCPlatformMacros.h

#define __CCLOGWITHFUNCTION(s, ...) \
    log("%s : %s",__FUNCTION__, StringUtils::format(s, ##__VA_ARGS__).c_str())

// cocos2d debug
#if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
#define CCLOG(...)       do {} while (0)
#define CCLOGINFO(...)   do {} while (0)
#define CCLOGERROR(...)  do {} while (0)
#define CCLOGWARN(...)   do {} while (0)

#elif COCOS2D_DEBUG == 1
#define CCLOG(format, ...)      cocos2d::log(format, ##__VA_ARGS__)
#define CCLOGERROR(format,...)  cocos2d::log(format, ##__VA_ARGS__)
#define CCLOGINFO(format,...)   do {} while (0)
#define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)

#elif COCOS2D_DEBUG > 1
#define CCLOG(format, ...)      cocos2d::log(format, ##__VA_ARGS__)
#define CCLOGERROR(format,...)  cocos2d::log(format, ##__VA_ARGS__)
#define CCLOGINFO(format,...)   cocos2d::log(format, ##__VA_ARGS__)
#define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)
#endif // COCOS2D_DEBUG
  • retain release break
./platform/CCPlatformMacros.h

#define CC_SAFE_DELETE(p)           do { delete (p); (p) = nullptr; } while(0)
#define CC_SAFE_DELETE_ARRAY(p)     do { if(p) { delete[] (p); (p) = nullptr; } } while(0)
#define CC_SAFE_FREE(p)             do { if(p) { free(p); (p) = nullptr; } } while(0)
#define CC_SAFE_RELEASE(p)          do { if(p) { (p)->release(); } } while(0)
#define CC_SAFE_RELEASE_NULL(p)     do { if(p) { (p)->release(); (p) = nullptr; } } while(0)
#define CC_SAFE_RETAIN(p)           do { if(p) { (p)->retain(); } } while(0)
#define CC_BREAK_IF(cond)           if(cond) break
Clone this wiki locally