In this article written by Akihiro Matsuura, author of the book Cocos2d-x Cookbook, we're going to install Cocos2d-x and set up the development environment. The following topics will be covered in this article:
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.
(For more resources related to this topic, see here.)
To follow this recipe, you need to download the zip file from the official site of Cocos2d-x (http://www.cocos2d-x.org/download).
In this article we've used version 3.4 which was the latest stable version that was available.
$ cd ~/cocos2d-x-v3.4
$ ./setup.py
$ source ~/.bash_profile
Open the .bash_profile file, and you will find that setup.py shows how to set each path in your system. You can view the .bash_profile file using the cat command:
$ cat ~/.bash_profile
Open the terminal and run the cocos command without parameters.
$ cocos
If you can see a window like the following screenshot, you have successfully completed the Cocos2d-x install process.
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 which will be for a different version.
Setting up the Android environment is a bit tough. If you started to develop at Cocos2d-x soon, you can turn after the settings part of Android. And you would do it when you run on Android. In this case, you don't have to install Android SDK, NDK, and Apache. 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.
$ cocos --help
Firstly, 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:
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.
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:
The result of this command is shown in the following screenshot:
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 by using Xcode or Eclipse. You can easier of there when you develop and debug.
The cocos run command has other parameters. They are the following:
The cocos command also includes some other commands, which are as follows:
cocos compile [-h] [-s SRC_DIR] [-q] [-p PLATFORM] [-m MODE]
cocos deploy [-h] [-s SRC_DIR] [-q] [-p PLATFORM] [-m MODE]
The run command continues to compile and deploy commands.
Before building the project by Xcode, you require Xcode with an iOS developer account to test it on a physical device. However, you can also test it on an iOS simulator. If you did not install Xcode, you can get it from Mac App Store. Once you have installed it, get it activated.
You can open your project by double-clicking on the file placed at: ~/Documents/MyGame/proj.ios_mac/MyGame.xcodeproj.
You should select an iOS simulator or real device on which you want to run your project.
If this is your first time building, it will take a long time. But continue to build with confidence as it's the first time. You can develop your game faster if you develop and debug it using Xcode rather than Eclipse.
You must finish the first recipe before you begin this step. If you have not finished it yet, you will need to install Eclipse.
importing cocos2d lib
LOCAL_SRC_FILES := hellocpp/main.cpp
../../Classes/AppDelegate.cpp
../../Classes/HelloWorldScene.cpp
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 cocos2d-x library, errors soon disappear. This allows us to set the path of NDK, Eclipse could compile C++. After you modified 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 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 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 CPP_FILES variable. The second and third lines add C++ files into LOCAL_C_INCLUDES variable. By doing so, C++ files will be automatically compiled in NDK. If you need to compile a file other than the extension .cpp file, you will need to add it manually.
If you want to manually build C++ in NDK, you can use the following command:
$ ./build_native.py
This script is located at the ~/Documenst/MyGame/proj.android . It uses ANDROID_SDK_ROOT and NDK_ROOT in it. If you want to see its options, run ./build_native.py –help.
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. The book Cocos2d-x Cookbook focuses on using version 3.4, which is the latest version of Cocos2d-x that was available at the time of writing. We focus on iOS and Android development, and we'll be using Mac because we need it to develop iOS applications.
Further resources on this subject: