android/testproject/jni/app-android.cpp
author Paul Hunkin <paul@bieh.net>
Tue, 27 Jul 2010 10:20:22 +0200
changeset 4715 9bb98766eed0
parent 4714 0f05f2f16fad
child 4716 f2c2a33a1a38
permissions -rw-r--r--
Shut down the C application properly on quit instead of crashing in the most horrible way possible
     1 /*******************************************************************************
     2                                Headers
     3 *******************************************************************************/
     4 #include <jni.h>
     5 #include <sys/time.h>
     6 #include <time.h>
     7 #include <android/log.h>
     8 #include <stdint.h>
     9 
    10 #include <stdio.h>
    11 #include <stdlib.h>
    12 #include <math.h>
    13 
    14 #include <pthread.h>
    15 
    16 #include "importgl.h"
    17 #include "egl.h"
    18 
    19 /*******************************************************************************
    20                                Globals
    21 *******************************************************************************/
    22 static long _getTime(void){
    23 	struct timeval  now;
    24 	gettimeofday(&now, NULL);
    25 	return (long)(now.tv_sec*1000 + now.tv_usec/1000);
    26 }
    27 
    28 JNIEnv* mEnv = NULL;
    29 JavaVM* mVM = NULL;
    30 
    31 //Main activity
    32 jclass mActivityInstance;
    33 
    34 //method signatures
    35 jmethodID midCreateGLContext;
    36 jmethodID midFlipBuffers;
    37 
    38 extern "C" int SDL_main();
    39 extern "C" int Android_OnKeyDown(int keycode);
    40 extern "C" int Android_OnKeyUp(int keycode);
    41 extern "C" int SDL_SendQuit();
    42 
    43 //If we're not the active app, don't try to render
    44 bool bRenderingEnabled = false;
    45 
    46 /*******************************************************************************
    47                  Functions called by JNI
    48 *******************************************************************************/	
    49 
    50 extern "C" void Java_org_libsdl_android_SDLActivity_nativeInit( JNIEnv* env, 
    51                                                                 jobject obj ){ 
    52                                                                    
    53 	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native Init");
    54 
    55 	mEnv = env;
    56 
    57 	bRenderingEnabled = true;
    58 
    59     SDL_main();
    60 }
    61 
    62 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved){
    63 
    64     JNIEnv* env = NULL;
    65     jint result = -1;
    66 
    67     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
    68         return result;
    69     }
    70 
    71     mEnv = env;
    72 
    73     __android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: OnLoad");
    74 
    75     jclass cls = mEnv->FindClass ("org/libsdl/android/SDLActivity"); 
    76     mActivityInstance = cls;
    77     midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    78     midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    79 
    80     if(!midCreateGLContext || !midFlipBuffers){
    81         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    82     }else{
    83         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
    84     }
    85     
    86     return JNI_VERSION_1_4;
    87 }
    88 
    89 extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyDown(JNIEnv*  env, 
    90                jobject obj, jint keycode){
    91     
    92     int r = Android_OnKeyDown(keycode);
    93     __android_log_print(ANDROID_LOG_INFO, "SDL", 
    94                         "SDL: native key down %d, %d\n", keycode, r);
    95                         
    96 }
    97 
    98 extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyUp(JNIEnv*  env, 
    99                jobject obj, jint keycode){
   100     
   101     int r = Android_OnKeyUp(keycode);
   102     __android_log_print(ANDROID_LOG_INFO, "SDL", 
   103                         "SDL: native key up %d, %d\n", keycode, r);
   104                         
   105 }
   106 
   107 extern "C" void Java_org_libsdl_android_SDLActivity_onNativeTouch(JNIEnv*  env, 
   108                jobject obj, jint action, jfloat x, jfloat y, jfloat p){
   109 
   110     __android_log_print(ANDROID_LOG_INFO, "SDL", 
   111                         "SDL: native touch event %d @ %f/%f, pressure %f\n", 
   112                         action, x, y, p);
   113                         
   114 }
   115 
   116 extern "C" void Java_org_libsdl_android_SDLActivity_nativeQuit( JNIEnv*  env, 
   117                                                                 jobject obj ){    
   118 
   119     //Stop rendering as we're no longer in the foreground
   120 	bRenderingEnabled = false;
   121 
   122     //Inject a SDL_QUIT event
   123     int r = SDL_SendQuit();
   124 
   125     __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native quit %d", r);        
   126 }
   127 
   128 
   129 
   130 /*******************************************************************************
   131                  Functions called by SDL
   132 *******************************************************************************/
   133 extern "C" void sdl_create_context(){
   134 	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
   135 
   136 	bRenderingEnabled = true;
   137 
   138     mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
   139 }
   140 
   141 extern "C" void sdl_render(){
   142 
   143     if(!bRenderingEnabled){
   144         return;
   145     }
   146 
   147     //When we get here, we've accumulated a full frame    
   148     mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
   149 }
   150