Android JNI之cocos2D源码分析

Cocos2d 做为一个跨平台的游戏引擎,对于Android中java与C的互相调用很有借鉴意义。本文只分析一下Cocos2d JNI 部分的封装代码。

代码结构

关于多平台的主要目录
  • cocos 主要源码
    • platform 支持不同平台的项目源码
      • android
      • ios
  • external cocos依赖的一些第三方开源工具,比如openssl、freetype 等

C++ 调用 java

cocos2d-x引擎对jni的操作进行了封装,提供了一个非常好用的类:JniHelper,定义了一些常用的接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

typedef std::unordered_map<JNIEnv*, std::vector<jobject>> LocalRefMapType;

static void setJavaVM(JavaVM *javaVM);
static JavaVM* getJavaVM();
static JNIEnv* getEnv();
static jobject getActivity();

static bool setClassLoaderFrom(jobject activityInstance);
static bool getStaticMethodInfo(JniMethodInfo &methodinfo,
const char *className,
const char *methodName,
const char *paramCode);
static bool getMethodInfo(JniMethodInfo &methodinfo,
const char *className,
const char *methodName,
const char *paramCode);

//转换java 字符串为C++字符串
static std::string jstring2string(jstring str);

static jmethodID loadclassMethod_methodID;
static jobject classloader;
static std::function<void()> classloaderCallback;

/**
调用java 无返回值的静态函数
@brief Call of Java static void method
@if no such method will log error
*/
template <typename... Ts>
static void callStaticVoidMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")V";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
LocalRefMapType localRefs;
t.env->CallStaticVoidMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env, localRefs);
} else {
reportError(className, methodName, signature);
}
}
/**========================================
还有很多类似的静态函数调用
callStaticBooleanMethod、callStaticIntMethod 。。。

template <typename... Ts> 类似于java中的泛型

通过 template <typename... Ts> 和 getJNISignature 根据实参自动拼接出调用java函数需要的参数类型字符串
========================================*/

static std::string getJNISignature() {
return "";
}

static std::string getJNISignature(bool) {
return "Z";
}

static std::string getJNISignature(char) {
return "C";
}

static std::string getJNISignature(short) {
return "S";
}

static std::string getJNISignature(int) {
return "I";
}

static std::string getJNISignature(long) {
return "J";
}

static std::string getJNISignature(float) {
return "F";
}

static std::string getJNISignature(double) {
return "D";
}

static std::string getJNISignature(const char*) {
return "Ljava/lang/String;";
}

static std::string getJNISignature(const std::string&) {
return "Ljava/lang/String;";
}

template <typename T>
static std::string getJNISignature(T x) {
// This template should never be instantiated
static_assert(sizeof(x) == 0, "Unsupported argument type");
return "";
}

template <typename T, typename... Ts>
static std::string getJNISignature(T x, Ts... xs) {
return getJNISignature(x) + getJNISignature(xs...);
}

java 调用C++函数

看过java JNI 的同学应该都了解过了。这里只简单说一下大概流程不做详细赘述了。

  1. 首先我们会在java 类中定义native函数
    private static native void nativeTouchesBegin(final int id, final float x, final float y);
  2. 然后我们在c++ 中定义对应的实现函数。需要注意的是java 类型和C类型的转换。下面展示一下cocos 对触摸事件的封装。凑字数了哈哈。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// TouchesJni.cpp

#include "base/CCDirector.h"
#include "base/CCEventKeyboard.h"
#include "base/CCEventDispatcher.h"
#include "platform/android/CCGLViewImpl-android.h"

#include <android/log.h>
#include <jni.h>

using namespace cocos2d;

extern "C" {
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv * env, jobject thiz, jint id, jfloat x, jfloat y) {
intptr_t idlong = id;
cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &idlong, &x, &y);
}

JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesEnd(JNIEnv * env, jobject thiz, jint id, jfloat x, jfloat y) {
intptr_t idlong = id;
cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &idlong, &x, &y);
}

JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesMove(JNIEnv * env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys) {
int size = env->GetArrayLength(ids);
jint id[size];
jfloat x[size];
jfloat y[size];

env->GetIntArrayRegion(ids, 0, size, id);
env->GetFloatArrayRegion(xs, 0, size, x);
env->GetFloatArrayRegion(ys, 0, size, y);

intptr_t idlong[size];
for(int i = 0; i < size; i++)
idlong[i] = id[i];

cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesMove(size, idlong, x, y);
}

JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesCancel(JNIEnv * env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys) {
int size = env->GetArrayLength(ids);
jint id[size];
jfloat x[size];
jfloat y[size];

env->GetIntArrayRegion(ids, 0, size, id);
env->GetFloatArrayRegion(xs, 0, size, x);
env->GetFloatArrayRegion(ys, 0, size, y);

intptr_t idlong[size];
for(int i = 0; i < size; i++)
idlong[i] = id[i];

cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesCancel(size, idlong, x, y);
}

#define KEYCODE_BACK 0x04
#define KEYCODE_MENU 0x52
#define KEYCODE_DPAD_UP 0x13
#define KEYCODE_DPAD_DOWN 0x14
#define KEYCODE_DPAD_LEFT 0x15
#define KEYCODE_DPAD_RIGHT 0x16
#define KEYCODE_ENTER 0x42
#define KEYCODE_PLAY 0x7e
#define KEYCODE_DPAD_CENTER 0x17


static std::unordered_map<int, cocos2d::EventKeyboard::KeyCode> g_keyCodeMap = {
{ KEYCODE_BACK , cocos2d::EventKeyboard::KeyCode::KEY_ESCAPE},
{ KEYCODE_MENU , cocos2d::EventKeyboard::KeyCode::KEY_MENU},
{ KEYCODE_DPAD_UP , cocos2d::EventKeyboard::KeyCode::KEY_DPAD_UP },
{ KEYCODE_DPAD_DOWN , cocos2d::EventKeyboard::KeyCode::KEY_DPAD_DOWN },
{ KEYCODE_DPAD_LEFT , cocos2d::EventKeyboard::KeyCode::KEY_DPAD_LEFT },
{ KEYCODE_DPAD_RIGHT , cocos2d::EventKeyboard::KeyCode::KEY_DPAD_RIGHT },
{ KEYCODE_ENTER , cocos2d::EventKeyboard::KeyCode::KEY_ENTER},
{ KEYCODE_PLAY , cocos2d::EventKeyboard::KeyCode::KEY_PLAY},
{ KEYCODE_DPAD_CENTER , cocos2d::EventKeyboard::KeyCode::KEY_DPAD_CENTER},

};

JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyEvent(JNIEnv * env, jobject thiz, jint keyCode, jboolean isPressed) {
Director* pDirector = Director::getInstance();

auto iterKeyCode = g_keyCodeMap.find(keyCode);
if (iterKeyCode == g_keyCodeMap.end()) {
return JNI_FALSE;
}

cocos2d::EventKeyboard::KeyCode cocos2dKey = g_keyCodeMap.at(keyCode);
cocos2d::EventKeyboard event(cocos2dKey, isPressed);
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
return JNI_TRUE;

}}