changeset 4723:74da47b2f5b7

More joystick stuff
author Paul Hunkin <paul@bieh.net>
date Tue, 27 Jul 2010 21:58:18 +0200
parents faa228f7ce5b
children d86332c0fb9b
files android/testproject/jni/app-android.cpp android/testproject/jni/lesson05.c android/testproject/src/org/libsdl/android/SDLActivity.java
diffstat 3 files changed, 72 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/android/testproject/jni/app-android.cpp	Tue Jul 27 21:21:24 2010 +0200
+++ b/android/testproject/jni/app-android.cpp	Tue Jul 27 21:58:18 2010 +0200
@@ -48,7 +48,7 @@
 bool bRenderingEnabled = false;
 
 //Feature IDs
-static const int FEATURE_SOUND = 1;
+static const int FEATURE_AUDIO = 1;
 static const int FEATURE_ACCEL = 2;
 
 //Accelerometer data storage
@@ -76,7 +76,7 @@
     mActivityInstance = cls;
     midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
     midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
-    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(I, I)V");
+    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
 
     if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature){
         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
@@ -196,7 +196,7 @@
 
 extern "C" void Android_EnableFeature(int featureid, bool enabled){
 
-    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers, 
+    mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature, 
                                 featureid, (int)enabled); 
 }
 
--- a/android/testproject/jni/lesson05.c	Tue Jul 27 21:21:24 2010 +0200
+++ b/android/testproject/jni/lesson05.c	Tue Jul 27 21:58:18 2010 +0200
@@ -511,7 +511,7 @@
     resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
 
 
-    testAudio();
+    //testAudio();
 
 
     /* wait for events */ 
--- a/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 21:21:24 2010 +0200
+++ b/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 21:58:18 2010 +0200
@@ -13,6 +13,8 @@
 import android.text.method.*;
 import android.text.*;
 import android.media.*;
+import android.hardware.*;
+import android.content.*;
 
 import java.lang.*;
 
@@ -25,11 +27,16 @@
     //Main components
     private static SDLActivity mSingleton;
     private static SDLSurface mSurface;
-    
+
+    //Audio
     private static AudioTrack mAudioTrack;
+    private static boolean bAudioIsEnabled;
+
+    //Sensors
+    private static boolean bAccelIsEnabled;
 
     //feature IDs. Must match up on the C side as well.
-    private static int FEATURE_SOUND = 1;
+    private static int FEATURE_AUDIO = 1;
     private static int FEATURE_ACCEL = 2;
 
     //Load the .so
@@ -52,6 +59,7 @@
         
     }
 
+    //Audio
     public static boolean initAudio(){        
 
         //blah. Hardcoded things are bad. FIXME when we have more sound stuff
@@ -61,10 +69,25 @@
                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                     AudioFormat.ENCODING_PCM_8BIT,
                     2048,
-                    AudioTrack.MODE_STREAM);        
+                    AudioTrack.MODE_STREAM);   
+        bAudioIsEnabled = true;     
         return true;
     }
 
+    //Accel
+    public static boolean initAccel(){
+        mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
+        bAccelIsEnabled = true;
+        return true;
+    }
+    
+    public static boolean closeAccel(){
+        mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, false);
+        bAccelIsEnabled = false;
+        return true;
+    }
+    
+
     //Events
     protected void onPause() {
         super.onPause();
@@ -87,7 +110,7 @@
     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);
 
 
 
@@ -117,7 +140,7 @@
 
         //Yuck. This is all horribly inelegent. If it gets to more than a few
         //'features' I'll rip this out and make something nicer, I promise :)
-        if(featureid == FEATURE_SOUND){
+        if(featureid == FEATURE_AUDIO){
             if(enabled == 1){
                 initAudio();
             }else{
@@ -125,6 +148,14 @@
                 //closeAudio(); 
             }
         }
+
+        else if(featureid == FEATURE_ACCEL){
+            if(enabled == 1){
+                initAccel();
+            }else{
+                closeAccel();
+            }
+        }
     }
 
 
@@ -157,7 +188,7 @@
     Because of this, that's where we set up the SDL thread
 */
 class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, 
-    View.OnKeyListener, View.OnTouchListener  {
+    View.OnKeyListener, View.OnTouchListener, SensorEventListener  {
 
     //This is what SDL runs in. It invokes SDL_main(), eventually
     private Thread mSDLThread;    
@@ -167,6 +198,9 @@
     private EGLSurface  mEGLSurface;
     private EGLDisplay  mEGLDisplay;
 
+    //Sensors
+    private static SensorManager mSensorManager;
+
     //Startup    
     public SDLSurface(Context context) {
         super(context);
@@ -176,7 +210,9 @@
         setFocusableInTouchMode(true);
         requestFocus();
         setOnKeyListener(this); 
-        setOnTouchListener(this);     
+        setOnTouchListener(this);   
+        
+        mSensorManager = (SensorManager)context.getSystemService("sensor");  
     }
 
     //Called when we have a valid drawing surface
@@ -321,6 +357,31 @@
         return true;
     }
 
+    //Sensor events
+    public void enableSensor(int sensortype, boolean enabled){
+        //TODO: This uses getDefaultSensor - what if we have >1 accels?
+        if(enabled){
+            mSensorManager.registerListener(this, 
+                            mSensorManager.getDefaultSensor(sensortype), 
+                            SensorManager.SENSOR_DELAY_GAME, null);
+        }else{
+            mSensorManager.unregisterListener(this, 
+                            mSensorManager.getDefaultSensor(sensortype));
+        }
+    }
+    
+    public void onAccuracyChanged(Sensor sensor, int accuracy){
+        //TODO
+    }
+
+    public void onSensorChanged(SensorEvent event){
+        if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
+            SDLActivity.onNativeAccel(  event.values[0],
+                                        event.values[1],
+                                        event.values[2] );
+        }
+    }
+
 
 }