changeset 4715:9bb98766eed0

Shut down the C application properly on quit instead of crashing in the most horrible way possible
author Paul Hunkin <paul@bieh.net>
date Tue, 27 Jul 2010 10:20:22 +0200
parents 0f05f2f16fad
children f2c2a33a1a38
files android/testproject/jni/app-android.cpp android/testproject/jni/lesson05.c android/testproject/src/org/libsdl/android/SDLActivity.java
diffstat 3 files changed, 40 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/android/testproject/jni/app-android.cpp	Tue Jul 27 09:58:17 2010 +0200
+++ b/android/testproject/jni/app-android.cpp	Tue Jul 27 10:20:22 2010 +0200
@@ -38,22 +38,29 @@
 extern "C" int SDL_main();
 extern "C" int Android_OnKeyDown(int keycode);
 extern "C" int Android_OnKeyUp(int keycode);
+extern "C" int SDL_SendQuit();
+
+//If we're not the active app, don't try to render
+bool bRenderingEnabled = false;
 
 /*******************************************************************************
                  Functions called by JNI
 *******************************************************************************/	
 
-extern "C" void Java_org_libsdl_android_SDLActivity_nativeInit( JNIEnv*  env, jobject obj )
-{    
-	__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: NativeInit");
+extern "C" void Java_org_libsdl_android_SDLActivity_nativeInit( JNIEnv* env, 
+                                                                jobject obj ){ 
+                                                                   
+	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native Init");
 
 	mEnv = env;
 
+	bRenderingEnabled = true;
+
     SDL_main();
 }
 
-extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
-{
+extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved){
+
     JNIEnv* env = NULL;
     jint result = -1;
 
@@ -85,6 +92,7 @@
     int r = Android_OnKeyDown(keycode);
     __android_log_print(ANDROID_LOG_INFO, "SDL", 
                         "SDL: native key down %d, %d\n", keycode, r);
+                        
 }
 
 extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyUp(JNIEnv*  env, 
@@ -93,13 +101,28 @@
     int r = Android_OnKeyUp(keycode);
     __android_log_print(ANDROID_LOG_INFO, "SDL", 
                         "SDL: native key up %d, %d\n", keycode, r);
+                        
 }
 
 extern "C" void Java_org_libsdl_android_SDLActivity_onNativeTouch(JNIEnv*  env, 
                jobject obj, jint action, jfloat x, jfloat y, jfloat p){
+
     __android_log_print(ANDROID_LOG_INFO, "SDL", 
                         "SDL: native touch event %d @ %f/%f, pressure %f\n", 
                         action, x, y, p);
+                        
+}
+
+extern "C" void Java_org_libsdl_android_SDLActivity_nativeQuit( JNIEnv*  env, 
+                                                                jobject obj ){    
+
+    //Stop rendering as we're no longer in the foreground
+	bRenderingEnabled = false;
+
+    //Inject a SDL_QUIT event
+    int r = SDL_SendQuit();
+
+    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native quit %d", r);        
 }
 
 
@@ -110,17 +133,18 @@
 extern "C" void sdl_create_context(){
 	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
 
+	bRenderingEnabled = true;
+
     mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
-    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context() return\n");
-
-   // exit(1);
 }
 
 extern "C" void sdl_render(){
 
-    //When we get here, we've accumulated a full frame
-   //__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_render()");
-    
+    if(!bRenderingEnabled){
+        return;
+    }
+
+    //When we get here, we've accumulated a full frame    
     mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
 }
 
--- a/android/testproject/jni/lesson05.c	Tue Jul 27 09:58:17 2010 +0200
+++ b/android/testproject/jni/lesson05.c	Tue Jul 27 10:20:22 2010 +0200
@@ -348,7 +348,7 @@
 	}
     }
 
-   
+    rotation++;
 
     return( TRUE );
 }
@@ -463,6 +463,7 @@
 			case SDL_QUIT:
 			    /* handle quit requests */
 			    done = TRUE;
+			    __android_log_print(ANDROID_LOG_INFO, "SDL","App is shutting down\n");
 			    break;
 			default:
 			    break;
--- a/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 09:58:17 2010 +0200
+++ b/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 10:20:22 2010 +0200
@@ -61,6 +61,7 @@
 
     //C functions we call
     public static native void nativeInit();
+    public static native void nativeQuit();
     public static native void onNativeKeyDown(int keycode);
     public static native void onNativeKeyUp(int keycode);
     public static native void onNativeTouch(int action, float x, 
@@ -69,7 +70,6 @@
 
 
 
-
     //Java functions called from C
     private static void createGLContext(){
         mSurface.initEGL();
@@ -139,6 +139,8 @@
     //Called when we lose the surface
     public void surfaceDestroyed(SurfaceHolder holder) {
         Log.v("SDL","Surface destroyed");
+        
+        SDLActivity.nativeQuit();
     }
 
     //Called when the surface is resized