mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-05 02:52:39 +08:00
398 lines
14 KiB
C++
398 lines
14 KiB
C++
/****************************************************************************
|
|
Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
http://www.cocos.com
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated engine source code (the "Software"), a limited,
|
|
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
|
to use Cocos Creator solely to develop games on your target platforms. You shall
|
|
not use Cocos Creator software for developing other software or tools that's
|
|
used for developing games. You are not granted to publish, distribute,
|
|
sublicense, and/or sell copies of Cocos Creator.
|
|
|
|
The software or tools in this License Agreement are licensed, not sold.
|
|
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
****************************************************************************/
|
|
#include "JniImp.h"
|
|
#include <unordered_map>
|
|
#include <android/log.h>
|
|
#include <android/asset_manager_jni.h>
|
|
#include <jni.h>
|
|
#include "JniHelper.h"
|
|
#include "platform/CCApplication.h"
|
|
#include "scripting/js-bindings/jswrapper/v8/ScriptEngine.hpp"
|
|
#include "scripting/js-bindings/event/EventDispatcher.h"
|
|
#include "platform/android/CCFileUtils-android.h"
|
|
|
|
#define JNI_IMP_LOG_TAG "JniImp"
|
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,JNI_IMP_LOG_TAG,__VA_ARGS__)
|
|
|
|
#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
|
|
|
|
using namespace cocos2d;
|
|
|
|
namespace
|
|
{
|
|
// 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 },
|
|
// };
|
|
|
|
std::string g_apkPath;
|
|
const std::string Cocos2dxHelperClassName = "org/cocos2dx/lib/Cocos2dxHelper";
|
|
const std::string Cocos2dxBitmapClassName = "org/cocos2dx/lib/Cocos2dxBitmap";
|
|
const std::string Cocos2dxRendererClassName = "org/cocos2dx/lib/Cocos2dxRenderer";
|
|
EditTextCallback s_editTextCallback = nullptr;
|
|
void* s_ctx = nullptr;
|
|
int g_deviceSampleRate = 44100;
|
|
int g_deviceAudioBufferSizeInFrames = 192;
|
|
int g_width = 0;
|
|
int g_height = 0;
|
|
|
|
cocos2d::Application* g_app = nullptr;
|
|
|
|
bool setCanvasCallback(se::Object* global)
|
|
{
|
|
se::AutoHandleScope scope;
|
|
se::ScriptEngine* se = se::ScriptEngine::getInstance();
|
|
char commandBuf[200] = {0};
|
|
sprintf(commandBuf, "var window = window || this; window.canvas = { width: %d, height: %d };",
|
|
g_width,
|
|
g_height);
|
|
se->evalString(commandBuf);
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
Application* cocos_android_app_init(JNIEnv* env);
|
|
|
|
extern "C"
|
|
{
|
|
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
|
|
{
|
|
JniHelper::setJavaVM(vm);
|
|
g_app = cocos_android_app_init(JniHelper::getEnv());
|
|
|
|
return JNI_VERSION_1_4;
|
|
}
|
|
|
|
/*****************************************************
|
|
* Cocos2dxActivity native functions implementation.
|
|
*****************************************************/
|
|
|
|
JNIEXPORT jintArray Java_org_cocos2dx_lib_Cocos2dxActivity_getGLContextAttrs(JNIEnv* env, jobject thiz)
|
|
{
|
|
//TODO
|
|
int tmp[7] = {8, 8, 8,
|
|
8, 0, 0, 0};
|
|
jintArray glContextAttrsJava = env->NewIntArray(7);
|
|
env->SetIntArrayRegion(glContextAttrsJava, 0, 7, tmp);
|
|
|
|
return glContextAttrsJava;
|
|
}
|
|
|
|
/*****************************************************
|
|
* Cocos2dxRenderer native functions implementation.
|
|
*****************************************************/
|
|
|
|
JNIEXPORT void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)
|
|
{
|
|
g_width = w;
|
|
g_height = h;
|
|
se::ScriptEngine* se = se::ScriptEngine::getInstance();
|
|
se->addRegisterCallback(setCanvasCallback);
|
|
|
|
g_app->start();
|
|
}
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender(JNIEnv* env)
|
|
{
|
|
EventDispatcher::dispatchTickEvent();
|
|
}
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnPause()
|
|
{
|
|
if (g_app)
|
|
g_app->applicationDidEnterBackground();
|
|
}
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnResume()
|
|
{
|
|
if (g_app)
|
|
g_app->applicationWillEnterForeground();
|
|
}
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInsertText(JNIEnv* env, jobject thiz, jstring text)
|
|
{
|
|
//TODO
|
|
}
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeDeleteBackward(JNIEnv* env, jobject thiz)
|
|
{
|
|
//TODO
|
|
}
|
|
|
|
JNIEXPORT jstring JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeGetContentText()
|
|
{
|
|
//TODO
|
|
}
|
|
|
|
JNIEXPORT void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnSurfaceChanged(JNIEnv* env, jobject thiz, jint w, jint h)
|
|
{
|
|
//TODO
|
|
}
|
|
|
|
/***********************************************************
|
|
* Cocos2dxAccelerometer native functions implementation.
|
|
***********************************************************/
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxAccelerometer_onSensorChanged(JNIEnv* env, jobject thiz, jfloat x, jfloat y, jfloat z, jlong timeStamp)
|
|
{
|
|
//TODO
|
|
}
|
|
|
|
/***********************************************************
|
|
* Touches native functions implementation.
|
|
***********************************************************/
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv * env, jobject thiz, jint id, jfloat x, jfloat y)
|
|
{
|
|
//TODO
|
|
}
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesEnd(JNIEnv * env, jobject thiz, jint id, jfloat x, jfloat y)
|
|
{
|
|
//TODO
|
|
}
|
|
|
|
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];
|
|
|
|
//TODO
|
|
// 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];
|
|
|
|
//TODO
|
|
// cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesCancel(size, idlong, x, y);
|
|
}
|
|
|
|
JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyEvent(JNIEnv * env, jobject thiz, jint keyCode, jboolean isPressed)
|
|
{
|
|
//TODO
|
|
return JNI_TRUE;
|
|
|
|
}
|
|
|
|
/***********************************************************
|
|
* Cocos2dxHelper native functions implementation.
|
|
***********************************************************/
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetApkPath(JNIEnv* env, jobject thiz, jstring apkPath)
|
|
{
|
|
g_apkPath = JniHelper::jstring2string(apkPath);
|
|
}
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetContext(JNIEnv* env, jobject thiz, jobject context, jobject assetManager)
|
|
{
|
|
JniHelper::setClassLoaderFrom(context);
|
|
FileUtilsAndroid::setassetmanager(AAssetManager_fromJava(env, assetManager));
|
|
}
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetAudioDeviceInfo(JNIEnv* env, jobject thiz, jboolean isSupportLowLatency, jint deviceSampleRate, jint deviceAudioBufferSizeInFrames)
|
|
{
|
|
g_deviceSampleRate = deviceSampleRate;
|
|
g_deviceAudioBufferSizeInFrames = deviceAudioBufferSizeInFrames;
|
|
LOGD("nativeSetAudioDeviceInfo: sampleRate: %d, bufferSizeInFrames: %d", g_deviceSampleRate, g_deviceAudioBufferSizeInFrames);
|
|
}
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetEditTextDialogResult(JNIEnv* env, jobject obj, jbyteArray text)
|
|
{
|
|
jsize size = env->GetArrayLength(text);
|
|
|
|
if (size > 0)
|
|
{
|
|
jbyte * data = (jbyte*)env->GetByteArrayElements(text, 0);
|
|
char* buffer = (char*)malloc(size+1);
|
|
if (buffer != nullptr)
|
|
{
|
|
memcpy(buffer, data, size);
|
|
buffer[size] = '\0';
|
|
// pass data to edittext's delegate
|
|
if (s_editTextCallback)
|
|
s_editTextCallback(buffer, s_ctx);
|
|
free(buffer);
|
|
}
|
|
env->ReleaseByteArrayElements(text, data, 0);
|
|
}
|
|
else
|
|
{
|
|
if (s_editTextCallback)
|
|
s_editTextCallback("", s_ctx);
|
|
}
|
|
}
|
|
|
|
/***********************************************************
|
|
* Cocos2dxAudioFocusManager native functions implementation.
|
|
***********************************************************/
|
|
|
|
JNIEXPORT void Java_org_cocos2dx_lib_Cocos2dxAudioFocusManager_nativeOnAudioFocusChange(JNIEnv* env, jobject thiz, jint focusChange)
|
|
{
|
|
// cocos_audioengine_focus_change(focusChange);
|
|
}
|
|
} // end of extern "C"
|
|
|
|
|
|
/***********************************************************
|
|
* Functions invoke from cpp to Java.
|
|
***********************************************************/
|
|
|
|
std::string getApkPathJNI()
|
|
{
|
|
return g_apkPath;
|
|
}
|
|
|
|
std::string getPackageNameJNI()
|
|
{
|
|
return JniHelper::callStaticStringMethod(Cocos2dxHelperClassName, "getCocos2dxPackageName");
|
|
}
|
|
|
|
int getObbAssetFileDescriptorJNI(const std::string& path, long* startOffset, long* size)
|
|
{
|
|
JniMethodInfo methodInfo;
|
|
int fd = 0;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo, Cocos2dxHelperClassName.c_str(), "getObbAssetFileDescriptor", "(Ljava/lang/String;)[J"))
|
|
{
|
|
jstring stringArg = methodInfo.env->NewStringUTF(path.c_str());
|
|
jlongArray newArray = (jlongArray)methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID, stringArg);
|
|
jsize theArrayLen = methodInfo.env->GetArrayLength(newArray);
|
|
|
|
if (3 == theArrayLen)
|
|
{
|
|
jboolean copy = JNI_FALSE;
|
|
jlong *array = methodInfo.env->GetLongArrayElements(newArray, ©);
|
|
fd = static_cast<int>(array[0]);
|
|
*startOffset = array[1];
|
|
*size = array[2];
|
|
methodInfo.env->ReleaseLongArrayElements(newArray, array, 0);
|
|
}
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
methodInfo.env->DeleteLocalRef(stringArg);
|
|
}
|
|
|
|
return fd;
|
|
}
|
|
|
|
int getDeviceSampleRate()
|
|
{
|
|
return g_deviceSampleRate;
|
|
}
|
|
|
|
int getDeviceAudioBufferSizeInFrames()
|
|
{
|
|
return g_deviceAudioBufferSizeInFrames;
|
|
}
|
|
|
|
void convertEncodingJNI(const std::string& src, int byteSize, const std::string& fromCharset, std::string& dst, const std::string& newCharset)
|
|
{
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo, Cocos2dxHelperClassName.c_str(), "conversionEncoding", "([BLjava/lang/String;Ljava/lang/String;)[B"))
|
|
{
|
|
jbyteArray strArray = methodInfo.env->NewByteArray(byteSize);
|
|
methodInfo.env->SetByteArrayRegion(strArray, 0, byteSize, reinterpret_cast<const jbyte*>(src.c_str()));
|
|
|
|
jstring stringArg1 = methodInfo.env->NewStringUTF(fromCharset.c_str());
|
|
jstring stringArg2 = methodInfo.env->NewStringUTF(newCharset.c_str());
|
|
|
|
jbyteArray newArray = (jbyteArray)methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID, strArray, stringArg1, stringArg2);
|
|
jsize theArrayLen = methodInfo.env->GetArrayLength(newArray);
|
|
methodInfo.env->GetByteArrayRegion(newArray, 0, theArrayLen, (jbyte*)dst.c_str());
|
|
|
|
methodInfo.env->DeleteLocalRef(strArray);
|
|
methodInfo.env->DeleteLocalRef(stringArg1);
|
|
methodInfo.env->DeleteLocalRef(stringArg2);
|
|
methodInfo.env->DeleteLocalRef(newArray);
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
}
|
|
}
|
|
|
|
int getFontSizeAccordingHeightJni(int height)
|
|
{
|
|
return JniHelper::callStaticIntMethod(Cocos2dxBitmapClassName, "getFontSizeAccordingHeight", height);
|
|
}
|
|
|
|
std::string getStringWithEllipsisJni(const std::string& text, float width, float fontSize)
|
|
{
|
|
return JniHelper::callStaticStringMethod(Cocos2dxBitmapClassName, "getStringWithEllipsis", text.c_str(), width, fontSize);
|
|
}
|
|
|
|
std::string getCurrentLanguageJNI()
|
|
{
|
|
return JniHelper::callStaticStringMethod(Cocos2dxHelperClassName, "getCurrentLanguage");
|
|
}
|
|
|
|
bool openURLJNI(const std::string& url)
|
|
{
|
|
return JniHelper::callStaticBooleanMethod(Cocos2dxHelperClassName, "openURL", url);
|
|
}
|
|
|
|
void setAnimationIntervalJNI(float interval)
|
|
{
|
|
JniHelper::callStaticVoidMethod(Cocos2dxRendererClassName, "setAnimationInterval", interval);
|
|
}
|