SDL_android_main.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. JNIEXPORT int JNICALL 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. (*env)->DeleteLocalRef(env, string);
  42. }
  43. if (!arg) {
  44. arg = SDL_strdup("");
  45. }
  46. argv[argc++] = arg;
  47. }
  48. argv[argc] = NULL;
  49. /* Run the application. */
  50. status = SDL_main(argc, argv);
  51. /* Release the arguments. */
  52. for (i = 0; i < argc; ++i) {
  53. SDL_free(argv[i]);
  54. }
  55. /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
  56. /* exit(status); */
  57. return status;
  58. }
  59. #endif /* __ANDROID__ */
  60. /* vi: set ts=4 sw=4 expandtab: */