android/testproject/jni/app-android.cpp
author Paul Hunkin <paul@bieh.net>
Tue, 27 Jul 2010 21:58:18 +0200
changeset 4723 74da47b2f5b7
parent 4722 faa228f7ce5b
child 4724 d86332c0fb9b
permissions -rw-r--r--
More joystick stuff
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@4722
    37
jmethodID midEnableFeature;
paul@4711
    38
paul@4711
    39
extern "C" int SDL_main();
paul@4713
    40
extern "C" int Android_OnKeyDown(int keycode);
paul@4713
    41
extern "C" int Android_OnKeyUp(int keycode);
paul@4716
    42
extern "C" void Android_SetScreenResolution(int width, int height);
paul@4716
    43
extern "C" void Android_OnResize(int width, int height, int format);
paul@4715
    44
extern "C" int SDL_SendQuit();
paul@4722
    45
extern "C" void Android_EnableFeature(int featureid, bool enabled);
paul@4715
    46
paul@4715
    47
//If we're not the active app, don't try to render
paul@4715
    48
bool bRenderingEnabled = false;
paul@4711
    49
paul@4722
    50
//Feature IDs
paul@4723
    51
static const int FEATURE_AUDIO = 1;
paul@4722
    52
static const int FEATURE_ACCEL = 2;
paul@4722
    53
paul@4722
    54
//Accelerometer data storage
paul@4722
    55
float fLastAccelerometer[3];
paul@4722
    56
paul@4711
    57
/*******************************************************************************
paul@4711
    58
                 Functions called by JNI
paul@4711
    59
*******************************************************************************/	
paul@4711
    60
paul@4716
    61
//Library init
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@4723
    79
    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
paul@4711
    80
paul@4722
    81
    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature){
paul@4711
    82
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
paul@4711
    83
    }else{
paul@4711
    84
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
paul@4711
    85
    }
paul@4711
    86
    
paul@4711
    87
    return JNI_VERSION_1_4;
paul@4711
    88
}
paul@4711
    89
paul@4716
    90
//Start up the SDL app
paul@4716
    91
extern "C" void Java_org_libsdl_android_SDLActivity_nativeInit( JNIEnv* env, 
paul@4716
    92
                                                                jobject obj ){ 
paul@4716
    93
                                                                   
paul@4716
    94
	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native Init");
paul@4716
    95
paul@4716
    96
	mEnv = env;
paul@4722
    97
	bRenderingEnabled = true;
paul@4716
    98
paul@4722
    99
	Android_EnableFeature(FEATURE_ACCEL, true);
paul@4716
   100
paul@4716
   101
    SDL_main();
paul@4716
   102
}
paul@4716
   103
paul@4716
   104
//Keydown
paul@4716
   105
extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyDown(JNIEnv* env, 
paul@4713
   106
               jobject obj, jint keycode){
paul@4713
   107
    
paul@4713
   108
    int r = Android_OnKeyDown(keycode);
paul@4714
   109
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
paul@4714
   110
                        "SDL: native key down %d, %d\n", keycode, r);
paul@4715
   111
                        
paul@4713
   112
}
paul@4713
   113
paul@4716
   114
//Keyup
paul@4716
   115
extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyUp(JNIEnv* env, 
paul@4713
   116
               jobject obj, jint keycode){
paul@4713
   117
    
paul@4713
   118
    int r = Android_OnKeyUp(keycode);
paul@4714
   119
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
paul@4714
   120
                        "SDL: native key up %d, %d\n", keycode, r);
paul@4715
   121
                        
paul@4714
   122
}
paul@4714
   123
paul@4716
   124
//Touch
paul@4716
   125
extern "C" void Java_org_libsdl_android_SDLActivity_onNativeTouch(JNIEnv* env, 
paul@4714
   126
               jobject obj, jint action, jfloat x, jfloat y, jfloat p){
paul@4715
   127
paul@4714
   128
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
paul@4714
   129
                        "SDL: native touch event %d @ %f/%f, pressure %f\n", 
paul@4714
   130
                        action, x, y, p);
paul@4716
   131
paul@4716
   132
    //TODO: Pass this off to the SDL multitouch stuff
paul@4715
   133
                        
paul@4715
   134
}
paul@4715
   135
paul@4716
   136
//Quit
paul@4715
   137
extern "C" void Java_org_libsdl_android_SDLActivity_nativeQuit( JNIEnv*  env, 
paul@4715
   138
                                                                jobject obj ){    
paul@4715
   139
paul@4715
   140
    //Stop rendering as we're no longer in the foreground
paul@4715
   141
	bRenderingEnabled = false;
paul@4715
   142
paul@4715
   143
    //Inject a SDL_QUIT event
paul@4715
   144
    int r = SDL_SendQuit();
paul@4715
   145
paul@4715
   146
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native quit %d", r);        
paul@4713
   147
}
paul@4713
   148
paul@4716
   149
//Screen size
paul@4716
   150
extern "C" void Java_org_libsdl_android_SDLActivity_nativeSetScreenSize(
paul@4716
   151
                JNIEnv*  env, jobject obj, jint width, jint height){
paul@4716
   152
paul@4716
   153
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
paul@4716
   154
                        "SDL: Set screen size on init: %d/%d\n", width, height);
paul@4716
   155
    Android_SetScreenResolution(width, height);
paul@4716
   156
                        
paul@4716
   157
}
paul@4716
   158
paul@4716
   159
//Resize
paul@4716
   160
extern "C" void Java_org_libsdl_android_SDLActivity_onNativeResize(
paul@4716
   161
                                        JNIEnv*  env, jobject obj, jint width, 
paul@4716
   162
                                        jint height, jint format){
paul@4716
   163
    Android_OnResize(width, height, format);
paul@4716
   164
}
paul@4716
   165
paul@4722
   166
extern "C" void Java_org_libsdl_android_SDLActivity_onNativeAccel(
paul@4722
   167
                                        JNIEnv*  env, jobject obj,
paul@4722
   168
                                        jfloat x, jfloat y, jfloat z){
paul@4722
   169
    fLastAccelerometer[0] = x;
paul@4722
   170
    fLastAccelerometer[1] = y;
paul@4722
   171
    fLastAccelerometer[2] = z;   
paul@4722
   172
}
paul@4722
   173
paul@4711
   174
paul@4711
   175
paul@4711
   176
/*******************************************************************************
paul@4722
   177
             Functions called by SDL into Java
paul@4711
   178
*******************************************************************************/
paul@4722
   179
extern "C" void Android_CreateContext(){
paul@4711
   180
	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
paul@4711
   181
paul@4715
   182
	bRenderingEnabled = true;
paul@4715
   183
paul@4711
   184
    mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
paul@4711
   185
}
paul@4711
   186
paul@4722
   187
extern "C" void Android_Render(){
paul@4711
   188
paul@4715
   189
    if(!bRenderingEnabled){
paul@4715
   190
        return;
paul@4715
   191
    }
paul@4715
   192
paul@4715
   193
    //When we get here, we've accumulated a full frame    
paul@4711
   194
    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
paul@4711
   195
}
paul@4711
   196
paul@4722
   197
extern "C" void Android_EnableFeature(int featureid, bool enabled){
paul@4722
   198
paul@4723
   199
    mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature, 
paul@4722
   200
                                featureid, (int)enabled); 
paul@4722
   201
}
paul@4722
   202