SDL_android_main.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. SDL_android_main.c, placed in the public domain by Sam Lantinga 3/13/14
  3. */
  4. #include "../../SDL_internal.h"
  5. #ifdef __ANDROID__
  6. /* Include the SDL main definition header */
  7. #include "SDL_main.h"
  8. /*******************************************************************************
  9. Functions called by JNI
  10. *******************************************************************************/
  11. #include <jni.h>
  12. /* Called before SDL_main() to initialize JNI bindings in SDL library */
  13. extern void SDL_Android_Init(JNIEnv* env, jclass cls);
  14. /* Start up the SDL app */
  15. int Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array)
  16. {
  17. int i;
  18. int argc;
  19. int status;
  20. /* This interface could expand with ABI negotiation, callbacks, etc. */
  21. SDL_Android_Init(env, cls);
  22. SDL_SetMainReady();
  23. /* Prepare the arguments. */
  24. int len = (*env)->GetArrayLength(env, array);
  25. char* argv[1 + len + 1];
  26. argc = 0;
  27. /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
  28. https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
  29. */
  30. argv[argc++] = SDL_strdup("app_process");
  31. for (i = 0; i < len; ++i) {
  32. const char* utf;
  33. char* arg = NULL;
  34. jstring string = (*env)->GetObjectArrayElement(env, array, i);
  35. if (string) {
  36. utf = (*env)->GetStringUTFChars(env, string, 0);
  37. if (utf) {
  38. arg = SDL_strdup(utf);
  39. (*env)->ReleaseStringUTFChars(env, string, utf);
  40. }
  41. }
  42. if (!arg) {
  43. arg = SDL_strdup("");
  44. }
  45. argv[argc++] = arg;
  46. }
  47. argv[argc] = NULL;
  48. /* Run the application. */
  49. status = SDL_main(argc, argv);
  50. /* Release the arguments. */
  51. for (i = 0; i < argc; ++i) {
  52. SDL_free(argv[i]);
  53. }
  54. /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
  55. /* exit(status); */
  56. return status;
  57. }
  58. #endif /* __ANDROID__ */
  59. /* vi: set ts=4 sw=4 expandtab: */