Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
I think this also fixes the bug relating to non-latin characters in filenames, since UNICODE wasn't defined in SDL_rwops.c
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2010 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 #include "SDL_android.h"
27 #include "events/SDL_events_c.h"
28 #include "video/android/SDL_androidkeyboard.h"
29 #include "video/android/SDL_androidtouch.h"
30 #include "video/android/SDL_androidvideo.h"
32 /* Impelemented in audio/android/SDL_androidaudio.c */
33 extern void Android_RunAudioThread();
36 /*******************************************************************************
37 This file links the Java side of Android with libsdl
38 *******************************************************************************/
40 #include <android/log.h>
43 /*******************************************************************************
45 *******************************************************************************/
46 static JNIEnv* mEnv = NULL;
47 static JNIEnv* mAudioEnv = NULL;
50 static jclass mActivityClass;
53 static jmethodID midCreateGLContext;
54 static jmethodID midFlipBuffers;
55 static jmethodID midAudioInit;
56 static jmethodID midAudioWriteShortBuffer;
57 static jmethodID midAudioWriteByteBuffer;
58 static jmethodID midAudioQuit;
60 // Accelerometer data storage
61 static float fLastAccelerometer[3];
64 /*******************************************************************************
65 Functions called by JNI
66 *******************************************************************************/
69 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
71 return JNI_VERSION_1_4;
74 // Called before SDL_main() to initialize JNI bindings
75 extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
77 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
82 midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
83 "createGLContext","()V");
84 midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
86 midAudioInit = mEnv->GetStaticMethodID(mActivityClass,
87 "audioInit", "(IZZI)Ljava/lang/Object;");
88 midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
89 "audioWriteShortBuffer", "([S)V");
90 midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
91 "audioWriteByteBuffer", "([B)V");
92 midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
95 if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
96 !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
97 __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
102 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
103 JNIEnv* env, jclass jcls,
104 jint width, jint height, jint format)
106 Android_SetScreenResolution(width, height, format);
110 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
111 JNIEnv* env, jclass jcls, jint keycode)
113 Android_OnKeyDown(keycode);
117 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
118 JNIEnv* env, jclass jcls, jint keycode)
120 Android_OnKeyUp(keycode);
124 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
125 JNIEnv* env, jclass jcls,
126 jint action, jfloat x, jfloat y, jfloat p)
128 Android_OnTouch(action, x, y, p);
132 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
133 JNIEnv* env, jclass jcls,
134 jfloat x, jfloat y, jfloat z)
136 fLastAccelerometer[0] = x;
137 fLastAccelerometer[1] = y;
138 fLastAccelerometer[2] = z;
142 extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
143 JNIEnv* env, jclass cls)
145 // Inject a SDL_QUIT event
149 extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
150 JNIEnv* env, jclass cls)
152 /* This is the audio thread, with a different environment */
155 Android_RunAudioThread();
159 /*******************************************************************************
160 Functions called by SDL into Java
161 *******************************************************************************/
162 extern "C" void Android_JNI_CreateContext()
164 mEnv->CallStaticVoidMethod(mActivityClass, midCreateGLContext);
167 extern "C" void Android_JNI_SwapWindow()
169 mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers);
172 extern "C" void Android_JNI_SetActivityTitle(const char *title)
176 mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
178 mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title));
182 extern "C" void Android_JNI_GetAccelerometerValues(float values[3])
185 for (i = 0; i < 3; ++i) {
186 values[i] = fLastAccelerometer[i];
193 static jboolean audioBuffer16Bit = JNI_FALSE;
194 static jboolean audioBufferStereo = JNI_FALSE;
195 static jobject audioBuffer = NULL;
196 static void* audioBufferPinned = NULL;
198 extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
200 int audioBufferFrames;
202 __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
203 audioBuffer16Bit = is16Bit;
204 audioBufferStereo = channelCount > 1;
206 audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
208 if (audioBuffer == NULL) {
209 __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
212 audioBuffer = mEnv->NewGlobalRef(audioBuffer);
214 jboolean isCopy = JNI_FALSE;
215 if (audioBuffer16Bit) {
216 audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
217 audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer);
219 audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
220 audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
222 if (audioBufferStereo) {
223 audioBufferFrames /= 2;
226 return audioBufferFrames;
229 extern "C" void * Android_JNI_GetAudioBuffer()
231 return audioBufferPinned;
234 extern "C" void Android_JNI_WriteAudioBuffer()
236 if (audioBuffer16Bit) {
237 mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
238 mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
240 mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
241 mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
244 /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
247 extern "C" void Android_JNI_CloseAudioDevice()
249 mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit);
252 mEnv->DeleteGlobalRef(audioBuffer);
254 audioBufferPinned = NULL;
258 /* vi: set ts=4 sw=4 expandtab: */