axmol/core/base/ccMacros.h

382 lines
16 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
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.
****************************************************************************/
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#ifndef __BASE_CCMACROS_H__
#define __BASE_CCMACROS_H__
#ifndef _USE_MATH_DEFINES
# define _USE_MATH_DEFINES
#endif
2014-04-27 01:35:57 +08:00
#include "base/CCConsole.h"
2014-09-10 07:50:02 +08:00
#include "platform/CCStdC.h"
2022-07-16 10:43:05 +08:00
#ifndef AXASSERT
2022-08-08 18:02:17 +08:00
# if _AX_DEBUG > 0
2022-07-15 19:17:01 +08:00
# if AX_ENABLE_SCRIPT_BINDING
extern bool AX_DLL cc_assert_script_compatible(const char* msg);
2022-07-16 10:43:05 +08:00
# define AXASSERT(cond, msg) \
do \
{ \
if (!(cond)) \
{ \
2021-08-27 11:41:11 +08:00
if (msg && *msg && !cc_assert_script_compatible(msg)) \
2022-08-08 18:02:17 +08:00
ax::log("Assert failed: %s", msg); \
2022-07-15 19:17:01 +08:00
AX_ASSERT(cond); \
} \
} while (0)
# else
2022-07-16 10:43:05 +08:00
# define AXASSERT(cond, msg) AX_ASSERT(cond)
# endif
# else
2022-07-16 10:43:05 +08:00
# define AXASSERT(cond, msg)
# endif
2022-07-16 10:43:05 +08:00
# define GP_ASSERT(cond) AXASSERT(cond, "")
2014-04-02 11:21:48 +08:00
// FIXME:: Backward compatible
2022-07-16 10:43:05 +08:00
# define CCAssert AXASSERT
#endif // AXASSERT
2014-04-30 08:37:36 +08:00
#include "base/ccConfig.h"
2014-08-07 20:01:30 +08:00
#include "base/ccRandom.h"
2022-07-15 19:17:01 +08:00
#define AX_HALF_PI (M_PI * 0.5f)
2021-07-15 17:01:57 +08:00
2022-07-15 19:17:01 +08:00
#define AX_DOUBLE_PI (M_PI * 2)
2021-07-15 17:01:57 +08:00
2022-07-16 10:43:05 +08:00
/** @def AXRANDOM_MINUS1_1
returns a random float between -1 and 1
*/
2022-08-08 18:02:17 +08:00
#define AXRANDOM_MINUS1_1() ax::rand_minus1_1()
2022-07-16 10:43:05 +08:00
/** @def AXRANDOM_0_1
returns a random float between 0 and 1
*/
2022-08-08 18:02:17 +08:00
#define AXRANDOM_0_1() ax::rand_0_1()
2022-07-15 19:17:01 +08:00
/** @def AX_DEGREES_TO_RADIANS
converts degrees to radians
*/
2022-07-15 19:17:01 +08:00
#define AX_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__)*0.01745329252f) // PI / 180
2022-07-15 19:17:01 +08:00
/** @def AX_RADIANS_TO_DEGREES
converts radians to degrees
*/
2022-07-15 19:17:01 +08:00
#define AX_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__)*57.29577951f) // PI * 180
2022-07-15 19:17:01 +08:00
#define AX_REPEAT_FOREVER (UINT_MAX - 1)
#define kRepeatForever AX_REPEAT_FOREVER
2022-07-15 19:17:01 +08:00
/** @def AX_BLEND_SRC
default gl blend src function. Compatible with premultiplied alpha images.
*/
2022-08-08 18:02:17 +08:00
#define AX_BLEND_SRC ax::backend::BlendFactor::ONE
#define AX_BLEND_DST ax::backend::BlendFactor::ONE_MINUS_SRC_ALPHA
2022-07-15 19:17:01 +08:00
/** @def AX_NODE_DRAW_SETUP [DEPRECATED]
Helpful macro that setups the GL server state, the correct GL program and sets the Model View Projection matrix
@since v2.0
*/
2022-07-15 19:17:01 +08:00
#define AX_NODE_DRAW_SETUP()
2022-07-15 19:17:01 +08:00
/** @def AX_DIRECTOR_END
Stops and removes the director from memory.
Removes the GLView from its parent
@since v0.99.4
*/
2022-07-15 19:17:01 +08:00
#define AX_DIRECTOR_END() \
do \
{ \
2022-08-08 18:02:17 +08:00
Director* __director = ax::Director::getInstance(); \
__director->end(); \
} while (0)
2022-07-15 19:17:01 +08:00
/** @def AX_CONTENT_SCALE_FACTOR
On Mac it returns 1;
On iPhone it returns 2 if RetinaDisplay is On. Otherwise it returns 1
*/
2022-08-08 18:02:17 +08:00
#define AX_CONTENT_SCALE_FACTOR() ax::Director::getInstance()->getContentScaleFactor()
/****************************/
/** RETINA DISPLAY ENABLED **/
/****************************/
2022-07-15 19:17:01 +08:00
/** @def AX_RECT_PIXELS_TO_POINTS
Converts a rect in pixels to points
*/
2022-07-15 19:17:01 +08:00
#define AX_RECT_PIXELS_TO_POINTS(__rect_in_pixels__) \
2022-08-08 18:02:17 +08:00
ax::Rect((__rect_in_pixels__).origin.x / AX_CONTENT_SCALE_FACTOR(), \
2022-07-15 19:17:01 +08:00
(__rect_in_pixels__).origin.y / AX_CONTENT_SCALE_FACTOR(), \
(__rect_in_pixels__).size.width / AX_CONTENT_SCALE_FACTOR(), \
(__rect_in_pixels__).size.height / AX_CONTENT_SCALE_FACTOR())
2022-07-15 19:17:01 +08:00
/** @def AX_RECT_POINTS_TO_PIXELS
Converts a rect in points to pixels
*/
2022-07-15 19:17:01 +08:00
#define AX_RECT_POINTS_TO_PIXELS(__rect_in_points_points__) \
2022-08-08 18:02:17 +08:00
ax::Rect((__rect_in_points_points__).origin.x* AX_CONTENT_SCALE_FACTOR(), \
2022-07-15 19:17:01 +08:00
(__rect_in_points_points__).origin.y* AX_CONTENT_SCALE_FACTOR(), \
(__rect_in_points_points__).size.width* AX_CONTENT_SCALE_FACTOR(), \
(__rect_in_points_points__).size.height* AX_CONTENT_SCALE_FACTOR())
2022-07-15 19:17:01 +08:00
/** @def AX_POINT_PIXELS_TO_POINTS
Converts a rect in pixels to points
*/
2022-07-15 19:17:01 +08:00
#define AX_POINT_PIXELS_TO_POINTS(__pixels__) \
2022-08-08 18:02:17 +08:00
ax::Vec2((__pixels__).x / AX_CONTENT_SCALE_FACTOR(), (__pixels__).y / AX_CONTENT_SCALE_FACTOR())
2022-07-15 19:17:01 +08:00
/** @def AX_POINT_POINTS_TO_PIXELS
Converts a rect in points to pixels
*/
2022-07-15 19:17:01 +08:00
#define AX_POINT_POINTS_TO_PIXELS(__points__) \
2022-08-08 18:02:17 +08:00
ax::Vec2((__points__).x* AX_CONTENT_SCALE_FACTOR(), (__points__).y* AX_CONTENT_SCALE_FACTOR())
2022-07-15 19:17:01 +08:00
/** @def AX_POINT_PIXELS_TO_POINTS
Converts a rect in pixels to points
*/
2022-07-15 19:17:01 +08:00
#define AX_SIZE_PIXELS_TO_POINTS(__size_in_pixels__) \
Vec2((__size_in_pixels__).width / AX_CONTENT_SCALE_FACTOR(), \
(__size_in_pixels__).height / AX_CONTENT_SCALE_FACTOR())
2022-07-15 19:17:01 +08:00
/** @def AX_POINT_POINTS_TO_PIXELS
Converts a rect in points to pixels
*/
2022-07-15 19:17:01 +08:00
#define AX_SIZE_POINTS_TO_PIXELS(__size_in_points__) \
Vec2((__size_in_points__).width* AX_CONTENT_SCALE_FACTOR(), (__size_in_points__).height* AX_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&)
/**
Helper macros which converts 4-byte little/big endian
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
2022-07-15 19:17:01 +08:00
#define AX_HOST_IS_BIG_ENDIAN (bool)(*(unsigned short*)"\0\xff" < 0x100)
#define AX_SWAP32(i) ((i & 0x000000ff) << 24 | (i & 0x0000ff00) << 8 | (i & 0x00ff0000) >> 8 | (i & 0xff000000) >> 24)
#define AX_SWAP16(i) ((i & 0x00ff) << 8 | (i & 0xff00) >> 8)
#define AX_SWAP_INT32_LITTLE_TO_HOST(i) ((AX_HOST_IS_BIG_ENDIAN == true) ? AX_SWAP32(i) : (i))
#define AX_SWAP_INT16_LITTLE_TO_HOST(i) ((AX_HOST_IS_BIG_ENDIAN == true) ? AX_SWAP16(i) : (i))
#define AX_SWAP_INT32_BIG_TO_HOST(i) ((AX_HOST_IS_BIG_ENDIAN == true) ? (i) : AX_SWAP32(i))
#define AX_SWAP_INT16_BIG_TO_HOST(i) ((AX_HOST_IS_BIG_ENDIAN == true) ? (i) : AX_SWAP16(i))
/**********************/
/** Profiling Macros **/
/**********************/
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_PROFILERS
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_DISPLAY_TIMERS() NS_AX::Profiler::getInstance()->displayTimers()
# define AX_PROFILER_PURGE_ALL() NS_AX::Profiler::getInstance()->releaseAllTimers()
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_START(__name__) NS_AX::ProfilingBeginTimingBlock(__name__)
# define AX_PROFILER_STOP(__name__) NS_AX::ProfilingEndTimingBlock(__name__)
# define AX_PROFILER_RESET(__name__) NS_AX::ProfilingResetTimingBlock(__name__)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_START_CATEGORY(__cat__, __name__) \
do \
{ \
if (__cat__) \
NS_AX::ProfilingBeginTimingBlock(__name__); \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_STOP_CATEGORY(__cat__, __name__) \
do \
{ \
if (__cat__) \
NS_AX::ProfilingEndTimingBlock(__name__); \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_RESET_CATEGORY(__cat__, __name__) \
do \
{ \
if (__cat__) \
NS_AX::ProfilingResetTimingBlock(__name__); \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_START_INSTANCE(__id__, __name__) \
do \
{ \
NS_AX::ProfilingBeginTimingBlock( \
NS_AX::String::createWithFormat("%08X - %s", __id__, __name__)->getCString()); \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_STOP_INSTANCE(__id__, __name__) \
do \
{ \
NS_AX::ProfilingEndTimingBlock( \
NS_AX::String::createWithFormat("%08X - %s", __id__, __name__)->getCString()); \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_RESET_INSTANCE(__id__, __name__) \
do \
{ \
NS_AX::ProfilingResetTimingBlock( \
NS_AX::String::createWithFormat("%08X - %s", __id__, __name__)->getCString()); \
} while (0)
#else
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_DISPLAY_TIMERS() \
do \
{ \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_PURGE_ALL() \
do \
{ \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_START(__name__) \
do \
{ \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_STOP(__name__) \
do \
{ \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_RESET(__name__) \
do \
{ \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_START_CATEGORY(__cat__, __name__) \
do \
{ \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_STOP_CATEGORY(__cat__, __name__) \
do \
{ \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_RESET_CATEGORY(__cat__, __name__) \
do \
{ \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_START_INSTANCE(__id__, __name__) \
do \
{ \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_STOP_INSTANCE(__id__, __name__) \
do \
{ \
} while (0)
2022-07-15 19:17:01 +08:00
# define AX_PROFILER_RESET_INSTANCE(__id__, __name__) \
do \
{ \
} while (0)
2015-06-16 08:43:37 +08:00
#endif
/*********************************/
/** 64bits Program Sense Macros **/
/*********************************/
#if defined(_M_X64) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) || defined(__x86_64) || \
defined(__arm64__) || defined(__aarch64__)
2022-07-15 19:17:01 +08:00
# define AX_64BITS 1
#else
2022-07-15 19:17:01 +08:00
# define AX_64BITS 0
#endif
/******************************************************************************************/
/** LittleEndian Sense Macro, from google protobuf see: **/
/** https://github.com/google/protobuf/blob/master/src/google/protobuf/io/coded_stream.h **/
/******************************************************************************************/
#ifdef _WIN32
// Assuming windows is always little-endian.
2022-07-15 19:17:01 +08:00
# if !defined(AX_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
# define AX_LITTLE_ENDIAN 1
# endif
# if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
// If MSVC has "/RTCc" set, it will complain about truncating casts at
// runtime. This file contains some intentional truncating casts.
# pragma runtime_checks("c", off)
# endif
#else
# ifdef __APPLE__
# include <machine/endian.h> // __BYTE_ORDER
# elif defined(__FreeBSD__)
# include <sys/endian.h> // __BYTE_ORDER
# else
# if !defined(__QNX__)
# include <endian.h> // __BYTE_ORDER
# endif
# endif
# if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
(defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \
2022-07-15 19:17:01 +08:00
!defined(AX_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
# define AX_LITTLE_ENDIAN 1
# endif
#endif
2022-07-15 19:17:01 +08:00
/** @def AX_INCREMENT_GL_DRAWS_BY_ONE
Increments the GL Draws counts by one.
The number of calls per frame are displayed on the screen when the Director's stats are enabled.
*/
2022-08-08 18:02:17 +08:00
#define AX_INCREMENT_GL_DRAWS(__n__) ax::Director::getInstance()->getRenderer()->addDrawnBatches(__n__)
2022-07-15 19:17:01 +08:00
#define AX_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(__drawcalls__, __vertices__) \
do \
{ \
2022-08-08 18:02:17 +08:00
auto __renderer__ = ax::Director::getInstance()->getRenderer(); \
__renderer__->addDrawnBatches(__drawcalls__); \
__renderer__->addDrawnVertices(__vertices__); \
} while (0)
/*******************/
/** Notifications **/
/*******************/
/** @def AnimationFrameDisplayedNotification
Notification name when a SpriteFrame is displayed
*/
#define AnimationFrameDisplayedNotification "CCAnimationFrameDisplayedNotification"
2015-07-17 11:08:27 +08:00
/*******************/
/** Notifications **/
/*******************/
/** @def Animate3DDisplayedNotification
Notification name when a frame in Animate3D is played
*/
#define Animate3DDisplayedNotification "CCAnimate3DDisplayedNotification"
// new callbacks based on C++11
2022-07-15 19:17:01 +08:00
#define AX_CALLBACK_0(__selector__, __target__, ...) std::bind(&__selector__, __target__, ##__VA_ARGS__)
#define AX_CALLBACK_1(__selector__, __target__, ...) \
std::bind(&__selector__, __target__, std::placeholders::_1, ##__VA_ARGS__)
2022-07-15 19:17:01 +08:00
#define AX_CALLBACK_2(__selector__, __target__, ...) \
std::bind(&__selector__, __target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
2022-07-15 19:17:01 +08:00
#define AX_CALLBACK_3(__selector__, __target__, ...) \
std::bind(&__selector__, __target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, \
##__VA_ARGS__)
#endif // __BASE_CCMACROS_H__