comparison android-project/src/org/libsdl/app/SDLActivity.java @ 4981:55b82067815b

Fill in the video mode with the correct screen format
author Sam Lantinga <slouken@libsdl.org>
date Wed, 12 Jan 2011 14:29:01 -0800
parents d9fdff945ec9
children 660d3a432102
comparison
equal deleted inserted replaced
4980:d9fdff945ec9 4981:55b82067815b
87 bAccelIsEnabled = false; 87 bAccelIsEnabled = false;
88 return true; 88 return true;
89 } 89 }
90 90
91 91
92 //Events 92 // Events
93 protected void onPause() { 93 protected void onPause() {
94 super.onPause(); 94 super.onPause();
95 } 95 }
96 96
97 protected void onResume() { 97 protected void onResume() {
98 super.onResume(); 98 super.onResume();
99 } 99 }
100 100
101 101
102 102 // C functions we call
103
104
105 //C functions we call
106 public static native void nativeInit(); 103 public static native void nativeInit();
107 public static native void nativeQuit(); 104 public static native void nativeQuit();
108 public static native void nativeSetScreenSize(int width, int height);
109 public static native void onNativeKeyDown(int keycode); 105 public static native void onNativeKeyDown(int keycode);
110 public static native void onNativeKeyUp(int keycode); 106 public static native void onNativeKeyUp(int keycode);
111 public static native void onNativeTouch(int action, float x, 107 public static native void onNativeTouch(int action, float x,
112 float y, float p); 108 float y, float p);
113 public static native void onNativeResize(int x, int y, int format); 109 public static native void onNativeResize(int x, int y, int format);
114 public static native void onNativeAccel(float x, float y, float z); 110 public static native void onNativeAccel(float x, float y, float z);
115 111
116 112
117
118 //Java functions called from C 113 //Java functions called from C
119 private static void createGLContext(){ 114 private static void createGLContext() {
120 mSurface.initEGL(); 115 mSurface.initEGL();
121 } 116 }
122 117
123 public static void flipBuffers(){ 118 public static void flipBuffers() {
124 mSurface.flipEGL(); 119 mSurface.flipEGL();
125 } 120 }
126 121
127 public static void updateAudio(byte [] buf){ 122 public static void updateAudio(byte [] buf) {
128 123
129 if(mAudioTrack == null){ 124 if(mAudioTrack == null){
130 return; 125 return;
131 } 126 }
132 127
134 mAudioTrack.play(); 129 mAudioTrack.play();
135 130
136 Log.v("SDL","Played some audio"); 131 Log.v("SDL","Played some audio");
137 } 132 }
138 133
139 public static void enableFeature(int featureid, int enabled){ 134 public static void enableFeature(int featureid, int enabled) {
140 Log.v("SDL","Feature " + featureid + " = " + enabled); 135 Log.v("SDL","Feature " + featureid + " = " + enabled);
141 136
142 //Yuck. This is all horribly inelegent. If it gets to more than a few 137 //Yuck. This is all horribly inelegent. If it gets to more than a few
143 //'features' I'll rip this out and make something nicer, I promise :) 138 //'features' I'll rip this out and make something nicer, I promise :)
144 if(featureid == FEATURE_AUDIO){ 139 if(featureid == FEATURE_AUDIO){
162 } 157 }
163 158
164 /** 159 /**
165 Simple nativeInit() runnable 160 Simple nativeInit() runnable
166 */ 161 */
167 class SDLRunner implements Runnable{ 162 class SDLMain implements Runnable {
168 public void run(){ 163 public void run() {
169 //SDLActivity.initAudio(); 164 // Runs SDL_main()
170
171 //Runs SDL_main()
172 SDLActivity.nativeInit(); 165 SDLActivity.nativeInit();
173 166
174 Log.v("SDL","SDL thread terminated"); 167 Log.v("SDL","SDL thread terminated");
175 } 168 }
176 } 169 }
183 Because of this, that's where we set up the SDL thread 176 Because of this, that's where we set up the SDL thread
184 */ 177 */
185 class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, 178 class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
186 View.OnKeyListener, View.OnTouchListener, SensorEventListener { 179 View.OnKeyListener, View.OnTouchListener, SensorEventListener {
187 180
188 //This is what SDL runs in. It invokes SDL_main(), eventually 181 // This is what SDL runs in. It invokes SDL_main(), eventually
189 private Thread mSDLThread; 182 private Thread mSDLThread;
190 183
191 //EGL private objects 184 // EGL private objects
192 private EGLContext mEGLContext; 185 private EGLContext mEGLContext;
193 private EGLSurface mEGLSurface; 186 private EGLSurface mEGLSurface;
194 private EGLDisplay mEGLDisplay; 187 private EGLDisplay mEGLDisplay;
195 188
196 //Sensors 189 // Sensors
197 private static SensorManager mSensorManager; 190 private static SensorManager mSensorManager;
198 191
199 //Startup 192 // Startup
200 public SDLSurface(Context context) { 193 public SDLSurface(Context context) {
201 super(context); 194 super(context);
202 getHolder().addCallback(this); 195 getHolder().addCallback(this);
203 196
204 setFocusable(true); 197 setFocusable(true);
205 setFocusableInTouchMode(true); 198 setFocusableInTouchMode(true);
206 requestFocus(); 199 requestFocus();
207 setOnKeyListener(this); 200 setOnKeyListener(this);
208 setOnTouchListener(this); 201 setOnTouchListener(this);
209 202
210 mSensorManager = (SensorManager)context.getSystemService("sensor"); 203 mSensorManager = (SensorManager)context.getSystemService("sensor");
211 } 204 }
212 205
213 //Called when we have a valid drawing surface 206 // Called when we have a valid drawing surface
214 public void surfaceCreated(SurfaceHolder holder) { 207 public void surfaceCreated(SurfaceHolder holder) {
215 Log.v("SDL","Surface created"); 208 }
216 209
217 int width = getWidth(); 210 // Called when we lose the surface
218 int height = getHeight();
219
220 //Set the width and height variables in C before we start SDL so we have
221 //it available on init
222 SDLActivity.nativeSetScreenSize(width, height);
223
224 //Now start up the C app thread
225 mSDLThread = new Thread(new SDLRunner(), "SDLThread");
226 mSDLThread.start();
227 }
228
229 //Called when we lose the surface
230 public void surfaceDestroyed(SurfaceHolder holder) { 211 public void surfaceDestroyed(SurfaceHolder holder) {
231 Log.v("SDL","Surface destroyed"); 212
232 213 // Send a quit message to the application
233 SDLActivity.nativeQuit(); 214 SDLActivity.nativeQuit();
234 215
235 //Now wait for the SDL thread to quit 216 // Now wait for the SDL thread to quit
236 try{ 217 if (mSDLThread != null) {
237 mSDLThread.wait(); 218 try {
238 }catch(Exception e){ 219 mSDLThread.wait();
239 Log.v("SDL","Problem stopping thread: " + e); 220 } catch(Exception e) {
240 } 221 Log.v("SDL","Problem stopping thread: " + e);
241 } 222 }
242 223 }
243 //Called when the surface is resized 224 }
244 public void surfaceChanged(SurfaceHolder holder, int format, 225
245 int width, int height) { 226 // Called when the surface is resized
227 public void surfaceChanged(SurfaceHolder holder,
228 int format, int width, int height) {
246 Log.v("SDL","Surface resized"); 229 Log.v("SDL","Surface resized");
247 230
231 int sdlFormat = 0;
232 switch (format) {
233 case PixelFormat.A_8:
234 Log.v("SDL","pixel format A_8");
235 break;
236 case PixelFormat.LA_88:
237 Log.v("SDL","pixel format LA_88");
238 break;
239 case PixelFormat.L_8:
240 Log.v("SDL","pixel format L_8");
241 break;
242 case PixelFormat.RGBA_4444:
243 Log.v("SDL","pixel format RGBA_4444");
244 sdlFormat = 0x85421002; // Doesn't have an SDL constant...
245 break;
246 case PixelFormat.RGBA_5551:
247 Log.v("SDL","pixel format RGBA_5551");
248 sdlFormat = 0x85441002; // Doesn't have an SDL constant...
249 break;
250 case PixelFormat.RGBA_8888:
251 Log.v("SDL","pixel format RGBA_8888");
252 sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
253 break;
254 case PixelFormat.RGBX_8888:
255 Log.v("SDL","pixel format RGBX_8888");
256 sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
257 break;
258 case PixelFormat.RGB_332:
259 Log.v("SDL","pixel format RGB_332");
260 sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
261 break;
262 case PixelFormat.RGB_565:
263 Log.v("SDL","pixel format RGB_565");
264 sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
265 break;
266 case PixelFormat.RGB_888:
267 Log.v("SDL","pixel format RGB_888");
268 // Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
269 sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
270 break;
271 }
248 SDLActivity.onNativeResize(width, height, format); 272 SDLActivity.onNativeResize(width, height, format);
273
274 // Now start up the C app thread
275 if (mSDLThread == null) {
276 mSDLThread = new Thread(new SDLMain(), "SDLThread");
277 mSDLThread.start();
278 }
249 } 279 }
250 280
251 //unused 281 //unused
252 public void onDraw(Canvas canvas) {} 282 public void onDraw(Canvas canvas) {}
253 283
254 284
255 //EGL functions 285 // EGL functions
256 public boolean initEGL(){ 286 public boolean initEGL() {
257 Log.v("SDL","Starting up"); 287 Log.v("SDL", "Starting up");
258 288
259 try{ 289 try {
260 290
261 EGL10 egl = (EGL10)EGLContext.getEGL(); 291 EGL10 egl = (EGL10)EGLContext.getEGL();
262 292
263 EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); 293 EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
264 294
281 egl.eglMakeCurrent(dpy, surface, surface, ctx); 311 egl.eglMakeCurrent(dpy, surface, surface, ctx);
282 312
283 mEGLContext = ctx; 313 mEGLContext = ctx;
284 mEGLDisplay = dpy; 314 mEGLDisplay = dpy;
285 mEGLSurface = surface; 315 mEGLSurface = surface;
286 316
287 317 } catch(Exception e) {
288 }catch(Exception e){
289 Log.v("SDL", e + ""); 318 Log.v("SDL", e + "");
290 for(StackTraceElement s : e.getStackTrace()){ 319 for(StackTraceElement s : e.getStackTrace()){
291 Log.v("SDL", s.toString()); 320 Log.v("SDL", s.toString());
292 } 321 }
293 } 322 }
294 Log.v("SDL","Done making!");
295 323
296 return true; 324 return true;
297 } 325 }
298 326
299 //EGL buffer flip 327 // EGL buffer flip
300 public void flipEGL(){ 328 public void flipEGL() {
301 try{ 329 try {
302
303 EGL10 egl = (EGL10)EGLContext.getEGL(); 330 EGL10 egl = (EGL10)EGLContext.getEGL();
304 GL10 gl = (GL10)mEGLContext.getGL(); 331 GL10 gl = (GL10)mEGLContext.getGL();
305 332
306 egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null); 333 egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
307 334
310 egl.eglWaitGL(); 337 egl.eglWaitGL();
311 338
312 egl.eglSwapBuffers(mEGLDisplay, mEGLSurface); 339 egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
313 340
314 341
315 }catch(Exception e){ 342 } catch(Exception e) {
316 Log.v("SDL", "flipEGL(): " + e); 343 Log.v("SDL", "flipEGL(): " + e);
317
318 for(StackTraceElement s : e.getStackTrace()){ 344 for(StackTraceElement s : e.getStackTrace()){
319 Log.v("SDL", s.toString()); 345 Log.v("SDL", s.toString());
320 } 346 }
321 } 347 }
322 } 348 }
323 349
324 350 // Key events
325
326 //Key events
327 public boolean onKey(View v, int keyCode, KeyEvent event){ 351 public boolean onKey(View v, int keyCode, KeyEvent event){
328 352
329 if(event.getAction() == KeyEvent.ACTION_DOWN){ 353 if (event.getAction() == KeyEvent.ACTION_DOWN) {
330 SDLActivity.onNativeKeyDown(keyCode); 354 SDLActivity.onNativeKeyDown(keyCode);
331 return true; 355 return true;
332 } 356 }
333 357 else if (event.getAction() == KeyEvent.ACTION_UP) {
334 else if(event.getAction() == KeyEvent.ACTION_UP){
335 SDLActivity.onNativeKeyUp(keyCode); 358 SDLActivity.onNativeKeyUp(keyCode);
336 return true; 359 return true;
337 } 360 }
338 361
339 return false; 362 return false;
340 } 363 }
341 364
342 //Touch events 365 // Touch events
343 public boolean onTouch(View v, MotionEvent event){ 366 public boolean onTouch(View v, MotionEvent event) {
344 367
345 int action = event.getAction(); 368 int action = event.getAction();
346 float x = event.getX(); 369 float x = event.getX();
347 float y = event.getY(); 370 float y = event.getY();
348 float p = event.getPressure(); 371 float p = event.getPressure();
350 //TODO: Anything else we need to pass? 373 //TODO: Anything else we need to pass?
351 SDLActivity.onNativeTouch(action, x, y, p); 374 SDLActivity.onNativeTouch(action, x, y, p);
352 return true; 375 return true;
353 } 376 }
354 377
355 //Sensor events 378 // Sensor events
356 public void enableSensor(int sensortype, boolean enabled){ 379 public void enableSensor(int sensortype, boolean enabled) {
357 //TODO: This uses getDefaultSensor - what if we have >1 accels? 380 //TODO: This uses getDefaultSensor - what if we have >1 accels?
358 if(enabled){ 381 if (enabled) {
359 mSensorManager.registerListener(this, 382 mSensorManager.registerListener(this,
360 mSensorManager.getDefaultSensor(sensortype), 383 mSensorManager.getDefaultSensor(sensortype),
361 SensorManager.SENSOR_DELAY_GAME, null); 384 SensorManager.SENSOR_DELAY_GAME, null);
362 }else{ 385 } else {
363 mSensorManager.unregisterListener(this, 386 mSensorManager.unregisterListener(this,
364 mSensorManager.getDefaultSensor(sensortype)); 387 mSensorManager.getDefaultSensor(sensortype));
365 } 388 }
366 } 389 }
367 390
368 public void onAccuracyChanged(Sensor sensor, int accuracy){ 391 public void onAccuracyChanged(Sensor sensor, int accuracy) {
369 //TODO 392 //TODO
370 } 393 }
371 394
372 public void onSensorChanged(SensorEvent event){ 395 public void onSensorChanged(SensorEvent event) {
373 if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){ 396 if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
374 SDLActivity.onNativeAccel( event.values[0], 397 SDLActivity.onNativeAccel(event.values[0],
375 event.values[1], 398 event.values[1],
376 event.values[2] ); 399 event.values[2]);
377 } 400 }
378 } 401 }
379
380 402
381 } 403 }
382 404
383