Cocos2d-x 开发笔记

1. 判断操作系统平台类型

根据Cocos2d-x源码可以获得所支持的平台类型

C++ cocos2d\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.
//编译环境下检测目标平台的宏值
//0 unknown
#define CC_TARGET_PLATFORM CC_PLATFORM_UNKNOWN
//1 iphone
#if defined(CC_TARGET_OS_IPHONE)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_IOS
#endif
//2 android
#if defined(ANDROID)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_ANDROID
#endif
//3 win32
#if defined(WIN32) && defined(_WINDOWS)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_WIN32
#endif
//4 maralade
#if defined(MARMALADE)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_MARMALADE
#endif
//5 linux
#if defined(LINUX)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_LINUX
#endif
//6 bada
#if defined(SHP)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_BADA
#endif
//7 blackberry
#if defined(__QNX__)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_BLACKBERRY
#endif
//8 mac
#if defined(CC_TARGET_OS_MAC)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_MAC
#endif
//9 native client
#if defined(__native_client__)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_NACL
#endif
//10 emscripten
#if defined(EMSCRIPTEN)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_EMSCRIPTEN
#endif
//11 tizen
#if defined(TIZEN)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_TIZEN
#endif
//12 WinRT(Windows Store App)
#if defined(WINRT) && defined(_WINRT)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_WINRT
#endif
//13 WP8(Windows Phone 8 App)
#if defined(WP8) && defined(_WP8)
#undef CC_TARGET_PLATFORM
#define CC_TARGET_PLATFORM CC_PLATFORM_WP8
#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

#endif
//__CC_PLATFORM_CONFIG_H__

Lua

Cocos2dx中预定义了所支持的操作系统平台

-- 目标平台操作系统
cc.PLATFORM_OS_WINDOWS = 0
cc.PLATFORM_OS_LINUX = 1
cc.PLATFORM_OS_MAC = 2
cc.PLATFORM_OS_ANDROID = 3
cc.PLATFORM_OS_IPHONE = 4
cc.PLATFORM_OS_IPAD = 5
cc.PLATFORM_OS_BLACKBERRY = 6
cc.PLATFORM_OS_NACL = 7
cc.PLATFORM_OS_EMSCRIPTEN = 8
cc.PLATFORM_OS_TIZEN = 9
cc.PLATFORM_WINRT = 10
cc.PLATFORM_OS_WP8 = 11

可使用getTargetPlatform获取目前操作系统平台的类型,其返回值为常量数值。

local targetPlatform = cc.Application:getInstance():getTargetPlatform()

为便于开发测试,并在做定义并区分,具体根据实际情况而定。

-- 平台类型定义
cc.PLATFORM_OS_APPLE_REAL = 12  -- 苹果真机:ipad或iphone
cc.PLATFORM_OS_DEVELOPER = 13 -- 开发者模式:windows或mac
cc.exports.TARGET_PLATFORM = application:getTargetPlatform()
if  TARGET_PLATFORM==cc.PLATFORM_OS_IPHONE or TARGET_PLATFORM==cc.PLATFORM_OS_IPAD then
   TARGET_PLATFORM = cc.PLATFORM_OS_APPLE_REAL
elseif TARGET_PLATFORM==cc.PLATFORM_OS_WINDOWS or TARGET_PLATFORM==cc.PLATFORM_OS_MAC then
   TARGET_PLATFORM = cc.PLATFORM_OS_DEVELOPER
end

你可能感兴趣的:(Cocos2d-x 开发笔记)