android/testproject/jni/app-android.cpp
author Paul Hunkin <paul@bieh.net>
Tue, 27 Jul 2010 09:58:17 +0200
changeset 4714 0f05f2f16fad
parent 4713 ba38983b10c2
child 4715 9bb98766eed0
permissions -rw-r--r--
- Modified build system
- Initial support for touch and key events
     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 
    42 /*******************************************************************************
    43                  Functions called by JNI
    44 *******************************************************************************/	
    45 
    46 extern "C" void Java_org_libsdl_android_SDLActivity_nativeInit( JNIEnv*  env, jobject obj )
    47 {    
    48 	__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: NativeInit");
    49 
    50 	mEnv = env;
    51 
    52     SDL_main();
    53 }
    54 
    55 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
    56 {
    57     JNIEnv* env = NULL;
    58     jint result = -1;
    59 
    60     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
    61         return result;
    62     }
    63 
    64     mEnv = env;
    65 
    66     __android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: OnLoad");
    67 
    68     jclass cls = mEnv->FindClass ("org/libsdl/android/SDLActivity"); 
    69     mActivityInstance = cls;
    70     midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    71     midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    72 
    73     if(!midCreateGLContext || !midFlipBuffers){
    74         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    75     }else{
    76         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
    77     }
    78     
    79     return JNI_VERSION_1_4;
    80 }
    81 
    82 extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyDown(JNIEnv*  env, 
    83                jobject obj, jint keycode){
    84     
    85     int r = Android_OnKeyDown(keycode);
    86     __android_log_print(ANDROID_LOG_INFO, "SDL", 
    87                         "SDL: native key down %d, %d\n", keycode, r);
    88 }
    89 
    90 extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyUp(JNIEnv*  env, 
    91                jobject obj, jint keycode){
    92     
    93     int r = Android_OnKeyUp(keycode);
    94     __android_log_print(ANDROID_LOG_INFO, "SDL", 
    95                         "SDL: native key up %d, %d\n", keycode, r);
    96 }
    97 
    98 extern "C" void Java_org_libsdl_android_SDLActivity_onNativeTouch(JNIEnv*  env, 
    99                jobject obj, jint action, jfloat x, jfloat y, jfloat p){
   100     __android_log_print(ANDROID_LOG_INFO, "SDL", 
   101                         "SDL: native touch event %d @ %f/%f, pressure %f\n", 
   102                         action, x, y, p);
   103 }
   104 
   105 
   106 
   107 /*******************************************************************************
   108                  Functions called by SDL
   109 *******************************************************************************/
   110 extern "C" void sdl_create_context(){
   111 	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
   112 
   113     mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
   114     __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context() return\n");
   115 
   116    // exit(1);
   117 }
   118 
   119 extern "C" void sdl_render(){
   120 
   121     //When we get here, we've accumulated a full frame
   122    //__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_render()");
   123     
   124     mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
   125 }
   126