Cocos2d-x Game Engine for Android C++ NDK Apps

Cocos2d-x Game Engine for Android C++ NDK Apps

Using a single C++ codebase you can deploy games to just about any mobile platform and since Cocos2d-x is open-source, you can tweak the game engine if needed.

The Cocos2d API is simple and powerful. With a couple of lines of code you can bring a few images and sounds to life using actions and create your own vibrant game. Cocos2d-x takes that powerful API and makes it portable using C++. It empowers you, the developer, and enables you to choose which platforms on which you want to develop and how to deploy them. In this article, I will explain how to build an NDK app for Android* on Intel® architecture.

Hello World and Cocos2d-x for Android*

Let’s build a “Hello, World” Cocos2d-x project for an Android device. I prefer to do this via a command line since there are no Cocos2d-x project templates available via the Eclipse* IDE at the moment.

The home directory of Cocos2d-x contains a shell script named create-android-projects.sh that you can use to generate the Android project.
Before launching this shell script, you will need to make a tiny customization at the top of the above file:

1 # set environment parameters

2 NDK_ROOT_LOCAL="/home/test/soft/android-ndk-r9"

3 ANDROID_SDK_ROOT_LOCAL="/home/test/soft/android-sdk-linux_86"
Modify the lines above so that the NDK_ROOT_LOCAL variable points to the directory where you installed the Android NDK and ANDROID_SDK_ROOT_LOCAL points to the place where you installed the Android SDK.

Run the create-android-project.sh script from the command line. There are several prompts to input various values. Input the package path and get a list of available Android API’s and their ids. You will need to use the id for the last item. Additionally, assign the project name (I called it: doto).

The created project directory is the line towards the end of the script output. That’s where your Android project has been created by the script. The project location (/home/test/Desktop/doto) will be referred to as $PROJECT_HOME for now.

Building the Project

There are two steps to building the project: compiling the C++ code with a command line script, and compiling the Java code with Eclipse.
Before compiling however, you will need to define the NDK_ROOT parameter so it points to $NDKROOTdirectory. Open the $PROJECT_HOME/proj.android/build_native.sh shell script and add the following line at the top of the file:
1 # paths

2 NDK_ROOT="/home/test/soft/android-ndk-r9”
Modify the above line so that the NDK_ROOT variable points to the directory where you installed the Android NDK ($NDKROOT).
To compile the C++ code, switch to the $PROJECT_HOME/proj.android folder via the command line and issue the following command:
./build_native.sh
This builds the C++ Cocos2d-x libraries, and the C++ code for your project.
To build the Java* code, it is necessary to create an Eclipse project.

Start Eclipse and go to File\New\Other. Choose Android\Android Project from Existing Code, and click Next. Click Browse and select the $PROJECT_HOME/proj.android folder.

At this point the project might have some errors.
One of the errors arises because Eclipse can’t find the resource “@drawable/icon” , so, expand the project tree on the left, open AndroidManifest.xml and change to the AndroidManifest.xml view so you can see the plain text. Look for the following line of code:
1 <application android:label="@string/app_name"

2 android:icon="@drawable/icon">
Change it by the correct icon name:

1 <application android:label="@string/app_name"

2 android:icon="@drawable/ic_launcher">

3 mMdify android:minSDKVersion:

4 <uses-sdk android:minSdkVersion="24"/>

Save the file.
The second error arises because Eclipse can’t find some Cocos2d-x Java classes. Select Project\Properties From the main menu and choose Java Build Path from the list on the left. In the Source tab click on Link Source and Browse to the following directory:

$COCOS2DX_HOME/cocos/platform/android/java/src
where $COCOS2DX_HOME is the directory where you installed the Cocos2d-x resources. In Folder name write cocos2dx-src, click Finish and OK.

Now the project has no errors.

Run the project. Results are presented below.

For more such Android resources and tools from Intel, please visit the Intel® Developer Zone

Source: https://software.intel.com/en-us/articles/cocos2dx-game-engine-for-android-c-ndk-apps

Promotion
Digit.in
Logo
Digit.in
Logo