之前在看Ref的时候看到了一些宏:

1
2
3
4
5
6
7
8
9
10
11
12
13
typedef void (Ref::*SEL_CallFunc)();
typedef void (Ref::*SEL_CallFuncN)(Node*);
typedef void (Ref::*SEL_CallFuncND)(Node*, void*);
typedef void (Ref::*SEL_CallFuncO)(Ref*);
typedef void (Ref::*SEL_MenuHandler)(Ref*);
typedef void (Ref::*SEL_SCHEDULE)(float);

#define callfunc_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFunc>(&_SELECTOR)
#define callfuncN_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncN>(&_SELECTOR)
#define callfuncND_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncND>(&_SELECTOR)
#define callfuncO_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncO>(&_SELECTOR)
#define menu_selector(_SELECTOR) static_cast<cocos2d::SEL_MenuHandler>(&_SELECTOR)
#define schedule_selector(_SELECTOR) static_cast<cocos2d::SEL_SCHEDULE>(&_SELECTOR)

我们用到CallFunc*MenuItem*Schedule的时候就需要用到这些东西
从本质上讲,这些宏就是就是帮你完成了一个函数指针的强制类型转换。
由于cocos2dx 3.0涉及到c++11,所以是用到了std::bindstd::function
我猜,这或许就是在3.x中取消了2.0中的通知中心的原因吧?


今天就先召集另外一些宏

CCPlatformConfig

主要是各种平台的定义,平台详情如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#define CC_PLATFORM_UNKNOWN            0
#define CC_PLATFORM_IOS 1
#define CC_PLATFORM_ANDROID 2
#define CC_PLATFORM_WIN32 3
#define CC_PLATFORM_MARMALADE 4
#define CC_PLATFORM_LINUX 5
#define CC_PLATFORM_BADA 6
#define CC_PLATFORM_BLACKBERRY 7
#define CC_PLATFORM_MAC 8
#define CC_PLATFORM_NACL 9
#define CC_PLATFORM_EMSCRIPTEN 10
#define CC_PLATFORM_TIZEN 11
#define CC_PLATFORM_QT5 12
#define CC_PLATFORM_WP8 13
#define CC_PLATFORM_WINRT 14

/* 这个是在编译的时候决定的 */
#define CC_TARGET_PLATFORM CC_PLATFORM_UNKNOWN

接下来每个平台都会定义类似下面的宏:

1
2
3
4
#if defined(CC_TARGET_OS_MAC)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_MAC
#endif

最后是一段关于win32的宏定义,不然会有很多警告。

1
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#ifndef __MINGW32__
#pragma warning (disable:4127) 
#endif 
#endif  // CC_PLATFORM_WIN32

ccConfig

这个文件主要是关乎引擎的配置文件, 在这里就列出部分的内容以供参考。

1
2
3
4
5
6
7
8
9
/* 物理引擎 */
#ifndef CC_USE_PHYSICS
#define CC_USE_PHYSICS 1
#endif

/* 脚本绑定 */
#ifndef CC_ENABLE_SCRIPT_BINDING
#define CC_ENABLE_SCRIPT_BINDING 1
#endif

CCPlatformMacros

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**************************************************
* 子类需要用一个类方法来创建对象的时候,例如Layer
* 使用这个有一个前提:需要先实现init方法
* 如果你需要在init中传入参数的话,那么还是自己写吧
**************************************************/
#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; \
} \
}

/* 和CREATE_FUNC只有函数名字有区别 */
CC_DEPRECATED_ATTRIBUTE static __TYPE__* node() \
{ \
__TYPE__ *pRet = new __TYPE__(); \
if (pRet && pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = NULL; \
return NULL; \
} \
}

/* 命名空间的宏 cocos2d */
#ifdef __cplusplus
#define NS_CC_BEGIN namespace cocos2d {
#define NS_CC_END }
#define USING_NS_CC using namespace cocos2d
#else
#define NS_CC_BEGIN
#define NS_CC_END
#define USING_NS_CC
#endif

/********************************************************
* 首先这些宏的目的在于迅速产生getter和setter
* 为毛要写这些宏呢,这应该是由于从objc那边搞过来的原因
* objc里面有@property,@synthesize关键字啊
* 注意:
* 这些getter和setter都是虚函数
* 这些getter和setter的变量类型都是protected
* 在这些getter和setter之后的成员默认都是public
********************************************************/

/************************声明**************************/

/* getter */
#define CC_PROPERTY_READONLY(varType, varName, funName)\
protected: varType varName;\
public: virtual varType get##funName(void) const;

/* const getter */
#define CC_PROPERTY_READONLY_PASS_BY_REF(varType, varName, funName)\
protected: varType varName;\
public: virtual const varType& get##funName(void) const;

/* getter && setter */
#define CC_PROPERTY(varType, varName, funName)\
protected: varType varName;\
public: virtual varType get##funName(void);\
public: virtual void set##funName(varType var);

#define CC_PROPERTY_PASS_BY_REF(varType, varName, funName)\
protected: varType varName;\
public: virtual const varType& get##funName(void) const;\
public: virtual void set##funName(const varType& var);

/************************实现**************************/

#define CC_SYNTHESIZE_READONLY(varType, varName, funName)\
protected: varType varName;\
public: virtual varType get##funName(void) const { return varName; }

#define CC_SYNTHESIZE_READONLY_PASS_BY_REF(varType, varName, funName)\
protected: varType varName;\
public: virtual const varType& get##funName(void) const { return varName; }

#define CC_SYNTHESIZE(varType, varName, funName)\
protected: varType varName;\
public: virtual varType get##funName(void) const { return varName; }\
public: virtual void set##funName(varType var){ varName = var; }

#define CC_SYNTHESIZE_PASS_BY_REF(varType, varName, funName)\
protected: varType varName;\
public: virtual const varType& get##funName(void) const { return varName; }\
public: virtual void set##funName(const varType& var){ varName = var; }

#define CC_SYNTHESIZE_RETAIN(varType, varName, funName) \
private: varType varName; \
public: virtual varType get##funName(void) const { return varName; } \
public: virtual void set##funName(varType var) \
{ \
if (varName != var) \
{ \
CC_SAFE_RETAIN(var); \
CC_SAFE_RELEASE(varName); \
varName = var; \
} \
}


/**********************************
* 这里有关内存和引用计数的一些宏
**********************************/

#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

/**************************************************
* 输出当前调用的函数名称,以及用户指定的一些东西
* __FUNCTION__这个宏就是指向当前使用的函数的名称
* 用于CCLOGWARN
**************************************************/
#define __CCLOGWITHFUNCTION(s, ...) \
log("%s : %s",__FUNCTION__, StringUtils::format(s, ##__VA_ARGS__).c_str())

/**************
* 调试用的宏
**************/

#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

/*************
* lua 调试宏
*************/
#if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0 || CC_LUA_ENGINE_DEBUG == 0
#define LUALOG(...)
#else
#define LUALOG(format, ...) cocos2d::log(format, ##__VA_ARGS__)
#endif // Lua engine debug

/******************
* 防止拷贝构造函数
******************/
#if defined(__GNUC__) && ((__GNUC__ >= 5) || ((__GNUG__ == 4) && (__GNUC_MINOR__ >= 4))) \
|| (defined(__clang__) && (__clang_major__ >= 3)) || (_MSC_VER >= 1800)
#define CC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName &) = delete; \
TypeName &operator =(const TypeName &) = delete;
#else
#define CC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName &); \
TypeName &operator =(const TypeName &);
#endif

/* 禁止构造函数和拷贝构造函数 */
#define CC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
TypeName(); \
CC_DISALLOW_COPY_AND_ASSIGN(TypeName)

/* 过时代码 */
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
#define CC_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
#elif _MSC_VER >= 1400 //vs 2005 or higher
#define CC_DEPRECATED_ATTRIBUTE __declspec(deprecated)
#else
#define CC_DEPRECATED_ATTRIBUTE
#endif

/* 一些输出 */
#if defined(__GNUC__) && (__GNUC__ >= 4)
#define CC_FORMAT_PRINTF(formatPos, argPos) __attribute__((__format__(printf, formatPos, argPos)))
#elif defined(__has_attribute)
#if __has_attribute(format)
#define CC_FORMAT_PRINTF(formatPos, argPos) __attribute__((__format__(printf, formatPos, argPos)))
#endif // __has_attribute(format)
#else
#define CC_FORMAT_PRINTF(formatPos, argPos)
#endif

/* 格式 */
#if defined(_MSC_VER)
#define CC_FORMAT_PRINTF_SIZE_T "%08lX"
#else
#define CC_FORMAT_PRINTF_SIZE_T "%08zX"
#endif

/* 弃用代码 */
#ifdef __GNUC__
#define CC_UNUSED __attribute__ ((unused))
#else
#define CC_UNUSED
#endif

//
// CC_REQUIRES_NULL_TERMINATION
//
#if !defined(CC_REQUIRES_NULL_TERMINATION)
#if defined(__APPLE_CC__) && (__APPLE_CC__ >= 5549)
#define CC_REQUIRES_NULL_TERMINATION __attribute__((sentinel(0,1)))
#elif defined(__GNUC__)
#define CC_REQUIRES_NULL_TERMINATION __attribute__((sentinel))
#else
#define CC_REQUIRES_NULL_TERMINATION
#endif
#endif

ccMacros

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef CCASSERT
#if COCOS2D_DEBUG > 0
#if CC_ENABLE_SCRIPT_BINDING
extern bool CC_DLL cc_assert_script_compatible(const char *msg);
#define CCASSERT(cond, msg) do { \
if (!(cond)) { \
if (!cc_assert_script_compatible(msg) && strlen(msg)) \
cocos2d::log("Assert failed: %s", msg); \
CC_ASSERT(cond); \
} \
} while (0)

#else
#define CCASSERT(cond, msg) CC_ASSERT(cond)
#endif
#else
#define CCASSERT(cond, msg)
#endif
// XXX: Backward compatible
#define CCAssert CCASSERT
#endif // CCASSERT

#include "ccConfig.h"

/* 交换函数 */
/* 不怎么用 */
#define CC_SWAP(x, y, type) \
{ type temp = (x); \
x = y; y = temp; \
}


/* 用这些随机函数之前先在全局调用srand */

/* 随机 [-1.0f, 1.0f] */
#define CCRANDOM_MINUS1_1() ((2.0f*((float)rand()/RAND_MAX))-1.0f)

/* 随机 [0.0f, 1.0f] */
#define CCRANDOM_0_1() ((float)rand()/RAND_MAX)

/* 角度 --> 弧度 */
#define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180

/* 弧度 --> 角度 */
#define CC_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f) // PI * 180

/* 无符号最大值 - 1 */
#define kRepeatForever (UINT_MAX -1)

/* 混合函数 */
#define CC_BLEND_SRC GL_ONE
#define CC_BLEND_DST GL_ONE_MINUS_SRC_ALPHA

/* 设置着色器 */
#define CC_NODE_DRAW_SETUP() \
do { \
CCASSERT(getShaderProgram(), "No shader program set for this node"); \
{ \
getShaderProgram()->use(); \
getShaderProgram()->setUniformsForBuiltins(_modelViewTransform); \
} \
} while(0)



/* 一般我就直接Director::getInstance()->end() */
#define CC_DIRECTOR_END() \
do { \
Director *__director = Director::getInstance(); \
__director->end(); \
} while(0)


/* 获得缩放比例 */
#define CC_CONTENT_SCALE_FACTOR() Director::getInstance()->getContentScaleFactor()

/*****************
* RETINA 显示屏 *
*****************/


#define CC_RECT_PIXELS_TO_POINTS(__rect_in_pixels__) \
Rect( (__rect_in_pixels__).origin.x / CC_CONTENT_SCALE_FACTOR(), (__rect_in_pixels__).origin.y / CC_CONTENT_SCALE_FACTOR(), \
(__rect_in_pixels__).size.width / CC_CONTENT_SCALE_FACTOR(), (__rect_in_pixels__).size.height / CC_CONTENT_SCALE_FACTOR() )


#define CC_RECT_POINTS_TO_PIXELS(__rect_in_points_points__) \
Rect( (__rect_in_points_points__).origin.x * CC_CONTENT_SCALE_FACTOR(), (__rect_in_points_points__).origin.y * CC_CONTENT_SCALE_FACTOR(), \
(__rect_in_points_points__).size.width * CC_CONTENT_SCALE_FACTOR(), (__rect_in_points_points__).size.height * CC_CONTENT_SCALE_FACTOR() )


#define CC_POINT_PIXELS_TO_POINTS(__pixels__) \
Point( (__pixels__).x / CC_CONTENT_SCALE_FACTOR(), (__pixels__).y / CC_CONTENT_SCALE_FACTOR())


#define CC_POINT_POINTS_TO_PIXELS(__points__) \
Point( (__points__).x * CC_CONTENT_SCALE_FACTOR(), (__points__).y * CC_CONTENT_SCALE_FACTOR())


#define CC_SIZE_PIXELS_TO_POINTS(__size_in_pixels__) \
Size( (__size_in_pixels__).width / CC_CONTENT_SCALE_FACTOR(), (__size_in_pixels__).height / CC_CONTENT_SCALE_FACTOR())


#define CC_SIZE_POINTS_TO_PIXELS(__size_in_points__) \
Size( (__size_in_points__).width * CC_CONTENT_SCALE_FACTOR(), (__size_in_points__).height * CC_CONTENT_SCALE_FACTOR())



#ifndef FLT_EPSILON
#define FLT_EPSILON 1.192092896e-07F
#endif // FLT_EPSILON

#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&);\
void operator=(const TypeName&)



/* 是否大端 */
#define CC_HOST_IS_BIG_ENDIAN (bool)(*(unsigned short *)"\0\xff" < 0x100)

/* 交换交换4字节 */
#define CC_SWAP32(i) ((i & 0x000000ff) << 24 | (i & 0x0000ff00) << 8 | (i & 0x00ff0000) >> 8 | (i & 0xff000000) >> 24)

/* 交换交换2字节 */
#define CC_SWAP16(i) ((i & 0x00ff) << 8 | (i &0xff00) >> 8)

/* 大小端转换 */
#define CC_SWAP_INT32_LITTLE_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? CC_SWAP32(i) : (i) )
#define CC_SWAP_INT16_LITTLE_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? CC_SWAP16(i) : (i) )
#define CC_SWAP_INT32_BIG_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? (i) : CC_SWAP32(i) )
#define CC_SWAP_INT16_BIG_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? (i): CC_SWAP16(i) )

/**********************/
/** Profiling Macros **/
/**********************/
#if CC_ENABLE_PROFILERS

#define CC_PROFILER_DISPLAY_TIMERS() Profiler::getInstance()->displayTimers()
#define CC_PROFILER_PURGE_ALL() Profiler::getInstance()->releaseAllTimers()

#define CC_PROFILER_START(__name__) ProfilingBeginTimingBlock(__name__)
#define CC_PROFILER_STOP(__name__) ProfilingEndTimingBlock(__name__)
#define CC_PROFILER_RESET(__name__) ProfilingResetTimingBlock(__name__)

#define CC_PROFILER_START_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingBeginTimingBlock(__name__); } while(0)
#define CC_PROFILER_STOP_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingEndTimingBlock(__name__); } while(0)
#define CC_PROFILER_RESET_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingResetTimingBlock(__name__); } while(0)

#define CC_PROFILER_START_INSTANCE(__id__, __name__) do{ ProfilingBeginTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
#define CC_PROFILER_STOP_INSTANCE(__id__, __name__) do{ ProfilingEndTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
#define CC_PROFILER_RESET_INSTANCE(__id__, __name__) do{ ProfilingResetTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)


#else

#define CC_PROFILER_DISPLAY_TIMERS() do {} while (0)
#define CC_PROFILER_PURGE_ALL() do {} while (0)

#define CC_PROFILER_START(__name__) do {} while (0)
#define CC_PROFILER_STOP(__name__) do {} while (0)
#define CC_PROFILER_RESET(__name__) do {} while (0)

#define CC_PROFILER_START_CATEGORY(__cat__, __name__) do {} while(0)
#define CC_PROFILER_STOP_CATEGORY(__cat__, __name__) do {} while(0)
#define CC_PROFILER_RESET_CATEGORY(__cat__, __name__) do {} while(0)

#define CC_PROFILER_START_INSTANCE(__id__, __name__) do {} while(0)
#define CC_PROFILER_STOP_INSTANCE(__id__, __name__) do {} while(0)
#define CC_PROFILER_RESET_INSTANCE(__id__, __name__) do {} while(0)

#endif

#if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
#define CHECK_GL_ERROR_DEBUG()
#else
#define CHECK_GL_ERROR_DEBUG() \
do { \
GLenum __error = glGetError(); \
if(__error) { \
cocos2d::log("OpenGL error 0x%04X in %s %s %d\n", __error, __FILE__, __FUNCTION__, __LINE__); \
} \
} while (false)

#endif

#define CC_INCREMENT_GL_DRAWS(__n__) Director::getInstance()->getRenderer()->addDrawnBatches(__n__)
#define CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(__drawcalls__, __vertices__) \
do { \
auto __renderer__ = Director::getInstance()->getRenderer(); \
__renderer__->addDrawnBatches(__drawcalls__); \
__renderer__->addDrawnVertices(__vertices__); \
} while(0)


/**********/
/** 通知 **/
/**********/

/* 不知道这个什么用的 */
#define AnimationFrameDisplayedNotification "CCAnimationFrameDisplayedNotification"

/* 基于c++11的回调函数的绑定 */
#define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
#define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
#define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)

总结

后续应该还会继续添加。