Putting everything together
We have accomplished our goal of embedding the core Java code in our Cocos2d-x game. Now we are going to show all the parts of our game that have been modified throughout this chapter together.
Here, we show the header file of the C++ JNI bridge (JniBridge.h
) that we have created from scratch:
#ifndef __JNI_BRIDGE_H__ #define __JNI_BRIDGE_H__ #include "cocos2d.h" class JniBridge { public: static void showToast(const char* message); }; #endif
Now that we have defined the header of our JniBridge
, let us write the implementation file (JniBridge.cpp
):
#include "JniBridge.h" #include "platform/android/jni/JniHelper.h" #define CLASS_NAME "com/packtpub/jni/JniFacade" #define METHOD_NAME "showToast" #define PARAM_CODE "(Ljava/lang/String;)V" USING_NS_CC; void JniBridge::showToast(const char* message) { JniMethodInfo method; JniHelper::getStaticMethodInfo(method, CLASS_NAME, METHOD_ NAME,PARAM_CODE)...