android/testproject/jni/app-android.cpp
author Paul Hunkin <paul@bieh.net>
Tue, 29 Jun 2010 00:40:12 +1200
changeset 4711 ed040b480a9f
child 4712 8319aa8fa4dc
permissions -rw-r--r--
- Restructured threads and application structure.
- Moved to SurfaceView instead of GLSurfaceView
- Moved to C++ for the android library
     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 
    40 /*******************************************************************************
    41                  Functions called by JNI
    42 *******************************************************************************/	
    43 
    44 extern "C" void Java_org_libsdl_android_TestActivity_nativeInit( JNIEnv*  env, jobject obj )
    45 {    
    46 	__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: NativeInit");
    47 
    48 	mEnv = env;
    49 
    50     SDL_main();
    51 }
    52 
    53 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
    54 {
    55     JNIEnv* env = NULL;
    56     jint result = -1;
    57 
    58     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
    59         return result;
    60     }
    61 
    62     mEnv = env;
    63 
    64     __android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: OnLoad");
    65 
    66     jclass cls = mEnv->FindClass ("org/libsdl/android/TestActivity"); 
    67     mActivityInstance = cls;
    68     midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    69     midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    70 
    71     if(!midCreateGLContext || !midFlipBuffers){
    72         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    73     }else{
    74         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
    75     }
    76     
    77     return JNI_VERSION_1_4;
    78 }
    79 
    80 
    81 
    82 /*******************************************************************************
    83                  Functions called by SDL
    84 *******************************************************************************/
    85 extern "C" void sdl_create_context(){
    86 	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
    87 
    88     mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
    89     __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context() return\n");
    90 
    91    // exit(1);
    92 }
    93 
    94 extern "C" void sdl_render(){
    95 
    96     //When we get here, we've accumulated a full frame
    97    //__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_render()");
    98     
    99     mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
   100 }
   101