src/main/android/SDL_android_main.c
changeset 9172 985481d7f3a1
parent 9171 3464a9bda1e2
child 9238 3e53bd4510a2
--- a/src/main/android/SDL_android_main.c	Fri Oct 17 23:26:02 2014 +0200
+++ b/src/main/android/SDL_android_main.c	Fri Oct 17 23:36:45 2014 +0200
@@ -17,22 +17,54 @@
 extern void SDL_Android_Init(JNIEnv* env, jclass cls);
 
 /* Start up the SDL app */
-int Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject obj)
+int Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array)
 {
+    int i;
+    int argc;
+    int status;
+
     /* This interface could expand with ABI negotiation, callbacks, etc. */
     SDL_Android_Init(env, cls);
 
     SDL_SetMainReady();
 
-    /* Run the application code! */
+    /* Prepare the arguments. */
+
+    int len = (*env)->GetArrayLength(env, array);
+    char* argv[1 + len + 1];
+    argc = 0;
     /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
        https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
      */
-    int status;
-    char *argv[2];
-    argv[0] = SDL_strdup("app_process");
-    argv[1] = NULL;
-    status = SDL_main(1, argv);
+    argv[argc++] = SDL_strdup("app_process");
+    for (i = 0; i < len; ++i) {
+        const char* utf;
+        char* arg = NULL;
+        jstring string = (*env)->GetObjectArrayElement(env, array, i);
+        if (string) {
+            utf = (*env)->GetStringUTFChars(env, string, 0);
+            if (utf) {
+                arg = SDL_strdup(utf);
+                (*env)->ReleaseStringUTFChars(env, string, utf);
+            }
+        }
+        if (!arg) {
+            arg = SDL_strdup("");
+        }
+        argv[argc++] = arg;
+    }
+    argv[argc] = NULL;
+
+
+    /* Run the application. */
+
+    status = SDL_main(argc, argv);
+
+    /* Release the arguments. */
+
+    for (i = 0; i < argc; ++i) {
+        SDL_free(argv[i]);
+    }
 
     /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
     /* exit(status); */