Fill in the video mode with the correct screen format
authorSam Lantinga
Wed, 12 Jan 2011 14:29:01 -0800
changeset 498155b82067815b
parent 4980 d9fdff945ec9
child 4982 660d3a432102
Fill in the video mode with the correct screen format
android-project/src/org/libsdl/app/SDLActivity.java
src/SDL_android.cpp
src/video/android/SDL_androidvideo.c
src/video/android/SDL_androidvideo.h
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Wed Jan 12 13:52:41 2011 -0800
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Wed Jan 12 14:29:01 2011 -0800
     1.3 @@ -89,7 +89,7 @@
     1.4      }
     1.5      
     1.6  
     1.7 -    //Events
     1.8 +    // Events
     1.9      protected void onPause() {
    1.10          super.onPause();
    1.11      }
    1.12 @@ -98,14 +98,10 @@
    1.13          super.onResume();
    1.14      }
    1.15  
    1.16 -    
    1.17  
    1.18 -
    1.19 -
    1.20 -    //C functions we call
    1.21 +    // C functions we call
    1.22      public static native void nativeInit();
    1.23      public static native void nativeQuit();
    1.24 -    public static native void nativeSetScreenSize(int width, int height);
    1.25      public static native void onNativeKeyDown(int keycode);
    1.26      public static native void onNativeKeyUp(int keycode);
    1.27      public static native void onNativeTouch(int action, float x, 
    1.28 @@ -114,17 +110,16 @@
    1.29      public static native void onNativeAccel(float x, float y, float z);
    1.30  
    1.31  
    1.32 -
    1.33      //Java functions called from C
    1.34 -    private static void createGLContext(){
    1.35 +    private static void createGLContext() {
    1.36          mSurface.initEGL();
    1.37      }
    1.38  
    1.39 -    public static void flipBuffers(){
    1.40 +    public static void flipBuffers() {
    1.41          mSurface.flipEGL();
    1.42      }
    1.43  
    1.44 -    public static void updateAudio(byte [] buf){
    1.45 +    public static void updateAudio(byte [] buf) {
    1.46      
    1.47          if(mAudioTrack == null){
    1.48              return;
    1.49 @@ -136,7 +131,7 @@
    1.50          Log.v("SDL","Played some audio");
    1.51      }
    1.52  
    1.53 -    public static void enableFeature(int featureid, int enabled){
    1.54 +    public static void enableFeature(int featureid, int enabled) {
    1.55           Log.v("SDL","Feature " + featureid + " = " + enabled);
    1.56  
    1.57          //Yuck. This is all horribly inelegent. If it gets to more than a few
    1.58 @@ -164,11 +159,9 @@
    1.59  /**
    1.60      Simple nativeInit() runnable
    1.61  */
    1.62 -class SDLRunner implements Runnable{
    1.63 -    public void run(){
    1.64 -        //SDLActivity.initAudio();
    1.65 -        
    1.66 -        //Runs SDL_main()
    1.67 +class SDLMain implements Runnable {
    1.68 +    public void run() {
    1.69 +        // Runs SDL_main()
    1.70          SDLActivity.nativeInit();
    1.71  
    1.72          Log.v("SDL","SDL thread terminated");
    1.73 @@ -185,18 +178,18 @@
    1.74  class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, 
    1.75      View.OnKeyListener, View.OnTouchListener, SensorEventListener  {
    1.76  
    1.77 -    //This is what SDL runs in. It invokes SDL_main(), eventually
    1.78 +    // This is what SDL runs in. It invokes SDL_main(), eventually
    1.79      private Thread mSDLThread;    
    1.80      
    1.81 -    //EGL private objects
    1.82 +    // EGL private objects
    1.83      private EGLContext  mEGLContext;
    1.84      private EGLSurface  mEGLSurface;
    1.85      private EGLDisplay  mEGLDisplay;
    1.86  
    1.87 -    //Sensors
    1.88 +    // Sensors
    1.89      private static SensorManager mSensorManager;
    1.90  
    1.91 -    //Startup    
    1.92 +    // Startup    
    1.93      public SDLSurface(Context context) {
    1.94          super(context);
    1.95          getHolder().addCallback(this); 
    1.96 @@ -206,57 +199,94 @@
    1.97          requestFocus();
    1.98          setOnKeyListener(this); 
    1.99          setOnTouchListener(this);   
   1.100 -        
   1.101 +
   1.102          mSensorManager = (SensorManager)context.getSystemService("sensor");  
   1.103      }
   1.104  
   1.105 -    //Called when we have a valid drawing surface
   1.106 +    // Called when we have a valid drawing surface
   1.107      public void surfaceCreated(SurfaceHolder holder) {
   1.108 -        Log.v("SDL","Surface created"); 
   1.109 -
   1.110 -        int width = getWidth();
   1.111 -        int height = getHeight();
   1.112 -
   1.113 -        //Set the width and height variables in C before we start SDL so we have
   1.114 -        //it available on init
   1.115 -        SDLActivity.nativeSetScreenSize(width, height);
   1.116 -
   1.117 -        //Now start up the C app thread
   1.118 -        mSDLThread = new Thread(new SDLRunner(), "SDLThread"); 
   1.119 -		mSDLThread.start();       
   1.120      }
   1.121  
   1.122 -    //Called when we lose the surface
   1.123 +    // Called when we lose the surface
   1.124      public void surfaceDestroyed(SurfaceHolder holder) {
   1.125 -        Log.v("SDL","Surface destroyed");
   1.126 -        
   1.127 +
   1.128 +        // Send a quit message to the application
   1.129          SDLActivity.nativeQuit();
   1.130  
   1.131 -        //Now wait for the SDL thread to quit
   1.132 -        try{
   1.133 -            mSDLThread.wait();
   1.134 -        }catch(Exception e){
   1.135 -            Log.v("SDL","Problem stopping thread: " + e);
   1.136 +        // Now wait for the SDL thread to quit
   1.137 +        if (mSDLThread != null) {
   1.138 +            try {
   1.139 +                mSDLThread.wait();
   1.140 +            } catch(Exception e) {
   1.141 +                Log.v("SDL","Problem stopping thread: " + e);
   1.142 +            }
   1.143          }
   1.144      }
   1.145  
   1.146 -    //Called when the surface is resized
   1.147 -    public void surfaceChanged(SurfaceHolder holder, int format, 
   1.148 -                                int width, int height) {
   1.149 +    // Called when the surface is resized
   1.150 +    public void surfaceChanged(SurfaceHolder holder,
   1.151 +                               int format, int width, int height) {
   1.152          Log.v("SDL","Surface resized");
   1.153 -        
   1.154 +
   1.155 +        int sdlFormat = 0;
   1.156 +        switch (format) {
   1.157 +        case PixelFormat.A_8:
   1.158 +            Log.v("SDL","pixel format A_8");
   1.159 +            break;
   1.160 +        case PixelFormat.LA_88:
   1.161 +            Log.v("SDL","pixel format LA_88");
   1.162 +            break;
   1.163 +        case PixelFormat.L_8:
   1.164 +            Log.v("SDL","pixel format L_8");
   1.165 +            break;
   1.166 +        case PixelFormat.RGBA_4444:
   1.167 +            Log.v("SDL","pixel format RGBA_4444");
   1.168 +            sdlFormat = 0x85421002; // Doesn't have an SDL constant...
   1.169 +            break;
   1.170 +        case PixelFormat.RGBA_5551:
   1.171 +            Log.v("SDL","pixel format RGBA_5551");
   1.172 +            sdlFormat = 0x85441002; // Doesn't have an SDL constant...
   1.173 +            break;
   1.174 +        case PixelFormat.RGBA_8888:
   1.175 +            Log.v("SDL","pixel format RGBA_8888");
   1.176 +            sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
   1.177 +            break;
   1.178 +        case PixelFormat.RGBX_8888:
   1.179 +            Log.v("SDL","pixel format RGBX_8888");
   1.180 +            sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
   1.181 +            break;
   1.182 +        case PixelFormat.RGB_332:
   1.183 +            Log.v("SDL","pixel format RGB_332");
   1.184 +            sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
   1.185 +            break;
   1.186 +        case PixelFormat.RGB_565:
   1.187 +            Log.v("SDL","pixel format RGB_565");
   1.188 +            sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
   1.189 +            break;
   1.190 +        case PixelFormat.RGB_888:
   1.191 +            Log.v("SDL","pixel format RGB_888");
   1.192 +            // Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
   1.193 +            sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
   1.194 +            break;
   1.195 +        }
   1.196          SDLActivity.onNativeResize(width, height, format);
   1.197 +
   1.198 +        // Now start up the C app thread
   1.199 +        if (mSDLThread == null) {
   1.200 +            mSDLThread = new Thread(new SDLMain(), "SDLThread"); 
   1.201 +            mSDLThread.start();       
   1.202 +        }
   1.203      }
   1.204  
   1.205      //unused
   1.206      public void onDraw(Canvas canvas) {}
   1.207  
   1.208 -    
   1.209 -    //EGL functions
   1.210 -    public boolean initEGL(){
   1.211 -        Log.v("SDL","Starting up");
   1.212  
   1.213 -        try{
   1.214 +    // EGL functions
   1.215 +    public boolean initEGL() {
   1.216 +        Log.v("SDL", "Starting up");
   1.217 +
   1.218 +        try {
   1.219  
   1.220              EGL10 egl = (EGL10)EGLContext.getEGL();
   1.221  
   1.222 @@ -283,23 +313,20 @@
   1.223              mEGLContext = ctx;
   1.224              mEGLDisplay = dpy;
   1.225              mEGLSurface = surface;
   1.226 -            
   1.227 -            
   1.228 -        }catch(Exception e){
   1.229 +
   1.230 +        } catch(Exception e) {
   1.231              Log.v("SDL", e + "");
   1.232              for(StackTraceElement s : e.getStackTrace()){
   1.233                  Log.v("SDL", s.toString());
   1.234              }
   1.235          }
   1.236 -        Log.v("SDL","Done making!");
   1.237  
   1.238          return true;
   1.239      }
   1.240  
   1.241 -    //EGL buffer flip
   1.242 -    public void flipEGL(){      
   1.243 -        try{
   1.244 -        
   1.245 +    // EGL buffer flip
   1.246 +    public void flipEGL() {
   1.247 +        try {
   1.248              EGL10 egl = (EGL10)EGLContext.getEGL();
   1.249              GL10 gl = (GL10)mEGLContext.getGL();
   1.250  
   1.251 @@ -312,26 +339,22 @@
   1.252              egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
   1.253  
   1.254              
   1.255 -        }catch(Exception e){
   1.256 +        } catch(Exception e) {
   1.257              Log.v("SDL", "flipEGL(): " + e);
   1.258 -
   1.259              for(StackTraceElement s : e.getStackTrace()){
   1.260                  Log.v("SDL", s.toString());
   1.261              }
   1.262          }
   1.263      }
   1.264  
   1.265 -
   1.266 -  
   1.267 -    //Key events
   1.268 +    // Key events
   1.269      public boolean onKey(View  v, int keyCode, KeyEvent event){
   1.270  
   1.271 -        if(event.getAction() == KeyEvent.ACTION_DOWN){
   1.272 +        if (event.getAction() == KeyEvent.ACTION_DOWN) {
   1.273              SDLActivity.onNativeKeyDown(keyCode);
   1.274              return true;
   1.275          }
   1.276 -        
   1.277 -        else if(event.getAction() == KeyEvent.ACTION_UP){
   1.278 +        else if (event.getAction() == KeyEvent.ACTION_UP) {
   1.279              SDLActivity.onNativeKeyUp(keyCode);
   1.280              return true;
   1.281          }
   1.282 @@ -339,8 +362,8 @@
   1.283          return false;
   1.284      }
   1.285  
   1.286 -    //Touch events
   1.287 -    public boolean onTouch(View v, MotionEvent event){
   1.288 +    // Touch events
   1.289 +    public boolean onTouch(View v, MotionEvent event) {
   1.290      
   1.291          int action = event.getAction();
   1.292          float x = event.getX();
   1.293 @@ -352,32 +375,30 @@
   1.294          return true;
   1.295      }
   1.296  
   1.297 -    //Sensor events
   1.298 -    public void enableSensor(int sensortype, boolean enabled){
   1.299 +    // Sensor events
   1.300 +    public void enableSensor(int sensortype, boolean enabled) {
   1.301          //TODO: This uses getDefaultSensor - what if we have >1 accels?
   1.302 -        if(enabled){
   1.303 +        if (enabled) {
   1.304              mSensorManager.registerListener(this, 
   1.305                              mSensorManager.getDefaultSensor(sensortype), 
   1.306                              SensorManager.SENSOR_DELAY_GAME, null);
   1.307 -        }else{
   1.308 +        } else {
   1.309              mSensorManager.unregisterListener(this, 
   1.310                              mSensorManager.getDefaultSensor(sensortype));
   1.311          }
   1.312      }
   1.313      
   1.314 -    public void onAccuracyChanged(Sensor sensor, int accuracy){
   1.315 +    public void onAccuracyChanged(Sensor sensor, int accuracy) {
   1.316          //TODO
   1.317      }
   1.318  
   1.319 -    public void onSensorChanged(SensorEvent event){
   1.320 -        if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
   1.321 -            SDLActivity.onNativeAccel(  event.values[0],
   1.322 -                                        event.values[1],
   1.323 -                                        event.values[2] );
   1.324 +    public void onSensorChanged(SensorEvent event) {
   1.325 +        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
   1.326 +            SDLActivity.onNativeAccel(event.values[0],
   1.327 +                                      event.values[1],
   1.328 +                                      event.values[2]);
   1.329          }
   1.330      }
   1.331  
   1.332 -
   1.333  }
   1.334  
   1.335 -
     2.1 --- a/src/SDL_android.cpp	Wed Jan 12 13:52:41 2011 -0800
     2.2 +++ b/src/SDL_android.cpp	Wed Jan 12 14:29:01 2011 -0800
     2.3 @@ -24,6 +24,7 @@
     2.4  extern "C" {
     2.5  #include "events/SDL_events_c.h"
     2.6  #include "video/android/SDL_androidkeyboard.h"
     2.7 +#include "video/android/SDL_androidvideo.h"
     2.8  }
     2.9  
    2.10  /*******************************************************************************
    2.11 @@ -49,9 +50,6 @@
    2.12  jmethodID midEnableFeature;
    2.13  jmethodID midUpdateAudio;
    2.14  
    2.15 -extern "C" void Android_SetScreenResolution(int width, int height);
    2.16 -extern "C" int SDL_SendQuit();
    2.17 -
    2.18  //If we're not the active app, don't try to render
    2.19  bool bRenderingEnabled = false;
    2.20  
    2.21 @@ -145,21 +143,12 @@
    2.22      SDL_SendQuit();
    2.23  }
    2.24  
    2.25 -// Screen size
    2.26 -extern "C" void Java_org_libsdl_app_SDLActivity_nativeSetScreenSize(
    2.27 -                JNIEnv*  env, jobject obj, jint width, jint height)
    2.28 -{
    2.29 -    __android_log_print(ANDROID_LOG_INFO, "SDL", 
    2.30 -                        "SDL: Set screen size on init: %d/%d\n", width, height);
    2.31 -    Android_SetScreenResolution(width, height);
    2.32 -}
    2.33 -
    2.34  // Resize
    2.35  extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
    2.36                                          JNIEnv*  env, jobject obj, jint width, 
    2.37                                          jint height, jint format)
    2.38  {
    2.39 -    /* FIXME: What is the relationship between this and the window? */
    2.40 +    Android_SetScreenResolution(width, height, format);
    2.41  }
    2.42  
    2.43  extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
    2.44 @@ -229,3 +218,5 @@
    2.45      __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
    2.46      
    2.47  }
    2.48 +
    2.49 +/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/video/android/SDL_androidvideo.c	Wed Jan 12 13:52:41 2011 -0800
     3.2 +++ b/src/video/android/SDL_androidvideo.c	Wed Jan 12 14:29:01 2011 -0800
     3.3 @@ -56,10 +56,11 @@
     3.4  /* Android driver bootstrap functions */
     3.5  
     3.6  
     3.7 -//These are filled in with real values in Android_SetScreenResolution on 
     3.8 -//init (before SDL_Main())
     3.9 -static int iScreenWidth = 320;
    3.10 -static int iScreenHeight = 240;
    3.11 +// These are filled in with real values in Android_SetScreenResolution on 
    3.12 +// init (before SDL_main())
    3.13 +static Uint32 iScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    3.14 +static int iScreenWidth = 0;
    3.15 +static int iScreenHeight = 0;
    3.16  
    3.17  
    3.18  static int
    3.19 @@ -122,8 +123,7 @@
    3.20  {
    3.21      SDL_DisplayMode mode;
    3.22  
    3.23 -    /* Use a fake 32-bpp desktop mode */
    3.24 -    mode.format = SDL_PIXELFORMAT_BGR888;
    3.25 +    mode.format = iScreenFormat;
    3.26      mode.w = iScreenWidth;
    3.27      mode.h = iScreenHeight;
    3.28      mode.refresh_rate = 0;
    3.29 @@ -146,11 +146,12 @@
    3.30  {
    3.31  }
    3.32  
    3.33 -
    3.34 -void Android_SetScreenResolution(int width, int height){
    3.35 +void
    3.36 +Android_SetScreenResolution(int width, int height, Uint32 format)
    3.37 +{
    3.38      iScreenWidth = width;
    3.39      iScreenHeight = height;   
    3.40 +    iScreenFormat = format;
    3.41  }
    3.42  
    3.43 -
    3.44  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/video/android/SDL_androidvideo.h	Wed Jan 12 13:52:41 2011 -0800
     4.2 +++ b/src/video/android/SDL_androidvideo.h	Wed Jan 12 14:29:01 2011 -0800
     4.3 @@ -26,6 +26,9 @@
     4.4  
     4.5  #include "../SDL_sysvideo.h"
     4.6  
     4.7 -#endif /* _SDL_ndsvideo_h */
     4.8 +/* Called by the JNI layer when the screen changes size or format */
     4.9 +extern void Android_SetScreenResolution(int width, int height, Uint32 format);
    4.10 +
    4.11 +#endif /* _SDL_androidvideo_h */
    4.12  
    4.13  /* vi: set ts=4 sw=4 expandtab: */