More joystick stuff
authorPaul Hunkin <paul@bieh.net>
Tue, 27 Jul 2010 21:58:18 +0200
changeset 472374da47b2f5b7
parent 4722 faa228f7ce5b
child 4724 d86332c0fb9b
More joystick stuff
android/testproject/jni/app-android.cpp
android/testproject/jni/lesson05.c
android/testproject/src/org/libsdl/android/SDLActivity.java
     1.1 --- a/android/testproject/jni/app-android.cpp	Tue Jul 27 21:21:24 2010 +0200
     1.2 +++ b/android/testproject/jni/app-android.cpp	Tue Jul 27 21:58:18 2010 +0200
     1.3 @@ -48,7 +48,7 @@
     1.4  bool bRenderingEnabled = false;
     1.5  
     1.6  //Feature IDs
     1.7 -static const int FEATURE_SOUND = 1;
     1.8 +static const int FEATURE_AUDIO = 1;
     1.9  static const int FEATURE_ACCEL = 2;
    1.10  
    1.11  //Accelerometer data storage
    1.12 @@ -76,7 +76,7 @@
    1.13      mActivityInstance = cls;
    1.14      midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    1.15      midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    1.16 -    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(I, I)V");
    1.17 +    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
    1.18  
    1.19      if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature){
    1.20          __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    1.21 @@ -196,7 +196,7 @@
    1.22  
    1.23  extern "C" void Android_EnableFeature(int featureid, bool enabled){
    1.24  
    1.25 -    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers, 
    1.26 +    mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature, 
    1.27                                  featureid, (int)enabled); 
    1.28  }
    1.29  
     2.1 --- a/android/testproject/jni/lesson05.c	Tue Jul 27 21:21:24 2010 +0200
     2.2 +++ b/android/testproject/jni/lesson05.c	Tue Jul 27 21:58:18 2010 +0200
     2.3 @@ -511,7 +511,7 @@
     2.4      resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
     2.5  
     2.6  
     2.7 -    testAudio();
     2.8 +    //testAudio();
     2.9  
    2.10  
    2.11      /* wait for events */ 
     3.1 --- a/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 21:21:24 2010 +0200
     3.2 +++ b/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 21:58:18 2010 +0200
     3.3 @@ -13,6 +13,8 @@
     3.4  import android.text.method.*;
     3.5  import android.text.*;
     3.6  import android.media.*;
     3.7 +import android.hardware.*;
     3.8 +import android.content.*;
     3.9  
    3.10  import java.lang.*;
    3.11  
    3.12 @@ -25,11 +27,16 @@
    3.13      //Main components
    3.14      private static SDLActivity mSingleton;
    3.15      private static SDLSurface mSurface;
    3.16 -    
    3.17 +
    3.18 +    //Audio
    3.19      private static AudioTrack mAudioTrack;
    3.20 +    private static boolean bAudioIsEnabled;
    3.21 +
    3.22 +    //Sensors
    3.23 +    private static boolean bAccelIsEnabled;
    3.24  
    3.25      //feature IDs. Must match up on the C side as well.
    3.26 -    private static int FEATURE_SOUND = 1;
    3.27 +    private static int FEATURE_AUDIO = 1;
    3.28      private static int FEATURE_ACCEL = 2;
    3.29  
    3.30      //Load the .so
    3.31 @@ -52,6 +59,7 @@
    3.32          
    3.33      }
    3.34  
    3.35 +    //Audio
    3.36      public static boolean initAudio(){        
    3.37  
    3.38          //blah. Hardcoded things are bad. FIXME when we have more sound stuff
    3.39 @@ -61,10 +69,25 @@
    3.40                      AudioFormat.CHANNEL_CONFIGURATION_MONO,
    3.41                      AudioFormat.ENCODING_PCM_8BIT,
    3.42                      2048,
    3.43 -                    AudioTrack.MODE_STREAM);        
    3.44 +                    AudioTrack.MODE_STREAM);   
    3.45 +        bAudioIsEnabled = true;     
    3.46          return true;
    3.47      }
    3.48  
    3.49 +    //Accel
    3.50 +    public static boolean initAccel(){
    3.51 +        mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
    3.52 +        bAccelIsEnabled = true;
    3.53 +        return true;
    3.54 +    }
    3.55 +    
    3.56 +    public static boolean closeAccel(){
    3.57 +        mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, false);
    3.58 +        bAccelIsEnabled = false;
    3.59 +        return true;
    3.60 +    }
    3.61 +    
    3.62 +
    3.63      //Events
    3.64      protected void onPause() {
    3.65          super.onPause();
    3.66 @@ -87,7 +110,7 @@
    3.67      public static native void onNativeTouch(int action, float x, 
    3.68                                              float y, float p);
    3.69      public static native void onNativeResize(int x, int y, int format);
    3.70 -
    3.71 +    public static native void onNativeAccel(float x, float y, float z);
    3.72  
    3.73  
    3.74  
    3.75 @@ -117,7 +140,7 @@
    3.76  
    3.77          //Yuck. This is all horribly inelegent. If it gets to more than a few
    3.78          //'features' I'll rip this out and make something nicer, I promise :)
    3.79 -        if(featureid == FEATURE_SOUND){
    3.80 +        if(featureid == FEATURE_AUDIO){
    3.81              if(enabled == 1){
    3.82                  initAudio();
    3.83              }else{
    3.84 @@ -125,6 +148,14 @@
    3.85                  //closeAudio(); 
    3.86              }
    3.87          }
    3.88 +
    3.89 +        else if(featureid == FEATURE_ACCEL){
    3.90 +            if(enabled == 1){
    3.91 +                initAccel();
    3.92 +            }else{
    3.93 +                closeAccel();
    3.94 +            }
    3.95 +        }
    3.96      }
    3.97  
    3.98  
    3.99 @@ -157,7 +188,7 @@
   3.100      Because of this, that's where we set up the SDL thread
   3.101  */
   3.102  class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, 
   3.103 -    View.OnKeyListener, View.OnTouchListener  {
   3.104 +    View.OnKeyListener, View.OnTouchListener, SensorEventListener  {
   3.105  
   3.106      //This is what SDL runs in. It invokes SDL_main(), eventually
   3.107      private Thread mSDLThread;    
   3.108 @@ -167,6 +198,9 @@
   3.109      private EGLSurface  mEGLSurface;
   3.110      private EGLDisplay  mEGLDisplay;
   3.111  
   3.112 +    //Sensors
   3.113 +    private static SensorManager mSensorManager;
   3.114 +
   3.115      //Startup    
   3.116      public SDLSurface(Context context) {
   3.117          super(context);
   3.118 @@ -176,7 +210,9 @@
   3.119          setFocusableInTouchMode(true);
   3.120          requestFocus();
   3.121          setOnKeyListener(this); 
   3.122 -        setOnTouchListener(this);     
   3.123 +        setOnTouchListener(this);   
   3.124 +        
   3.125 +        mSensorManager = (SensorManager)context.getSystemService("sensor");  
   3.126      }
   3.127  
   3.128      //Called when we have a valid drawing surface
   3.129 @@ -321,6 +357,31 @@
   3.130          return true;
   3.131      }
   3.132  
   3.133 +    //Sensor events
   3.134 +    public void enableSensor(int sensortype, boolean enabled){
   3.135 +        //TODO: This uses getDefaultSensor - what if we have >1 accels?
   3.136 +        if(enabled){
   3.137 +            mSensorManager.registerListener(this, 
   3.138 +                            mSensorManager.getDefaultSensor(sensortype), 
   3.139 +                            SensorManager.SENSOR_DELAY_GAME, null);
   3.140 +        }else{
   3.141 +            mSensorManager.unregisterListener(this, 
   3.142 +                            mSensorManager.getDefaultSensor(sensortype));
   3.143 +        }
   3.144 +    }
   3.145 +    
   3.146 +    public void onAccuracyChanged(Sensor sensor, int accuracy){
   3.147 +        //TODO
   3.148 +    }
   3.149 +
   3.150 +    public void onSensorChanged(SensorEvent event){
   3.151 +        if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
   3.152 +            SDLActivity.onNativeAccel(  event.values[0],
   3.153 +                                        event.values[1],
   3.154 +                                        event.values[2] );
   3.155 +        }
   3.156 +    }
   3.157 +
   3.158  
   3.159  }
   3.160