Shut down the C application properly on quit instead of crashing in the most horrible way possible
authorPaul Hunkin <paul@bieh.net>
Tue, 27 Jul 2010 10:20:22 +0200
changeset 47159bb98766eed0
parent 4714 0f05f2f16fad
child 4716 f2c2a33a1a38
Shut down the C application properly on quit instead of crashing in the most horrible way possible
android/testproject/jni/app-android.cpp
android/testproject/jni/lesson05.c
android/testproject/src/org/libsdl/android/SDLActivity.java
     1.1 --- a/android/testproject/jni/app-android.cpp	Tue Jul 27 09:58:17 2010 +0200
     1.2 +++ b/android/testproject/jni/app-android.cpp	Tue Jul 27 10:20:22 2010 +0200
     1.3 @@ -38,22 +38,29 @@
     1.4  extern "C" int SDL_main();
     1.5  extern "C" int Android_OnKeyDown(int keycode);
     1.6  extern "C" int Android_OnKeyUp(int keycode);
     1.7 +extern "C" int SDL_SendQuit();
     1.8 +
     1.9 +//If we're not the active app, don't try to render
    1.10 +bool bRenderingEnabled = false;
    1.11  
    1.12  /*******************************************************************************
    1.13                   Functions called by JNI
    1.14  *******************************************************************************/	
    1.15  
    1.16 -extern "C" void Java_org_libsdl_android_SDLActivity_nativeInit( JNIEnv*  env, jobject obj )
    1.17 -{    
    1.18 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: NativeInit");
    1.19 +extern "C" void Java_org_libsdl_android_SDLActivity_nativeInit( JNIEnv* env, 
    1.20 +                                                                jobject obj ){ 
    1.21 +                                                                   
    1.22 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native Init");
    1.23  
    1.24  	mEnv = env;
    1.25  
    1.26 +	bRenderingEnabled = true;
    1.27 +
    1.28      SDL_main();
    1.29  }
    1.30  
    1.31 -extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
    1.32 -{
    1.33 +extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved){
    1.34 +
    1.35      JNIEnv* env = NULL;
    1.36      jint result = -1;
    1.37  
    1.38 @@ -85,6 +92,7 @@
    1.39      int r = Android_OnKeyDown(keycode);
    1.40      __android_log_print(ANDROID_LOG_INFO, "SDL", 
    1.41                          "SDL: native key down %d, %d\n", keycode, r);
    1.42 +                        
    1.43  }
    1.44  
    1.45  extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyUp(JNIEnv*  env, 
    1.46 @@ -93,13 +101,28 @@
    1.47      int r = Android_OnKeyUp(keycode);
    1.48      __android_log_print(ANDROID_LOG_INFO, "SDL", 
    1.49                          "SDL: native key up %d, %d\n", keycode, r);
    1.50 +                        
    1.51  }
    1.52  
    1.53  extern "C" void Java_org_libsdl_android_SDLActivity_onNativeTouch(JNIEnv*  env, 
    1.54                 jobject obj, jint action, jfloat x, jfloat y, jfloat p){
    1.55 +
    1.56      __android_log_print(ANDROID_LOG_INFO, "SDL", 
    1.57                          "SDL: native touch event %d @ %f/%f, pressure %f\n", 
    1.58                          action, x, y, p);
    1.59 +                        
    1.60 +}
    1.61 +
    1.62 +extern "C" void Java_org_libsdl_android_SDLActivity_nativeQuit( JNIEnv*  env, 
    1.63 +                                                                jobject obj ){    
    1.64 +
    1.65 +    //Stop rendering as we're no longer in the foreground
    1.66 +	bRenderingEnabled = false;
    1.67 +
    1.68 +    //Inject a SDL_QUIT event
    1.69 +    int r = SDL_SendQuit();
    1.70 +
    1.71 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native quit %d", r);        
    1.72  }
    1.73  
    1.74  
    1.75 @@ -110,17 +133,18 @@
    1.76  extern "C" void sdl_create_context(){
    1.77  	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
    1.78  
    1.79 +	bRenderingEnabled = true;
    1.80 +
    1.81      mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
    1.82 -    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context() return\n");
    1.83 -
    1.84 -   // exit(1);
    1.85  }
    1.86  
    1.87  extern "C" void sdl_render(){
    1.88  
    1.89 -    //When we get here, we've accumulated a full frame
    1.90 -   //__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_render()");
    1.91 -    
    1.92 +    if(!bRenderingEnabled){
    1.93 +        return;
    1.94 +    }
    1.95 +
    1.96 +    //When we get here, we've accumulated a full frame    
    1.97      mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
    1.98  }
    1.99  
     2.1 --- a/android/testproject/jni/lesson05.c	Tue Jul 27 09:58:17 2010 +0200
     2.2 +++ b/android/testproject/jni/lesson05.c	Tue Jul 27 10:20:22 2010 +0200
     2.3 @@ -348,7 +348,7 @@
     2.4  	}
     2.5      }
     2.6  
     2.7 -   
     2.8 +    rotation++;
     2.9  
    2.10      return( TRUE );
    2.11  }
    2.12 @@ -463,6 +463,7 @@
    2.13  			case SDL_QUIT:
    2.14  			    /* handle quit requests */
    2.15  			    done = TRUE;
    2.16 +			    __android_log_print(ANDROID_LOG_INFO, "SDL","App is shutting down\n");
    2.17  			    break;
    2.18  			default:
    2.19  			    break;
     3.1 --- a/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 09:58:17 2010 +0200
     3.2 +++ b/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 10:20:22 2010 +0200
     3.3 @@ -61,6 +61,7 @@
     3.4  
     3.5      //C functions we call
     3.6      public static native void nativeInit();
     3.7 +    public static native void nativeQuit();
     3.8      public static native void onNativeKeyDown(int keycode);
     3.9      public static native void onNativeKeyUp(int keycode);
    3.10      public static native void onNativeTouch(int action, float x, 
    3.11 @@ -69,7 +70,6 @@
    3.12  
    3.13  
    3.14  
    3.15 -
    3.16      //Java functions called from C
    3.17      private static void createGLContext(){
    3.18          mSurface.initEGL();
    3.19 @@ -139,6 +139,8 @@
    3.20      //Called when we lose the surface
    3.21      public void surfaceDestroyed(SurfaceHolder holder) {
    3.22          Log.v("SDL","Surface destroyed");
    3.23 +        
    3.24 +        SDLActivity.nativeQuit();
    3.25      }
    3.26  
    3.27      //Called when the surface is resized