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