Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
222 lines (175 loc) · 7 KB

SDL_android.cpp

File metadata and controls

222 lines (175 loc) · 7 KB
 
Jan 7, 2011
Jan 7, 2011
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"
Jan 12, 2011
Jan 12, 2011
24
25
26
extern "C" {
#include "events/SDL_events_c.h"
#include "video/android/SDL_androidkeyboard.h"
Jan 12, 2011
Jan 12, 2011
27
#include "video/android/SDL_androidvideo.h"
Jan 12, 2011
Jan 12, 2011
28
29
}
Aug 17, 2010
Aug 17, 2010
30
31
32
33
34
35
36
37
38
39
/*******************************************************************************
This file links the Java side of Android with libsdl
*******************************************************************************/
#include <jni.h>
#include <android/log.h>
/*******************************************************************************
Globals
*******************************************************************************/
Jan 7, 2011
Jan 7, 2011
40
JavaVM* mVM = NULL;
Aug 17, 2010
Aug 17, 2010
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
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;
//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
Jan 7, 2011
Jan 7, 2011
66
*******************************************************************************/
Aug 17, 2010
Aug 17, 2010
67
Jan 7, 2011
Jan 7, 2011
68
69
70
71
// Library init
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
mVM = vm;
Aug 17, 2010
Aug 17, 2010
72
Jan 7, 2011
Jan 7, 2011
73
74
return JNI_VERSION_1_4;
}
Aug 17, 2010
Aug 17, 2010
75
Jan 7, 2011
Jan 7, 2011
76
77
78
// Called before SDL_main() to initialize JNI bindings
extern "C" void SDL_Android_Init(JNIEnv* env)
{
Aug 17, 2010
Aug 17, 2010
79
80
mEnv = env;
Jan 7, 2011
Jan 7, 2011
81
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
Aug 17, 2010
Aug 17, 2010
82
83
84
85
86
87
88
89
90
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 ||
Jan 7, 2011
Jan 7, 2011
91
!midUpdateAudio) {
Aug 17, 2010
Aug 17, 2010
92
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
Jan 7, 2011
Jan 7, 2011
93
} else {
Aug 17, 2010
Aug 17, 2010
94
95
96
97
98
99
#ifdef DEBUG
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
#endif
}
}
Jan 7, 2011
Jan 7, 2011
100
// Keydown
Aug 17, 2010
Aug 17, 2010
101
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(JNIEnv* env,
Jan 7, 2011
Jan 7, 2011
102
103
jobject obj, jint keycode)
{
Aug 17, 2010
Aug 17, 2010
104
105
#ifdef DEBUG
__android_log_print(ANDROID_LOG_INFO, "SDL",
Jan 12, 2011
Jan 12, 2011
106
"SDL: native key down %d\n", keycode);
Aug 17, 2010
Aug 17, 2010
107
#endif
Jan 12, 2011
Jan 12, 2011
108
Android_OnKeyDown(keycode);
Aug 17, 2010
Aug 17, 2010
109
110
}
Jan 7, 2011
Jan 7, 2011
111
// Keyup
Aug 17, 2010
Aug 17, 2010
112
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(JNIEnv* env,
Jan 7, 2011
Jan 7, 2011
113
114
jobject obj, jint keycode)
{
Aug 17, 2010
Aug 17, 2010
115
116
#ifdef DEBUG
__android_log_print(ANDROID_LOG_INFO, "SDL",
Jan 12, 2011
Jan 12, 2011
117
"SDL: native key up %d\n", keycode);
Aug 17, 2010
Aug 17, 2010
118
#endif
Jan 12, 2011
Jan 12, 2011
119
Android_OnKeyUp(keycode);
Aug 17, 2010
Aug 17, 2010
120
121
}
Jan 7, 2011
Jan 7, 2011
122
// Touch
Aug 17, 2010
Aug 17, 2010
123
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(JNIEnv* env,
Jan 7, 2011
Jan 7, 2011
124
125
jobject obj, jint action, jfloat x, jfloat y, jfloat p)
{
Aug 17, 2010
Aug 17, 2010
126
127
128
129
130
131
132
133
134
#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
}
Jan 7, 2011
Jan 7, 2011
135
// Quit
Aug 17, 2010
Aug 17, 2010
136
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv* env,
Jan 7, 2011
Jan 7, 2011
137
138
139
140
jobject obj )
{
// Stop rendering as we're no longer in the foreground
bRenderingEnabled = false;
Aug 17, 2010
Aug 17, 2010
141
Jan 7, 2011
Jan 7, 2011
142
143
// Inject a SDL_QUIT event
SDL_SendQuit();
Aug 17, 2010
Aug 17, 2010
144
145
}
Jan 7, 2011
Jan 7, 2011
146
// Resize
Aug 17, 2010
Aug 17, 2010
147
148
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
JNIEnv* env, jobject obj, jint width,
Jan 7, 2011
Jan 7, 2011
149
150
jint height, jint format)
{
Jan 12, 2011
Jan 12, 2011
151
Android_SetScreenResolution(width, height, format);
Aug 17, 2010
Aug 17, 2010
152
153
154
155
}
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
JNIEnv* env, jobject obj,
Jan 7, 2011
Jan 7, 2011
156
157
jfloat x, jfloat y, jfloat z)
{
Aug 17, 2010
Aug 17, 2010
158
159
160
161
162
163
164
165
166
167
fLastAccelerometer[0] = x;
fLastAccelerometer[1] = y;
fLastAccelerometer[2] = z;
}
/*******************************************************************************
Functions called by SDL into Java
*******************************************************************************/
Jan 7, 2011
Jan 7, 2011
168
169
170
extern "C" void Android_CreateContext()
{
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
Aug 17, 2010
Aug 17, 2010
171
Jan 7, 2011
Jan 7, 2011
172
bRenderingEnabled = true;
Aug 17, 2010
Aug 17, 2010
173
174
175
176
mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext );
}
Jan 7, 2011
Jan 7, 2011
177
178
179
extern "C" void Android_Render()
{
if (!bRenderingEnabled) {
Aug 17, 2010
Aug 17, 2010
180
181
182
return;
}
Jan 7, 2011
Jan 7, 2011
183
184
// When we get here, we've accumulated a full frame
mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers);
Aug 17, 2010
Aug 17, 2010
185
186
}
Jan 7, 2011
Jan 7, 2011
187
188
extern "C" void Android_EnableFeature(int featureid, bool enabled)
{
Aug 17, 2010
Aug 17, 2010
189
190
191
192
mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature,
featureid, (int)enabled);
}
Jan 7, 2011
Jan 7, 2011
193
194
extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len)
{
Aug 17, 2010
Aug 17, 2010
195
196
197
198
199
//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.
Jan 7, 2011
Jan 7, 2011
200
if(!mAudioThreadEnv) {
Aug 17, 2010
Aug 17, 2010
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
__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");
}
Jan 12, 2011
Jan 12, 2011
221
222
/* vi: set ts=4 sw=4 expandtab: */