Install cocos2dx

523

1. Cocos2d Tutorial and Guide list:

https://github.com/MobileAppTracking/cocos2dx-plugin

2. Create new project:
Run/cmd
cocos new hello -p com.vn.hello -l cpp -d q:/android/project
3. Plugin:
http://stnguyen.com/cocos2d-x/call-java-functions-from-cpp.html
http://stnguyen.com/cocos2d-x/call-cpp-functions-from-java.html
http://stnguyen.com/cocos2d-x/call-cpp-functions-from-java.html
http://www.slideshare.net/vienbk91/jni-48734418

Call C++ functions from Java in cocos2d-x

If you’ve read Part 1. Calling Java functions from C++ in cocos2d-x, you’ll agree with me that the task is not as hard as it seems. Fortunately, the other way around, i.e., calling C++ functions from Java, is also quite straight forward.

The following note actually applies for all native Android applications, not just cocos2d-x games.

Simplest case: void method with no arguments

There’re 2 steps to make a C++ function callable in Java code:

  1. Declare a native method in Java code
  2. Name the C++ function follows a rule

1. Declare a native method in Java code

Suppose there’s a awesomeCppFunction() you wanna call from Java, first you’ll need to declare it as native method in your Java class:

Calling this method in Java is as normal as calling any ordinary Java method:

2. Name the C++ function follows a rule

C++ function name must contain Java full-path classname which declared its signature in step (1).

In the above example, your C++ function must be namedJava_com_myhouse_HappyJavaClass_awesomeCppFunction.

It must be declared in .cpp file like so:

The first 2 arguments JNIEnv* env, jobject thiz are required and are used to extract data sent from Java to C++.

Pass parameters and return value

On Java side, you declare method params and return type as usual:

On C++ side, you’ll need to “convert” Java types to JNI (native) types.

JNI Types Java Type
void void
jboolean boolean
jbyte byte
jchar char
jshort short
jint int
jlong long
jfloat float
jdouble double
jobject All Java objects
jclass java.lang.Class objects
jstring java.lang.String objects
jobjectArray Array of objects
jbooleanArray Array of booleans
jbyteArray Array of bytes
jshortArray Array of shorts
jintArray Array of integers
jlongArray Array of longs
jfloatArray Array of floats
jdoubleArray Array of doubles

Add parameters after the 2 required params:

Use primitive type parameters

Primitive data (e.g., jboolean, jint, jchar, jfloat…) can be used as normal C++ equivalent data types:

Use object parameters

Objects passed as parameters need to be converted to C++ equivalent data types:

Examples:

String

Array of integers:

Example from here:

Array of Strings:

Example from here:

Return primitive data

Returning primitive data (e.g., jboolean, jint, jchar, jfloat…) is just as you normally do in C++:

Return object

To return an object, you’ll need to convert your C++ object to equivalent JNI object.

Examples:

String

Array of integers

Example extracted from this stackoverflow answer:

Array of Strings

Example extracted from this stackoverflow answer: