comparison android-project/src/org/libsdl/app/SDLActivity.java @ 4989:58b6bb4a45e9

More Android cleanup: * Formalized the interface with Java methods in SDL_android.h * We don't need the feature system, at least right now * Fixed waiting for the SDLMain thread
author Sam Lantinga <slouken@libsdl.org>
date Wed, 12 Jan 2011 17:53:06 -0800
parents f9af88a9c823
children dc9924fc5070
comparison
equal deleted inserted replaced
4988:f9af88a9c823 4989:58b6bb4a45e9
28 private static SDLActivity mSingleton; 28 private static SDLActivity mSingleton;
29 private static SDLSurface mSurface; 29 private static SDLSurface mSurface;
30 30
31 // Audio 31 // Audio
32 private static AudioTrack mAudioTrack; 32 private static AudioTrack mAudioTrack;
33 private static boolean bAudioIsEnabled;
34
35 // Sensors
36 private static boolean bAccelIsEnabled;
37
38 // feature IDs. Must match up on the C side as well.
39 private static int FEATURE_AUDIO = 1;
40 private static int FEATURE_ACCEL = 2;
41 33
42 // Load the .so 34 // Load the .so
43 static { 35 static {
44 System.loadLibrary("SDL"); 36 System.loadLibrary("SDL");
45 System.loadLibrary("main"); 37 System.loadLibrary("main");
46 } 38 }
47 39
48 // Setup 40 // Setup
49 protected void onCreate(Bundle savedInstanceState) { 41 protected void onCreate(Bundle savedInstanceState) {
42 //Log.v("SDL", "onCreate()");
50 super.onCreate(savedInstanceState); 43 super.onCreate(savedInstanceState);
51 44
52 // So we can call stuff from static callbacks 45 // So we can call stuff from static callbacks
53 mSingleton = this; 46 mSingleton = this;
54 47
55 // Set up the surface 48 // Set up the surface
56 mSurface = new SDLSurface(getApplication()); 49 mSurface = new SDLSurface(getApplication());
57 setContentView(mSurface); 50 setContentView(mSurface);
58 SurfaceHolder holder = mSurface.getHolder(); 51 SurfaceHolder holder = mSurface.getHolder();
59 holder.setType(SurfaceHolder.SURFACE_TYPE_GPU); 52 holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
60 53 }
61 }
62
63 // Audio
64 public static boolean initAudio(){
65
66 // blah. Hardcoded things are bad. FIXME when we have more sound stuff
67 // working properly.
68 mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
69 11025,
70 AudioFormat.CHANNEL_CONFIGURATION_MONO,
71 AudioFormat.ENCODING_PCM_8BIT,
72 2048,
73 AudioTrack.MODE_STREAM);
74 bAudioIsEnabled = true;
75 return true;
76 }
77
78 // Accel
79 public static boolean initAccel(){
80 mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
81 bAccelIsEnabled = true;
82 return true;
83 }
84
85 public static boolean closeAccel(){
86 mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, false);
87 bAccelIsEnabled = false;
88 return true;
89 }
90
91 54
92 // Events 55 // Events
93 protected void onPause() { 56 protected void onPause() {
57 //Log.v("SDL", "onPause()");
94 super.onPause(); 58 super.onPause();
95 } 59 }
96 60
97 protected void onResume() { 61 protected void onResume() {
62 //Log.v("SDL", "onResume()");
98 super.onResume(); 63 super.onResume();
99 } 64 }
100 65
101 66
102 // C functions we call 67 // C functions we call
119 mSurface.flipEGL(); 84 mSurface.flipEGL();
120 } 85 }
121 86
122 public static void updateAudio(byte [] buf) { 87 public static void updateAudio(byte [] buf) {
123 88
124 if(mAudioTrack == null){ 89 if(mAudioTrack == null) {
125 return; 90 // Hardcoded things are bad. FIXME when we have more sound stuff
126 } 91 // working properly.
127 92 mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
93 11025,
94 AudioFormat.CHANNEL_CONFIGURATION_MONO,
95 AudioFormat.ENCODING_PCM_8BIT,
96 2048,
97 AudioTrack.MODE_STREAM);
98 }
99
128 mAudioTrack.write(buf, 0, buf.length); 100 mAudioTrack.write(buf, 0, buf.length);
129 mAudioTrack.play(); 101 mAudioTrack.play();
130 102
131 Log.v("SDL","Played some audio"); 103 Log.v("SDL", "Played some audio");
132 } 104 }
133 105
134 public static void enableFeature(int featureid, int enabled) {
135 Log.v("SDL","Feature " + featureid + " = " + enabled);
136
137 // Yuck. This is all horribly inelegent. If it gets to more than a few
138 // 'features' I'll rip this out and make something nicer, I promise :)
139 if(featureid == FEATURE_AUDIO){
140 if(enabled == 1){
141 initAudio();
142 }else{
143 // We don't have one of these yet...
144 //closeAudio();
145 }
146 }
147
148 else if(featureid == FEATURE_ACCEL){
149 if(enabled == 1){
150 initAccel();
151 }else{
152 closeAccel();
153 }
154 }
155 }
156
157 } 106 }
158 107
159 /** 108 /**
160 Simple nativeInit() runnable 109 Simple nativeInit() runnable
161 */ 110 */
162 class SDLMain implements Runnable { 111 class SDLMain implements Runnable {
163 public void run() { 112 public void run() {
164 // Runs SDL_main() 113 // Runs SDL_main()
165 SDLActivity.nativeInit(); 114 SDLActivity.nativeInit();
166 115
167 Log.v("SDL","SDL thread terminated"); 116 //Log.v("SDL", "SDL thread terminated");
168 } 117 }
169 } 118 }
170 119
171 120
172 /** 121 /**
203 mSensorManager = (SensorManager)context.getSystemService("sensor"); 152 mSensorManager = (SensorManager)context.getSystemService("sensor");
204 } 153 }
205 154
206 // Called when we have a valid drawing surface 155 // Called when we have a valid drawing surface
207 public void surfaceCreated(SurfaceHolder holder) { 156 public void surfaceCreated(SurfaceHolder holder) {
157 //Log.v("SDL", "surfaceCreated()");
158
159 enableSensor(Sensor.TYPE_ACCELEROMETER, true);
208 } 160 }
209 161
210 // Called when we lose the surface 162 // Called when we lose the surface
211 public void surfaceDestroyed(SurfaceHolder holder) { 163 public void surfaceDestroyed(SurfaceHolder holder) {
164 //Log.v("SDL", "surfaceDestroyed()");
212 165
213 // Send a quit message to the application 166 // Send a quit message to the application
214 SDLActivity.nativeQuit(); 167 SDLActivity.nativeQuit();
215 168
216 // Now wait for the SDL thread to quit 169 // Now wait for the SDL thread to quit
217 if (mSDLThread != null) { 170 if (mSDLThread != null) {
218 try { 171 //synchronized (mSDLThread) {
219 mSDLThread.wait(); 172 try {
220 } catch(Exception e) { 173 mSDLThread.join();
221 Log.v("SDL","Problem stopping thread: " + e); 174 } catch(Exception e) {
222 } 175 Log.v("SDL", "Problem stopping thread: " + e);
223 } 176 }
177 //}
178 mSDLThread = null;
179
180 //Log.v("SDL", "Finished waiting for SDL thread");
181 }
182
183 enableSensor(Sensor.TYPE_ACCELEROMETER, false);
224 } 184 }
225 185
226 // Called when the surface is resized 186 // Called when the surface is resized
227 public void surfaceChanged(SurfaceHolder holder, 187 public void surfaceChanged(SurfaceHolder holder,
228 int format, int width, int height) { 188 int format, int width, int height) {
229 Log.v("SDL","Surface resized"); 189 //Log.v("SDL", "surfaceChanged()");
230 190
231 int sdlFormat = 0; 191 int sdlFormat = 0;
232 switch (format) { 192 switch (format) {
233 case PixelFormat.A_8: 193 case PixelFormat.A_8:
234 Log.v("SDL","pixel format A_8"); 194 Log.v("SDL", "pixel format A_8");
235 break; 195 break;
236 case PixelFormat.LA_88: 196 case PixelFormat.LA_88:
237 Log.v("SDL","pixel format LA_88"); 197 Log.v("SDL", "pixel format LA_88");
238 break; 198 break;
239 case PixelFormat.L_8: 199 case PixelFormat.L_8:
240 Log.v("SDL","pixel format L_8"); 200 Log.v("SDL", "pixel format L_8");
241 break; 201 break;
242 case PixelFormat.RGBA_4444: 202 case PixelFormat.RGBA_4444:
243 Log.v("SDL","pixel format RGBA_4444"); 203 Log.v("SDL", "pixel format RGBA_4444");
244 sdlFormat = 0x85421002; // SDL_PIXELFORMAT_RGBA4444 204 sdlFormat = 0x85421002; // SDL_PIXELFORMAT_RGBA4444
245 break; 205 break;
246 case PixelFormat.RGBA_5551: 206 case PixelFormat.RGBA_5551:
247 Log.v("SDL","pixel format RGBA_5551"); 207 Log.v("SDL", "pixel format RGBA_5551");
248 sdlFormat = 0x85441002; // SDL_PIXELFORMAT_RGBA5551 208 sdlFormat = 0x85441002; // SDL_PIXELFORMAT_RGBA5551
249 break; 209 break;
250 case PixelFormat.RGBA_8888: 210 case PixelFormat.RGBA_8888:
251 Log.v("SDL","pixel format RGBA_8888"); 211 Log.v("SDL", "pixel format RGBA_8888");
252 sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888 212 sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
253 break; 213 break;
254 case PixelFormat.RGBX_8888: 214 case PixelFormat.RGBX_8888:
255 Log.v("SDL","pixel format RGBX_8888"); 215 Log.v("SDL", "pixel format RGBX_8888");
256 sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888 216 sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
257 break; 217 break;
258 case PixelFormat.RGB_332: 218 case PixelFormat.RGB_332:
259 Log.v("SDL","pixel format RGB_332"); 219 Log.v("SDL", "pixel format RGB_332");
260 sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332 220 sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
261 break; 221 break;
262 case PixelFormat.RGB_565: 222 case PixelFormat.RGB_565:
263 Log.v("SDL","pixel format RGB_565"); 223 Log.v("SDL", "pixel format RGB_565");
264 sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 224 sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
265 break; 225 break;
266 case PixelFormat.RGB_888: 226 case PixelFormat.RGB_888:
267 Log.v("SDL","pixel format RGB_888"); 227 Log.v("SDL", "pixel format RGB_888");
268 // Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead? 228 // Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
269 sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888 229 sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
270 break; 230 break;
271 } 231 }
272 SDLActivity.onNativeResize(width, height, sdlFormat); 232 SDLActivity.onNativeResize(width, height, sdlFormat);
314 mEGLDisplay = dpy; 274 mEGLDisplay = dpy;
315 mEGLSurface = surface; 275 mEGLSurface = surface;
316 276
317 } catch(Exception e) { 277 } catch(Exception e) {
318 Log.v("SDL", e + ""); 278 Log.v("SDL", e + "");
319 for(StackTraceElement s : e.getStackTrace()){ 279 for(StackTraceElement s : e.getStackTrace()) {
320 Log.v("SDL", s.toString()); 280 Log.v("SDL", s.toString());
321 } 281 }
322 } 282 }
323 283
324 return true; 284 return true;
326 286
327 // EGL buffer flip 287 // EGL buffer flip
328 public void flipEGL() { 288 public void flipEGL() {
329 try { 289 try {
330 EGL10 egl = (EGL10)EGLContext.getEGL(); 290 EGL10 egl = (EGL10)EGLContext.getEGL();
331 GL10 gl = (GL10)mEGLContext.getGL();
332 291
333 egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null); 292 egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
334 293
335 // drawing here 294 // drawing here
336 295
339 egl.eglSwapBuffers(mEGLDisplay, mEGLSurface); 298 egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
340 299
341 300
342 } catch(Exception e) { 301 } catch(Exception e) {
343 Log.v("SDL", "flipEGL(): " + e); 302 Log.v("SDL", "flipEGL(): " + e);
344 for(StackTraceElement s : e.getStackTrace()){ 303 for(StackTraceElement s : e.getStackTrace()) {
345 Log.v("SDL", s.toString()); 304 Log.v("SDL", s.toString());
346 } 305 }
347 } 306 }
348 } 307 }
349 308
350 // Key events 309 // Key events
351 public boolean onKey(View v, int keyCode, KeyEvent event){ 310 public boolean onKey(View v, int keyCode, KeyEvent event) {
352 311
353 if (event.getAction() == KeyEvent.ACTION_DOWN) { 312 if (event.getAction() == KeyEvent.ACTION_DOWN) {
313 //Log.v("SDL", "key down: " + keyCode);
354 SDLActivity.onNativeKeyDown(keyCode); 314 SDLActivity.onNativeKeyDown(keyCode);
355 return true; 315 return true;
356 } 316 }
357 else if (event.getAction() == KeyEvent.ACTION_UP) { 317 else if (event.getAction() == KeyEvent.ACTION_UP) {
318 //Log.v("SDL", "key up: " + keyCode);
358 SDLActivity.onNativeKeyUp(keyCode); 319 SDLActivity.onNativeKeyUp(keyCode);
359 return true; 320 return true;
360 } 321 }
361 322
362 return false; 323 return false;