GameCorder.net

このエントリーをはてなブックマークに追加

convert java to c++

Way of converting java to c++. First create Navetive function. let's see the example.

		// has two args String data String id
    public static native void getNativeBluetoothData(String data,String id);
		

In cocos2dx we have AppActivity from the beginning. Write public static native function in AppActivity, And call this function want to call.

Then we have to create source file for native function.
In Classes/platform/android/jni

Source file name has a rule to obey. In this case.

org_cocos2dx_cpp_AppActivity.cpp

Pacage name in AppActivity is
package org.cocos2dx.cpp
so in header org_cocos2dx_cpp_

Let's write the code in org_cocos2dx_cpp_AppActivity.cpp

// 1.function
JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_getNativeBluetoothData(JNIEnv *env,jobject thiz,jstring jsonStr,jstring id)
{
    // 2.convert string
    auto arg1 = env->GetStringUTFChars(jsonStr, nullptr);
    std::string string1 = arg1;
    env->ReleaseStringUTFChars(jsonStr, arg1);
    // arg2
    auto arg2 = env->GetStringUTFChars(id, nullptr);
    std::string string2 = arg2;
    env->ReleaseStringUTFChars(id, arg2);
    // 3.create scene from cocos2dx
    MenuScene* scene = dynamic_cast(cocos2d::CCDirector::sharedDirector()->getRunningScene());
    // 4.call c++ function
    scene->getBluetoothData(string1.c_str(),string2.c_str());
}
		

I define function two args both String
String equals jstring.
and let see another infomation.

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

1.Define function

in args have two arg not familiar.
JNIEnv *env
jobject thiz

This two need to declare to define function

Next is header file.
Name is org_cocos2dx_cpp_AppActivity.h

	#include 

	#ifndef _Included_org_cocos2dx_cpp_AppActivity
	#define _Included_org_cocos2dx_cpp_AppActivity

	extern "C" {

	JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_getNativeBluetoothData
	   (JNIEnv* env,jobject thiz,jstring jsonStr,jstring id);

	#endif

In Header file needs JNIEXPORT in header.
and JNICALL ahead of function call.