1. Integrate Cocos2d-x (C++) into an Android application (Java)

    This is the Android pendant of the Cocos2d-x within UKKit (iOS) tutorial. As you can see, it’s a bit more complex/long with Android as you need to use JNI and configure Eclipse to merge Java and C++ code, whereas Objective C and C++ can be merged with no problem in Xcode for your iOS projects.

    Most tutorials about Cocos2d-x and Android will explain you how to compile and run a pure Cocos2d-x application (you will learn a lot on this excellent Cocos2d-x Android tutorial by French developer Jean-Yves Mengant).
    Here, you will learn how to launch different Cocos2d-x activities within your classic Java Android application.


    I used Cocos2d-x v1 but I guess this shouldn’t be too much different with Cocos2d-x v2.

    I’m on MacOSX but there are very few things specific to the OS I think, so that should work fine on any OS.

    1. You have an Android (Java) project. For some activities in your app, you want to use the Cocos2d-x engine.

    2. Download the sample project SkeletonCocos2dxApp.zip [UPDATED 2012/06/10 fix Activity end]. We’ll copy some files from it in this tutorial. But you can also open it, study it and run it, to quickly see how it works. I’ve not tested, but you might need to setup Eclipse for C++ environment before compiling the sample project (steps 17. 18. 19. 20. 21. 22. and 23.).

    3. For this tutorial, you don’t need to download the latest sources of Cocos2d-x, because they are included in the sample project (I used Cocos2d-x v1 / Opengl 1.1). When you complete the tutorial and understand how the integration of Cocos2d-x into your java project works, you can easily update with the latest cocos2d-x source files if needed.
    For your informations this is a quick summary of the modifications added to the Cocos2d-x official sources :
    - The org.cocos2dx.libs package was modified from the official cocos2d-x HelloWorld example to allow the “BACK” key to be caught in your activity, and to customize the starting CCScene of your activity.
    - The AppDelegate.cpp was modified to allow to customize the starting CCScene.

    4. Download the latest Android NDK. Cocos2d-x is C++ and the NDK is necessary to mix java and C++ together in your Android app.

    5. Unpack the NDK and put it somewhere, at the same level of the Android SDK is not a bad idea.

    6. Now we’ll start to modify your Android project. We’ll add the HelloWorld And HelloWorldBis Cocos2d-x activities to your project. Once this is done and once you undersand how it works, you can easily modify these activities to your needs.

    7. The resources (images, textures, sounds, files) of your Cocos2d-x CCNodes and game logic must be added in the “assets" directory of your Android project. Copy the content of the "assets" directory from the sample project into your project.

    8. Copy from the sample project, the package “org.cocos2dx.lib" on your project (in "src" directory).

    9. Copy from the sample project, the package “org.cocos2dx.helloworld" in your project. These are the 2 Cocos2d-x activities, that you can rename and modify later to your needs.

    10. Modify your manifest.xml to declare those 2 activities.
            <activity android:name="org.cocos2dx.helloworld.Cocos2dxHelloWorld"
                      android:label="Hello World Cocos2dx"
                      android:screenOrientation="landscape"
                      android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                      android:configChanges="orientation">
            </activity>
            
            <activity android:name="org.cocos2dx.helloworld.Cocos2dxHelloWorldBis"
                      android:label="Hello World Bis Cocos2dx"
                      android:screenOrientation="landscape"
                      android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                      android:configChanges="orientation">
            </activity>
    
    11. Copy from the sample project, the directory “jni" into your Android project. "cocos2dx" and "CocosDenshion" are the C++ sources copied from official Cocos2d-x package. "Classes" contains the Cocos2d-x CCNodes and game logic specific to your project. And "main/main.cpp" contains the JNI methods called by the Cocos2d-x activities (like Cocos2dxHelloWorld), used to customize which CCScene an activity will start. (.
    12. Don’t know JNI? JNI stands for Java Native Interface, it’s a Java feature and not specific to Android. You don’t need to know that much to run this tutorial, but you can learn more on the Wikipedia article about JNI.

    13. Create your own Cocos2d-x activity that suits your needs? Don’t do this before you have the HelloWorld examples running in your app, but here is how you would do this :
    - Create the Cocos2dxActivity java class (it is a good idea to duplicate the Cocos2dxHelloWorld.java class). Let’s say we’ve created a Cocos2dxJigsawPuzzle.java class in com.mycompany package.
    - Don’t forget to declare your new activity in the Manifest.xml.
    - Add the following JNI method in the “jni/main/main.cpp" file : void Java_com_mycompany_Cocos2dxJigsawPuzzle_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h) { … }.
    - Note that the default parameters are just the width (w) and the height (h) of the OpenGL surface to create, but you can (and most of the time you will) use more parameters in the JNI method. Example void Java_com_mycompany_Cocos2dxJigsawPuzzle_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h, jint columnsCount, jint rowsCount) { … }.
    - Add the following JNI method in the “jni/main/main.cpp" file : void Java_com_mycompany_Cocos2dxJigsawPuzzle_nativeEnd(JNIEnv* env, jobject thiz) { CCDirector::sharedDirector()->end(); }. It’s used to kill the Cocos2d-x environment when the player presses the “BACK” button.

    14. Launch the Cocos2d-x activity(ies) somewhere in your project (when taping on a button for example?) :
        		Intent intent = new Intent();
        		intent.setClass(SkeletonActivity.this, Cocos2dxHelloWorld.class);
        		SkeletonActivity.this.startActivity(intent);
    
    15. Done for the project modification! Now let’s compile all this.

    16. Copy from the sample project the “build_native.sh” file. Edit it and set the right values. For the NDK_ROOT, use the directory you unpacked/moved the android NDK to. For APP_ANDROID_ROOT, use the root directory of your Android project.


    17. Now we need to compile from Eclipse.

    18. If it’s not already done, install the C++ environment for Eclipse. This is basically the installation of CDT C++ plugins via the “Help/Install New Software” menu in Eclipse. Perfectly explained and illustrated in the paragraph “Setting up Eclipse for Cocos2D-X Development" of this article.

    19. Now we need to convert the project to a C++ project. Right click on the project in the left tree and select “New/Other”.


    Then pick “Convert to a C/C++ project”.


    Select “C++”/”Makefile project”/”— Other Toolchain —” and click on “Finish”


    20. Now we need to tell Eclipse to run our “build_native.sh” makefile. Right click on the project in the left tree and select “Properties”. Select “C/C++ Build”, uncheck “Use default build command”, and enter “bash ${workspace_loc:/SkeletonApp}/build_native.sh NDK_DEBUG=0 V=1” in the “Build command:” field.


    22. Now we need to fix the C++ compilation problems/warnings with Eclipse. Right click on the project in the left tree and select “Properties”. Select “C/C++ General”/”Path and Symbols”/”GNU C++”. Then use the “Add…” button to add directories to the path as shown below (not sure all are necessary, and don’t forget to replace the NDK path with your own path).
    /Library/Android/android-ndk-r8/sources/cxx-stl/gnu-libstdc++/include
    /Library/Android/android-ndk-r8/platforms/android-8/arch-arm/usr/include
    /Library/Android/android-ndk-r8/sources/cxx-stl/system/include
    /Library/Android/android-ndk-r8/sources/cxx-stl/gabi++/include


    23. Eclipse is not very good with C++, so in addition we need to deactivate some warnings/errors. Still in the project properties, select “C/C++ General”/”Code Analysis” and uncheck everything as shown below (not sure you really need to uncheck everything but just to be sure…).


    24. We’re done. Run the project and that should work. Actually, that will certainly not work at the first try, but hey you’re a developer. You still have a working sample project to compare with your project.

    25. Stay tuned @jpsarda.
     
  1. kyungtaek reblogged this from jpsarda
  2. gavinbowman likes this
  3. jpsarda posted this