Cocos2d-x 3.0 + CMake + QtCreator + MinGW build 自己的 project
前言
Cocos2d-x 3.0 正式版出來了. 這一版開始使用在產生 Project 有了一些改變 .- 使用 CMake
- 有自己的 cocos console
其他就不詳述了. 這篇的重點是交代如何讓 Cocos2d-x 在 Windows 底下用 MingGW build 自己的 Project.
對岸的已經有人成功在 Qt Createor + MinGW 環境下 build,
http://blog.csdn.net/akof1314/article/details/18189069
詳情不再重述. 我基本上也是參照同樣做法. 只是在過程中我發現還是有些東西要改.
修改 CCFileUtilsWin32.cpp
用過 Cocos2d 的大概都知道, cocos2d-x 用 FileUtils.fullPathForFilename() 去告訴你檔案的絕對位置. 通常是用這個去找 Recource 目錄下的檔案位置. 在 Win32 的實作卻把 Resource 目錄指定到目前執行的目錄. 所以這跟原定的 Resources 目錄不符. 所以我在 cocos2d-x-3.0\cocos\2d\platform\win32\CCFileUtilsWin32.cpp 大概第 69 行的位置:
加了一行變成:
char utf8Path[CC_MAX_PATH] = {0}; int nNum = WideCharToMultiByte(CP_UTF8, 0, utf16Path, -1, utf8Path, sizeof(utf8Path), NULL, NULL); s_resourcePath = convertPathFormatToUnixStyle(utf8Path); s_resourcePath.append("/"); }
加了一行變成:
char utf8Path[CC_MAX_PATH] = {0}; int nNum = WideCharToMultiByte(CP_UTF8, 0, utf16Path, -1, utf8Path, sizeof(utf8Path), NULL, NULL); s_resourcePath = convertPathFormatToUnixStyle(utf8Path); s_resourcePath.append("/Resources"); s_resourcePath.append("/"); }
這樣在 Windows 底下執行的時候就會去找 Resource 目錄. 這樣改不是很好, 但是目前先能動就可以了.
修改CMakeList.txt
Cocos2d-x 的 C++ Project Template 放在 cocos2d-x-3.0\templates\cpp-template-default 底下, 可是這個目錄底下的 CMakeList.txt 看起來只有維護 Linux 的部分, 真是浪費了 CMake. 所以我稍微做了一些修改讓 MinGW 也可以編譯.
CMakeFile.txt
cmake_minimum_required(VERSION 2.6) set(APP_NAME MyGame) project (${APP_NAME}) include(cocos2d/build/BuildHelpers.CMakeLists.txt) option(USE_CHIPMUNK "Use chipmunk for physics library" ON) option(USE_BOX2D "Use box2d for physics library" OFF) option(DEBUG_MODE "Debug or release?" ON) if(DEBUG_MODE) set(CMAKE_BUILD_TYPE DEBUG) else(DEBUG_MODE) set(CMAKE_BUILD_TYPE RELEASE) endif(DEBUG_MODE) if(NOT MSVC)# all gcc 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} -std=c99") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") endif() if(USE_CHIPMUNK) message("Using chipmunk ...") add_definitions(-DCC_ENABLE_CHIPMUNK_INTEGRATION=1) elseif(USE_BOX2D) message("Using box2d ...") add_definitions(-DCC_ENABLE_BOX2D_INTEGRATION=1) else(USE_CHIPMUNK) message(FATAL_ERROR "Must choose a physics library.") endif(USE_CHIPMUNK) # architecture if ( CMAKE_SIZEOF_VOID_P EQUAL 8 ) set(ARCH_DIR "64-bit") else() set(ARCH_DIR "32-bit") endif() if(WIN32) # Win32 ADD_DEFINITIONS (-D_USRDLL -DCOCOS2DXWIN32_EXPORTS -D_WINDOWS -DWIN32) if(MINGW) add_definitions( -D__SSIZE_T ) endif(MINGW) set(PLATFORM_FOLDER win32) elseif(APPLE)# osx or ios set(PLATFORM_FOLDER mac) else() # Assume Linux set(PLATFORM_FOLDER linux) endif() set(GAME_SRC proj.${PLATFORM_FOLDER}/main.cpp Classes/AppDelegate.cpp Classes/HelloWorldScene.cpp ) set(COCOS2D_ROOT ${CMAKE_SOURCE_DIR}/cocos2d) include_directories( ${COCOS2D_ROOT} ${COCOS2D_ROOT}/../Classes ${COCOS2D_ROOT}/cocos ${COCOS2D_ROOT}/cocos/audio/include ${COCOS2D_ROOT}/cocos/2d ${COCOS2D_ROOT}/cocos/2d/renderer ${COCOS2D_ROOT}/cocos/2d/platform ${COCOS2D_ROOT}/cocos/2d/platform/desktop ${COCOS2D_ROOT}/cocos/2d/platform/${PLATFORM_FOLDER} ${COCOS2D_ROOT}/cocos/base ${COCOS2D_ROOT}/cocos/deprecated ${COCOS2D_ROOT}/cocos/physics ${COCOS2D_ROOT}/cocos/editor-support ${COCOS2D_ROOT}/cocos/math/kazmath ${COCOS2D_ROOT}/extensions ${COCOS2D_ROOT}/external ${COCOS2D_ROOT}/external/edtaa3func ${COCOS2D_ROOT}/external/jpeg/include/${PLATFORM_FOLDER} ${COCOS2D_ROOT}/external/tiff/include/${PLATFORM_FOLDER} ${COCOS2D_ROOT}/external/webp/include/${PLATFORM_FOLDER} ${COCOS2D_ROOT}/external/tinyxml2 ${COCOS2D_ROOT}/external/unzip ${COCOS2D_ROOT}/external/chipmunk/include/chipmunk ${COCOS2D_ROOT}/external/freetype2/include/${PLATFORM_FOLDER} ${COCOS2D_ROOT}/external/websockets/include/${PLATFORM_FOLDER} ${COCOS2D_ROOT}/external/spidermonkey/include/${PLATFORM_FOLDER} ${COCOS2D_ROOT}/external/xxhash ) link_directories( /usr/local/lib ${COCOS2D_ROOT}/external/jpeg/prebuilt/${PLATFORM_FOLDER}/${ARCH_DIR} ${COCOS2D_ROOT}/external/tiff/prebuilt/${PLATFORM_FOLDER}/${ARCH_DIR} ${COCOS2D_ROOT}/external/webp/prebuilt/${PLATFORM_FOLDER}/${ARCH_DIR} ${COCOS2D_ROOT}/external/freetype2/prebuilt/${PLATFORM_FOLDER}/${ARCH_DIR} ${COCOS2D_ROOT}/external/websockets/prebuilt/${PLATFORM_FOLDER}/${ARCH_DIR} ${COCOS2D_ROOT}/external/spidermonkey/prebuilt/${PLATFORM_FOLDER}/${ARCH_DIR} ) #include dependency if(WIN32) if(MINGW) add_definitions(-DGLEW_STATIC) endif() include_directories( ${COCOS2D_ROOT}/external/${PLATFORM_FOLDER}-specific/gles/include/OGLES ${COCOS2D_ROOT}/external/${PLATFORM_FOLDER}-specific/icon/include ${COCOS2D_ROOT}/external/${PLATFORM_FOLDER}-specific/zlib/include external/curl/include/${PLATFORM_FOLDER} ) elseif(APPLE) else() include_directories( /usr/local/include/GLFW ${COCOS2D_ROOT}/external/linux-specific/fmod/include/${ARCH_DIR} ) link_directories( /usr/local/lib ${COCOS2D_ROOT}/external/linux-specific/fmod/prebuilt/${ARCH_DIR} ) endif(WIN32) # kazmath add_subdirectory(${COCOS2D_ROOT}/cocos/math/kazmath) # chipmunk library add_subdirectory(${COCOS2D_ROOT}/external/chipmunk/src) # box2d library add_subdirectory(${COCOS2D_ROOT}/external/Box2D) # unzip library add_subdirectory(${COCOS2D_ROOT}/external/unzip) # tinyxml2 library add_subdirectory(${COCOS2D_ROOT}/external/tinyxml2) # audio add_subdirectory(${COCOS2D_ROOT}/cocos/audio) # xxhash library add_subdirectory(${COCOS2D_ROOT}/external/xxhash) # cocos base library add_subdirectory(${COCOS2D_ROOT}/cocos/base) # cocos 2d library add_subdirectory(${COCOS2D_ROOT}/cocos/2d) # cocos storage add_subdirectory(${COCOS2D_ROOT}/cocos/storage) # ui add_subdirectory(${COCOS2D_ROOT}/cocos/ui) # network add_subdirectory(${COCOS2D_ROOT}/cocos/network) # extensions add_subdirectory(${COCOS2D_ROOT}/extensions) ## Editor Support # spine add_subdirectory(${COCOS2D_ROOT}/cocos/editor-support/spine) # cocosbuilder add_subdirectory(${COCOS2D_ROOT}/cocos/editor-support/cocosbuilder) # cocostudio add_subdirectory(${COCOS2D_ROOT}/cocos/editor-support/cocostudio) # add the executable add_executable(${APP_NAME} ${GAME_SRC} ) if ( CMAKE_SIZEOF_VOID_P EQUAL 8 ) set(FMOD_LIB "fmodex64") else() set(FMOD_LIB "fmodex") endif() target_link_libraries(${APP_NAME} ui network storage spine cocostudio cocosbuilder extensions audio cocos2d ) set(APP_BIN_DIR "${CMAKE_BINARY_DIR}/bin") set(APP_DIR "${CMAKE_BINARY_DIR}") set_target_properties(${APP_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${APP_BIN_DIR}") pre_build(${APP_NAME} COMMAND ${CMAKE_COMMAND} -E remove_directory ${APP_BIN_DIR}/Resources COMMAND ${CMAKE_COMMAND} -E remove_directory ${APP_DIR}/Resources COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Resources ${APP_BIN_DIR}/Resources COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Resources ${APP_DIR}/Resources )後面在 pre_build 的部分我把同一份 Resources copy 到 ${CMAKE_BINARY_DIR}/bin 以及 ${CMAKE_BINARY_DIR} 底下, 是因為在 QtCreator 他的執行目錄是 CMake 建置的目錄而不是 bin 底下, 所以直接跑會因為找不到 Resources 裡面的設定就當機了. 也許有人 CMake 比較強可以用更好的方法改一下, 但是目前以能執行為優先. 這樣就可以用 cocos 的 script 產生 project 檔了.
Cocos console script
詳情請看 cocos2d-x-3.0\README.md 的步驟, 要跑 cocos script 一定要裝 Python 2.x 並把 python 2.x 設定到執行路徑 (應該安裝程式會做)步驟
- 打開命令提示字元
- 用 set PATH 把 cocos 執行路徑設好. 例如: set PATH=%PATH%;C:\cocos2d-x-3.0\tools\cocos2d-console\bin
- 執行 cocos. 例如: cocos new CocosTest -p com.your_company.CocosTest -l cpp -d C:\
這樣 cocos script 就會在 C:\ 產生 CocosTest 目錄. 因為只是單純的 Copy, 所以 CMakeList.txt 的 Project Name 還是叫 MyGame, 這時可以去修改 CocosTest 目錄底下的 CMakeList.txt
set(APP_NAME MyGame) => set(APP_NAME CocosTest)
Qt Creator
按照對岸網友的步驟打開 Qt Creator 並按照上面步驟修改 "\cocos2d-x-3.0\cocos\2d\CMakeLists.txt"把 glew32改成glew32.a
並把所有用到的 dll copy 到 CMake Project 目錄下的 bin 目錄. 以下是我測試 cpptest 的檔案.
glfw3.dll
libbz2-1.dll
libcurl-4.dll
libfreetype-6.dll
libiconv-2.dll
libidn-11.dll
libintl-8.dll
libjpeg-8.dll
libkazmath.dll
liblzma-5.dll
libpng16-16.dll
librtmp-1.dll
librtmp.dll
libssh2-1.dll
libtiff-5.dll
libwebp-5.dll
libwebpdecoder-1.dll
libwebpdemux-1.dll
libwebpmux-1.dll
zlib1.dll
大概就是這樣, 如果有遺漏的我再補上.
留言
張貼留言