You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-05 12:10:55 +00:00
Cleanup Android C++ code
This commit is contained in:
@@ -51,13 +51,12 @@
|
||||
#include <android/input.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static JavaClassWrapper *java_class_wrapper = NULL;
|
||||
static OS_Android *os_android = NULL;
|
||||
static AndroidInputHandler *input_handler = NULL;
|
||||
static GodotJavaWrapper *godot_java = NULL;
|
||||
static GodotIOJavaWrapper *godot_io_java = NULL;
|
||||
static JavaClassWrapper *java_class_wrapper = nullptr;
|
||||
static OS_Android *os_android = nullptr;
|
||||
static AndroidInputHandler *input_handler = nullptr;
|
||||
static GodotJavaWrapper *godot_java = nullptr;
|
||||
static GodotIOJavaWrapper *godot_io_java = nullptr;
|
||||
|
||||
static bool initialized = false;
|
||||
static SafeNumeric<int> step; // Shared between UI and render threads
|
||||
|
||||
static Size2 new_size;
|
||||
@@ -120,8 +119,6 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHei
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *env, jclass clazz, jobject activity, jobject godot_instance, jobject p_asset_manager, jboolean p_use_apk_expansion) {
|
||||
initialized = true;
|
||||
|
||||
JavaVM *jvm;
|
||||
env->GetJavaVM(&jvm);
|
||||
|
||||
@@ -140,9 +137,6 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *en
|
||||
|
||||
os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);
|
||||
|
||||
char wd[500];
|
||||
getcwd(wd, 500);
|
||||
|
||||
godot_java->on_video_init(env);
|
||||
}
|
||||
|
||||
@@ -170,21 +164,21 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ondestroy(JNIEnv *env
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jclass clazz, jobjectArray p_cmdline) {
|
||||
setup_android_thread();
|
||||
|
||||
const char **cmdline = NULL;
|
||||
jstring *j_cmdline = NULL;
|
||||
const char **cmdline = nullptr;
|
||||
jstring *j_cmdline = nullptr;
|
||||
int cmdlen = 0;
|
||||
if (p_cmdline) {
|
||||
cmdlen = env->GetArrayLength(p_cmdline);
|
||||
if (cmdlen) {
|
||||
cmdline = (const char **)memalloc((cmdlen + 1) * sizeof(const char *));
|
||||
ERR_FAIL_NULL_MSG(cmdline, "Out of memory.");
|
||||
cmdline[cmdlen] = NULL;
|
||||
cmdline[cmdlen] = nullptr;
|
||||
j_cmdline = (jstring *)memalloc(cmdlen * sizeof(jstring));
|
||||
ERR_FAIL_NULL_MSG(j_cmdline, "Out of memory.");
|
||||
|
||||
for (int i = 0; i < cmdlen; i++) {
|
||||
jstring string = (jstring)env->GetObjectArrayElement(p_cmdline, i);
|
||||
const char *rawString = env->GetStringUTFChars(string, 0);
|
||||
const char *rawString = env->GetStringUTFChars(string, nullptr);
|
||||
|
||||
cmdline[i] = rawString;
|
||||
j_cmdline[i] = string;
|
||||
@@ -213,8 +207,9 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jc
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_resize(JNIEnv *env, jclass clazz, jint width, jint height) {
|
||||
if (os_android)
|
||||
if (os_android) {
|
||||
os_android->set_display_size(Size2(width, height));
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *env, jclass clazz) {
|
||||
@@ -232,18 +227,18 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *en
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_back(JNIEnv *env, jclass clazz) {
|
||||
if (step.get() == 0)
|
||||
if (step.get() == 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
if (os_android->get_main_loop()) {
|
||||
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_GO_BACK_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, jclass clazz) {
|
||||
if (step.get() == -1)
|
||||
if (step.get() == -1) {
|
||||
return true;
|
||||
|
||||
}
|
||||
if (step.get() == 0) {
|
||||
// Since Godot is initialized on the UI thread, _main_thread_id was set to that thread's id,
|
||||
// but for Godot purposes, the main thread is the one running the game loop
|
||||
@@ -278,9 +273,9 @@ JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env,
|
||||
}
|
||||
|
||||
void touch_preprocessing(JNIEnv *env, jclass clazz, jint input_device, jint ev, jint pointer, jint pointer_count, jfloatArray positions, jint buttons_mask, jfloat vertical_factor, jfloat horizontal_factor) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
Vector<AndroidInputHandler::TouchPos> points;
|
||||
for (int i = 0; i < pointer_count; i++) {
|
||||
jfloat p[3];
|
||||
@@ -315,33 +310,33 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_touch__IIII_3FIFF(JNI
|
||||
|
||||
// Called on the UI thread
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_hover(JNIEnv *env, jclass clazz, jint p_type, jfloat p_x, jfloat p_y) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
input_handler->process_hover(p_type, Point2(p_x, p_y));
|
||||
}
|
||||
|
||||
// Called on the UI thread
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_doubleTap(JNIEnv *env, jclass clazz, jint p_button_mask, jint p_x, jint p_y) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
input_handler->process_double_tap(p_button_mask, Point2(p_x, p_y));
|
||||
}
|
||||
|
||||
// Called on the UI thread
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_scroll(JNIEnv *env, jclass clazz, jint p_x, jint p_y) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
input_handler->process_scroll(Point2(p_x, p_y));
|
||||
}
|
||||
|
||||
// Called on the UI thread
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env, jclass clazz, jint p_device, jint p_button, jboolean p_pressed) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
AndroidInputHandler::JoypadEvent jevent;
|
||||
jevent.device = p_device;
|
||||
jevent.type = AndroidInputHandler::JOY_EVENT_BUTTON;
|
||||
@@ -353,9 +348,9 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env
|
||||
|
||||
// Called on the UI thread
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env, jclass clazz, jint p_device, jint p_axis, jfloat p_value) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
AndroidInputHandler::JoypadEvent jevent;
|
||||
jevent.device = p_device;
|
||||
jevent.type = AndroidInputHandler::JOY_EVENT_AXIS;
|
||||
@@ -367,24 +362,26 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env,
|
||||
|
||||
// Called on the UI thread
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, jclass clazz, jint p_device, jint p_hat_x, jint p_hat_y) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
AndroidInputHandler::JoypadEvent jevent;
|
||||
jevent.device = p_device;
|
||||
jevent.type = AndroidInputHandler::JOY_EVENT_HAT;
|
||||
int hat = 0;
|
||||
if (p_hat_x != 0) {
|
||||
if (p_hat_x < 0)
|
||||
if (p_hat_x < 0) {
|
||||
hat |= InputDefault::HAT_MASK_LEFT;
|
||||
else
|
||||
} else {
|
||||
hat |= InputDefault::HAT_MASK_RIGHT;
|
||||
}
|
||||
}
|
||||
if (p_hat_y != 0) {
|
||||
if (p_hat_y < 0)
|
||||
if (p_hat_y < 0) {
|
||||
hat |= InputDefault::HAT_MASK_UP;
|
||||
else
|
||||
} else {
|
||||
hat |= InputDefault::HAT_MASK_DOWN;
|
||||
}
|
||||
}
|
||||
jevent.hat = hat;
|
||||
|
||||
@@ -393,18 +390,18 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, j
|
||||
|
||||
// Called on the UI thread
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jclass clazz, jint p_device, jboolean p_connected, jstring p_name) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
String name = jstring_to_string(p_name, env);
|
||||
input_handler->joy_connection_changed(p_device, p_connected, name);
|
||||
}
|
||||
|
||||
// Called on the UI thread
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_key(JNIEnv *env, jclass clazz, jint p_keycode, jint p_scancode, jint p_unicode_char, jboolean p_pressed) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
input_handler->process_key_event(p_keycode, p_scancode, p_unicode_char, p_pressed);
|
||||
}
|
||||
|
||||
@@ -425,16 +422,16 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jclass clazz) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
os_android->main_loop_focusin();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusout(JNIEnv *env, jclass clazz) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
os_android->main_loop_focusout();
|
||||
}
|
||||
|
||||
@@ -446,7 +443,7 @@ JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_callobject(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
|
||||
Object *obj = ObjectDB::get_instance(ID);
|
||||
ERR_FAIL_COND(!obj);
|
||||
ERR_FAIL_NULL(obj);
|
||||
|
||||
int res = env->PushLocalFrame(16);
|
||||
ERR_FAIL_COND(res != 0);
|
||||
@@ -457,26 +454,26 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_callobject(JNIEnv *en
|
||||
Variant *vlist = (Variant *)alloca(sizeof(Variant) * count);
|
||||
Variant **vptr = (Variant **)alloca(sizeof(Variant *) * count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
jobject obj = env->GetObjectArrayElement(params, i);
|
||||
jobject jobj = env->GetObjectArrayElement(params, i);
|
||||
Variant v;
|
||||
if (obj)
|
||||
v = _jobject_to_variant(env, obj);
|
||||
if (jobj) {
|
||||
v = _jobject_to_variant(env, jobj);
|
||||
}
|
||||
memnew_placement(&vlist[i], Variant);
|
||||
vlist[i] = v;
|
||||
vptr[i] = &vlist[i];
|
||||
env->DeleteLocalRef(obj);
|
||||
};
|
||||
env->DeleteLocalRef(jobj);
|
||||
}
|
||||
|
||||
Variant::CallError err;
|
||||
obj->call(str_method, (const Variant **)vptr, count, err);
|
||||
// something
|
||||
|
||||
env->PopLocalFrame(NULL);
|
||||
env->PopLocalFrame(nullptr);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_calldeferred(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
|
||||
Object *obj = ObjectDB::get_instance(ID);
|
||||
ERR_FAIL_COND(!obj);
|
||||
ERR_FAIL_NULL(obj);
|
||||
|
||||
int res = env->PushLocalFrame(16);
|
||||
ERR_FAIL_COND(res != 0);
|
||||
@@ -487,16 +484,17 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_calldeferred(JNIEnv *
|
||||
Variant args[VARIANT_ARG_MAX];
|
||||
|
||||
for (int i = 0; i < MIN(count, VARIANT_ARG_MAX); i++) {
|
||||
jobject obj = env->GetObjectArrayElement(params, i);
|
||||
if (obj)
|
||||
args[i] = _jobject_to_variant(env, obj);
|
||||
env->DeleteLocalRef(obj);
|
||||
};
|
||||
jobject jobj = env->GetObjectArrayElement(params, i);
|
||||
if (jobj) {
|
||||
args[i] = _jobject_to_variant(env, jobj);
|
||||
}
|
||||
env->DeleteLocalRef(jobj);
|
||||
}
|
||||
|
||||
static_assert(VARIANT_ARG_MAX == 8, "This code needs to be updated if VARIANT_ARG_MAX != 8");
|
||||
obj->call_deferred(str_method, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
|
||||
// something
|
||||
env->PopLocalFrame(NULL);
|
||||
|
||||
env->PopLocalFrame(nullptr);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_requestPermissionResult(JNIEnv *env, jclass clazz, jstring p_permission, jboolean p_result) {
|
||||
@@ -511,9 +509,9 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_requestPermissionResu
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNIEnv *env, jclass clazz) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
// We force redraw to ensure we render at least once when resuming the app.
|
||||
Main::force_redraw();
|
||||
if (os_android->get_main_loop()) {
|
||||
@@ -522,9 +520,9 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNI
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIEnv *env, jclass clazz) {
|
||||
if (step.get() <= 0)
|
||||
if (step.get() <= 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
if (os_android->get_main_loop()) {
|
||||
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APP_PAUSED);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user