Working with C++ and Cocos2dx

Working with C++ and Cocos2dx

Recently I’ve started my foray into the C++ world proper. I have always toyed a bit with the language and its predecessor C but I never really got into it fully, preferring instead languages such as C# and Haskell. However, due to the nature of the industry I work in, and due to how C and C++ make you think more about memory-efficient implementations, I thought it was high time I delved deeper into it. So far, personally I prefer the simpler C and languages such as C# and Haskell but hopefully this will change as I gather more knowledge about the language itself.

Just reading C++ tutorials and books is a bit boring though and learning is often more ‘fun’ when it is applied. Especially when it is applied to games! Hence, I’ve decided once again to try re-writing Space Generals. (Yes, yes, I’ve said this before). This time I opted for mobile and using the Cocos2dx engine.

Setting up Cocos2dx was not that hard but finding good up-to-date information online is hard since the engine is going through constant iterations. I am hoping the following set of steps help anyone who is looking to setup their Windows environment to be able to develop for Windows and for the Android platforms. They are very terse on purpose but should work. Let me know if you need more information and I’ll try dig it out for you.

Disclaimer 1: Cocos2d and Cocos2dx are in constant development so bear in mind that the information I have below might have already become outdated. Let me know and if I have some time I’ll update it accordingly.

Disclaimer 2: I work with a combination of Visual Studio 2013 and Eclipse Juno. If your target is simply Android, Eclipse should be enough. I assume you have some basic knowledge of how these two IDEs work. Having some knowledge of Android development would also be an asset.

Creating a Cocos2d-x project for Windows

Download and install

  1. Download latest v2.0 python 2.7.9. (Not 3.0): https://www.python.org/downloads/release/python-279/
  2. Download latest Cocos2dx (at the time of writing this is Cocos2d-x-3.4.zip): http://www.cocos2d-x.org/
  3. Install Python to your drive, e.g.: C:\Program Files\Python\2.7.9
  4. Make sure you add Python to your PATH environment variable.
  5. Unzip Cocos2dx to your hard-drive. For the sake of these steps I will use: C:\SDKs

Creating the project

  1. In command-line:
    1. Goto to the Cocos2dx folder: C:\SDKs\Cocos2d-x-3.4\tools\cocos2d-console\bin
    2. Create the project via> cocos.py new AwesomeGame -p com.awesomestudio.awesomegame -l cpp -d C:\GameDevelopment
    3. Run the project for the first time via> cocos run -s “C:\GameDevelopment\AwesomeGame” -p win32

Creating a Cocos2d-x project for Android

The steps below assume the above section has been completed successfully.

Download and extract

  1. Download and install JRE
  2. Download the Android NDK: https://developer.android.com/tools/sdk/ndk/index.html
  3. Download the Android SDK: http://developer.android.com/sdk/installing/index.html
  4. Download Apache ANT: http://ant.apache.org/bindownload.cgi
  5. Unzip Android NDK to C:\SDKs
  6. Unzip Android SDK to C:\SDKs
  7. Unzip Apache AND to C:\SDKs

Setup for deployment

  1. Run setup.py found in C:\SDKs\Cocos2d-x-3.4
  2. Supply path for NDK_ROOT, e.g.: C:\SDKs\android-ndk-r10d
  3. Supply path for ANDROID_SDK_ROOT, e.g.: C:\SDKs\adt-bundle-windows-x86_64-20140702\sdk
  4. Supply path for ANT_ROOT, e.g.: C:\SDKs\apache-ant-1.9.4-bin\apache-ant-1.9.4\bin
  5. A restart of the machine might be necessary at this point.

Compiling and running the solution

  1. In command-line:
    1. Goto to your android project folder, e.g.: C:\GameDevelopment\AwesomeGame\proj.android
    2. Run build_native.py.
  2. In SDKManager, update to latest toolkit.
  3. Open eclipse:
    1. Use any workspace. I used: C:\EclipseWorkspace
    2. Import solution into workspace, no need to copy in. You need to import cocos2dxlib and your game project.
    3. Remove libcocos2dx as a dependency. This was one of my major headaches while trying to configure my setup.
    4. Remove the default project dependencies (again something I didn’t know I had to do).
    5. Another problem I encountered was with my .cproject file. If that happens try recreating the project via cocos.py and then build_native.py.

Running and debugging

In Eclipse, assuming you have an Android virtual device setup, hitting run should make your game work using the default Cocos2dx-generated application. Unfortunately I had some problems with this due to a known issue related to my system configuration. Sometimes you cannot debug your application due to your graphics card not supporting a particular version of OpenGL which android supports. I am not a 100% sure why as I only did a quick Google search. There is help out there though if you encounter this problem. I hope you have more luck than me with this. If so feel free to comment.

In the end I resorted to coding and debugging my game in Visual Studio and then building it in Eclipse to create the apk which I then manually move to my Android device.

Hope that helps anyone trying their hand with Cocos2dx!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.