annotate android/project/jni/android-support.cpp @ 4919:716b2cbf4c9e

First pass at Windows multi-touch gesture support
author Sam Lantinga <slouken@libsdl.org>
date Tue, 30 Nov 2010 17:58:51 -0800
parents 9076cdb027af
children 4f40b11e1045
rev   line source
4726
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
1 /*******************************************************************************
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
2 This file links the Java side of Android with libsdl
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
3 *******************************************************************************/
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
4 #include <jni.h>
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
5 #include <sys/time.h>
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
6 #include <time.h>
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
7 #include <android/log.h>
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
8 #include <stdint.h>
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
9 #include <stdio.h>
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
10 #include <stdlib.h>
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
11 #include <math.h>
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
12 #include <pthread.h>
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
13
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
14 #define DEBUG
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
15
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
16
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
17 /*******************************************************************************
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
18 Globals
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
19 *******************************************************************************/
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
20 static long _getTime(void){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
21 struct timeval now;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
22 gettimeofday(&now, NULL);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
23 return (long)(now.tv_sec*1000 + now.tv_usec/1000);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
24 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
25
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
26 JNIEnv* mEnv = NULL;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
27 JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
28 JavaVM* mVM = NULL;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
29
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
30 //Main activity
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
31 jclass mActivityInstance;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
32
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
33 //method signatures
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
34 jmethodID midCreateGLContext;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
35 jmethodID midFlipBuffers;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
36 jmethodID midEnableFeature;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
37 jmethodID midUpdateAudio;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
38
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
39 extern "C" int SDL_main();
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
40 extern "C" int Android_OnKeyDown(int keycode);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
41 extern "C" int Android_OnKeyUp(int keycode);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
42 extern "C" void Android_SetScreenResolution(int width, int height);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
43 extern "C" void Android_OnResize(int width, int height, int format);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
44 extern "C" int SDL_SendQuit();
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
45 extern "C" void Android_EnableFeature(int featureid, bool enabled);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
46
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
47 //If we're not the active app, don't try to render
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
48 bool bRenderingEnabled = false;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
49
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
50 //Feature IDs
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
51 static const int FEATURE_AUDIO = 1;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
52 static const int FEATURE_ACCEL = 2;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
53
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
54 //Accelerometer data storage
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
55 float fLastAccelerometer[3];
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
56
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
57
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
58 /*******************************************************************************
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
59 Functions called by JNI
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
60 *******************************************************************************/
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
61
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
62 //Library init
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
63 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
64
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
65 JNIEnv* env = NULL;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
66 jint result = -1;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
67
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
68 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
69 return result;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
70 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
71
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
72 mEnv = env;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
73
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
74 __android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: OnLoad");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
75
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
76 jclass cls = mEnv->FindClass ("org/libsdl/app/SDLActivity");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
77 mActivityInstance = cls;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
78 midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
79 midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
80 midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
81 midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
82
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
83 if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature ||
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
84 !midUpdateAudio){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
85 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
86 }else{
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
87 #ifdef DEBUG
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
88 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
89 #endif
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
90 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
91
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
92 return JNI_VERSION_1_4;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
93 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
94
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
95 //Start up the SDL app
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
96 extern "C" void Java_org_libsdl_app_SDLActivity_nativeInit( JNIEnv* env,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
97 jobject obj ){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
98
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
99 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native Init");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
100
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
101 mEnv = env;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
102 bRenderingEnabled = true;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
103
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
104 Android_EnableFeature(FEATURE_ACCEL, true);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
105
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
106 SDL_main();
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
107 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
108
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
109 //Keydown
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
110 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(JNIEnv* env,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
111 jobject obj, jint keycode){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
112
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
113 int r = Android_OnKeyDown(keycode);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
114 #ifdef DEBUG
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
115 __android_log_print(ANDROID_LOG_INFO, "SDL",
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
116 "SDL: native key down %d, %d\n", keycode, r);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
117 #endif
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
118
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
119 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
120
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
121 //Keyup
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
122 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(JNIEnv* env,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
123 jobject obj, jint keycode){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
124
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
125 int r = Android_OnKeyUp(keycode);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
126 #ifdef DEBUG
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
127 __android_log_print(ANDROID_LOG_INFO, "SDL",
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
128 "SDL: native key up %d, %d\n", keycode, r);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
129 #endif
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
130
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
131 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
132
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
133 //Touch
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
134 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(JNIEnv* env,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
135 jobject obj, jint action, jfloat x, jfloat y, jfloat p){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
136
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
137 #ifdef DEBUG
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
138 __android_log_print(ANDROID_LOG_INFO, "SDL",
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
139 "SDL: native touch event %d @ %f/%f, pressure %f\n",
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
140 action, x, y, p);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
141 #endif
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
142
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
143 //TODO: Pass this off to the SDL multitouch stuff
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
144
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
145 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
146
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
147 //Quit
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
148 extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv* env,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
149 jobject obj ){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
150
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
151 //Stop rendering as we're no longer in the foreground
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
152 bRenderingEnabled = false;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
153
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
154 //Inject a SDL_QUIT event
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
155 int r = SDL_SendQuit();
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
156
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
157 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native quit %d", r);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
158 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
159
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
160 //Screen size
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
161 extern "C" void Java_org_libsdl_app_SDLActivity_nativeSetScreenSize(
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
162 JNIEnv* env, jobject obj, jint width, jint height){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
163
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
164 __android_log_print(ANDROID_LOG_INFO, "SDL",
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
165 "SDL: Set screen size on init: %d/%d\n", width, height);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
166 Android_SetScreenResolution(width, height);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
167
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
168 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
169
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
170 //Resize
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
171 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
172 JNIEnv* env, jobject obj, jint width,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
173 jint height, jint format){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
174 Android_OnResize(width, height, format);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
175 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
176
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
177 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
178 JNIEnv* env, jobject obj,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
179 jfloat x, jfloat y, jfloat z){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
180 fLastAccelerometer[0] = x;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
181 fLastAccelerometer[1] = y;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
182 fLastAccelerometer[2] = z;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
183 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
184
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
185
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
186
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
187 /*******************************************************************************
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
188 Functions called by SDL into Java
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
189 *******************************************************************************/
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
190 extern "C" void Android_CreateContext(){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
191 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
192
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
193 bRenderingEnabled = true;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
194
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
195 mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext );
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
196 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
197
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
198 extern "C" void Android_Render(){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
199
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
200 if(!bRenderingEnabled){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
201 return;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
202 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
203
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
204 //When we get here, we've accumulated a full frame
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
205 mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers );
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
206 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
207
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
208 extern "C" void Android_EnableFeature(int featureid, bool enabled){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
209
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
210 mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
211 featureid, (int)enabled);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
212 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
213
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
214 extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
215
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
216 //Annoyingly we can't just call into Java from any thread. Because the audio
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
217 //callback is dispatched from the SDL audio thread (that wasn't made from
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
218 //java, we have to do some magic here to let the JVM know about the thread.
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
219 //Because everything it touches on the Java side is static anyway, it's
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
220 //not a big deal, just annoying.
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
221 if(!mAudioThreadEnv){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
222 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Need to set up audio thread env\n");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
223
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
224 mVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
225
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
226 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
227 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
228
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
229 jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
230
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
231 //blah. We probably should rework this so we avoid the copy.
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
232 mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
233
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
234 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
235
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
236 mAudioThreadEnv->CallStaticVoidMethod( mActivityInstance,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
237 midUpdateAudio, arr );
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
238
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
239 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
240
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
241 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
242