This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDLActivity.java
368 lines (292 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package org.libsdl.app;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.egl.*;
import android.app.*;
import android.content.*;
import android.view.*;
import android.os.*;
import android.util.Log;
import android.graphics.*;
import android.text.method.*;
import android.text.*;
import android.media.*;
import android.hardware.*;
import android.content.*;
import java.lang.*;
/**
SDL Activity
*/
public class SDLActivity extends Activity {
27
// Main components
28
29
30
private static SDLActivity mSingleton;
private static SDLSurface mSurface;
31
// Audio
32
33
private static AudioTrack mAudioTrack;
34
// Load the .so
35
36
37
38
39
static {
System.loadLibrary("SDL");
System.loadLibrary("main");
}
40
// Setup
41
protected void onCreate(Bundle savedInstanceState) {
42
//Log.v("SDL", "onCreate()");
43
44
super.onCreate(savedInstanceState);
45
// So we can call stuff from static callbacks
46
47
mSingleton = this;
48
// Set up the surface
49
50
51
52
53
54
mSurface = new SDLSurface(getApplication());
setContentView(mSurface);
SurfaceHolder holder = mSurface.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
}
55
// Events
56
protected void onPause() {
57
//Log.v("SDL", "onPause()");
58
59
60
61
super.onPause();
}
protected void onResume() {
62
//Log.v("SDL", "onResume()");
63
64
65
66
super.onResume();
}
67
// C functions we call
68
69
70
71
72
73
74
75
76
77
public static native void nativeInit();
public static native void nativeQuit();
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);
public static native void onNativeTouch(int action, float x,
float y, float p);
public static native void onNativeResize(int x, int y, int format);
public static native void onNativeAccel(float x, float y, float z);
78
// Java functions called from C
79
private static void createGLContext() {
80
81
82
mSurface.initEGL();
}
83
public static void flipBuffers() {
84
85
86
mSurface.flipEGL();
}
87
public static void updateAudio(byte [] buf) {
88
89
90
91
92
93
94
95
96
97
if(mAudioTrack == null) {
// Hardcoded things are bad. FIXME when we have more sound stuff
// working properly.
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT,
2048,
AudioTrack.MODE_STREAM);
98
}
99
100
101
102
mAudioTrack.write(buf, 0, buf.length);
mAudioTrack.play();
103
Log.v("SDL", "Played some audio");
104
105
106
107
108
109
110
}
}
/**
Simple nativeInit() runnable
*/
111
112
113
class SDLMain implements Runnable {
public void run() {
// Runs SDL_main()
114
115
SDLActivity.nativeInit();
116
//Log.v("SDL", "SDL thread terminated");
117
118
119
120
121
122
123
124
125
126
127
128
129
}
}
/**
SDLSurface. This is what we draw on, so we need to know when it's created
in order to do anything useful.
Because of this, that's where we set up the SDL thread
*/
class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
View.OnKeyListener, View.OnTouchListener, SensorEventListener {
130
// This is what SDL runs in. It invokes SDL_main(), eventually
131
132
private Thread mSDLThread;
133
// EGL private objects
134
135
136
137
private EGLContext mEGLContext;
private EGLSurface mEGLSurface;
private EGLDisplay mEGLDisplay;
138
// Sensors
139
140
private static SensorManager mSensorManager;
141
// Startup
142
143
144
145
146
147
148
149
150
public SDLSurface(Context context) {
super(context);
getHolder().addCallback(this);
setFocusable(true);
setFocusableInTouchMode(true);
requestFocus();
setOnKeyListener(this);
setOnTouchListener(this);
151
152
153
154
mSensorManager = (SensorManager)context.getSystemService("sensor");
}
155
// Called when we have a valid drawing surface
156
public void surfaceCreated(SurfaceHolder holder) {
157
158
159
//Log.v("SDL", "surfaceCreated()");
enableSensor(Sensor.TYPE_ACCELEROMETER, true);
160
161
}
162
// Called when we lose the surface
163
public void surfaceDestroyed(SurfaceHolder holder) {
164
//Log.v("SDL", "surfaceDestroyed()");
165
166
// Send a quit message to the application
167
168
SDLActivity.nativeQuit();
169
170
// Now wait for the SDL thread to quit
if (mSDLThread != null) {
171
172
173
174
175
176
177
178
179
180
//synchronized (mSDLThread) {
try {
mSDLThread.join();
} catch(Exception e) {
Log.v("SDL", "Problem stopping thread: " + e);
}
//}
mSDLThread = null;
//Log.v("SDL", "Finished waiting for SDL thread");
181
}
182
183
enableSensor(Sensor.TYPE_ACCELEROMETER, false);
184
185
}
186
187
188
// Called when the surface is resized
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
189
//Log.v("SDL", "surfaceChanged()");
190
191
int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default
192
193
switch (format) {
case PixelFormat.A_8:
194
Log.v("SDL", "pixel format A_8");
195
196
break;
case PixelFormat.LA_88:
197
Log.v("SDL", "pixel format LA_88");
198
199
break;
case PixelFormat.L_8:
200
Log.v("SDL", "pixel format L_8");
201
202
break;
case PixelFormat.RGBA_4444:
203
Log.v("SDL", "pixel format RGBA_4444");
204
sdlFormat = 0x85421002; // SDL_PIXELFORMAT_RGBA4444
205
206
break;
case PixelFormat.RGBA_5551:
207
Log.v("SDL", "pixel format RGBA_5551");
208
sdlFormat = 0x85441002; // SDL_PIXELFORMAT_RGBA5551
209
210
break;
case PixelFormat.RGBA_8888:
211
Log.v("SDL", "pixel format RGBA_8888");
212
213
214
sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
break;
case PixelFormat.RGBX_8888:
215
Log.v("SDL", "pixel format RGBX_8888");
216
217
218
sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
break;
case PixelFormat.RGB_332:
219
Log.v("SDL", "pixel format RGB_332");
220
221
222
sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
break;
case PixelFormat.RGB_565:
223
Log.v("SDL", "pixel format RGB_565");
224
225
226
sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
break;
case PixelFormat.RGB_888:
227
Log.v("SDL", "pixel format RGB_888");
228
229
230
// Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
break;
231
232
233
default:
Log.v("SDL", "pixel format unknown " + format);
break;
234
}
235
SDLActivity.onNativeResize(width, height, sdlFormat);
236
237
238
239
240
241
// Now start up the C app thread
if (mSDLThread == null) {
mSDLThread = new Thread(new SDLMain(), "SDLThread");
mSDLThread.start();
}
242
243
}
244
// unused
245
246
247
public void onDraw(Canvas canvas) {}
248
249
250
251
252
// EGL functions
public boolean initEGL() {
Log.v("SDL", "Starting up");
try {
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
EGL10 egl = (EGL10)EGLContext.getEGL();
EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
int[] version = new int[2];
egl.eglInitialize(dpy, version);
int[] configSpec = {
//EGL10.EGL_DEPTH_SIZE, 16,
EGL10.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
EGLConfig config = configs[0];
EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
egl.eglMakeCurrent(dpy, surface, surface, ctx);
mEGLContext = ctx;
mEGLDisplay = dpy;
mEGLSurface = surface;
279
280
} catch(Exception e) {
281
Log.v("SDL", e + "");
282
for(StackTraceElement s : e.getStackTrace()) {
283
284
285
286
287
288
289
Log.v("SDL", s.toString());
}
}
return true;
}
290
291
292
// EGL buffer flip
public void flipEGL() {
try {
293
294
295
296
EGL10 egl = (EGL10)EGLContext.getEGL();
egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
297
// drawing here
298
299
300
301
302
303
egl.eglWaitGL();
egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
304
} catch(Exception e) {
305
Log.v("SDL", "flipEGL(): " + e);
306
for(StackTraceElement s : e.getStackTrace()) {
307
308
309
310
311
Log.v("SDL", s.toString());
}
}
}
312
// Key events
313
public boolean onKey(View v, int keyCode, KeyEvent event) {
314
315
if (event.getAction() == KeyEvent.ACTION_DOWN) {
316
//Log.v("SDL", "key down: " + keyCode);
317
318
319
SDLActivity.onNativeKeyDown(keyCode);
return true;
}
320
else if (event.getAction() == KeyEvent.ACTION_UP) {
321
//Log.v("SDL", "key up: " + keyCode);
322
323
324
325
326
327
328
SDLActivity.onNativeKeyUp(keyCode);
return true;
}
return false;
}
329
330
// Touch events
public boolean onTouch(View v, MotionEvent event) {
331
332
333
334
335
336
int action = event.getAction();
float x = event.getX();
float y = event.getY();
float p = event.getPressure();
337
// TODO: Anything else we need to pass?
338
339
340
341
SDLActivity.onNativeTouch(action, x, y, p);
return true;
}
342
343
// Sensor events
public void enableSensor(int sensortype, boolean enabled) {
344
// TODO: This uses getDefaultSensor - what if we have >1 accels?
345
if (enabled) {
346
347
348
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(sensortype),
SensorManager.SENSOR_DELAY_GAME, null);
349
} else {
350
351
352
353
354
mSensorManager.unregisterListener(this,
mSensorManager.getDefaultSensor(sensortype));
}
}
355
public void onAccuracyChanged(Sensor sensor, int accuracy) {
356
// TODO
357
358
}
359
360
361
362
363
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
SDLActivity.onNativeAccel(event.values[0],
event.values[1],
event.values[2]);
364
365
366
367
}
}
}