2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCMACROS_H__
|
|
|
|
#define __CCMACROS_H__
|
|
|
|
|
|
|
|
#ifndef _USE_MATH_DEFINES
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
#endif
|
|
|
|
|
2014-01-11 11:04:07 +08:00
|
|
|
#include "CCConsole.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCStdC.h"
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
#ifndef CCASSERT
|
2012-12-26 18:33:55 +08:00
|
|
|
#if COCOS2D_DEBUG > 0
|
2014-02-20 17:01:13 +08:00
|
|
|
#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
|
2012-12-22 10:13:37 +08:00
|
|
|
#else
|
2014-02-20 17:01:13 +08:00
|
|
|
#define CCASSERT(cond, msg)
|
2012-12-22 10:13:37 +08:00
|
|
|
#endif
|
2014-04-02 11:21:48 +08:00
|
|
|
|
|
|
|
#define GP_ASSERT(cond) CCASSERT(cond, "")
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
// XXX: Backward compatible
|
|
|
|
#define CCAssert CCASSERT
|
|
|
|
#endif // CCASSERT
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#include "ccConfig.h"
|
|
|
|
|
|
|
|
/** @def CC_SWAP
|
|
|
|
simple macro that swaps 2 variables
|
|
|
|
*/
|
|
|
|
#define CC_SWAP(x, y, type) \
|
|
|
|
{ type temp = (x); \
|
|
|
|
x = y; y = temp; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @def CCRANDOM_MINUS1_1
|
|
|
|
returns a random float between -1 and 1
|
|
|
|
*/
|
|
|
|
#define CCRANDOM_MINUS1_1() ((2.0f*((float)rand()/RAND_MAX))-1.0f)
|
|
|
|
|
|
|
|
/** @def CCRANDOM_0_1
|
|
|
|
returns a random float between 0 and 1
|
|
|
|
*/
|
|
|
|
#define CCRANDOM_0_1() ((float)rand()/RAND_MAX)
|
|
|
|
|
|
|
|
/** @def CC_DEGREES_TO_RADIANS
|
|
|
|
converts degrees to radians
|
|
|
|
*/
|
|
|
|
#define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180
|
|
|
|
|
|
|
|
/** @def CC_RADIANS_TO_DEGREES
|
|
|
|
converts radians to degrees
|
|
|
|
*/
|
|
|
|
#define CC_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f) // PI * 180
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
#define kRepeatForever (UINT_MAX -1)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** @def CC_BLEND_SRC
|
|
|
|
default gl blend src function. Compatible with premultiplied alpha images.
|
|
|
|
*/
|
|
|
|
#define CC_BLEND_SRC GL_ONE
|
|
|
|
#define CC_BLEND_DST GL_ONE_MINUS_SRC_ALPHA
|
|
|
|
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
/** @def CC_NODE_DRAW_SETUP
|
|
|
|
Helpful macro that setups the GL server state, the correct GL program and sets the Model View Projection matrix
|
|
|
|
@since v2.0
|
|
|
|
*/
|
2012-03-16 17:56:19 +08:00
|
|
|
#define CC_NODE_DRAW_SETUP() \
|
|
|
|
do { \
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(getShaderProgram(), "No shader program set for this node"); \
|
2012-04-19 14:35:52 +08:00
|
|
|
{ \
|
|
|
|
getShaderProgram()->use(); \
|
2013-12-07 09:42:16 +08:00
|
|
|
getShaderProgram()->setUniformsForBuiltins(_modelViewTransform); \
|
2012-04-19 14:35:52 +08:00
|
|
|
} \
|
2012-03-14 14:55:17 +08:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
|
|
/** @def CC_DIRECTOR_END
|
|
|
|
Stops and removes the director from memory.
|
2013-06-20 14:13:12 +08:00
|
|
|
Removes the GLView from its parent
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
@since v0.99.4
|
|
|
|
*/
|
2013-07-12 06:24:23 +08:00
|
|
|
#define CC_DIRECTOR_END() \
|
2012-04-19 14:35:52 +08:00
|
|
|
do { \
|
2013-07-12 06:24:23 +08:00
|
|
|
Director *__director = Director::getInstance(); \
|
|
|
|
__director->end(); \
|
2012-03-14 14:55:17 +08:00
|
|
|
} while(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** @def CC_CONTENT_SCALE_FACTOR
|
|
|
|
On Mac it returns 1;
|
|
|
|
On iPhone it returns 2 if RetinaDisplay is On. Otherwise it returns 1
|
|
|
|
*/
|
2013-07-12 06:24:23 +08:00
|
|
|
#define CC_CONTENT_SCALE_FACTOR() Director::getInstance()->getContentScaleFactor()
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
/****************************/
|
|
|
|
/** RETINA DISPLAY ENABLED **/
|
|
|
|
/****************************/
|
|
|
|
|
|
|
|
/** @def CC_RECT_PIXELS_TO_POINTS
|
|
|
|
Converts a rect in pixels to points
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
#define CC_RECT_PIXELS_TO_POINTS(__rect_in_pixels__) \
|
2013-07-12 14:30:26 +08:00
|
|
|
Rect( (__rect_in_pixels__).origin.x / CC_CONTENT_SCALE_FACTOR(), (__rect_in_pixels__).origin.y / CC_CONTENT_SCALE_FACTOR(), \
|
2012-04-19 14:35:52 +08:00
|
|
|
(__rect_in_pixels__).size.width / CC_CONTENT_SCALE_FACTOR(), (__rect_in_pixels__).size.height / CC_CONTENT_SCALE_FACTOR() )
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
/** @def CC_RECT_POINTS_TO_PIXELS
|
|
|
|
Converts a rect in points to pixels
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
#define CC_RECT_POINTS_TO_PIXELS(__rect_in_points_points__) \
|
2013-07-12 14:30:26 +08:00
|
|
|
Rect( (__rect_in_points_points__).origin.x * CC_CONTENT_SCALE_FACTOR(), (__rect_in_points_points__).origin.y * CC_CONTENT_SCALE_FACTOR(), \
|
2012-04-19 14:35:52 +08:00
|
|
|
(__rect_in_points_points__).size.width * CC_CONTENT_SCALE_FACTOR(), (__rect_in_points_points__).size.height * CC_CONTENT_SCALE_FACTOR() )
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
/** @def CC_POINT_PIXELS_TO_POINTS
|
|
|
|
Converts a rect in pixels to points
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
#define CC_POINT_PIXELS_TO_POINTS(__pixels__) \
|
2014-04-15 18:08:29 +08:00
|
|
|
Vector2( (__pixels__).x / CC_CONTENT_SCALE_FACTOR(), (__pixels__).y / CC_CONTENT_SCALE_FACTOR())
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
/** @def CC_POINT_POINTS_TO_PIXELS
|
|
|
|
Converts a rect in points to pixels
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
#define CC_POINT_POINTS_TO_PIXELS(__points__) \
|
2014-04-15 18:08:29 +08:00
|
|
|
Vector2( (__points__).x * CC_CONTENT_SCALE_FACTOR(), (__points__).y * CC_CONTENT_SCALE_FACTOR())
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
/** @def CC_POINT_PIXELS_TO_POINTS
|
|
|
|
Converts a rect in pixels to points
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
#define CC_SIZE_PIXELS_TO_POINTS(__size_in_pixels__) \
|
2013-07-12 14:30:26 +08:00
|
|
|
Size( (__size_in_pixels__).width / CC_CONTENT_SCALE_FACTOR(), (__size_in_pixels__).height / CC_CONTENT_SCALE_FACTOR())
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
/** @def CC_POINT_POINTS_TO_PIXELS
|
|
|
|
Converts a rect in points to pixels
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
#define CC_SIZE_POINTS_TO_PIXELS(__size_in_points__) \
|
2013-07-12 14:30:26 +08:00
|
|
|
Size( (__size_in_points__).width * CC_CONTENT_SCALE_FACTOR(), (__size_in_points__).height * CC_CONTENT_SCALE_FACTOR())
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
#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&)
|
|
|
|
|
|
|
|
/**
|
2012-09-17 15:02:24 +08:00
|
|
|
Helper macros which converts 4-byte little/big endian
|
2012-04-19 14:35:52 +08:00
|
|
|
integral number to the machine native number representation
|
|
|
|
|
|
|
|
It should work same as apples CFSwapInt32LittleToHost(..)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// when define returns true it means that our architecture uses big endian
|
|
|
|
#define CC_HOST_IS_BIG_ENDIAN (bool)(*(unsigned short *)"\0\xff" < 0x100)
|
|
|
|
#define CC_SWAP32(i) ((i & 0x000000ff) << 24 | (i & 0x0000ff00) << 8 | (i & 0x00ff0000) >> 8 | (i & 0xff000000) >> 24)
|
|
|
|
#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) )
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
/**********************/
|
|
|
|
/** Profiling Macros **/
|
|
|
|
/**********************/
|
|
|
|
#if CC_ENABLE_PROFILERS
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
#define CC_PROFILER_DISPLAY_TIMERS() Profiler::getInstance()->displayTimers()
|
|
|
|
#define CC_PROFILER_PURGE_ALL() Profiler::getInstance()->releaseAllTimers()
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
#define CC_PROFILER_START(__name__) ProfilingBeginTimingBlock(__name__)
|
|
|
|
#define CC_PROFILER_STOP(__name__) ProfilingEndTimingBlock(__name__)
|
|
|
|
#define CC_PROFILER_RESET(__name__) ProfilingResetTimingBlock(__name__)
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
#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)
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
#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)
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
#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
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
#if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
|
|
|
|
#define CHECK_GL_ERROR_DEBUG()
|
|
|
|
#else
|
2012-03-16 17:56:19 +08:00
|
|
|
#define CHECK_GL_ERROR_DEBUG() \
|
2012-04-19 14:35:52 +08:00
|
|
|
do { \
|
|
|
|
GLenum __error = glGetError(); \
|
|
|
|
if(__error) { \
|
2013-07-24 06:20:22 +08:00
|
|
|
cocos2d::log("OpenGL error 0x%04X in %s %s %d\n", __error, __FILE__, __FUNCTION__, __LINE__); \
|
2012-04-19 14:35:52 +08:00
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
#endif
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
/** @def CC_INCREMENT_GL_DRAWS_BY_ONE
|
|
|
|
Increments the GL Draws counts by one.
|
2013-06-20 14:13:12 +08:00
|
|
|
The number of calls per frame are displayed on the screen when the Director's stats are enabled.
|
2012-03-16 13:42:53 +08:00
|
|
|
*/
|
2014-02-08 11:37:44 +08:00
|
|
|
#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)
|
2012-03-16 13:42:53 +08:00
|
|
|
|
|
|
|
/*******************/
|
|
|
|
/** Notifications **/
|
|
|
|
/*******************/
|
2013-06-20 14:13:12 +08:00
|
|
|
/** @def AnimationFrameDisplayedNotification
|
|
|
|
Notification name when a SpriteFrame is displayed
|
2012-03-16 13:42:53 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
#define AnimationFrameDisplayedNotification "CCAnimationFrameDisplayedNotification"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
// new callbacks based on 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__)
|
2014-04-10 10:07:01 +08:00
|
|
|
#define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)
|
2014-02-20 10:53:49 +08:00
|
|
|
|
2013-01-10 09:47:14 +08:00
|
|
|
#endif // __CCMACROS_H__
|