最近项目中用到了cocos2dx来开发一个基于陀螺仪的demo,忙了半个星期,一边学习一边开发,总算搞了出来。这里备份下开发中学习到的一些cocos2dx知识。
CONTENTS
判断当前平台:
实例代码:
bool AppDelegate::applicationDidFinishLaunching() { CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); pDirector->setOpenGLView(pEGLView); //如果是windows 就缩放0.7。 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) pEGLView->setFrameZoomFactor(0.8f); #endif pDirector->setDisplayStats(true); pDirector->setAnimationInterval(1.0 / 60); CCScene *pScene = HelloWorld::scene(); pDirector->runWithScene(pScene); return true; }
源码参考:
CC_TARGET_PLATFORM 宏的定义在【 cocos2d-x-2.2.1\cocos2dx\platform\CCPlatformConfig.h 】文件中。
#ifndef __CC_PLATFORM_CONFIG_H__ #define __CC_PLATFORM_CONFIG_H__ /** Config of cocos2d-x project, per target platform. */ ////////////////////////////////////////////////////////////////////////// // pre configure ////////////////////////////////////////////////////////////////////////// // define supported target platform macro which CC uses. #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_WINRT 12 #define CC_PLATFORM_WP8 13 // Determine target platform by compile environment macro. #define CC_TARGET_PLATFORM CC_PLATFORM_UNKNOWN // mac #if defined(CC_TARGET_OS_MAC) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_MAC #endif // iphone #if defined(CC_TARGET_OS_IPHONE) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_IOS #endif // android #if defined(ANDROID) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_ANDROID #endif // WinRT (Windows Store App) #if defined(WINRT) && defined(_WINRT) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_WINRT #endif // WP8 (Windows Phone 8 App) #if defined(WP8) && defined(_WP8) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_WP8 #endif // win32 #if defined(WIN32) && defined(_WINDOWS) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_WIN32 #endif // linux #if defined(LINUX) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_LINUX #endif // marmalade #if defined(MARMALADE) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_MARMALADE #endif // bada #if defined(SHP) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_BADA #endif // qnx #if defined(__QNX__) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_BLACKBERRY #endif // native client #if defined(__native_client__) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_NACL #endif // Emscripten #if defined(EMSCRIPTEN) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_EMSCRIPTEN #endif // tizen #if defined(TIZEN) #undef CC_TARGET_PLATFORM #define CC_TARGET_PLATFORM CC_PLATFORM_TIZEN #endif ////////////////////////////////////////////////////////////////////////// // post configure ////////////////////////////////////////////////////////////////////////// // check user set platform #if ! CC_TARGET_PLATFORM #error "Cannot recognize the target platform; are you targeting an unsupported platform?" #endif #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) #pragma warning (disable:4127) #endif // CC_PLATFORM_WIN32 #endif // __CC_PLATFORM_CONFIG_H__
发表评论