In this tutorial how to create a simple game using the Cocos2d-x framework in a Windows development environment and how to compile it to run on Windows 8 and Android.
What is Cocos2d-x?
One of the advantages of this framework is to create games that can be deployed on different platforms (Android*, iOS, Win32*, Windows* Phone, Windows* 8, Mac*, Linux*, etc.) keeping the same code base and making a few platform-specific adaptations for each one.
If you want to know more about Cocos2d-x and its documentation, check out: http://www.cocos2d-x.org/.
Creating your first game
1. Download the latest version of the framework from the site and unzip it in your development environment. In this tutorial, the version 2.2.2 was used, and the framework was unzipped to the Desktop (C:\Users\felipe.pedroso\Desktop\cocos2d-x-2.2).
2. To create a new project on cocos2d-x, we are going to use a Python* script (create_project.py) that creates the whole project structure inside the folder where the framework was unzipped. If you don’t have the Python runtime installed, download the 2.7.6 version from this link: http://www.python.org/download/.
3. Open the command prompt (cmd.exe) and execute the following commands:
- Go to the script folder (it’s important to run the script inside the ‘project-creator’ folder)
- Run the script with the following command:
python create_project.py -project MyFirstGame -package com.example.myfirstgame -language cpp
The parameters are as follows:
project: The name of your project/game
package: The package name of your app (e.g., com.myCompany.MyFirstGame)
Note: To run the python command from the command prompt, add the folder where Python was installed to the environment variable path.
The created project will contain the base code of the game (Classes), the resources (images, audio, etc.), and one project for each framework-supported platform.
Building as a Win32 App (Windows* 7 or Windows 8 desktop mode)
1. Open the MyFirstGame.sln file inside the proj.win32 folder from the project directory using Visual Studio.
2. Build the project by pressing F6 (or use the menu Build -> Build Solution) and run the project pressing F5 (or use the menu Debug->Start Debugging).
If nothing went wrong, you’ll see the following window:
Building as a Windows Store App
To build your project as a Windows Store App, open the MyFirstgame.sln file inside the proj.winrt folder and build it using the same procedure that was used on the Win32 project.
After building and running, you’ll see the following screen:
Note: the cocos2d-x used in this tutorial didn’t work with Windows* 8.1.
Building as a Android App
In the same way that Python was added to the Windows path, add the directories, tools, and platform-tools from the Android SDK, the root directory from NDK, and the bin directory from Apache Ant in order to use them to build your app.
1. Open a new command prompt (cmd.exe) and run the following commands to configure the environment variables that are necessary to compile the Android app:
The variables we used are:
COCOS2DX_ROOT: the directory where the framework was unzipped
NDK_TOOLCHAIN_VERSION: the version of the NDK toolchain that will be used to build the project
NDK_MODULE_PATH: the modules that need to be included on the NDK build. In this case, we are using the prebuilt modules from cocos2d-x
2. With the environment variables configured, go to the Android project folder:
3. Copy the game resources (images, sounds, etc.) to the assets folder:
rmdir /S /Q assets
xcopy /E ..\Resources .\assets
4. Run the following command to build the native modules:
ndk-build.cmd -C . APP_ABI="armeabi armeabi-v7a x86"
This command will generate the native libraries for three different architectures: ARM, ARM-NEON*, and x86. This will allow your game to run on these architectures taking the best from them.
5. After finishing the build process, build the Android app with the ant command:
Now, to install the app in a device or emulator use the command:
adb install -r bin\MyFirstGame.apk
After that, you just need to run your app:
OK, now your game can run on at least three platforms: Android, Windows 7, and Windows 8!