comparison src/SDL_android.cpp @ 4989:58b6bb4a45e9

More Android cleanup: * Formalized the interface with Java methods in SDL_android.h * We don't need the feature system, at least right now * Fixed waiting for the SDLMain thread
author Sam Lantinga <slouken@libsdl.org>
date Wed, 12 Jan 2011 17:53:06 -0800
parents 55b82067815b
children 9f9bea41e88f
comparison
equal deleted inserted replaced
4988:f9af88a9c823 4989:58b6bb4a45e9
19 Sam Lantinga 19 Sam Lantinga
20 slouken@libsdl.org 20 slouken@libsdl.org
21 */ 21 */
22 #include "SDL_config.h" 22 #include "SDL_config.h"
23 23
24 #include "SDL_android.h"
25
24 extern "C" { 26 extern "C" {
25 #include "events/SDL_events_c.h" 27 #include "events/SDL_events_c.h"
26 #include "video/android/SDL_androidkeyboard.h" 28 #include "video/android/SDL_androidkeyboard.h"
27 #include "video/android/SDL_androidvideo.h" 29 #include "video/android/SDL_androidvideo.h"
28 } 30 }
45 jclass mActivityInstance; 47 jclass mActivityInstance;
46 48
47 //method signatures 49 //method signatures
48 jmethodID midCreateGLContext; 50 jmethodID midCreateGLContext;
49 jmethodID midFlipBuffers; 51 jmethodID midFlipBuffers;
50 jmethodID midEnableFeature;
51 jmethodID midUpdateAudio; 52 jmethodID midUpdateAudio;
52
53 //If we're not the active app, don't try to render
54 bool bRenderingEnabled = false;
55 53
56 //Feature IDs 54 //Feature IDs
57 static const int FEATURE_AUDIO = 1; 55 static const int FEATURE_AUDIO = 1;
58 static const int FEATURE_ACCEL = 2; 56 static const int FEATURE_ACCEL = 2;
59 57
82 80
83 jclass cls = mEnv->FindClass ("org/libsdl/app/SDLActivity"); 81 jclass cls = mEnv->FindClass ("org/libsdl/app/SDLActivity");
84 mActivityInstance = cls; 82 mActivityInstance = cls;
85 midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V"); 83 midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
86 midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V"); 84 midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
87 midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
88 midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V"); 85 midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
89 86
90 if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature || 87 if(!midCreateGLContext || !midFlipBuffers || !midUpdateAudio) {
91 !midUpdateAudio) {
92 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n"); 88 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
93 } else { 89 } else {
94 #ifdef DEBUG 90 #ifdef DEBUG
95 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n"); 91 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
96 #endif 92 #endif
134 130
135 // Quit 131 // Quit
136 extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv* env, 132 extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv* env,
137 jobject obj ) 133 jobject obj )
138 { 134 {
139 // Stop rendering as we're no longer in the foreground
140 bRenderingEnabled = false;
141
142 // Inject a SDL_QUIT event 135 // Inject a SDL_QUIT event
143 SDL_SendQuit(); 136 SDL_SendQuit();
144 } 137 }
145 138
146 // Resize 139 // Resize
163 156
164 157
165 /******************************************************************************* 158 /*******************************************************************************
166 Functions called by SDL into Java 159 Functions called by SDL into Java
167 *******************************************************************************/ 160 *******************************************************************************/
168 extern "C" void Android_CreateContext() 161 extern "C" void Android_JNI_CreateContext()
169 { 162 {
170 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n"); 163 mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext);
171 164 }
172 bRenderingEnabled = true; 165
173 166 extern "C" void Android_JNI_SwapWindow()
174 mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 167 {
175 }
176
177 extern "C" void Android_Render()
178 {
179 if (!bRenderingEnabled) {
180 return;
181 }
182
183 // When we get here, we've accumulated a full frame
184 mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers); 168 mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers);
185 } 169 }
186 170
187 extern "C" void Android_EnableFeature(int featureid, bool enabled) 171 extern "C" void Android_JNI_UpdateAudioBuffer(unsigned char *buf, int len)
188 {
189 mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature,
190 featureid, (int)enabled);
191 }
192
193 extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len)
194 { 172 {
195 //Annoyingly we can't just call into Java from any thread. Because the audio 173 //Annoyingly we can't just call into Java from any thread. Because the audio
196 //callback is dispatched from the SDL audio thread (that wasn't made from 174 //callback is dispatched from the SDL audio thread (that wasn't made from
197 //java, we have to do some magic here to let the JVM know about the thread. 175 //java, we have to do some magic here to let the JVM know about the thread.
198 //Because everything it touches on the Java side is static anyway, it's 176 //Because everything it touches on the Java side is static anyway, it's