This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_android.cpp
231 lines (183 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2010 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
24
25
26
27
28
29
30
31
32
33
/*******************************************************************************
This file links the Java side of Android with libsdl
*******************************************************************************/
#include <jni.h>
#include <android/log.h>
/*******************************************************************************
Globals
*******************************************************************************/
34
JavaVM* mVM = NULL;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
JNIEnv* mEnv = NULL;
JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
//Main activity
jclass mActivityInstance;
//method signatures
jmethodID midCreateGLContext;
jmethodID midFlipBuffers;
jmethodID midEnableFeature;
jmethodID midUpdateAudio;
extern "C" int Android_OnKeyDown(int keycode);
extern "C" int Android_OnKeyUp(int keycode);
extern "C" void Android_SetScreenResolution(int width, int height);
extern "C" void Android_OnResize(int width, int height, int format);
extern "C" int SDL_SendQuit();
extern "C" void Android_EnableFeature(int featureid, bool enabled);
//If we're not the active app, don't try to render
bool bRenderingEnabled = false;
//Feature IDs
static const int FEATURE_AUDIO = 1;
static const int FEATURE_ACCEL = 2;
//Accelerometer data storage
float fLastAccelerometer[3];
/*******************************************************************************
Functions called by JNI
67
*******************************************************************************/
68
69
70
71
72
// Library init
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
mVM = vm;
73
74
75
return JNI_VERSION_1_4;
}
76
77
78
79
// Called before SDL_main() to initialize JNI bindings
extern "C" void SDL_Android_Init(JNIEnv* env)
{
80
81
mEnv = env;
82
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
83
84
85
86
87
88
89
90
91
jclass cls = mEnv->FindClass ("org/libsdl/app/SDLActivity");
mActivityInstance = cls;
midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature ||
92
!midUpdateAudio) {
93
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
94
} else {
95
96
97
98
99
100
#ifdef DEBUG
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
#endif
}
}
101
// Keydown
102
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(JNIEnv* env,
103
104
jobject obj, jint keycode)
{
105
106
107
108
109
110
111
int r = Android_OnKeyDown(keycode);
#ifdef DEBUG
__android_log_print(ANDROID_LOG_INFO, "SDL",
"SDL: native key down %d, %d\n", keycode, r);
#endif
}
112
// Keyup
113
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(JNIEnv* env,
114
115
jobject obj, jint keycode)
{
116
117
118
119
120
121
122
int r = Android_OnKeyUp(keycode);
#ifdef DEBUG
__android_log_print(ANDROID_LOG_INFO, "SDL",
"SDL: native key up %d, %d\n", keycode, r);
#endif
}
123
// Touch
124
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(JNIEnv* env,
125
126
jobject obj, jint action, jfloat x, jfloat y, jfloat p)
{
127
128
129
130
131
132
133
134
135
#ifdef DEBUG
__android_log_print(ANDROID_LOG_INFO, "SDL",
"SDL: native touch event %d @ %f/%f, pressure %f\n",
action, x, y, p);
#endif
//TODO: Pass this off to the SDL multitouch stuff
}
136
// Quit
137
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv* env,
138
139
140
141
jobject obj )
{
// Stop rendering as we're no longer in the foreground
bRenderingEnabled = false;
142
143
144
// Inject a SDL_QUIT event
SDL_SendQuit();
145
146
}
147
// Screen size
148
extern "C" void Java_org_libsdl_app_SDLActivity_nativeSetScreenSize(
149
150
JNIEnv* env, jobject obj, jint width, jint height)
{
151
152
153
154
155
__android_log_print(ANDROID_LOG_INFO, "SDL",
"SDL: Set screen size on init: %d/%d\n", width, height);
Android_SetScreenResolution(width, height);
}
156
// Resize
157
158
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
JNIEnv* env, jobject obj, jint width,
159
160
jint height, jint format)
{
161
162
163
164
165
Android_OnResize(width, height, format);
}
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
JNIEnv* env, jobject obj,
166
167
jfloat x, jfloat y, jfloat z)
{
168
169
170
171
172
173
174
175
176
177
fLastAccelerometer[0] = x;
fLastAccelerometer[1] = y;
fLastAccelerometer[2] = z;
}
/*******************************************************************************
Functions called by SDL into Java
*******************************************************************************/
178
179
180
extern "C" void Android_CreateContext()
{
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
181
182
bRenderingEnabled = true;
183
184
185
186
mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext );
}
187
188
189
extern "C" void Android_Render()
{
if (!bRenderingEnabled) {
190
191
192
return;
}
193
194
// When we get here, we've accumulated a full frame
mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers);
195
196
}
197
198
extern "C" void Android_EnableFeature(int featureid, bool enabled)
{
199
200
201
202
mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature,
featureid, (int)enabled);
}
203
204
extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len)
{
205
206
207
208
209
//Annoyingly we can't just call into Java from any thread. Because the audio
//callback is dispatched from the SDL audio thread (that wasn't made from
//java, we have to do some magic here to let the JVM know about the thread.
//Because everything it touches on the Java side is static anyway, it's
//not a big deal, just annoying.
210
if(!mAudioThreadEnv) {
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Need to set up audio thread env\n");
mVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
}
jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
//blah. We probably should rework this so we avoid the copy.
mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
mAudioThreadEnv->CallStaticVoidMethod( mActivityInstance,
midUpdateAudio, arr );
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
}