Sunday, April 6, 2014

Installing cocos2dx 3.0 on Windows



Cocos2d-x is a multi-platform framework for building 2d games, interactive books, demos and other graphical applications. It is based on cocos2d-iphone, but instead of using Objective-C, it uses C++. It works on iOS, Android, Windows Phone, OS X, Windows and Linux.
This guide is based on Cocos2dx-3.0. I am using Windows 7 and plan to release it for android devices first, however i had several issues trying to make it work so i will post the whole process to make it easy for you.

To get started you will need to download a lot of things:
Cocos2d-x: Get the 3.0 version in this case.
Android SDK: There is a full packed release that includes eclipse and the ADT already, making your life easier. You can find it on android developer’s page.
Android NDK: Cocos2d-x uses native C++ code to get into the mobile application, so you need the android native development kit as well. There are some enhanced versions by Crystax. You can use the latest.
Python: You will need this to run a path setting script that comes along with Cocos2d-x. You can find all the stable releases for Windows on the download page.
Apache Ant: Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. You should get the binary release.
Visual Studio Express 2012: The IDE and the tools for compiling for Windows. Developing it on a desktop OS makes the process faster than compiling and running on the device every time. Express version is free and can be downloaded directly from microsoft’s page.
And thats it!!!

Ok, now that you are done with the downloads lets get to the installing. You can install Python as you would install any other program however the rest of the downloads are compressed packages. Uncompress every package on a folder you can find easily.

Look on the root folder of Cocos2d-x for a python script named setup.py. This script will open a console and will ask you for the paths to the other pieces. It will look like:
-> Adding COCOS2D_CONSOLE_ROOT environment variable… ALREADY ADDED
-> Looking for NDK_ROOT envrironment variable… FOUND
-> Looking for ANDROID_SDK_ROOT envrironment variable… FOUND
-> Looking for ANT_ROOT envrironment variable… FOUND
For NDK_ROOT and ANDROID_SDK_ROOT just add the path where you uncompressed them. For ANT_ROOT add the path to the bin folder inside the root directory of ant.

Once this is done you can already compile the tests of the cocos2d-x framework to your android smartphone. Let’s create a blank project and compile it.
Inside the cocos2d-x directory look for tools/cocos2d-console/bin and then run install.py. This console makes creating projects and compiling for android quite easy. 
To create a game type on the comman prompt: > cocos new “My Game” -l cpp -p org.cocos2d.mygame and press enter. Once this is done go inside the project that was just created and type:

 >cocos compile -p android -m debug

If your command prompt says it cant figure what cocos is, add it to your path (cocos-console/bin) and restart it.
This process will take a while but once it is done you should have a .apk file inside the android folder of the project. Move it to your phone and install it so you can see how the hello world for cocos2d-x looks like!!!
 
Now we get to the part that took me a while: setting up the windows development enviroment.
First install microsoft visual studio express 2012. This may take a while.
Inside the game we created are several folders. Go to the one named proj.win32 and run Game1.sln. This will open visual studio:




You can run the solution and it will start compiling (may take long the first time). To do that click on the arrow “Local Windows Debugger”.
There are some issues with the current setting of the visual studio project so we will fix em now. Go to the project’s properties:



Find the Linker’s section and add the following at Ignore Specific Default Libraries: libcmt.lib; libcmtd.lib; msvcrt.lib.
Next step is important. Cocos2dx has several libs that you can add depending on what you are planning to do. In this example we are going to add Box2D but it is the same for anything else inside the cocos package.

Add libBox2D.lib to additional dependencies



To compile Box2D go to your game's directory and find cocos2d/external/Box2D/proj.win32 and run Box2D.vcxproj. This will open the visual studio project containing Box2D code. Compile it (make sure you are using debug settings).
Back at our game’s properties go to the Linker’s general configuration page and add to Additional Library Directories:
cocos2d-x/build/Debug.win32

With this you should have everything you need to make your project work including physics. 

You can run your apps directly on a device using the cocos-console. Make sure your device is in developer mode. You can find more info about setting it up here.

To run something directly on the device type on the command prompt:
> cocos run -p android

With the basic example it wont have any errors, but if you added extra libs you may need to set it up a little to make it work. Go to your game/proj.android/jni and open Android.mk. That file helps find the dependencies and libraries required. Whenever you add new source files or libraries you need to update this file too.

To add more source files just follow the pattern:

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp \
                   ../../Classes/MyNewSource.cpp

To add new include directories:

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../cocos2d

Notice the second one has a "+="

To add new libraries and load them use:

LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static

$(call import-module,Box2D)
$(call import-module,editor-support/cocostudio)

This one is a little tricky but you can solve all the compiling/linking issues using it.

If you find any mistakes, parts you don't understand or something you would like to be explained better feel free to leave a comment :).

No comments:

Post a Comment