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
To do this, first declare a native method
showOptions in AppActivity class.
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
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.
Next, create the implementation of that function.
Finally, all you have to do is to call
AppActivity.showOptions() when the button is pressed.
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.