Added orientation sensor support

This commit is contained in:
Sergii Pylypenko
2015-07-14 21:28:45 +03:00
parent 4635e72dd5
commit b7733367a3
4 changed files with 59 additions and 4 deletions

View File

@@ -258,6 +258,9 @@ echo >> AndroidAppSettings.cfg
echo "# Application uses gyroscope (y) or (n), the gyroscope will be used as joystick 1 axes 2-4" >> AndroidAppSettings.cfg echo "# Application uses gyroscope (y) or (n), the gyroscope will be used as joystick 1 axes 2-4" >> AndroidAppSettings.cfg
echo AppUsesGyroscope=$AppUsesGyroscope >> AndroidAppSettings.cfg echo AppUsesGyroscope=$AppUsesGyroscope >> AndroidAppSettings.cfg
echo >> AndroidAppSettings.cfg echo >> AndroidAppSettings.cfg
echo "# Application uses orientation sensor (y) or (n), reported as joystick 1 axes 8-10" >> AndroidAppSettings.cfg
echo AppUsesOrientationSensor=$AppUsesOrientationSensor >> AndroidAppSettings.cfg
echo >> AndroidAppSettings.cfg
echo "# Use gyroscope to move mouse cursor (y) or (n), it eats battery, and can be disabled in settings, do not use with AppUsesGyroscope setting" >> AndroidAppSettings.cfg echo "# Use gyroscope to move mouse cursor (y) or (n), it eats battery, and can be disabled in settings, do not use with AppUsesGyroscope setting" >> AndroidAppSettings.cfg
echo MoveMouseWithGyroscope=$MoveMouseWithGyroscope >> AndroidAppSettings.cfg echo MoveMouseWithGyroscope=$MoveMouseWithGyroscope >> AndroidAppSettings.cfg
echo >> AndroidAppSettings.cfg echo >> AndroidAppSettings.cfg
@@ -595,6 +598,12 @@ else
AppUsesGyroscope=false AppUsesGyroscope=false
fi fi
if [ "$AppUsesOrientationSensor" = "y" ] ; then
AppUsesOrientationSensor=true
else
AppUsesOrientationSensor=false
fi
if [ "$MoveMouseWithGyroscope" = "y" ] ; then if [ "$MoveMouseWithGyroscope" = "y" ] ; then
MoveMouseWithGyroscope=true MoveMouseWithGyroscope=true
else else
@@ -805,6 +814,7 @@ $SEDI "s/public static boolean AppUsesSecondJoystick = .*;/public static boolean
$SEDI "s/public static boolean AppUsesThirdJoystick = .*;/public static boolean AppUsesThirdJoystick = $AppUsesThirdJoystick;/" project/src/Globals.java $SEDI "s/public static boolean AppUsesThirdJoystick = .*;/public static boolean AppUsesThirdJoystick = $AppUsesThirdJoystick;/" project/src/Globals.java
$SEDI "s/public static boolean AppUsesAccelerometer = .*;/public static boolean AppUsesAccelerometer = $AppUsesAccelerometer;/" project/src/Globals.java $SEDI "s/public static boolean AppUsesAccelerometer = .*;/public static boolean AppUsesAccelerometer = $AppUsesAccelerometer;/" project/src/Globals.java
$SEDI "s/public static boolean AppUsesGyroscope = .*;/public static boolean AppUsesGyroscope = $AppUsesGyroscope;/" project/src/Globals.java $SEDI "s/public static boolean AppUsesGyroscope = .*;/public static boolean AppUsesGyroscope = $AppUsesGyroscope;/" project/src/Globals.java
$SEDI "s/public static boolean AppUsesOrientationSensor = .*;/public static boolean AppUsesOrientationSensor = $AppUsesOrientationSensor;/" project/src/Globals.java
$SEDI "s/public static boolean MoveMouseWithGyroscope = .*;/public static boolean MoveMouseWithGyroscope = $MoveMouseWithGyroscope;/" project/src/Globals.java $SEDI "s/public static boolean MoveMouseWithGyroscope = .*;/public static boolean MoveMouseWithGyroscope = $MoveMouseWithGyroscope;/" project/src/Globals.java
$SEDI "s/public static boolean AppUsesMultitouch = .*;/public static boolean AppUsesMultitouch = $AppUsesMultitouch;/" project/src/Globals.java $SEDI "s/public static boolean AppUsesMultitouch = .*;/public static boolean AppUsesMultitouch = $AppUsesMultitouch;/" project/src/Globals.java
$SEDI "s/public static boolean NonBlockingSwapBuffers = .*;/public static boolean NonBlockingSwapBuffers = $NonBlockingSwapBuffers;/" project/src/Globals.java $SEDI "s/public static boolean NonBlockingSwapBuffers = .*;/public static boolean NonBlockingSwapBuffers = $NonBlockingSwapBuffers;/" project/src/Globals.java

View File

@@ -35,6 +35,7 @@ import android.hardware.Sensor;
import android.hardware.SensorEvent; import android.hardware.SensorEvent;
import android.util.Log; import android.util.Log;
import android.widget.TextView; import android.widget.TextView;
import android.os.Build;
class AccelerometerReader implements SensorEventListener class AccelerometerReader implements SensorEventListener
@@ -43,6 +44,7 @@ class AccelerometerReader implements SensorEventListener
private SensorManager _manager = null; private SensorManager _manager = null;
public boolean openedBySDL = false; public boolean openedBySDL = false;
public static final GyroscopeListener gyro = new GyroscopeListener(); public static final GyroscopeListener gyro = new GyroscopeListener();
public static final OrientationListener orientation = new OrientationListener();
public AccelerometerReader(Activity context) public AccelerometerReader(Activity context)
{ {
@@ -53,9 +55,10 @@ class AccelerometerReader implements SensorEventListener
{ {
if( _manager != null ) if( _manager != null )
{ {
Log.i("SDL", "libSDL: stopping accelerometer/gyroscope"); Log.i("SDL", "libSDL: stopping accelerometer/gyroscope/orientation");
_manager.unregisterListener(this); _manager.unregisterListener(this);
_manager.unregisterListener(gyro); _manager.unregisterListener(gyro);
_manager.unregisterListener(orientation);
} }
} }
@@ -73,6 +76,14 @@ class AccelerometerReader implements SensorEventListener
Log.i("SDL", "libSDL: starting gyroscope"); Log.i("SDL", "libSDL: starting gyroscope");
_manager.registerListener(gyro, _manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME); _manager.registerListener(gyro, _manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
} }
if( (Globals.AppUsesOrientationSensor) && _manager != null &&
_manager.getDefaultSensor(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ? Sensor.TYPE_GAME_ROTATION_VECTOR : Sensor.TYPE_ROTATION_VECTOR) != null )
{
Log.i("SDL", "libSDL: starting orientation sensor");
_manager.registerListener(orientation, _manager.getDefaultSensor(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ? Sensor.TYPE_GAME_ROTATION_VECTOR : Sensor.TYPE_ROTATION_VECTOR),
SensorManager.SENSOR_DELAY_GAME);
}
} }
public void onSensorChanged(SensorEvent event) public void onSensorChanged(SensorEvent event)
@@ -129,7 +140,10 @@ class AccelerometerReader implements SensorEventListener
SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
if ( manager == null && manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) == null ) if ( manager == null && manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) == null )
return; return;
manager.registerListener(l, manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME); manager.registerListener(gyro, manager.getDefaultSensor(
Globals.AppUsesOrientationSensor ? Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ?
Sensor.TYPE_GAME_ROTATION_VECTOR : Sensor.TYPE_ROTATION_VECTOR : Sensor.TYPE_GYROSCOPE),
SensorManager.SENSOR_DELAY_GAME);
} }
public void unregisterListener(Activity context,SensorEventListener l) public void unregisterListener(Activity context,SensorEventListener l)
{ {
@@ -140,6 +154,21 @@ class AccelerometerReader implements SensorEventListener
} }
} }
static class OrientationListener implements SensorEventListener
{
public OrientationListener()
{
}
public void onSensorChanged(SensorEvent event)
{
nativeOrientation(event.values[0], event.values[1], event.values[2]);
}
public void onAccuracyChanged(Sensor s, int a)
{
}
}
private static native void nativeAccelerometer(float accX, float accY, float accZ); private static native void nativeAccelerometer(float accX, float accY, float accZ);
private static native void nativeGyroscope(float X, float Y, float Z); private static native void nativeGyroscope(float X, float Y, float Z);
private static native void nativeOrientation(float X, float Y, float Z);
} }

View File

@@ -61,6 +61,7 @@ class Globals
public static boolean AppUsesThirdJoystick = false; public static boolean AppUsesThirdJoystick = false;
public static boolean AppUsesAccelerometer = false; public static boolean AppUsesAccelerometer = false;
public static boolean AppUsesGyroscope = false; public static boolean AppUsesGyroscope = false;
public static boolean AppUsesOrientationSensor = false;
public static boolean AppUsesMultitouch = false; public static boolean AppUsesMultitouch = false;
public static boolean NonBlockingSwapBuffers = false; public static boolean NonBlockingSwapBuffers = false;
public static boolean ResetSdlConfigForThisVersion = false; public static boolean ResetSdlConfigForThisVersion = false;

View File

@@ -1063,7 +1063,6 @@ JAVA_EXPORT_NAME(AccelerometerReader_nativeAccelerometer) ( JNIEnv* env, jobjec
SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 7, fminf(32767.0f, fmaxf(-32767.0f, accPosZ*1000.0f))); SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 7, fminf(32767.0f, fmaxf(-32767.0f, accPosZ*1000.0f)));
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(AccelerometerReader_nativeGyroscope) ( JNIEnv* env, jobject thiz, jfloat X, jfloat Y, jfloat Z ) JAVA_EXPORT_NAME(AccelerometerReader_nativeGyroscope) ( JNIEnv* env, jobject thiz, jfloat X, jfloat Y, jfloat Z )
{ {
@@ -1099,6 +1098,22 @@ JAVA_EXPORT_NAME(AccelerometerReader_nativeGyroscope) ( JNIEnv* env, jobject th
} }
} }
JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(AccelerometerReader_nativeOrientation) ( JNIEnv* env, jobject thiz, jfloat X, jfloat Y, jfloat Z )
{
#if SDL_VERSION_ATLEAST(1,3,0)
#else
if( !SDL_CurrentVideoSurface )
return;
#endif
//__android_log_print(ANDROID_LOG_INFO, "libSDL", "Orientation %f %f %f", X, Y, Z);
SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 8, NORMALIZE_FLOAT_32767(X));
SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 9, NORMALIZE_FLOAT_32767(Y));
SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 10, NORMALIZE_FLOAT_32767(Z));
}
static int getClickTimeout(int v) static int getClickTimeout(int v)
{ {
switch(v) switch(v)
@@ -1489,7 +1504,7 @@ int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
} }
if( joystick->index == JOY_ACCELGYRO ) if( joystick->index == JOY_ACCELGYRO )
{ {
joystick->naxes = 8; // Normalized accelerometer = axes 0-1, gyroscope = axes 2-4, raw accelerometer = axes 5-7 joystick->naxes = 11; // Normalized accelerometer = axes 0-1, gyroscope = axes 2-4, raw accelerometer = axes 5-7, orientation = axes 8-10
if( !moveMouseWithGyroscope ) if( !moveMouseWithGyroscope )
SDL_ANDROID_CallJavaStartAccelerometerGyroscope(1); SDL_ANDROID_CallJavaStartAccelerometerGyroscope(1);
} }