Create a cross platform game with Cocos2DX and C++


If you like to code in C++ and you are interested in building a cross platform game, there is a library out there you can use, it is called Cocos2DX.

The cool thing about it is that “theoretically” you write the code once and it should work on any platform… In practice it gets a little complicated, but with a bit of stubbornness anything is possible.

There is a big list of games already using it and I guess now one of the most famous is Hero Academy (which I am not sure why they have not released on Android), also Glu mobile seems to like using it.

You will not be able to find huge amounts of tutorials online, and the official website is confusing at best, although there is one that is pretty good, Paralaxer.

The tutorial is free, and I believe for a few dollars you can buy the entire source code of the game, worth checking out.

For now I will limit this guide to explaining how to create an executable for Windows, got that it shouldn’t be too “difficult” to also deploy on Mac, Linux, Android and iOS.

First things first

  1. Make sure to download the Cocos2dX library and uncompress it somewhere you will remember.
  2. Install Microsoft Visual C++ 2010 Express.

Setting the Folder

Create a folder to contain the project, in my case the folder name is TestCocos2Dx so that the class files and the resources can stay outside of your Windows project, in this folder also copy “cocos2dx” and “CocosDenshion” folder that you find in the Cocos2dx library you downloaded before.

Also Download this ZIP (Contains Classes and Resources that you can modify) and uncompress it in the same folder.

Setting the Project

Open Visual Studio C++ and create a new Project.

Add the existing projects of Cocos2DX and CocosDenshion to it by clikcing on File->Add->Existing Projects… and loading the vcxproj in the proj.win32 folder.

At this point you can delete everything from the MyGame_Windows project except for External Dependencies. Although add the classes contained in the “Classes” folder and add 2 new classes:

Main.cpp

Main.h

Solution Properties

Right click on the Solution->Properties and make sure that Project MyGame_Windows has all the dependencies checked out.

 Project Properties

In Project->Properties, under Common Properties -> Framework and References, add the references of libcocos2d and libCocosDenshion and set to both “Reference Assembly Output” to False.

Configuration Properties

General:

  • Output Directory = $(SolutionDir)$(Configuration).win32\
  • Intermediate Directory = $(Configuration).win32\

Debugging

  • Working Directory = $(ProjectDir)..\..\Resources

C/C++ General

  • Additional Include Directories
  • ..\..\cocos2dx\platform\third_party\win32\OGLES
  • ..\..\cocos2dx\platform\win32
  • ..\..\cocos2dx\kazmath\include
  • ..\..\cocos2dx\include
  • ..\..\cocos2dx
  • ..\..\Classes

C/C++ Precompiled Headers

  • Precompiled Header, make sure nothing is writtein inside.

Linker Input

  • Additinal Dependencies add opengl32.lib, glew32.lib and libcocos2d.lib.

 

At this point you should be able to run the test without a problem.

Next posts I will cover how to use the same code to compile to Android and iOS.


2 thoughts on “Create a cross platform game with Cocos2DX and C++

Leave a comment

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