src/main/android/SDL_android_main.c
changeset 9172 985481d7f3a1
parent 9171 3464a9bda1e2
child 9238 3e53bd4510a2
equal deleted inserted replaced
9171:3464a9bda1e2 9172:985481d7f3a1
    15 
    15 
    16 /* Called before SDL_main() to initialize JNI bindings in SDL library */
    16 /* Called before SDL_main() to initialize JNI bindings in SDL library */
    17 extern void SDL_Android_Init(JNIEnv* env, jclass cls);
    17 extern void SDL_Android_Init(JNIEnv* env, jclass cls);
    18 
    18 
    19 /* Start up the SDL app */
    19 /* Start up the SDL app */
    20 int Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject obj)
    20 int Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array)
    21 {
    21 {
       
    22     int i;
       
    23     int argc;
       
    24     int status;
       
    25 
    22     /* This interface could expand with ABI negotiation, callbacks, etc. */
    26     /* This interface could expand with ABI negotiation, callbacks, etc. */
    23     SDL_Android_Init(env, cls);
    27     SDL_Android_Init(env, cls);
    24 
    28 
    25     SDL_SetMainReady();
    29     SDL_SetMainReady();
    26 
    30 
    27     /* Run the application code! */
    31     /* Prepare the arguments. */
       
    32 
       
    33     int len = (*env)->GetArrayLength(env, array);
       
    34     char* argv[1 + len + 1];
       
    35     argc = 0;
    28     /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
    36     /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
    29        https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
    37        https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
    30      */
    38      */
    31     int status;
    39     argv[argc++] = SDL_strdup("app_process");
    32     char *argv[2];
    40     for (i = 0; i < len; ++i) {
    33     argv[0] = SDL_strdup("app_process");
    41         const char* utf;
    34     argv[1] = NULL;
    42         char* arg = NULL;
    35     status = SDL_main(1, argv);
    43         jstring string = (*env)->GetObjectArrayElement(env, array, i);
       
    44         if (string) {
       
    45             utf = (*env)->GetStringUTFChars(env, string, 0);
       
    46             if (utf) {
       
    47                 arg = SDL_strdup(utf);
       
    48                 (*env)->ReleaseStringUTFChars(env, string, utf);
       
    49             }
       
    50         }
       
    51         if (!arg) {
       
    52             arg = SDL_strdup("");
       
    53         }
       
    54         argv[argc++] = arg;
       
    55     }
       
    56     argv[argc] = NULL;
       
    57 
       
    58 
       
    59     /* Run the application. */
       
    60 
       
    61     status = SDL_main(argc, argv);
       
    62 
       
    63     /* Release the arguments. */
       
    64 
       
    65     for (i = 0; i < argc; ++i) {
       
    66         SDL_free(argv[i]);
       
    67     }
    36 
    68 
    37     /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
    69     /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
    38     /* exit(status); */
    70     /* exit(status); */
    39 
    71 
    40     return status;
    72     return status;