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
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:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
CCEGLView* eglView = CCEGLView::sharedOpenGLView();
Right click on the Solution->Properties and make sure that Project MyGame_Windows has all the dependencies checked out.
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.
- Output Directory = $(SolutionDir)$(Configuration).win32\
- Intermediate Directory = $(Configuration).win32\
- Working Directory = $(ProjectDir)..\..\Resources
- Additional Include Directories
C/C++ Precompiled Headers
- Precompiled Header, make sure nothing is writtein inside.
- 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.