Final fix to accelerometer input

This commit is contained in:
pelya
2010-07-09 18:50:43 +03:00
parent fa534f2570
commit cad2c8ff2a
4 changed files with 19 additions and 13 deletions

View File

@@ -21,6 +21,7 @@
#include "SDL.h" #include "SDL.h"
#include <stdlib.h> #include <stdlib.h>
#include "global.h" #include "global.h"
#include "surfaceDB.h"
#include <android/log.h> #include <android/log.h>
using namespace std; using namespace std;
@@ -67,6 +68,8 @@ SdlCompat_AcceleratedSurface *Video::init(){
SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
__android_log_print(ANDROID_LOG_INFO, "Alien Blaster", "Initializing video done"); __android_log_print(ANDROID_LOG_INFO, "Alien Blaster", "Initializing video done");
empty = surfaceDB.loadSurface(FN_ALIENBLASTER_INTRO);
return screen; return screen;
} }
@@ -79,8 +82,9 @@ void Video::clearScreen() {
r.y = 0; r.y = 0;
r.w = screen->w; r.w = screen->w;
r.h = screen->h; r.h = screen->h;
*/
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0) ); SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0) );
*/
SDL_BlitSurface(empty, NULL, screen, NULL);
} }
void Video::toggleFullscreen() { void Video::toggleFullscreen() {

View File

@@ -32,6 +32,7 @@ class Video {
private: private:
SdlCompat_AcceleratedSurface *screen; SdlCompat_AcceleratedSurface *screen;
SdlCompat_AcceleratedSurface *empty;
public: public:
Video(); Video();

View File

@@ -92,18 +92,11 @@ static void updateOrientation ( float accX, float accY, float accZ )
{ {
// TODO: use accelerometer as joystick, make this configurable // TODO: use accelerometer as joystick, make this configurable
// Currenly it's used as cursor + KP7/KP9 keys // Currenly it's used as cursor + KP7/KP9 keys
static const float dx = 0.1, dy = 0.1, dz = 0.1; static const float dx = 0.04, dy = 0.1, dz = 0.1;
static float midX = 0, midY = 0, midZ = 0; static float midX = 0, midY = 0, midZ = 0;
static int pressLeft = 0, pressRight = 0, pressUp = 0, pressDown = 0, pressR = 0, pressL = 0; static int pressLeft = 0, pressRight = 0, pressUp = 0, pressDown = 0, pressR = 0, pressL = 0;
static int count = 0;
count++;
if(count > 30)
{
__android_log_print(ANDROID_LOG_INFO, "libSDL", "Accel: %-1.3f %-1.3f %-1.3f : %-1.3f %-1.3f %-1.3f", accX, accY, accZ, midX, midY, midZ);
}
if( accX < midX - dx ) if( accX < midX - dx )
{ {
if( !pressLeft ) if( !pressLeft )
@@ -152,7 +145,7 @@ static void updateOrientation ( float accX, float accY, float accZ )
{ {
//__android_log_print(ANDROID_LOG_INFO, "libSDL", "Accelerometer: press up, acc %f mid %f d %f", accY, midY, dy); //__android_log_print(ANDROID_LOG_INFO, "libSDL", "Accelerometer: press up, acc %f mid %f d %f", accY, midY, dy);
pressUp = 1; pressUp = 1;
SDL_SendKeyboardKey( SDL_PRESSED, SDL_SCANCODE_UP ); SDL_SendKeyboardKey( SDL_PRESSED, SDL_SCANCODE_DOWN );
} }
} }
else else
@@ -161,7 +154,7 @@ static void updateOrientation ( float accX, float accY, float accZ )
{ {
//__android_log_print(ANDROID_LOG_INFO, "libSDL", "Accelerometer: release up, acc %f mid %f d %f", accY, midY, dy); //__android_log_print(ANDROID_LOG_INFO, "libSDL", "Accelerometer: release up, acc %f mid %f d %f", accY, midY, dy);
pressUp = 0; pressUp = 0;
SDL_SendKeyboardKey( SDL_RELEASED, SDL_SCANCODE_UP ); SDL_SendKeyboardKey( SDL_RELEASED, SDL_SCANCODE_DOWN );
} }
} }
if( accY < midY - dy*2 ) if( accY < midY - dy*2 )
@@ -173,7 +166,7 @@ static void updateOrientation ( float accX, float accY, float accZ )
{ {
//__android_log_print(ANDROID_LOG_INFO, "libSDL", "Accelerometer: press down, acc %f mid %f d %f", accY, midY, dy); //__android_log_print(ANDROID_LOG_INFO, "libSDL", "Accelerometer: press down, acc %f mid %f d %f", accY, midY, dy);
pressDown = 1; pressDown = 1;
SDL_SendKeyboardKey( SDL_PRESSED, SDL_SCANCODE_DOWN ); SDL_SendKeyboardKey( SDL_PRESSED, SDL_SCANCODE_UP );
} }
} }
else else
@@ -182,7 +175,7 @@ static void updateOrientation ( float accX, float accY, float accZ )
{ {
//__android_log_print(ANDROID_LOG_INFO, "libSDL", "Accelerometer: release down, acc %f mid %f d %f", accY, midY, dy); //__android_log_print(ANDROID_LOG_INFO, "libSDL", "Accelerometer: release down, acc %f mid %f d %f", accY, midY, dy);
pressDown = 0; pressDown = 0;
SDL_SendKeyboardKey( SDL_RELEASED, SDL_SCANCODE_DOWN ); SDL_SendKeyboardKey( SDL_RELEASED, SDL_SCANCODE_UP );
} }
} }
if( accY > midY + dy*2 ) if( accY > midY + dy*2 )
@@ -237,6 +230,12 @@ JAVA_EXPORT_NAME(AccelerometerReader_nativeAccelerometer) ( JNIEnv* env, jobjec
float normal = sqrt(accPosX*accPosX+accPosY*accPosY+accPosZ*accPosZ); float normal = sqrt(accPosX*accPosX+accPosY*accPosY+accPosZ*accPosZ);
if(normal <= 0.0000001f) if(normal <= 0.0000001f)
normal = 1.0f; normal = 1.0f;
static int count = 0;
count++;
if(count > 50)
{
__android_log_print(ANDROID_LOG_INFO, "libSDL", "Accel: %-1.3f %-1.3f %-1.3f", accPosX/normal, accPosY/normal, accPosZ/normal);
}
updateOrientation (accPosX/normal, accPosY/normal, 0.0f); updateOrientation (accPosX/normal, accPosY/normal, 0.0f);
} }

View File

@@ -48,12 +48,14 @@ class AccelerometerReader implements SensorListener {
v[2] = values[2]; v[2] = values[2];
if (sensor == SensorManager.SENSOR_ACCELEROMETER) { if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
/*
if( Globals.HorizontalOrientation ) if( Globals.HorizontalOrientation )
{ {
v[0] = values[2]; v[0] = values[2];
v[1] = values[1]; v[1] = values[1];
v[2] = values[0]; v[2] = values[0];
} }
*/
nativeAccelerometer(v[0], v[1], v[2]); nativeAccelerometer(v[0], v[1], v[2]);
} }
if (sensor == SensorManager.SENSOR_ORIENTATION) { if (sensor == SensorManager.SENSOR_ORIENTATION) {