annotate android/project/jni/android-support.cpp @ 4957:5e42031a8d70

Need to set something valid for argc/argv
author Sam Lantinga <slouken@libsdl.org>
date Tue, 04 Jan 2011 15:25:49 -0800
parents 4f40b11e1045
children
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
4953
4f40b11e1045 Fixed prototype for SDL_main()
Sam Lantinga <slouken@libsdl.org>
parents: 4726
diff changeset
39 extern "C" int SDL_main(int argc, char *argv[]);
4726
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
4957
5e42031a8d70 Need to set something valid for argc/argv
Sam Lantinga <slouken@libsdl.org>
parents: 4953
diff changeset
106 char *argv[2];
5e42031a8d70 Need to set something valid for argc/argv
Sam Lantinga <slouken@libsdl.org>
parents: 4953
diff changeset
107 argv[0] = strdup("SDL_app");
5e42031a8d70 Need to set something valid for argc/argv
Sam Lantinga <slouken@libsdl.org>
parents: 4953
diff changeset
108 argv[1] = NULL;
5e42031a8d70 Need to set something valid for argc/argv
Sam Lantinga <slouken@libsdl.org>
parents: 4953
diff changeset
109 SDL_main(1, argv);
4726
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
110 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
111
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
112 //Keydown
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
113 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(JNIEnv* env,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
114 jobject obj, jint keycode){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
115
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
116 int r = Android_OnKeyDown(keycode);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
117 #ifdef DEBUG
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
118 __android_log_print(ANDROID_LOG_INFO, "SDL",
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
119 "SDL: native key down %d, %d\n", keycode, r);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
120 #endif
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
121
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
122 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
123
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
124 //Keyup
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
125 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(JNIEnv* env,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
126 jobject obj, jint keycode){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
127
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
128 int r = Android_OnKeyUp(keycode);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
129 #ifdef DEBUG
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
130 __android_log_print(ANDROID_LOG_INFO, "SDL",
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
131 "SDL: native key up %d, %d\n", keycode, r);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
132 #endif
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
133
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
134 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
135
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
136 //Touch
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
137 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(JNIEnv* env,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
138 jobject obj, jint action, jfloat x, jfloat y, jfloat p){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
139
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
140 #ifdef DEBUG
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
141 __android_log_print(ANDROID_LOG_INFO, "SDL",
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
142 "SDL: native touch event %d @ %f/%f, pressure %f\n",
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
143 action, x, y, p);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
144 #endif
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
145
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
146 //TODO: Pass this off to the SDL multitouch stuff
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
147
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
148 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
149
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
150 //Quit
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
151 extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv* env,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
152 jobject obj ){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
153
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
154 //Stop rendering as we're no longer in the foreground
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
155 bRenderingEnabled = false;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
156
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
157 //Inject a SDL_QUIT event
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
158 int r = SDL_SendQuit();
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
159
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
160 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native quit %d", r);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
161 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
162
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
163 //Screen size
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
164 extern "C" void Java_org_libsdl_app_SDLActivity_nativeSetScreenSize(
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
165 JNIEnv* env, jobject obj, jint width, jint height){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
166
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
167 __android_log_print(ANDROID_LOG_INFO, "SDL",
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
168 "SDL: Set screen size on init: %d/%d\n", width, height);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
169 Android_SetScreenResolution(width, height);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
170
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
171 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
172
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
173 //Resize
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
174 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
175 JNIEnv* env, jobject obj, jint width,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
176 jint height, jint format){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
177 Android_OnResize(width, height, format);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
178 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
179
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
180 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
181 JNIEnv* env, jobject obj,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
182 jfloat x, jfloat y, jfloat z){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
183 fLastAccelerometer[0] = x;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
184 fLastAccelerometer[1] = y;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
185 fLastAccelerometer[2] = z;
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
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
189
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
190 /*******************************************************************************
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
191 Functions called by SDL into Java
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
192 *******************************************************************************/
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
193 extern "C" void Android_CreateContext(){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
194 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
195
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
196 bRenderingEnabled = true;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
197
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
198 mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext );
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
199 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
200
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
201 extern "C" void Android_Render(){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
202
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
203 if(!bRenderingEnabled){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
204 return;
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
205 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
206
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
207 //When we get here, we've accumulated a full frame
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
208 mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers );
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
209 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
210
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
211 extern "C" void Android_EnableFeature(int featureid, bool enabled){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
212
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
213 mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
214 featureid, (int)enabled);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
215 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
216
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
217 extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
218
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
219 //Annoyingly we can't just call into Java from any thread. Because the audio
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
220 //callback is dispatched from the SDL audio thread (that wasn't made from
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
221 //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
222 //Because everything it touches on the Java side is static anyway, it's
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
223 //not a big deal, just annoying.
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
224 if(!mAudioThreadEnv){
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
225 __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
226
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
227 mVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
228
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
229 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
230 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
231
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
232 jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
233
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
234 //blah. We probably should rework this so we avoid the copy.
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
235 mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
236
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
237 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
238
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
239 mAudioThreadEnv->CallStaticVoidMethod( mActivityInstance,
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
240 midUpdateAudio, arr );
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
241
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
242 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
243
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
244 }
9076cdb027af Cleanups.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
245