1
0

ccMacros.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #ifndef __BASE_CCMACROS_H__
  24. #define __BASE_CCMACROS_H__
  25. #ifndef _USE_MATH_DEFINES
  26. #define _USE_MATH_DEFINES
  27. #endif
  28. #include "base/CCConsole.h"
  29. #include "platform/CCStdC.h"
  30. #ifndef CCASSERT
  31. #if COCOS2D_DEBUG > 0
  32. #if CC_ENABLE_SCRIPT_BINDING
  33. extern bool CC_DLL cc_assert_script_compatible(const char *msg);
  34. #define CCASSERT(cond, msg) do { \
  35. if (!(cond)) { \
  36. if (!cc_assert_script_compatible(msg) && strlen(msg)) \
  37. cocos2d::log("Assert failed: %s", msg); \
  38. CC_ASSERT(cond); \
  39. } \
  40. } while (0)
  41. #else
  42. #define CCASSERT(cond, msg) CC_ASSERT(cond)
  43. #endif
  44. #else
  45. #define CCASSERT(cond, msg)
  46. #endif
  47. #define GP_ASSERT(cond) CCASSERT(cond, "")
  48. // FIXME:: Backward compatible
  49. #define CCAssert CCASSERT
  50. #endif // CCASSERT
  51. #include "base/ccConfig.h"
  52. /** @def CC_SWAP
  53. simple macro that swaps 2 variables
  54. @deprecated use std::swap() instead
  55. */
  56. #define CC_SWAP(x, y, type) \
  57. { type temp = (x); \
  58. x = y; y = temp; \
  59. }
  60. #include "base/ccRandom.h"
  61. /** @def CCRANDOM_MINUS1_1
  62. returns a random float between -1 and 1
  63. */
  64. #define CCRANDOM_MINUS1_1() cocos2d::rand_minus1_1()
  65. /** @def CCRANDOM_0_1
  66. returns a random float between 0 and 1
  67. */
  68. #define CCRANDOM_0_1() cocos2d::rand_0_1()
  69. /** @def CC_DEGREES_TO_RADIANS
  70. converts degrees to radians
  71. */
  72. #define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180
  73. /** @def CC_RADIANS_TO_DEGREES
  74. converts radians to degrees
  75. */
  76. #define CC_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f) // PI * 180
  77. #define CC_REPEAT_FOREVER (UINT_MAX -1)
  78. #define kRepeatForever CC_REPEAT_FOREVER
  79. /** @def CC_BLEND_SRC
  80. default gl blend src function. Compatible with premultiplied alpha images.
  81. */
  82. #define CC_BLEND_SRC GL_ONE
  83. #define CC_BLEND_DST GL_ONE_MINUS_SRC_ALPHA
  84. /** @def CC_NODE_DRAW_SETUP
  85. Helpful macro that setups the GL server state, the correct GL program and sets the Model View Projection matrix
  86. @since v2.0
  87. */
  88. #define CC_NODE_DRAW_SETUP() \
  89. do { \
  90. CCASSERT(getGLProgram(), "No shader program set for this node"); \
  91. { \
  92. getGLProgram()->use(); \
  93. getGLProgram()->setUniformsForBuiltins(_modelViewTransform); \
  94. } \
  95. } while(0)
  96. /** @def CC_DIRECTOR_END
  97. Stops and removes the director from memory.
  98. Removes the GLView from its parent
  99. @since v0.99.4
  100. */
  101. #define CC_DIRECTOR_END() \
  102. do { \
  103. Director *__director = cocos2d::Director::getInstance(); \
  104. __director->end(); \
  105. } while(0)
  106. /** @def CC_CONTENT_SCALE_FACTOR
  107. On Mac it returns 1;
  108. On iPhone it returns 2 if RetinaDisplay is On. Otherwise it returns 1
  109. */
  110. #define CC_CONTENT_SCALE_FACTOR() cocos2d::Director::getInstance()->getContentScaleFactor()
  111. /****************************/
  112. /** RETINA DISPLAY ENABLED **/
  113. /****************************/
  114. /** @def CC_RECT_PIXELS_TO_POINTS
  115. Converts a rect in pixels to points
  116. */
  117. #define CC_RECT_PIXELS_TO_POINTS(__rect_in_pixels__) \
  118. cocos2d::Rect( (__rect_in_pixels__).origin.x / CC_CONTENT_SCALE_FACTOR(), (__rect_in_pixels__).origin.y / CC_CONTENT_SCALE_FACTOR(), \
  119. (__rect_in_pixels__).size.width / CC_CONTENT_SCALE_FACTOR(), (__rect_in_pixels__).size.height / CC_CONTENT_SCALE_FACTOR() )
  120. /** @def CC_RECT_POINTS_TO_PIXELS
  121. Converts a rect in points to pixels
  122. */
  123. #define CC_RECT_POINTS_TO_PIXELS(__rect_in_points_points__) \
  124. cocos2d::Rect( (__rect_in_points_points__).origin.x * CC_CONTENT_SCALE_FACTOR(), (__rect_in_points_points__).origin.y * CC_CONTENT_SCALE_FACTOR(), \
  125. (__rect_in_points_points__).size.width * CC_CONTENT_SCALE_FACTOR(), (__rect_in_points_points__).size.height * CC_CONTENT_SCALE_FACTOR() )
  126. /** @def CC_POINT_PIXELS_TO_POINTS
  127. Converts a rect in pixels to points
  128. */
  129. #define CC_POINT_PIXELS_TO_POINTS(__pixels__) \
  130. cocos2d::Vec2( (__pixels__).x / CC_CONTENT_SCALE_FACTOR(), (__pixels__).y / CC_CONTENT_SCALE_FACTOR())
  131. /** @def CC_POINT_POINTS_TO_PIXELS
  132. Converts a rect in points to pixels
  133. */
  134. #define CC_POINT_POINTS_TO_PIXELS(__points__) \
  135. cocos2d::Vec2( (__points__).x * CC_CONTENT_SCALE_FACTOR(), (__points__).y * CC_CONTENT_SCALE_FACTOR())
  136. /** @def CC_POINT_PIXELS_TO_POINTS
  137. Converts a rect in pixels to points
  138. */
  139. #define CC_SIZE_PIXELS_TO_POINTS(__size_in_pixels__) \
  140. cocos2d::Size( (__size_in_pixels__).width / CC_CONTENT_SCALE_FACTOR(), (__size_in_pixels__).height / CC_CONTENT_SCALE_FACTOR())
  141. /** @def CC_POINT_POINTS_TO_PIXELS
  142. Converts a rect in points to pixels
  143. */
  144. #define CC_SIZE_POINTS_TO_PIXELS(__size_in_points__) \
  145. cocos2d::Size( (__size_in_points__).width * CC_CONTENT_SCALE_FACTOR(), (__size_in_points__).height * CC_CONTENT_SCALE_FACTOR())
  146. #ifndef FLT_EPSILON
  147. #define FLT_EPSILON 1.192092896e-07F
  148. #endif // FLT_EPSILON
  149. #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  150. TypeName(const TypeName&);\
  151. void operator=(const TypeName&)
  152. /**
  153. Helper macros which converts 4-byte little/big endian
  154. integral number to the machine native number representation
  155. It should work same as apples CFSwapInt32LittleToHost(..)
  156. */
  157. /// when define returns true it means that our architecture uses big endian
  158. #define CC_HOST_IS_BIG_ENDIAN (bool)(*(unsigned short *)"\0\xff" < 0x100)
  159. #define CC_SWAP32(i) ((i & 0x000000ff) << 24 | (i & 0x0000ff00) << 8 | (i & 0x00ff0000) >> 8 | (i & 0xff000000) >> 24)
  160. #define CC_SWAP16(i) ((i & 0x00ff) << 8 | (i &0xff00) >> 8)
  161. #define CC_SWAP_INT32_LITTLE_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? CC_SWAP32(i) : (i) )
  162. #define CC_SWAP_INT16_LITTLE_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? CC_SWAP16(i) : (i) )
  163. #define CC_SWAP_INT32_BIG_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? (i) : CC_SWAP32(i) )
  164. #define CC_SWAP_INT16_BIG_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? (i): CC_SWAP16(i) )
  165. /**********************/
  166. /** Profiling Macros **/
  167. /**********************/
  168. #if CC_ENABLE_PROFILERS
  169. #define CC_PROFILER_DISPLAY_TIMERS() NS_CC::Profiler::getInstance()->displayTimers()
  170. #define CC_PROFILER_PURGE_ALL() NS_CC::Profiler::getInstance()->releaseAllTimers()
  171. #define CC_PROFILER_START(__name__) NS_CC::ProfilingBeginTimingBlock(__name__)
  172. #define CC_PROFILER_STOP(__name__) NS_CC::ProfilingEndTimingBlock(__name__)
  173. #define CC_PROFILER_RESET(__name__) NS_CC::ProfilingResetTimingBlock(__name__)
  174. #define CC_PROFILER_START_CATEGORY(__cat__, __name__) do{ if(__cat__) NS_CC::ProfilingBeginTimingBlock(__name__); } while(0)
  175. #define CC_PROFILER_STOP_CATEGORY(__cat__, __name__) do{ if(__cat__) NS_CC::ProfilingEndTimingBlock(__name__); } while(0)
  176. #define CC_PROFILER_RESET_CATEGORY(__cat__, __name__) do{ if(__cat__) NS_CC::ProfilingResetTimingBlock(__name__); } while(0)
  177. #define CC_PROFILER_START_INSTANCE(__id__, __name__) do{ NS_CC::ProfilingBeginTimingBlock( NS_CC::String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
  178. #define CC_PROFILER_STOP_INSTANCE(__id__, __name__) do{ NS_CC::ProfilingEndTimingBlock( NS_CC::String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
  179. #define CC_PROFILER_RESET_INSTANCE(__id__, __name__) do{ NS_CC::ProfilingResetTimingBlock( NS_CC::String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
  180. #else
  181. #define CC_PROFILER_DISPLAY_TIMERS() do {} while (0)
  182. #define CC_PROFILER_PURGE_ALL() do {} while (0)
  183. #define CC_PROFILER_START(__name__) do {} while (0)
  184. #define CC_PROFILER_STOP(__name__) do {} while (0)
  185. #define CC_PROFILER_RESET(__name__) do {} while (0)
  186. #define CC_PROFILER_START_CATEGORY(__cat__, __name__) do {} while(0)
  187. #define CC_PROFILER_STOP_CATEGORY(__cat__, __name__) do {} while(0)
  188. #define CC_PROFILER_RESET_CATEGORY(__cat__, __name__) do {} while(0)
  189. #define CC_PROFILER_START_INSTANCE(__id__, __name__) do {} while(0)
  190. #define CC_PROFILER_STOP_INSTANCE(__id__, __name__) do {} while(0)
  191. #define CC_PROFILER_RESET_INSTANCE(__id__, __name__) do {} while(0)
  192. #endif
  193. #if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
  194. #define CHECK_GL_ERROR_DEBUG()
  195. #else
  196. #define CHECK_GL_ERROR_DEBUG() \
  197. do { \
  198. GLenum __error = glGetError(); \
  199. if(__error) { \
  200. cocos2d::log("OpenGL error 0x%04X in %s %s %d\n", __error, __FILE__, __FUNCTION__, __LINE__); \
  201. } \
  202. } while (false)
  203. #endif
  204. /**
  205. * GL assertion that can be used for any OpenGL function call.
  206. *
  207. * This macro will assert if an error is detected when executing
  208. * the specified GL code. This macro will do nothing in release
  209. * mode and is therefore safe to use for realtime/per-frame GL
  210. * function calls.
  211. */
  212. #if defined(NDEBUG) || (defined(__APPLE__) && !defined(DEBUG))
  213. #define CC_GL_ASSERT( gl_code ) gl_code
  214. #else
  215. #define CC_GL_ASSERT( gl_code ) do \
  216. { \
  217. gl_code; \
  218. __gl_error_code = glGetError(); \
  219. CC_ASSERT(__gl_error_code == GL_NO_ERROR, "Error"); \
  220. } while(0)
  221. #endif
  222. /*********************************/
  223. /** 64bits Program Sense Macros **/
  224. /*********************************/
  225. #if defined(_M_X64) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) || defined(__x86_64)
  226. #define CC_64BITS 1
  227. #else
  228. #define CC_64BITS 0
  229. #endif
  230. /** @def CC_INCREMENT_GL_DRAWS_BY_ONE
  231. Increments the GL Draws counts by one.
  232. The number of calls per frame are displayed on the screen when the Director's stats are enabled.
  233. */
  234. #define CC_INCREMENT_GL_DRAWS(__n__) cocos2d::Director::getInstance()->getRenderer()->addDrawnBatches(__n__)
  235. #define CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(__drawcalls__, __vertices__) \
  236. do { \
  237. auto __renderer__ = cocos2d::Director::getInstance()->getRenderer(); \
  238. __renderer__->addDrawnBatches(__drawcalls__); \
  239. __renderer__->addDrawnVertices(__vertices__); \
  240. } while(0)
  241. /*******************/
  242. /** Notifications **/
  243. /*******************/
  244. /** @def AnimationFrameDisplayedNotification
  245. Notification name when a SpriteFrame is displayed
  246. */
  247. #define AnimationFrameDisplayedNotification "CCAnimationFrameDisplayedNotification"
  248. /*******************/
  249. /** Notifications **/
  250. /*******************/
  251. /** @def Animate3DDisplayedNotification
  252. Notification name when a frame in Animate3D is played
  253. */
  254. #define Animate3DDisplayedNotification "CCAnimate3DDisplayedNotification"
  255. // new callbacks based on C++11
  256. #define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
  257. #define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
  258. #define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
  259. #define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)
  260. #endif // __BASE_CCMACROS_H__