Gyroscope support

This commit is contained in:
pelya
2013-02-01 19:16:25 +02:00
parent 45128c67b4
commit a12c6ada8d
12 changed files with 86 additions and 28 deletions

View File

@@ -308,7 +308,7 @@ fi
if [ -z "$AppUsesAccelerometer" -o -z "$AUTO" ]; then if [ -z "$AppUsesAccelerometer" -o -z "$AUTO" ]; then
echo echo
echo -n "Application uses accelerometer (y) or (n), the accelerometer will be used as joystick 0 axes 2-3 ($AppUsesAccelerometer): " echo -n "Application uses accelerometer (y) or (n), the accelerometer will be used as joystick 1 axes 0-1 ($AppUsesAccelerometer): "
read var read var
if [ -n "$var" ] ; then if [ -n "$var" ] ; then
AppUsesAccelerometer="$var" AppUsesAccelerometer="$var"
@@ -316,6 +316,16 @@ if [ -n "$var" ] ; then
fi fi
fi fi
if [ -z "$AppUsesGyroscope" -o -z "$AUTO" ]; then
echo
echo -n "Application uses gyroscope (y) or (n), the gyroscope will be used as joystick 1 axes 2-4 ($AppUsesGyroscope): "
read var
if [ -n "$var" ] ; then
AppUsesGyroscope="$var"
CHANGED=1
fi
fi
if [ -z "$AppUsesMultitouch" -o -z "$AUTO" ]; then if [ -z "$AppUsesMultitouch" -o -z "$AUTO" ]; then
echo echo
echo "Application uses multitouch (y) or (n), multitouch events are passed as SDL_JOYBUTTONDOWN/SDL_JOYBALLMOTION events" echo "Application uses multitouch (y) or (n), multitouch events are passed as SDL_JOYBUTTONDOWN/SDL_JOYBALLMOTION events"
@@ -671,6 +681,7 @@ echo AppNeedsArrowKeys=$AppNeedsArrowKeys >> AndroidAppSettings.cfg
echo AppNeedsTextInput=$AppNeedsTextInput >> AndroidAppSettings.cfg echo AppNeedsTextInput=$AppNeedsTextInput >> AndroidAppSettings.cfg
echo AppUsesJoystick=$AppUsesJoystick >> AndroidAppSettings.cfg echo AppUsesJoystick=$AppUsesJoystick >> AndroidAppSettings.cfg
echo AppUsesAccelerometer=$AppUsesAccelerometer >> AndroidAppSettings.cfg echo AppUsesAccelerometer=$AppUsesAccelerometer >> AndroidAppSettings.cfg
echo AppUsesGyroscope=$AppUsesGyroscope >> AndroidAppSettings.cfg
echo AppUsesMultitouch=$AppUsesMultitouch >> AndroidAppSettings.cfg echo AppUsesMultitouch=$AppUsesMultitouch >> AndroidAppSettings.cfg
echo NonBlockingSwapBuffers=$NonBlockingSwapBuffers >> AndroidAppSettings.cfg echo NonBlockingSwapBuffers=$NonBlockingSwapBuffers >> AndroidAppSettings.cfg
echo RedefinedKeys=\"$RedefinedKeys\" >> AndroidAppSettings.cfg echo RedefinedKeys=\"$RedefinedKeys\" >> AndroidAppSettings.cfg
@@ -840,6 +851,12 @@ else
AppUsesAccelerometer=false AppUsesAccelerometer=false
fi fi
if [ "$AppUsesGyroscope" = "y" ] ; then
AppUsesGyroscope=true
else
AppUsesGyroscope=false
fi
if [ "$AppUsesMultitouch" = "y" ] ; then if [ "$AppUsesMultitouch" = "y" ] ; then
AppUsesMultitouch=true AppUsesMultitouch=true
else else
@@ -973,6 +990,7 @@ cat project/src/Globals.java | \
sed "s/public static boolean AppNeedsTextInput = .*;/public static boolean AppNeedsTextInput = $AppNeedsTextInput;/" | \ sed "s/public static boolean AppNeedsTextInput = .*;/public static boolean AppNeedsTextInput = $AppNeedsTextInput;/" | \
sed "s/public static boolean AppUsesJoystick = .*;/public static boolean AppUsesJoystick = $AppUsesJoystick;/" | \ sed "s/public static boolean AppUsesJoystick = .*;/public static boolean AppUsesJoystick = $AppUsesJoystick;/" | \
sed "s/public static boolean AppUsesAccelerometer = .*;/public static boolean AppUsesAccelerometer = $AppUsesAccelerometer;/" | \ sed "s/public static boolean AppUsesAccelerometer = .*;/public static boolean AppUsesAccelerometer = $AppUsesAccelerometer;/" | \
sed "s/public static boolean AppUsesGyroscope = .*;/public static boolean AppUsesGyroscope = $AppUsesGyroscope;/" | \
sed "s/public static boolean AppUsesMultitouch = .*;/public static boolean AppUsesMultitouch = $AppUsesMultitouch;/" | \ sed "s/public static boolean AppUsesMultitouch = .*;/public static boolean AppUsesMultitouch = $AppUsesMultitouch;/" | \
sed "s/public static boolean NonBlockingSwapBuffers = .*;/public static boolean NonBlockingSwapBuffers = $NonBlockingSwapBuffers;/" | \ sed "s/public static boolean NonBlockingSwapBuffers = .*;/public static boolean NonBlockingSwapBuffers = $NonBlockingSwapBuffers;/" | \
sed "s/public static boolean ResetSdlConfigForThisVersion = .*;/public static boolean ResetSdlConfigForThisVersion = $ResetSdlConfigForThisVersion;/" | \ sed "s/public static boolean ResetSdlConfigForThisVersion = .*;/public static boolean ResetSdlConfigForThisVersion = $ResetSdlConfigForThisVersion;/" | \

View File

@@ -41,32 +41,35 @@ class AccelerometerReader implements SensorEventListener
{ {
private SensorManager _manager = null; private SensorManager _manager = null;
public boolean openedBySDL = false;
public AccelerometerReader(Activity context) public AccelerometerReader(Activity context)
{ {
_manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); _manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
start();
} }
public synchronized void stop() public synchronized void stop()
{ {
if( _manager != null ) if( _manager != null )
{ {
System.out.println("libSDL: stopping accelerometer/gyroscope");
_manager.unregisterListener(this); _manager.unregisterListener(this);
} }
} }
public synchronized void start() public synchronized void start()
{ {
if( Globals.UseAccelerometerAsArrowKeys || Globals.AppUsesAccelerometer ) if( (Globals.UseAccelerometerAsArrowKeys || Globals.AppUsesAccelerometer) && _manager != null )
{ {
if( _manager != null ) System.out.println("libSDL: starting accelerometer");
{ // TODO: orientation allows for 3rd axis - azimuth, but it will be way too hard to the user
System.out.println("libSDL: starting accelerometer"); // if( ! _manager.registerListener(this, _manager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME) )
// TODO: orientation allows for 3rd axis - azimuth, but it will be way too hard to the user _manager.registerListener(this, _manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
// if( ! _manager.registerListener(this, _manager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME) ) }
_manager.registerListener(this, _manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); if( Globals.AppUsesGyroscope && _manager != null )
} {
System.out.println("libSDL: starting gyroscope");
_manager.registerListener(this, _manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
} }
} }
@@ -79,19 +82,18 @@ class AccelerometerReader implements SensorEventListener
else else
nativeAccelerometer(event.values[0], event.values[1], event.values[2]); // TODO: not tested! nativeAccelerometer(event.values[0], event.values[1], event.values[2]); // TODO: not tested!
} }
else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE)
{ {
if( Globals.HorizontalOrientation ) //if( Globals.HorizontalOrientation )
nativeOrientation(event.values[1], -event.values[2], event.values[0]); nativeGyroscope(event.values[0], event.values[1], event.values[2]);
else // TODO: vertical orientation
nativeOrientation(event.values[2], event.values[1], event.values[0]);
} }
} }
public synchronized void onAccuracyChanged(Sensor s, int a) { public synchronized void onAccuracyChanged(Sensor s, int a) {
} }
private native void nativeAccelerometer(float accX, float accY, float accZ); private native void nativeAccelerometer(float accX, float accY, float accZ);
private native void nativeOrientation(float accX, float accY, float accZ); private native void nativeGyroscope(float X, float Y, float Z);
} }

View File

@@ -54,6 +54,7 @@ class Globals
public static boolean AppNeedsTextInput = true; public static boolean AppNeedsTextInput = true;
public static boolean AppUsesJoystick = false; public static boolean AppUsesJoystick = false;
public static boolean AppUsesAccelerometer = false; public static boolean AppUsesAccelerometer = false;
public static boolean AppUsesGyroscope = 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

@@ -580,6 +580,15 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer
return context.isScreenKeyboardShown() ? 1 : 0; return context.isScreenKeyboardShown() ? 1 : 0;
} }
public void startAccelerometerGyroscope(int started)
{
accelerometer.openedBySDL = (started != 0);
if( accelerometer.openedBySDL && !mPaused )
accelerometer.start();
else
accelerometer.stop();
}
public void exitApp() public void exitApp()
{ {
nativeDone(); nativeDone();
@@ -769,7 +778,7 @@ class DemoGLSurfaceView extends GLSurfaceView_SDL {
System.out.println("libSDL: DemoGLSurfaceView.onResume(): mRenderer.mGlSurfaceCreated " + mRenderer.mGlSurfaceCreated + " mRenderer.mPaused " + mRenderer.mPaused); System.out.println("libSDL: DemoGLSurfaceView.onResume(): mRenderer.mGlSurfaceCreated " + mRenderer.mGlSurfaceCreated + " mRenderer.mPaused " + mRenderer.mPaused);
if( mRenderer.mGlSurfaceCreated && ! mRenderer.mPaused || Globals.NonBlockingSwapBuffers ) if( mRenderer.mGlSurfaceCreated && ! mRenderer.mPaused || Globals.NonBlockingSwapBuffers )
mRenderer.nativeGlContextRecreated(); mRenderer.nativeGlContextRecreated();
if( mRenderer.accelerometer != null ) // For some reason it crashes here often - are we getting this event before initialization? if( mRenderer.accelerometer != null && mRenderer.accelerometer.openedBySDL ) // For some reason it crashes here often - are we getting this event before initialization?
mRenderer.accelerometer.start(); mRenderer.accelerometer.start();
}; };

View File

@@ -18,6 +18,7 @@ CompatibilityHacksStaticInit=n
CompatibilityHacksTextInputEmulatesHwKeyboard=n CompatibilityHacksTextInputEmulatesHwKeyboard=n
CompatibilityHacksPreventAudioChopping=n CompatibilityHacksPreventAudioChopping=n
CompatibilityHacksAppIgnoresAudioBufferSize=n CompatibilityHacksAppIgnoresAudioBufferSize=n
CompatibilityHacksAdditionalPreloadedSharedLibraries=""
AppUsesMouse=y AppUsesMouse=y
AppNeedsTwoButtonMouse=y AppNeedsTwoButtonMouse=y
ShowMouseCursor=n ShowMouseCursor=n
@@ -26,6 +27,7 @@ AppNeedsArrowKeys=y
AppNeedsTextInput=y AppNeedsTextInput=y
AppUsesJoystick=y AppUsesJoystick=y
AppUsesAccelerometer=y AppUsesAccelerometer=y
AppUsesGyroscope=y
AppUsesMultitouch=y AppUsesMultitouch=y
NonBlockingSwapBuffers=n NonBlockingSwapBuffers=n
RedefinedKeys="SPACE RETURN NO_REMAP NO_REMAP SPACE ESCAPE" RedefinedKeys="SPACE RETURN NO_REMAP NO_REMAP SPACE ESCAPE"
@@ -45,7 +47,9 @@ CompiledLibraries="sdl_mixer sdl_image"
CustomBuildScript=n CustomBuildScript=n
AppCflags='-O2 -finline-functions' AppCflags='-O2 -finline-functions'
AppLdflags='' AppLdflags=''
AppOverlapsSystemHeaders=
AppSubdirsBuild='' AppSubdirsBuild=''
AppBuildExclude=''
AppCmdline='' AppCmdline=''
ReadmeText='^Readme text' ReadmeText='^Readme text'
MinimumScreenSize=s MinimumScreenSize=s

View File

@@ -437,7 +437,7 @@ int main(int argc, char* argv[])
// some random colors // some random colors
int colors[MAX_POINTERS] = { 0xaaaaaa, 0xffffff, 0x888888, 0xcccccc, 0x666666, 0x999999, 0xdddddd, 0xeeeeee, 0xaaaaaa, 0xffffff, 0x888888, 0xcccccc, 0x666666, 0x999999, 0xdddddd, 0xeeeeee }; int colors[MAX_POINTERS] = { 0xaaaaaa, 0xffffff, 0x888888, 0xcccccc, 0x666666, 0x999999, 0xdddddd, 0xeeeeee, 0xaaaaaa, 0xffffff, 0x888888, 0xcccccc, 0x666666, 0x999999, 0xdddddd, 0xeeeeee };
struct TouchPointer_t { int x; int y; int pressure; int pressed; } touchPointers[MAX_POINTERS]; struct TouchPointer_t { int x; int y; int pressure; int pressed; } touchPointers[MAX_POINTERS];
int accel[2], screenjoy[4], gamepads[4][8]; int accel[5], screenjoy[4], gamepads[4][8];
SDL_Surface *mouse[4]; SDL_Surface *mouse[4];
int screenKeyboardShown = 0; int screenKeyboardShown = 0;
@@ -601,6 +601,7 @@ int main(int argc, char* argv[])
} }
int joyInput[][3] = { int joyInput[][3] = {
{accel[0], accel[1], 10}, {accel[0], accel[1], 10},
{accel[2], accel[3], 10 + abs(accel[4]) * 100 / 32767},
{screenjoy[0], screenjoy[1], 10}, {screenjoy[0], screenjoy[1], 10},
{screenjoy[2], screenjoy[3], 10}, {screenjoy[2], screenjoy[3], 10},
{gamepads[0][0], gamepads[0][1], 10 + gamepads[0][4] * 100 / 32767}, {gamepads[0][0], gamepads[0][1], 10 + gamepads[0][4] * 100 / 32767},
@@ -674,7 +675,7 @@ int main(int argc, char* argv[])
} }
if(evt.jaxis.which == 1) if(evt.jaxis.which == 1)
{ {
accel[evt.jaxis.axis] = evt.jaxis.value; accel[evt.jaxis.axis] = evt.jaxis.value; // accelerometer and gyroscope
} }
if(evt.jaxis.which >= 2) if(evt.jaxis.which >= 2)
{ {

View File

@@ -1,3 +1,3 @@
#!/bin/sh #!/bin/sh
adb shell rm /sdcard/Android/data/ws.openarena.sdl/files/libsdl-DownloadFinished-10.flag adb shell rm /sdcard/Android/data/ws.openarena.sdl/files/libsdl-DownloadFinished-10.flag
adb shell rm -r /sdcard/Android/data/ws.openarena.sdl/files/.openarena [ -n "$1" ] && adb shell rm -r /sdcard/Android/data/ws.openarena.sdl/files/.openarena

View File

@@ -864,14 +864,27 @@ JAVA_EXPORT_NAME(AccelerometerReader_nativeAccelerometer) ( JNIEnv* env, jobjec
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(AccelerometerReader_nativeOrientation) ( JNIEnv* env, jobject thiz, jfloat accX, jfloat accY, jfloat accZ ) JAVA_EXPORT_NAME(AccelerometerReader_nativeGyroscope) ( JNIEnv* env, jobject thiz, jfloat X, jfloat Y, jfloat Z )
{ {
#if SDL_VERSION_ATLEAST(1,3,0) #if SDL_VERSION_ATLEAST(1,3,0)
#else #else
if( !SDL_CurrentVideoSurface ) if( !SDL_CurrentVideoSurface )
return; return;
#endif #endif
updateOrientation (accX, accY, accZ); // TODO: make values in range 0.0:1.0 while ( X != 0.0f || Y != 0.0f || Z != 0.0f )
{
float dx = ( X >= 1.0f ? 1.0f : ( X <= -1.0f ? -1.0f : X ) );
float dy = ( Y >= 1.0f ? 1.0f : ( Y <= -1.0f ? -1.0f : Y ) );
float dz = ( Z >= 1.0f ? 1.0f : ( Z <= -1.0f ? -1.0f : Z ) );
X -= dx;
Y -= dy;
Z -= dz;
SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 2, NORMALIZE_FLOAT_32767(dx));
SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 3, NORMALIZE_FLOAT_32767(dy));
SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 4, NORMALIZE_FLOAT_32767(dz));
}
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
@@ -1434,7 +1447,8 @@ int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
} }
if( joystick->index == JOY_ACCELGYRO ) if( joystick->index == JOY_ACCELGYRO )
{ {
joystick->naxes = 2; // Accelerometer/gyroscope angles joystick->naxes = 5; // Accelerometer = axes 0-1, gyroscope = axes 2-4
SDL_ANDROID_CallJavaStartAccelerometerGyroscope(1);
} }
if( joystick->index >= JOY_GAMEPAD1 || joystick->index <= JOY_GAMEPAD4 ) if( joystick->index >= JOY_GAMEPAD1 || joystick->index <= JOY_GAMEPAD4 )
{ {
@@ -1453,6 +1467,8 @@ void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
void SDL_SYS_JoystickClose(SDL_Joystick *joystick) void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
{ {
SDL_ANDROID_CurrentJoysticks[joystick->index] = NULL; SDL_ANDROID_CurrentJoysticks[joystick->index] = NULL;
if( joystick->index == JOY_ACCELGYRO )
SDL_ANDROID_CallJavaStartAccelerometerGyroscope(0);
return; return;
} }

View File

@@ -65,6 +65,7 @@ static jmethodID JavaShowScreenKeyboard = NULL;
static jmethodID JavaToggleScreenKeyboardWithoutTextInput = NULL; static jmethodID JavaToggleScreenKeyboardWithoutTextInput = NULL;
static jmethodID JavaHideScreenKeyboard = NULL; static jmethodID JavaHideScreenKeyboard = NULL;
static jmethodID JavaIsScreenKeyboardShown = NULL; static jmethodID JavaIsScreenKeyboardShown = NULL;
static jmethodID JavaStartAccelerometerGyroscope = NULL;
static jmethodID JavaGetAdvertisementParams = NULL; static jmethodID JavaGetAdvertisementParams = NULL;
static jmethodID JavaSetAdvertisementVisible = NULL; static jmethodID JavaSetAdvertisementVisible = NULL;
static jmethodID JavaSetAdvertisementPosition = NULL; static jmethodID JavaSetAdvertisementPosition = NULL;
@@ -297,6 +298,11 @@ int SDL_ANDROID_IsScreenKeyboardShown()
return SDL_ANDROID_IsScreenKeyboardShownFlag; return SDL_ANDROID_IsScreenKeyboardShownFlag;
} }
void SDL_ANDROID_CallJavaStartAccelerometerGyroscope(int start)
{
(*JavaEnv)->CallVoidMethod( JavaEnv, JavaRenderer, JavaStartAccelerometerGyroscope, (jint) start );
}
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(DemoRenderer_nativeInitJavaCallbacks) ( JNIEnv* env, jobject thiz ) JAVA_EXPORT_NAME(DemoRenderer_nativeInitJavaCallbacks) ( JNIEnv* env, jobject thiz )
{ {
@@ -309,7 +315,8 @@ JAVA_EXPORT_NAME(DemoRenderer_nativeInitJavaCallbacks) ( JNIEnv* env, jobject t
JavaToggleScreenKeyboardWithoutTextInput = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "showScreenKeyboardWithoutTextInputField", "()V"); JavaToggleScreenKeyboardWithoutTextInput = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "showScreenKeyboardWithoutTextInputField", "()V");
JavaHideScreenKeyboard = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "hideScreenKeyboard", "()V"); JavaHideScreenKeyboard = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "hideScreenKeyboard", "()V");
JavaIsScreenKeyboardShown = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "isScreenKeyboardShown", "()I"); JavaIsScreenKeyboardShown = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "isScreenKeyboardShown", "()I");
// TODO: implement it JavaStartAccelerometerGyroscope = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "startAccelerometerGyroscope", "(I)V");
JavaGetAdvertisementParams = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "getAdvertisementParams", "([I)V"); JavaGetAdvertisementParams = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "getAdvertisementParams", "([I)V");
JavaSetAdvertisementVisible = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "setAdvertisementVisible", "(I)V"); JavaSetAdvertisementVisible = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "setAdvertisementVisible", "(I)V");
JavaSetAdvertisementPosition = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "setAdvertisementPosition", "(II)V"); JavaSetAdvertisementPosition = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "setAdvertisementPosition", "(II)V");

View File

@@ -72,7 +72,7 @@ extern void SDL_ANDROID_WarpMouse(int x, int y);
extern void SDL_ANDROID_DrawMouseCursor(int x, int y, int size, int alpha); extern void SDL_ANDROID_DrawMouseCursor(int x, int y, int size, int alpha);
extern void SDL_ANDROID_DrawMouseCursorIfNeeded(); extern void SDL_ANDROID_DrawMouseCursorIfNeeded();
extern void SDL_ANDROID_CallJavaTogglePlainAndroidSoftKeyboardInput(); extern void SDL_ANDROID_CallJavaTogglePlainAndroidSoftKeyboardInput();
extern void SDL_ANDROID_CallJavaStartAccelerometerGyroscope(int start);
#if SDL_VERSION_ATLEAST(1,3,0) #if SDL_VERSION_ATLEAST(1,3,0)
extern SDL_Window * ANDROID_CurrentWindow; extern SDL_Window * ANDROID_CurrentWindow;