android/testproject/jni/app-android.cpp
changeset 4724 d86332c0fb9b
parent 4723 74da47b2f5b7
child 4726 9076cdb027af
equal deleted inserted replaced
4723:74da47b2f5b7 4724:d86332c0fb9b
    24 	gettimeofday(&now, NULL);
    24 	gettimeofday(&now, NULL);
    25 	return (long)(now.tv_sec*1000 + now.tv_usec/1000);
    25 	return (long)(now.tv_sec*1000 + now.tv_usec/1000);
    26 }
    26 }
    27 
    27 
    28 JNIEnv* mEnv = NULL;
    28 JNIEnv* mEnv = NULL;
       
    29 JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
    29 JavaVM* mVM = NULL;
    30 JavaVM* mVM = NULL;
    30 
    31 
    31 //Main activity
    32 //Main activity
    32 jclass mActivityInstance;
    33 jclass mActivityInstance;
    33 
    34 
    34 //method signatures
    35 //method signatures
    35 jmethodID midCreateGLContext;
    36 jmethodID midCreateGLContext;
    36 jmethodID midFlipBuffers;
    37 jmethodID midFlipBuffers;
    37 jmethodID midEnableFeature;
    38 jmethodID midEnableFeature;
       
    39 jmethodID midUpdateAudio;
    38 
    40 
    39 extern "C" int SDL_main();
    41 extern "C" int SDL_main();
    40 extern "C" int Android_OnKeyDown(int keycode);
    42 extern "C" int Android_OnKeyDown(int keycode);
    41 extern "C" int Android_OnKeyUp(int keycode);
    43 extern "C" int Android_OnKeyUp(int keycode);
    42 extern "C" void Android_SetScreenResolution(int width, int height);
    44 extern "C" void Android_SetScreenResolution(int width, int height);
    52 static const int FEATURE_ACCEL = 2;
    54 static const int FEATURE_ACCEL = 2;
    53 
    55 
    54 //Accelerometer data storage
    56 //Accelerometer data storage
    55 float fLastAccelerometer[3];
    57 float fLastAccelerometer[3];
    56 
    58 
       
    59 
    57 /*******************************************************************************
    60 /*******************************************************************************
    58                  Functions called by JNI
    61                  Functions called by JNI
    59 *******************************************************************************/	
    62 *******************************************************************************/	
    60 
    63 
    61 //Library init
    64 //Library init
    75     jclass cls = mEnv->FindClass ("org/libsdl/android/SDLActivity"); 
    78     jclass cls = mEnv->FindClass ("org/libsdl/android/SDLActivity"); 
    76     mActivityInstance = cls;
    79     mActivityInstance = cls;
    77     midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    80     midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    78     midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    81     midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    79     midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
    82     midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
    80 
    83     midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
    81     if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature){
    84 
       
    85     if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature ||
       
    86         !midUpdateAudio){
    82         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    87         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    83     }else{
    88     }else{
    84         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
    89         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
    85     }
    90     }
    86     
    91     
   198 
   203 
   199     mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature, 
   204     mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature, 
   200                                 featureid, (int)enabled); 
   205                                 featureid, (int)enabled); 
   201 }
   206 }
   202 
   207 
       
   208 extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len){
       
   209 
       
   210     //Annoyingly we can't just call into Java from any thread. Because the audio
       
   211     //callback is dispatched from the SDL audio thread (that wasn't made from
       
   212     //java, we have to do some magic here to let the JVM know about the thread.
       
   213     //Because everything it touches on the Java side is static anyway, it's 
       
   214     //not a big deal, just annoying.
       
   215     if(!mAudioThreadEnv){
       
   216         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Need to set up audio thread env\n");
       
   217 
       
   218         mJVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
       
   219 
       
   220         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
       
   221     }
       
   222     
       
   223     jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
       
   224 
       
   225     //blah. We probably should rework this so we avoid the copy. 
       
   226     mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
       
   227     
       
   228     __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
       
   229 
       
   230     mAudioThreadEnv->CallStaticVoidMethod(  mActivityInstance, 
       
   231                                             midUpdateAudio, arr );
       
   232 
       
   233     __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
       
   234     
       
   235 }
       
   236