Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Shut down the C application properly on quit instead of crashing in t…

…he most horrible way possible
  • Loading branch information
bieh committed Jul 27, 2010
1 parent 0c36b9b commit e61a95e7a0e299857d62adac6ff614ca87ce5dd0
@@ -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;

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


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

@@ -348,7 +348,7 @@ int drawGLScene( GLvoid )
}
}


rotation++;

return( TRUE );
}
@@ -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;
@@ -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,
@@ -69,7 +70,6 @@ public static native void onNativeTouch(int action, float x,




//Java functions called from C
private static void createGLContext(){
mSurface.initEGL();
@@ -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

0 comments on commit e61a95e

Please sign in to comment.