Quantcast
Channel: Intel Developer Zone Articles
Viewing all articles
Browse latest Browse all 3384

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

$
0
0

The Cocos2d-x engine is an open source, cross-platform game engine that is widely adopted by developers all over the world and gives developers the technology platform from which they can build games that work seamlessly across multiple platforms quickly and effectively.

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:


# set environment parameters
NDK_ROOT_LOCAL="/home/test/soft/android-ndk-r9"
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 $NDKROOT directory. Open the $PROJECT_HOME/proj.android/build_native.sh shell script and add the following line at the top of the file:


# paths
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:

<application android:label="@string/app_name"
                 android:icon="@drawable/icon">

Change it by the correct icon name:

<application android:label="@string/app_name"
                 android:icon="@drawable/ic_launcher">
mMdify android:minSDKVersion:<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 writecocos2dx-src, click Finish and OK.

Now the project has no errors.

Run the project. Results are presented below.

Congratulations, you have now successfully built an NDK project with Cocos2d-x on an Android device.

Related Articles and Resources

About the Author

Vitaliy Kalinin works in the Software & Services Group at Intel Corporation. He is a PhD student at Lobachevsky State University in Nizhny Novgorod, Russia. His Bachelor’s degree is in economics and mathematics. His Master’s degree is applied economics and informatics. His main interest is mobile technologies and game development.


Viewing all articles
Browse latest Browse all 3384

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>