ccMacros.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #pragma once
  25. /*
  26. #ifndef _USE_MATH_DEFINES
  27. #define _USE_MATH_DEFINES
  28. #endif
  29. */
  30. #include "base/CCLog.h"
  31. #include "base/ccConfig.h"
  32. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  33. #include <BaseTsd.h>
  34. #ifndef __SSIZE_T
  35. #define __SSIZE_T
  36. typedef SSIZE_T ssize_t;
  37. #endif // __SSIZE_T
  38. #endif
  39. #ifndef CCASSERT
  40. #if COCOS2D_DEBUG > 0
  41. // todo: minggo
  42. // #if CC_ENABLE_SCRIPT_BINDING
  43. // extern bool CC_DLL cc_assert_script_compatible(const char *msg);
  44. // #define CCASSERT(cond, msg) do { \
  45. // if (!(cond)) { \
  46. // if (!cc_assert_script_compatible(msg) && strlen(msg)) \
  47. // cocos2d::log("Assert failed: %s", msg); \
  48. // CC_ASSERT(cond); \
  49. // } \
  50. // } while (0)
  51. // #else
  52. #define CCASSERT(cond, msg) CC_ASSERT(cond)
  53. // #endif
  54. #else
  55. #define CCASSERT(cond, msg)
  56. #endif
  57. #define GP_ASSERT(cond) CCASSERT(cond, "")
  58. #endif // CCASSERT
  59. /** @def CC_DEGREES_TO_RADIANS
  60. converts degrees to radians
  61. */
  62. #define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180
  63. /** @def CC_RADIANS_TO_DEGREES
  64. converts radians to degrees
  65. */
  66. #define CC_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f) // PI * 180
  67. /** @def CC_SIGN
  68. gets sign of value
  69. */
  70. #define CC_SIGN(__VALUE__) ((__VALUE__) > 0 ? 1 : ((__VALUE__) < 0 ? -1 : 0))
  71. #ifndef FLT_EPSILON
  72. #define FLT_EPSILON 1.192092896e-07F
  73. #endif // FLT_EPSILON
  74. #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  75. TypeName(const TypeName&);\
  76. void operator=(const TypeName&)
  77. /** @def CC_SWAP
  78. simple macro that swaps 2 variables
  79. @deprecated use std::swap() instead
  80. */
  81. #define CC_SWAP(x, y, type) \
  82. { type temp = (x); \
  83. x = y; y = temp; \
  84. }
  85. /**
  86. Helper macros which converts 4-byte little/big endian
  87. integral number to the machine native number representation
  88. It should work same as apples CFSwapInt32LittleToHost(..)
  89. */
  90. /// when define returns true it means that our architecture uses big endian
  91. #define CC_HOST_IS_BIG_ENDIAN (bool)(*(unsigned short *)"\0\xff" < 0x100)
  92. #define CC_SWAP32(i) ((i & 0x000000ff) << 24 | (i & 0x0000ff00) << 8 | (i & 0x00ff0000) >> 8 | (i & 0xff000000) >> 24)
  93. #define CC_SWAP16(i) ((i & 0x00ff) << 8 | (i &0xff00) >> 8)
  94. #define CC_SWAP_INT32_LITTLE_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? CC_SWAP32(i) : (i) )
  95. #define CC_SWAP_INT16_LITTLE_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? CC_SWAP16(i) : (i) )
  96. #define CC_SWAP_INT32_BIG_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? (i) : CC_SWAP32(i) )
  97. #define CC_SWAP_INT16_BIG_TO_HOST(i) ((CC_HOST_IS_BIG_ENDIAN == true)? (i): CC_SWAP16(i) )
  98. #if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
  99. #define CHECK_GL_ERROR_DEBUG()
  100. #else
  101. #define CHECK_GL_ERROR_DEBUG() \
  102. do { \
  103. GLenum __error = glGetError(); \
  104. if(__error) { \
  105. cocos2d::log("OpenGL error 0x%04X in %s %s %d\n", __error, __FILE__, __FUNCTION__, __LINE__); \
  106. } \
  107. } while (false)
  108. #endif // !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
  109. /**
  110. * GL assertion that can be used for any OpenGL function call.
  111. *
  112. * This macro will assert if an error is detected when executing
  113. * the specified GL code. This macro will do nothing in release
  114. * mode and is therefore safe to use for realtime/per-frame GL
  115. * function calls.
  116. */
  117. #if defined(NDEBUG) || (defined(__APPLE__) && !defined(DEBUG))
  118. #define CC_GL_ASSERT( gl_code ) gl_code
  119. #else
  120. #define CC_GL_ASSERT( gl_code ) do \
  121. { \
  122. gl_code; \
  123. __gl_error_code = glGetError(); \
  124. CC_ASSERT(__gl_error_code == GL_NO_ERROR, "Error"); \
  125. } while(0)
  126. #endif // defined(NDEBUG) || (defined(__APPLE__) && !defined(DEBUG))
  127. /*********************************/
  128. /** 64bits Program Sense Macros **/
  129. /*********************************/
  130. #if defined(_M_X64) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) || defined(__x86_64)
  131. #define CC_64BITS 1
  132. #else
  133. #define CC_64BITS 0
  134. #endif
  135. // new callbacks based on C++11
  136. #define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
  137. #define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
  138. #define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
  139. #define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)
  140. // Generic macros
  141. /// @name namespace cocos2d
  142. /// @{
  143. #ifdef __cplusplus
  144. #define NS_CC_BEGIN namespace cocos2d {
  145. #define NS_CC_END }
  146. #define USING_NS_CC using namespace cocos2d
  147. #define NS_CC ::cocos2d
  148. #else
  149. #define NS_CC_BEGIN
  150. #define NS_CC_END
  151. #define USING_NS_CC
  152. #define NS_CC
  153. #endif
  154. // end of namespace group
  155. /// @}
  156. #define CC_SAFE_DELETE(p) do { delete (p); (p) = nullptr; } while(0)
  157. #define CC_SAFE_DELETE_ARRAY(p) do { if(p) { delete[] (p); (p) = nullptr; } } while(0)
  158. #define CC_SAFE_FREE(p) do { if(p) { free(p); (p) = nullptr; } } while(0)
  159. #define CC_SAFE_RELEASE(p) do { if(p) { (p)->release(); } } while(0)
  160. #define CC_SAFE_RELEASE_NULL(p) do { if(p) { (p)->release(); (p) = nullptr; } } while(0)
  161. #define CC_SAFE_RETAIN(p) do { if(p) { (p)->retain(); } } while(0)
  162. #define CC_BREAK_IF(cond) if(cond) break
  163. #define __CCLOGWITHFUNCTION(s, ...) \
  164. cocos2d::log("%s : %s",__FUNCTION__, cocos2d::StringUtils::format(s, ##__VA_ARGS__).c_str())
  165. /// @name Cocos2d debug
  166. /// @{
  167. #if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
  168. #define CCLOG(...) do {} while (0)
  169. #define CCLOGINFO(...) do {} while (0)
  170. #define CCLOGERROR(...) do {} while (0)
  171. #define CCLOGWARN(...) do {} while (0)
  172. #elif COCOS2D_DEBUG == 1
  173. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  174. #define COCOS_LOG_TAG "cocos2d-x"
  175. #define CCLOG(...) __android_log_print(ANDROID_LOG_DEBUG, COCOS_LOG_TAG, __VA_ARGS__)
  176. #define CCLOGINFO(format,...) do {} while (0)
  177. #define CCLOGWARN(...) __android_log_print(ANDROID_LOG_WARN, COCOS_LOG_TAG, __VA_ARGS__)
  178. #define CCLOGERROR(...) __android_log_print(ANDROID_LOG_ERROR, COCOS_LOG_TAG, __VA_ARGS__)
  179. #else
  180. #define CCLOG(format, ...) cocos2d::log(format, ##__VA_ARGS__)
  181. #define CCLOGERROR(format,...) cocos2d::log(format, ##__VA_ARGS__)
  182. #define CCLOGINFO(format,...) do {} while (0)
  183. #define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)
  184. #endif
  185. #elif COCOS2D_DEBUG > 1
  186. #define CCLOG(format, ...) cocos2d::log(format, ##__VA_ARGS__)
  187. #define CCLOGERROR(format,...) cocos2d::log(format, ##__VA_ARGS__)
  188. #define CCLOGINFO(format,...) cocos2d::log(format, ##__VA_ARGS__)
  189. #define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)
  190. #endif // COCOS2D_DEBUG
  191. // end of debug group
  192. /// @}
  193. /** @def CC_DISALLOW_COPY_AND_ASSIGN(TypeName)
  194. * A macro to disallow the copy constructor and operator= functions.
  195. * This should be used in the private: declarations for a class
  196. */
  197. #if defined(__GNUC__) && ((__GNUC__ >= 5) || ((__GNUG__ == 4) && (__GNUC_MINOR__ >= 4))) \
  198. || (defined(__clang__) && (__clang_major__ >= 3)) || (_MSC_VER >= 1800)
  199. #define CC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
  200. TypeName(const TypeName &) = delete; \
  201. TypeName &operator =(const TypeName &) = delete;
  202. #else
  203. #define CC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
  204. TypeName(const TypeName &); \
  205. TypeName &operator =(const TypeName &);
  206. #endif
  207. /** @def CC_DEPRECATED_ATTRIBUTE
  208. * Only certain compilers support __attribute__((deprecated)).
  209. */
  210. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  211. #define CC_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
  212. #elif _MSC_VER >= 1400 //vs 2005 or higher
  213. #define CC_DEPRECATED_ATTRIBUTE __declspec(deprecated)
  214. #else
  215. #define CC_DEPRECATED_ATTRIBUTE
  216. #endif
  217. /** @def CC_DEPRECATED(...)
  218. * Macro to mark things deprecated as of a particular version
  219. * can be used with arbitrary parameters which are thrown away.
  220. * e.g. CC_DEPRECATED(4.0) or CC_DEPRECATED(4.0, "not going to need this anymore") etc.
  221. */
  222. #define CC_DEPRECATED(...) CC_DEPRECATED_ATTRIBUTE
  223. /** @def CC_FORMAT_PRINTF(formatPos, argPos)
  224. * Only certain compiler support __attribute__((format))
  225. *
  226. * @param formatPos 1-based position of format string argument.
  227. * @param argPos 1-based position of first format-dependent argument.
  228. */
  229. #if defined(__GNUC__) && (__GNUC__ >= 4)
  230. #define CC_FORMAT_PRINTF(formatPos, argPos) __attribute__((__format__(printf, formatPos, argPos)))
  231. #elif defined(__has_attribute)
  232. #if __has_attribute(format)
  233. #define CC_FORMAT_PRINTF(formatPos, argPos) __attribute__((__format__(printf, formatPos, argPos)))
  234. #endif // __has_attribute(format)
  235. #else
  236. #define CC_FORMAT_PRINTF(formatPos, argPos)
  237. #endif
  238. #if defined(_MSC_VER)
  239. #define CC_FORMAT_PRINTF_SIZE_T "%08lX"
  240. #else
  241. #define CC_FORMAT_PRINTF_SIZE_T "%08zX"
  242. #endif
  243. #ifdef __GNUC__
  244. #define CC_UNUSED __attribute__ ((unused))
  245. #else
  246. #define CC_UNUSED
  247. #endif
  248. /** @def CC_REQUIRES_NULL_TERMINATION
  249. *
  250. */
  251. #if !defined(CC_REQUIRES_NULL_TERMINATION)
  252. #if defined(__APPLE_CC__) && (__APPLE_CC__ >= 5549)
  253. #define CC_REQUIRES_NULL_TERMINATION __attribute__((sentinel(0,1)))
  254. #elif defined(__GNUC__)
  255. #define CC_REQUIRES_NULL_TERMINATION __attribute__((sentinel))
  256. #else
  257. #define CC_REQUIRES_NULL_TERMINATION
  258. #endif
  259. #endif