Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Shut down the C application properly on quit instead of crashing in t…
Browse files Browse the repository at this point in the history
…he most horrible way possible
  • Loading branch information
bieh committed Jul 27, 2010
1 parent 0c36b9b commit e61a95e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
46 changes: 35 additions & 11 deletions android/testproject/jni/app-android.cpp
Expand Up @@ -38,22 +38,29 @@ jmethodID midFlipBuffers;
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;

Expand Down Expand Up @@ -85,6 +92,7 @@ extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyDown(JNIEnv* env
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,
Expand All @@ -93,13 +101,28 @@ extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyUp(JNIEnv* env,
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);
}


Expand All @@ -110,17 +133,18 @@ extern "C" void Java_org_libsdl_android_SDLActivity_onNativeTouch(JNIEnv* env,
extern "C" void sdl_create_context(){
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");

mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext );
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context() return\n");
bRenderingEnabled = true;

// exit(1);
mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext );
}

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 );
}

3 changes: 2 additions & 1 deletion android/testproject/jni/lesson05.c
Expand Up @@ -348,7 +348,7 @@ int drawGLScene( GLvoid )
}
}


rotation++;

return( TRUE );
}
Expand Down Expand Up @@ -463,6 +463,7 @@ int SDL_main( int argc, char **argv )
case SDL_QUIT:
/* handle quit requests */
done = TRUE;
__android_log_print(ANDROID_LOG_INFO, "SDL","App is shutting down\n");
break;
default:
break;
Expand Down
4 changes: 3 additions & 1 deletion android/testproject/src/org/libsdl/android/SDLActivity.java
Expand Up @@ -61,6 +61,7 @@ protected void onResume() {

//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,
Expand All @@ -69,7 +70,6 @@ public static native void onNativeTouch(int action, float x,




//Java functions called from C
private static void createGLContext(){
mSurface.initEGL();
Expand Down Expand Up @@ -139,6 +139,8 @@ public void surfaceCreated(SurfaceHolder holder) {
//Called when we lose the surface
public void surfaceDestroyed(SurfaceHolder holder) {
Log.v("SDL","Surface destroyed");

SDLActivity.nativeQuit();
}

//Called when the surface is resized
Expand Down

0 comments on commit e61a95e

Please sign in to comment.