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