3dphotoshoot
diff src/android/amain.c @ 1:7eb73ce46dd0
fucking jni man ... wtf
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 14 May 2015 23:46:56 +0300 |
parents | a4bf2687e406 |
children | cf5964db7ff3 |
line diff
1.1 --- a/src/android/amain.c Tue May 12 05:31:21 2015 +0300 1.2 +++ b/src/android/amain.c Thu May 14 23:46:56 2015 +0300 1.3 @@ -1,7 +1,8 @@ 1.4 #include <stdio.h> 1.5 #include <stdlib.h> 1.6 #include <EGL/egl.h> 1.7 -#include "android_native_app_glue.h" 1.8 +#include <jni.h> 1.9 +#include "native_glue.h" 1.10 #include "logger.h" 1.11 #include "game.h" 1.12 #include "timer.h" 1.13 @@ -21,6 +22,28 @@ 1.14 static int win_width, win_height; 1.15 static int init_done; 1.16 1.17 +static JavaVM *jvm; 1.18 +static JNIEnv *jni; 1.19 +static jclass activity_class; 1.20 + 1.21 +static int call_foo(const char *s, int n) 1.22 +{ 1.23 + jmethodID foo_id; 1.24 + jstring jstr; 1.25 + int res; 1.26 + 1.27 + if(!(foo_id = (*jni)->GetStaticMethodID(jni, activity_class, "foo", "(Ljava/lang/String;I)V"))) { 1.28 + fprintf(stderr, "static method foo not found\n"); 1.29 + return -1; 1.30 + } 1.31 + 1.32 + jstr = (*jni)->NewStringUTF(jni, s); 1.33 + res = (*jni)->CallStaticIntMethod(jni, activity_class, foo_id, jstr, n); 1.34 + (*jni)->DeleteLocalRef(jni, jstr); 1.35 + 1.36 + return res; 1.37 +} 1.38 + 1.39 void android_main(struct android_app *app_ptr) 1.40 { 1.41 app_dummy(); 1.42 @@ -31,6 +54,15 @@ 1.43 1.44 start_logger(); 1.45 1.46 + jvm = app->activity->vm; 1.47 + if((*jvm)->AttachCurrentThread(jvm, &jni, 0) != 0) { 1.48 + fprintf(stderr, "failed to attach native thread to Java VM\n"); 1.49 + exit(1); 1.50 + } 1.51 + activity_class = app->activity_class; 1.52 + 1.53 + printf("JNI call returned: %d\n", call_foo("testing C->java jni calls", 42)); 1.54 + 1.55 for(;;) { 1.56 int num_events; 1.57 struct android_poll_source *pollsrc; 1.58 @@ -42,6 +74,7 @@ 1.59 } 1.60 1.61 if(app->destroyRequested) { 1.62 + (*jvm)->DetachCurrentThread(jvm); 1.63 return; 1.64 } 1.65