In this chapter, we're going to install Cocos2d-x and set up the development environment. The following topics will be covered in this chapter:
Setting up our Android environment
Installing Cocos2d-x
Using the Cocos command
Building the project using Xcode
Building the project using Eclipse
Implementing multi-resolution support
Preparing your original game
Cocos2d-x is an open source, cross-platform game engine, which is free and mature. It can publish games for mobile devices and desktops, including iPhone, iPad, Android, Kindle, Windows, and Mac. Cocos2d-x is written in C++, so it can build on any platform. Cocos2d-x is open source written in C++, so we can feel free to read the game framework. Cocos2d-x is not a black box, and this proves to be a big advantage for us when we use it. Cocos2d-x version 3, which supports C++11, was only recently released. It also supports 3D and has an improved rendering performance. This book focuses on using version 3.4, which is the latest version of Cocos2d-x that was available at the time of writing this book. This book also focuses on iOS and Android development, and we'll be using Mac because we need it to develop iOS applications. This chapter explains how to set up Cocos2d-x.
We begin by setting up our Android environment. If you wish to build only on iOS, you can skip this step. To follow this recipe, you will need some files.
The following list provides the prerequisites that need to be downloaded to set up Android:
Eclipse ADT (Android Developer Tools) with the Android SDK:
https://dl.google.com/android/adt/adt-bundle-mac-x86_64-20140702.zip
Eclipse ADT includes the Android SDK and Eclipse IDE. This is the Android development tool that is used to develop Android applications. Android Studio is an Android development IDE, but it is not supported to build NDK. The official site states that a version of Android Studio that supports NDK will be released soon. That's why we use Eclipse in this book.
Android NDK (Native Development Kit):
https://dl.google.com/android/ndk/android-ndk-r10c-darwin-x86_64.bin
The NDK is required to build an Android application. You have to use NDK r10c. This is because compiling and linking errors may occur when using NDK r9 or an earlier version.
Apache ANT:
You can download Apache ANT from http://ant.apache.org/bindownload.cgi
This is a java library that aids in building software. At the time of writing this book, version 1.9.4 was the latest stable version available.
You begin by installing Eclipse ADT with the Android SDK, and then continue to unzip the zip file to any working directory you are aware of. I recommend that you unzip it to the
Documents
folder (~/adt-bundle-mac-x86_64-20140702
). ADT includes Android SDK and Eclipse. The SDK and Eclipse folders are located under the ADT folder. We call the SDK folder path that is located under the ADT folderANDROID_SDK_ROOT
. You have to remember it because you will use it the next recipe. Now, you can launch Eclipse from~/adt-bundle-mac-x86_64-20140702/eclipse/Eclipse.app
.The next step is to update Android SDK:
Open Eclipse from the
eclipse
folder located in ADT.Go to Window | Android SDK Manager.
After opening Android SDK Manager, check Tools and the latest Android SDK (
API21
),Android 2.3.3(API10)
, and any other SDK if necessary, as shown in the following screenshot:Select each license and click on Accept, as shown in the following screenshot:
After you accept all licenses, you will see that the Install button is enabled. Click on it.
You have to wait for a long time to update and install the SDKs.
Installing NDK:
Open the terminal window and change the directory to the path from which you downloaded the package. Change the permission on the downloaded package and execute the package. For example:
$ chmod 700 android-ndk-r10c-darwin-x86_64.bin $ ./android-ndk-r10c-darwin-x86_64.bin
Finally, you move the
NDK
folder to theDocuments
folder. We call the installation path for NDKNDK_ROOT
.NDK_ROOT
is the address of the folder that contains the files, it helps the Cocos2dx engine to locate the native files of Android. You have to rememberNDK_ROOT
because you will use it in the next recipe.Installing Apache ANT:
Unzip the file to the
Documents
folder. That's all. We callANT_ROOT
the installation path for ANT. You have to rememberANT_ROOT
, as we'll be using it in the next recipe.Installing Java:
By entering the following command in the terminal, you can automatically install Java (if you haven't installed it earlier):
$ java --version
After installing it, you can check that it was successfully installed by entering the command again.
Let's take a look at what we did throughout the recipe:
Installing Eclipse: You can use Eclipse as an editor for Cocos2d-x
Installing ADT: You can develop Android applications on Eclipse
Installing NDK: You can build a C++ source code for Java
Installing ANT: You can use command line tools for Cocos2d-x
Now you've finished setting up the Android development environment. At this point, you know how to install them and their path. In the next recipe, you will use them to build and execute Android applications. This will be very useful when you want to debug Android applications.
To follow this recipe, you need to download the zip file from the official site of Cocos2d-x (http://www.cocos2d-x.org/download).
At the time of writing this book, version 3.4 was the latest stable version that was available. This version will be used throughout this book.
Unzip your file to any folder. This time, we will install the user's home directory. For example, if the user name is
syuhari
, then the install path is/Users/syuhari/cocos2d-x-3.4
. In this book, we call itCOCOS_ROOT
.The following steps will guide you through the process of setting up Cocos2d-x:
Open the terminal
Change the directory in terminal to
COCOS_ROOT
, using the following command:$ cd ~/cocos2d-x-v3.4
Run
setup.py
, using the following command:$ ./setup.py
The terminal will ask you for
NDK_ROOT
. Enter intoNDK_ROOT
path.The terminal will then ask you for
ANDROID_SDK_ROOT
. Enter theANDROID_SDK_ROOT
path.Finally, the terminal will ask you for
ANT_ROOT
. Enter theANT_ROOT
path.After the execution of the
setup.py
command, you need to execute the following command to add the system variables:$ source ~/.bash_profile
We now verify whether Cocos2d-x can be installed:
Let's take a look at what we did throughout the above recipe. You can install Cocos2d-x by just unzipping it. You know setup.py
is only setting up the cocos
command and the path for Android build in the environment. Installing Cocos2d-x is very easy and simple. If you want to install a different version of Cocos2d-x, you can do that too. To do so, you need to follow the same steps that are given in this recipe, but they will be for a different version.
Setting up the Android environment is a bit tough. If you recently started to develop Cocos2d-x, you can skip the settings part of Android. and you can do it when you run on Android. In this case, you don't have to install Android SDK, NDK, and Apache ANT. Also, when you run setup.py
, you only press Enter without entering a path for each question.
The next step is using the cocos
command. It is a cross-platform tool with which you can create a new project, build it, run it, and deploy it. The cocos
command works for all Cocos2d-x supported platforms and you don't need to use an IDE if you don't want to. In this recipe, we take a look at this command and explain how to use it.
You can use the
cocos
command help by executing it with the--help
parameter, as follows:$ cocos --help
We then move on to generating our new project:
First, we create a new Cocos2d-x project with the
cocos new
command, as shown here:$ cocos new MyGame -p com.example.mygame -l cpp -d ~/Documents/
The result of this command is shown the following screenshot:
Behind the
new
parameter is the project name. The other parameters that are mentioned denote the following:MyGame
is the name of your project.-p
is the package name for Android. This is the application ID in the Google Play store. So, you should use the reverse domain name as the unique name.-l
is the programming language used for the project. You should usecpp
because we will use C++ in this book.-d
is the location in which to generate the new project. This time, we generate it in the user's documents directory.
You can look up these variables using the following command:
$ cocos new —help
Congratulations, you can generate your new project. The next step is to build and run using the
cocos
command.Compiling the project:
If you want to build and run for iOS, you need to execute the following command:
$ cocos run -s ~/Documents/MyGame -p ios
The parameters that are mentioned are explained as follows:
-s
is the directory of the project. This could be an absolute path or a relative path.-p
denotes which platform to run on. If you want to run on Android you use-p android
. The available options are IOS, Android, Win32, Mac, and Linux.You can run
cocos run –help
for more detailed information.
The result of this command is shown in the following screenshot:
You can now build and run iOS applications on cocos2d-x. However, you have to wait for a long time if this is your first time building an iOS application. It takes a long time to build a Cocos2d-x library, depending on if it was a clean build or a first build.
The cocos
command can create a new project and build it. You should use the cocos
command if you want to create a new project. Of course, you can build using Xcode or Eclipse. You can easily develop and debug using these tools.
The cocos run
command has other parameters. They are the following:
The cocos
command also includes some other commands, which are as follows:
The
compile
command: This command is used to build a project. The following patterns are useful parameters. You can see all parameters and options if you execute thecocos compile [–h]
command:cocos compile [-h] [-s SRC_DIR] [-q] [-p PLATFORM] [-m MODE]
The
deploy
command: This command only takes effect when the target platform is Android. It will re-install the specified project to the android device or simulator:cocos deploy [-h] [-s SRC_DIR] [-q] [-p PLATFORM] [-m MODE]
You must finish the first recipe before you begin this step. If you have not finished it yet, you will need to install Eclipse.
Setting up
NDK_ROOT
:Open the preference of Eclipse
Open C++ | Build | Environment
Click on Add and set the new variable, the name is
NDK_ROOT
, and the value isNDK_ROOT
path:Importing your project into Eclipse:
Import the project into Eclipse at
~/Documents/MyGame/proj.android
:Importing the Cocos2d-x library into Eclipse:
The first time, Eclipse will ask you to select a way to run your application. Select Android Application and click on OK, as shown in the following screenshot:
If you connected to the Android device on your Mac, you can run your game on your real device or an emulator. The following screenshot shows that it is running on Nexus5:
If you added
cpp
files into your project, you have to modify theAndroid.mk
file at~/Documents/MyGame/proj.android/jni/Android.mk
. This file is needed to build the NDK. This fix is required to add files.The original
Android.mk
would look as follows:LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorldScene.cpp
If you added the
TitleScene.cpp
file, you have to modify it as shown in the following code:LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorldScene.cpp \ ../../Classes/TitleScene.cpp
The preceding example shows an instance of when you add the TitleScene.cpp
file. However, if you are also adding other files, you need to add all the added files.
You get lots of errors when importing your project into Eclipse, but don't panic. After importing the Cocos2d-x library, errors soon disappear. This allows us to set the path of the NDK, Eclipse could compile C++. After you have modified the C++ codes, run your project in Eclipse. Eclipse automatically compiles C++ codes, Java codes, and then runs.
It is a tedious task to fix Android.mk
again to add the C++ files. The following code is the original Android.mk
:
LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorldScene.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
The following code is the customized Android.mk
that adds C++ files automatically:
CPP_FILES := $(shell find $(LOCAL_PATH)/../../Classes -name *.cpp) LOCAL_SRC_FILES := hellocpp/main.cpp LOCAL_SRC_FILES += $(CPP_FILES:$(LOCAL_PATH)/%=%) LOCAL_C_INCLUDES := $(shell find $(LOCAL_PATH)/../../Classes -type d)
The first line of the code gets C++ files to the Classes
directory into the CPP_FILES
variable. The second and third lines add C++ files into the LOCAL_C_INCLUDES
variable. By doing so, C++ files will be automatically compiled in the NDK. If you need to compile a file other than the extension .cpp
file, you will need to add it manually.
You may notice a difference in screen appearance on different devices. In some previous recipes, there is an iOS's screenshot and a Nexus 5's screenshot. It shows different image sizes. This image is HelloWorld.png
located at MyGame/Resources
. It is 480 x 320 pixels. In this recipe, we explain how to maintain the same size regardless of screen size.
Open AppDelegate.cpp
through Xcode, and modify the AppDelegate::applicationDidFinishLaunching()
method by adding the code after the director->setAnimationInterval(1.0/60.0);
line, as shown in the following code:
director->setAnimationInterval(1.0 / 60); glview->setDesignResolutionSize(640, 960, ResolutionPolicy::NO_BORDER);
In this book, we design the game with a screen size of iPhone's 3.5 inch screen. So, we set this screen size to the design resolution size by using the setDesignResolutionSize
method. The last parameter is resolution policy. The following screenshot is the Nexus 5's screenshot after implementing multi-resolution:

The following screenshot is the iPhone 5 simulator's screenshot. You now know that both screenshots have the same appearance:

The resolution policy has EXACT_FIT
, NO_BORDER
, SHOW_ALL
, FIXED_HEIGHT,
and FIXED_WIDTH
. These are explained as follows:
EXACT_FIT
: The entire application is visible in the specified area without trying to preserve the original aspect ratio.NO_BORDER
: The entire application fills the specified area, without distortion but possibly with some cropping, while maintaining the original aspect ratio of the application.SHOW_ALL
: The entire application is visible in the specified area without distortion, while maintaining the internal the aspect ratio of the application. Borders can appear on two sides of the application.FIXED_HEIGHT
: The application takes the height of the design resolution size and modifies the width of the internal canvas so that it fits the aspect ratio of the device. No distortion will occur, however, you must make sure your application works on different aspect ratios.FIXED_WIDTH
: The application takes the width of the design resolution size and modifies the height of the internal canvas so that it fits the aspect ratio of the device. No distortion will occur, however, you must make sure your application works on different aspect ratios.
By implementing multi-resolution, regardless of screen size, you will maintain the image on the screen.
In the next chapter, we will start the original game. You know there are a lot of comments and codes in HelloWorldScene.cpp
and the HelloWorldScene.h
file. That's why we will remove unnecessary codes in the template project and get started with the original game right away.
Open
HelloWorldScene.h
and remove themenuCloseCallback
method and unnecessary comments. NowHelloWorldScene.h
should look like the following code:#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
The next step is to open
HelloWorldScene.cpp
and remove unnecessary comments, codes, and methods. NowHelloWorldScene.cpp
should look like the following code:#include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene() { auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if ( !Layer::init() ) { return false; } return true; }
The next step is to remove unnecessary images in
resources
. RemoveCloseNormal.png
,CloseSelected.png
andHelloWorld.png
from theResources
folder in Xcode:Finally, if you are developing only iOS and Android applications, you don't need files for other platforms such as Linux, Windows, and Windows Phone. You should remove these files.
Before removing platform files, it should look like the following screenshot:
After removing platform files, it should look like the following screenshot:
With this recipe, you can get the simplest project ready before removing unnecessary comments, codes, and methods. Removing unnecessary platform codes and resources is important for reducing the size of your application. If you start building your original game from scratch, you will need to follow this recipe or chances are, you may get a black screen if you build and run this project. In the next chapter, you can start coding within this simple project.