Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Cocos2d-x cookbook

You're reading from   Cocos2d-x cookbook Over 50 hands-on recipes to help you efficiently administer and maintain your games with Cocos2d-x

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781783284757
Length 254 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Akihiro Matsuura Akihiro Matsuura
Author Profile Icon Akihiro Matsuura
Akihiro Matsuura
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with Cocos2d-x FREE CHAPTER 2. Creating Sprites 3. Working with Labels 4. Building Scenes and Layers 5. Creating GUIs 6. Playing Sounds 7. Working with Resource Files 8. Working with Hardware 9. Controlling Physics 10. Improving Games with Extra Features 11. Taking Advantages Index

Building the project using Eclipse

Getting ready

You must finish the first recipe before you begin this step. If you have not finished it yet, you will need to install Eclipse.

How to do it...

  1. Setting up NDK_ROOT:
    • Open the preference of Eclipse
    • Open C++ | Build | Environment
      How to do it...
  2. Click on Add and set the new variable, the name is NDK_ROOT, and the value is NDK_ROOT path:
    How to do it...
  3. Importing your project into Eclipse:
    • Open the file and click on Import
    • Go to Android | Existing Android Code into Workspace
    • Click on Next
    How to do it...
  4. Import the project into Eclipse at ~/Documents/MyGame/proj.android:
    How to do it...
  5. Importing the Cocos2d-x library into Eclipse:
    • Perform the same steps from Step 3 to Step 4.
    • Import the project cocos2d lib at ~/Documents/MyGame/cocos2d/cocos/platform/android/java, using the following command:
      importing cocos2d lib
      
    How to do it...
  6. Build and Run:
    • Click on the Run icon
    • 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:
      How to do it...
    • 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:
      How to do it...
  7. If you added cpp files into your project, you have to modify the Android.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.
  8. The original Android.mk would look as follows:
    LOCAL_SRC_FILES := hellocpp/main.cpp \
                       ../../Classes/AppDelegate.cpp \
                       ../../Classes/HelloWorldScene.cpp
  9. 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.

How it works...

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.

There's more...

If you want to manually build C++ in NDK, you can use the following command:

$ ./build_native.py

This script is located in ~/Documents/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.

You have been reading a chapter from
Cocos2d-x cookbook
Published in: Nov 2015
Publisher:
ISBN-13: 9781783284757
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image