author | Xeli |
Sat, 12 Nov 2011 17:06:49 +0100 | |
branch | hedgeroid |
changeset 6328 | d14adf1c7721 |
parent 6047 | 10011f051f9c |
child 6592 | cf12f07d6f24 |
permissions | -rw-r--r-- |
5337 | 1 |
/* |
2 |
SDL - Simple DirectMedia Layer |
|
3 |
Copyright (C) 1997-2011 Sam Lantinga |
|
4 |
||
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. |
|
9 |
||
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. |
|
14 |
||
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 |
|
18 |
||
19 |
Sam Lantinga |
|
20 |
slouken@libsdl.org |
|
21 |
*/ |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
5540
diff
changeset
|
22 |
|
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
5540
diff
changeset
|
23 |
#define org_hedgewars_hedgeroid org_hedgewars_hedgeroid |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
5540
diff
changeset
|
24 |
|
5337 | 25 |
#include "SDL_config.h" |
26 |
#include "SDL_stdinc.h" |
|
27 |
||
28 |
#include "SDL_android.h" |
|
29 |
||
30 |
extern "C" { |
|
31 |
#include "../../events/SDL_events_c.h" |
|
32 |
#include "../../video/android/SDL_androidkeyboard.h" |
|
33 |
#include "../../video/android/SDL_androidtouch.h" |
|
34 |
#include "../../video/android/SDL_androidvideo.h" |
|
35 |
||
36 |
/* Impelemented in audio/android/SDL_androidaudio.c */ |
|
37 |
extern void Android_RunAudioThread(); |
|
38 |
} // C |
|
39 |
||
40 |
/******************************************************************************* |
|
41 |
This file links the Java side of Android with libsdl |
|
42 |
*******************************************************************************/ |
|
43 |
#include <jni.h> |
|
44 |
#include <android/log.h> |
|
45 |
||
46 |
||
47 |
/******************************************************************************* |
|
48 |
Globals |
|
49 |
*******************************************************************************/ |
|
50 |
static JNIEnv* mEnv = NULL; |
|
51 |
static JNIEnv* mAudioEnv = NULL; |
|
52 |
||
53 |
// Main activity |
|
54 |
static jclass mActivityClass; |
|
55 |
||
56 |
// method signatures |
|
57 |
static jmethodID midCreateGLContext; |
|
58 |
static jmethodID midFlipBuffers; |
|
59 |
static jmethodID midAudioInit; |
|
60 |
static jmethodID midAudioWriteShortBuffer; |
|
61 |
static jmethodID midAudioWriteByteBuffer; |
|
62 |
static jmethodID midAudioQuit; |
|
63 |
||
64 |
// Accelerometer data storage |
|
65 |
static float fLastAccelerometer[3]; |
|
66 |
||
67 |
||
68 |
/******************************************************************************* |
|
69 |
Functions called by JNI |
|
70 |
*******************************************************************************/ |
|
71 |
||
72 |
// Library init |
|
73 |
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) |
|
74 |
{ |
|
75 |
return JNI_VERSION_1_4; |
|
76 |
} |
|
77 |
||
78 |
// Called before SDL_main() to initialize JNI bindings |
|
79 |
extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls) |
|
80 |
{ |
|
81 |
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()"); |
|
82 |
||
83 |
mEnv = env; |
|
84 |
mActivityClass = cls; |
|
85 |
||
86 |
midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass, |
|
87 |
"createGLContext","(II)Z"); |
|
88 |
midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass, |
|
89 |
"flipBuffers","()V"); |
|
90 |
midAudioInit = mEnv->GetStaticMethodID(mActivityClass, |
|
91 |
"audioInit", "(IZZI)Ljava/lang/Object;"); |
|
92 |
midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass, |
|
93 |
"audioWriteShortBuffer", "([S)V"); |
|
94 |
midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass, |
|
95 |
"audioWriteByteBuffer", "([B)V"); |
|
96 |
midAudioQuit = mEnv->GetStaticMethodID(mActivityClass, |
|
97 |
"audioQuit", "()V"); |
|
98 |
||
99 |
if(!midCreateGLContext || !midFlipBuffers || !midAudioInit || |
|
100 |
!midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) { |
|
101 |
__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly"); |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
// Resize |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
5540
diff
changeset
|
106 |
extern "C" void Java_org_hedgewars_hedgeroid_SDLActivity_onNativeResize( |
5337 | 107 |
JNIEnv* env, jclass jcls, |
108 |
jint width, jint height, jint format) |
|
109 |
{ |
|
110 |
Android_SetScreenResolution(width, height, format); |
|
111 |
} |
|
112 |
||
113 |
// Keydown |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
5540
diff
changeset
|
114 |
extern "C" void Java_org_hedgewars_hedgeroid_SDLActivity_onNativeKeyDown( |
5337 | 115 |
JNIEnv* env, jclass jcls, jint keycode) |
116 |
{ |
|
117 |
Android_OnKeyDown(keycode); |
|
118 |
} |
|
119 |
||
120 |
// Keyup |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
5540
diff
changeset
|
121 |
extern "C" void Java_org_hedgewars_hedgeroid_SDLActivity_onNativeKeyUp( |
5337 | 122 |
JNIEnv* env, jclass jcls, jint keycode) |
123 |
{ |
|
124 |
Android_OnKeyUp(keycode); |
|
125 |
} |
|
126 |
||
127 |
// Touch |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
5540
diff
changeset
|
128 |
extern "C" void Java_org_hedgewars_hedgeroid_SDLActivity_onNativeTouch( |
5337 | 129 |
JNIEnv* env, jclass jcls, |
6328 | 130 |
jint touch_device_id_in, jint pointer_finger_id_in, |
131 |
jint action, jfloat x, jfloat y, jfloat p) |
|
5337 | 132 |
{ |
6328 | 133 |
Android_OnTouch(touch_device_id_in, pointer_finger_id_in, action, x, y, p); |
5337 | 134 |
} |
135 |
||
136 |
// Accelerometer |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
5540
diff
changeset
|
137 |
extern "C" void Java_org_hedgewars_hedgeroid_SDLActivity_onNativeAccel( |
5337 | 138 |
JNIEnv* env, jclass jcls, |
139 |
jfloat x, jfloat y, jfloat z) |
|
140 |
{ |
|
141 |
fLastAccelerometer[0] = x; |
|
142 |
fLastAccelerometer[1] = y; |
|
143 |
fLastAccelerometer[2] = z; |
|
144 |
} |
|
145 |
||
146 |
// Quit |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
5540
diff
changeset
|
147 |
extern "C" void Java_org_hedgewars_hedgeroid_SDLActivity_nativeQuit( |
5337 | 148 |
JNIEnv* env, jclass cls) |
149 |
{ |
|
150 |
// Inject a SDL_QUIT event |
|
151 |
SDL_SendQuit(); |
|
152 |
} |
|
153 |
||
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
5540
diff
changeset
|
154 |
extern "C" void Java_org_hedgewars_hedgeroid_SDLActivity_nativeRunAudioThread( |
5337 | 155 |
JNIEnv* env, jclass cls) |
156 |
{ |
|
157 |
/* This is the audio thread, with a different environment */ |
|
158 |
mAudioEnv = env; |
|
159 |
||
160 |
Android_RunAudioThread(); |
|
161 |
} |
|
162 |
||
163 |
||
164 |
/******************************************************************************* |
|
165 |
Functions called by SDL into Java |
|
166 |
*******************************************************************************/ |
|
167 |
extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion) |
|
168 |
{ |
|
169 |
if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) { |
|
170 |
return SDL_TRUE; |
|
171 |
} else { |
|
172 |
return SDL_FALSE; |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
extern "C" void Android_JNI_SwapWindow() |
|
177 |
{ |
|
178 |
mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers); |
|
179 |
} |
|
180 |
||
181 |
extern "C" void Android_JNI_SetActivityTitle(const char *title) |
|
182 |
{ |
|
183 |
jmethodID mid; |
|
184 |
||
185 |
mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V"); |
|
186 |
if (mid) { |
|
187 |
mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title)); |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
extern "C" void Android_JNI_GetAccelerometerValues(float values[3]) |
|
192 |
{ |
|
193 |
int i; |
|
194 |
for (i = 0; i < 3; ++i) { |
|
195 |
values[i] = fLastAccelerometer[i]; |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
// |
|
200 |
// Audio support |
|
201 |
// |
|
202 |
static jboolean audioBuffer16Bit = JNI_FALSE; |
|
203 |
static jboolean audioBufferStereo = JNI_FALSE; |
|
204 |
static jobject audioBuffer = NULL; |
|
205 |
static void* audioBufferPinned = NULL; |
|
206 |
||
207 |
extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames) |
|
208 |
{ |
|
209 |
int audioBufferFrames; |
|
210 |
||
211 |
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device"); |
|
212 |
audioBuffer16Bit = is16Bit; |
|
213 |
audioBufferStereo = channelCount > 1; |
|
214 |
||
215 |
audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames); |
|
216 |
||
217 |
if (audioBuffer == NULL) { |
|
218 |
__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!"); |
|
219 |
return 0; |
|
220 |
} |
|
221 |
audioBuffer = mEnv->NewGlobalRef(audioBuffer); |
|
222 |
||
223 |
jboolean isCopy = JNI_FALSE; |
|
224 |
if (audioBuffer16Bit) { |
|
225 |
audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy); |
|
226 |
audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer); |
|
227 |
} else { |
|
228 |
audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy); |
|
229 |
audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer); |
|
230 |
} |
|
231 |
if (audioBufferStereo) { |
|
232 |
audioBufferFrames /= 2; |
|
233 |
} |
|
234 |
||
235 |
return audioBufferFrames; |
|
236 |
} |
|
237 |
||
238 |
extern "C" void * Android_JNI_GetAudioBuffer() |
|
239 |
{ |
|
240 |
return audioBufferPinned; |
|
241 |
} |
|
242 |
||
243 |
extern "C" void Android_JNI_WriteAudioBuffer() |
|
244 |
{ |
|
245 |
if (audioBuffer16Bit) { |
|
246 |
mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT); |
|
247 |
mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer); |
|
248 |
} else { |
|
249 |
mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT); |
|
250 |
mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer); |
|
251 |
} |
|
252 |
||
253 |
/* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */ |
|
254 |
} |
|
255 |
||
256 |
extern "C" void Android_JNI_CloseAudioDevice() |
|
257 |
{ |
|
258 |
mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit); |
|
259 |
||
260 |
if (audioBuffer) { |
|
261 |
mEnv->DeleteGlobalRef(audioBuffer); |
|
262 |
audioBuffer = NULL; |
|
263 |
audioBufferPinned = NULL; |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
/* vi: set ts=4 sw=4 expandtab: */ |