read

Continuing my struggle with Java-C++ interoperability, calling a C++ function from Java is not straight forward as well. In my cocos2d-x game, I need to call a function called showOptions in a C++ class from AppActivity Java class when user taps on a button outside Cocos2dxGLSurfaceView.

To do this, first declare a native method showOptions in AppActivity class.

public class AppActivity extends Cocos2dxActivity {
    public static native void showOptions ();


}

Then in some C++ header file, we need to add the JNI declaration for showOptions method with Java full-path classname. To get this, run the following from proj.android directory.

javah -classpath bin/classes:src org.cocos2dx.cpp.AppActivity

That javah command will create a new file called org_cocos2dx_cpp_AppActivity.h. You can simply include this header in your project, or just copy the interface declaraions and paste them in your own header file.

#include <jni.h>
extern "C" {
    JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_showOptions
    (JNIEnv * , jclass);
}

Next, create the implementation of that function.

JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_showOptions
(JNIEnv * , jclass)
{
    // call the C++ showOptions method.
    NativeBridge::getInstance()->showOptions();
}

Finally, all you have to do is to call AppActivity.showOptions() when the button is pressed.

However, calling AppActivity.showOptions() directly will crash the game with call to OpenGL ES API with no current context (logged once per thread) error.

To fix this, all you have to do is to call it on a GL thread.

optionsButton.setOnTouchListener(new View.OnTouchListener() {       
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        if (event.getAction() == MotionEvent.ACTION_UP) {
            instance.runOnGLThread (new Runnable () {
                @Override
                public void run () {                
                    AppActivity.showOptions();
                }
            });
        }

        return true;
    }
});
Image

NICNOCQUEE

Blog about iOS, Programming, Japan, or Random Stuff.

« Older Newer »