clion 已经发布一段时间了,作为jetbrains的狂热fan。试用了下clion来开发c++项目。感觉很不错,因为自己一直都有想做前端开发的想法。业余写点cocos2dx的东东。所以就选择搭建一个
cocos2dx的项目,分享经验。
首先搭建好cocos环境,使用命令cocos new mygame6 -p com.cl.game -l cpp -d ./d
创建好项目后。修改
PLATFORM_SPECIFIC_SRC
这里对应的平台介入对应的文件,我这里是mac os平台
proj.ios_mac/mac/main.cpp
注意默认是ios哈。其他linux win自己看看。
然后就可以编译了,clion集成cmake。 呵呵 挺不错。以下是我项目的CMakeLists.txt:
#/**************************************************************************** #Copyright(c)2013-2014cocos2d-x.org # #http://www.cocos2d-x.org # #Permissionisherebygranted,freeofcharge,toanypersonobtainingacopy #ofthissoftwareandassociateddocumentationfiles(the"Software"),todeal #intheSoftwarewithoutrestriction,includingwithoutlimitationtherights #touse,copy,modify,merge,publish,distribute,sublicense,and/orsell #copiesoftheSoftware,andtopermitpersonstowhomtheSoftwareis #furnishedtodoso,subjecttothefollowingconditions: #Theabovecopyrightnoticeandthispermissionnoticeshallbeincludedin #allcopiesorsubstantialportionsoftheSoftware. #THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND,EXPRESSOR #IMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOFMERCHANTABILITY,#FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.INNOEVENTSHALLTHE #AUTHORSORCOPYRIGHTHOLDERSBELIABLEFORANYCLAIM,DAMAGESOROTHER #LIABILITY,WHETHERINANACTIONOFCONTRACT,TORTOROTHERWISE,ARISINGFROM,#OUTOFORINCONNECTIONWITHTHESOFTWAREORTHEUSEOROTHERDEALINGSIN #THESOFTWARE. #****************************************************************************/ cmake_policy(SETCMP0017NEW) cmake_minimum_required(VERSION2.8) set(APP_NAMEmygame6) project(${APP_NAME}) set(COCOS2D_ROOT${CMAKE_SOURCE_DIR}/cocos2d) set(CMAKE_MODULE_PATH${CMAKE_MODULE_PATH}"${COCOS2D_ROOT}/cmake/Modules/") include(CocosBuildHelpers) #libcocos2d set(BUILD_CPP_TESTSOFFCACHEBOOL"turnoffbuildcpp-tests") set(BUILD_LUA_LIBSOFFCACHEBOOL"turnoffbuildlua-tests") add_subdirectory(${COCOS2D_ROOT}) #Somemacrodefinitions if(WINDOWS) if(BUILD_SHARED_LIBS) ADD_DEFINITIONS(-D_USRDLL-D_EXPORT_DLL_-D_USEGUIDLL-D_USREXDLL-D_USRSTUdioDLL) else() ADD_DEFINITIONS(-DCC_STATIC) endif() ADD_DEFINITIONS(-DCOCOS2DXWIN32_EXPORTS-D_WINDOWS-DWIN32-D_WIN32) set(PLATFORM_FOLDERwin32) elseif(MACOSXORAPPLE) ADD_DEFINITIONS(-DCC_TARGET_OS_MAC) ADD_DEFINITIONS(-DUSE_FILE32API) set(PLATFORM_FOLDERmac) elseif(LINUX) ADD_DEFINITIONS(-DLINUX) set(PLATFORM_FOLDERlinux) elseif(ANDROID) ADD_DEFINITIONS(-DUSE_FILE32API) set(PLATFORM_FOLDERandroid) else() message(FATAL_ERROR"Unsupportedplatform,CMakewillexit") endif() #Compileroptions if(MSVC) ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS-D_SCL_SECURE_NO_WARNINGS -wd4251-wd4244-wd4334-wd4005-wd4820-wd4710 -wd4514-wd4056-wd4996-wd4099) else() set(CMAKE_C_FLAGS_DEBUG"-g-Wall-DCOCOS2D_DEBUG=1") set(CMAKE_CXX_FLAGS_DEBUG${CMAKE_C_FLAGS_DEBUG}) set(CMAKE_C_FLAGS"${CMAKE_C_FLAGS}-fno-exceptions-std=c99") set(CMAKE_CXX_FLAGS"${CMAKE_CXX_FLAGS}-fno-exceptions-std=c++11-Wno-deprecated-declarations-Wno-reorder") if(CLANG) set(CMAKE_CXX_FLAGS"${CMAKE_CXX_FLAGS}-stdlib=libc++") endif() endif(MSVC) set(PLATFORM_SPECIFIC_SRC) set(PLATFORM_SPECIFIC_HEADERS) if(MACOSXORAPPLE) set(PLATFORM_SPECIFIC_SRC proj.ios_mac/mac/main.cpp ) elseif(LINUX)#assumelinux set(PLATFORM_SPECIFIC_SRC proj.linux/main.cpp ) elseif(WIN32) set(PLATFORM_SPECIFIC_SRC proj.win32/main.cpp ) set(PLATFORM_SPECIFIC_HEADERS proj.win32/main.h proj.win32/resource.h ) endif() #cmake使用 #http://blog.atime.me/note/cmake.html #项目依赖头文件引入,注意编译iosmac将对应的依赖文件在xcode中处理 include_directories( ${COCOS2D_ROOT}/external ${COCOS2D_ROOT}/external/tinyxml2 ${COCOS2D_ROOT}/external/glfw3/include/mac ${COCOS2D_ROOT}/cocos ${COCOS2D_ROOT}/cocos/editor-support lib/protobuf/include src ) #查找指定路径下的源文件并保存在指定的变量名中 aux_source_directory(srcapp_src) aux_source_directory(src/scenescence_src) aux_source_directory(src/protoproto_src) aux_source_directory(src/managermanager_src) aux_source_directory(src/gameObjectgameobj_src) aux_source_directory(src/controllercontroller_src) aux_source_directory(src/commonscommons_src) aux_source_directory(src/commons/netnet_src) aux_source_directory(src/commons/soundsound_src) #使用net_src中的源文件生成静态库net #add_library(netSTATIC${net_src}) add_executable( ${APP_NAME} ${PLATFORM_SPECIFIC_SRC} ${app_src} ${scence_src} ${proto_src} ${manager_src} ${gameobj_src} ${controller_src} ${commons_src} ${net_src} ${sound_src} ) #外联静态库 set(proto_lib${CMAKE_SOURCE_DIR}/lib/protobuf) target_link_libraries(${APP_NAME}cocos2d${proto_lib}/libprotobuf.a) set(APP_BIN_DIR"${CMAKE_SOURCE_DIR}/bin") set_target_properties(${APP_NAME}PROPERTIES RUNTIME_OUTPUT_DIRECTORY"${APP_BIN_DIR}") if(WIN32) #alsocopyingdllstobinarydirectoryfortheexecutabletorun pre_build(${APP_NAME} COMMAND${CMAKE_COMMAND}-Eremove_directory${APP_BIN_DIR}/Resources COMMAND${CMAKE_COMMAND}-Ecopy_directory${CMAKE_CURRENT_SOURCE_DIR}/Resources${APP_BIN_DIR}/Resources COMMAND${CMAKE_COMMAND}-Ecopy${COCOS2D_ROOT}/external/win32-specific/gles/prebuilt/glew32.dll${APP_BIN_DIR}/${CMAKE_BUILD_TYPE} COMMAND${CMAKE_COMMAND}-Ecopy${COCOS2D_ROOT}/external/win32-specific/zlib/prebuilt/zlib1.dll${APP_BIN_DIR}/${CMAKE_BUILD_TYPE} ) else() pre_build(${APP_NAME} COMMAND${CMAKE_COMMAND}-Eremove_directory${APP_BIN_DIR}/Resources COMMAND${CMAKE_COMMAND}-Ecopy_directory${CMAKE_CURRENT_SOURCE_DIR}/Resources${APP_BIN_DIR} ) endif()
直接编译即可。clion和idea一样,可以屏蔽不显示的文件夹。在eidtor->file type下面设置。我个人感觉cocos2dx项目的文件显示杂七杂八很乱。设置后的效果:
是不是感觉很清爽了?!
接下来开始编译ios 和mac平台的项目。 因为cmake构建的项目,导致一些依赖库和头文件 以及资源文件无法被链接编译。网上找了很多资源,cocos2dx官方也没有提供脚本
一键生成,首先打开xcode,通过xcode将对应的资源文件,头文件,依赖库添加进去。成功编译ios mac。android的话直接注意编译android.mk就可以。
编译出来如上图所示。
clion的集成了终端可以很方便的进行调用。打开即用。
如果用惯了idea或者appcode,那么clion的操作基本就很熟悉了。
附加mygame6的地址:http://git.oschina.net/502959937/mygame6
该项目初步完成了基于cocos2dx的网络模块,依赖googprotobuf,和对应的数据分发设计,实现了一个简单的微信打飞机的(未完成),cocos 2dx3.6+cocosstudio2.5.
运行效果:
后续添加一些游戏中常用的工具类。如解析xml--到vo,缓存接口等。欢提交pr
最后或许你觉得xcode可以满足你目前需求,但是作为一个习惯了idea的dev-er.clion绝对是开发c++首选。个人感受哈。