Ver Fonte

Linux input fixed, TODO: fix Android camera

mikymod há 13 anos atrás
pai
commit
84eeebc1da

+ 79 - 80
android/src/crown/android/CrownAccelerometer.java

@@ -19,33 +19,28 @@ import crown.android.CrownEnum;
 */
 public class CrownAccelerometer
 {
-    private final float MIN_VALUE = -1.0f;
-    private final float MAX_VALUE = 1.0f;
+    private static final float MIN_VALUE = 0.0f;
+    private static final float MAX_VALUE = 1.0f;
 
-    private SensorManager sensorManager;
-    private Sensor sensor;
-    private SensorEventListener sensorEventListener;
+    private static SensorManager sensorManager;
+    private static Sensor sensor;
+    private static SensorEventListener sensorEventListener;
 
-    private float x;
-    private float y;
-    private float z; 
-    private float lastX;
-    private float lastY;
-    private float lastZ;
+    private static float mX;
+    private static float mY;
+    private static float mZ; 
+    private static float lastX;
+    private static float lastY;
+    private static float lastZ;
 
 //-----------------------------------------------------------------------------------
-    public CrownAccelerometer()
-    {
-    }
-
-//-----------------------------------------------------------------------------------
-    private boolean initAccelerometer(Context context)
+    private static boolean initAccelerometer(Context context)
     {
     	// already initialized!
-    	if (sensorEventListener != null)
-    	{
-    	    return true;
-    	}
+    	// if (sensorEventListener != null)
+    	// {
+    	//     return true;
+    	// }
 
     	sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
 
@@ -61,10 +56,15 @@ public class CrownAccelerometer
 	    	@Override
 	    	public void onSensorChanged(SensorEvent event) 
 			{
-                lowPassFiltering(event.values[0], event.values[1], event.values[2]);
+                mX = event.values[0];
+                mY = event.values[1];
+                mZ = event.values[2];
 
-				CrownLib.pushFloatEvent(CrownEnum.OSET_ACCELEROMETER, x, y, z, 0);
+				CrownLib.pushFloatEvent(CrownEnum.OSET_ACCELEROMETER, mX, mY, mZ, 0);
 
+                lastX = mX;
+                lastY = mY;
+                lastZ = mZ;
     		}    		
     	};
 
@@ -73,97 +73,96 @@ public class CrownAccelerometer
     }
 
 //-----------------------------------------------------------------------------------
-    public boolean startListening(Context context)
+    public static boolean startListening(Context context)
     {
     	return initAccelerometer(context); 
     }
 
 //-----------------------------------------------------------------------------------
-    public void stopListening()
+    public static void stopListening()
     {
     	sensorManager.unregisterListener(sensorEventListener);
     }
 
 //-----------------------------------------------------------------------------------
-    public float getX()
+    public static float getX()
     {
-    	return x;
+    	return mX;
     }
 
 //-----------------------------------------------------------------------------------
-    public float getY()
+    public static float getY()
     {
-    	return y;
+    	return mY;
     }
 
 //-----------------------------------------------------------------------------------
-    public float getZ()
+    public static float getZ()
     {
-    	return z;
+    	return mZ;
     }
 
 //-----------------------------------------------------------------------------------
-    public float getLastX()
+    public static float getLastX()
     {
     	return lastX;
     }
 
 //-----------------------------------------------------------------------------------
-    public float getLastY()
+    public static float getLastY()
     {
     	return lastY;
     }
 
 //-----------------------------------------------------------------------------------
-    public float getLastZ()
+    public static float getLastZ()
     {
     	return lastZ;
     }
 
-//-----------------------------------------------------------------------------------
-    private void lowPassFiltering(float uX, float uY, float uZ)
-    {
-        float updateFreq = 30; // match this to your update speed
-        float cutOffFreq = 0.9f;
-        float timeRC = 1.0f / cutOffFreq;
-        float dt = 1.0f / updateFreq;
-        float filterConstant = timeRC / (dt + timeRC);
-        float alpha = filterConstant; 
-        float kAccelerometerMinStep = 0.033f;
-        float kAccelerometerNoiseAttenuation = 3.0f;
-
-        float d = clamp(Math.abs(norm(x, y, z) - norm(uX, uY, uZ)) / kAccelerometerMinStep - 1.0f, MIN_VALUE, MAX_VALUE);
-        alpha = d * filterConstant / kAccelerometerNoiseAttenuation + (1.0f - d) * filterConstant;
-
-        x = (float) (alpha * (x + uX - lastX));
-        y = (float) (alpha * (y + uY - lastY));
-        z = (float) (alpha * (z + uZ - lastZ));
-
-
-        lastX = x;
-        lastY = y;
-        lastZ = z;
-    }
-
-//-----------------------------------------------------------------------------------
-    private float clamp(float v, float min, float max)
-    {
-        if (v < min)
-        {
-            v = min;
-        }
-        else if (v > max)
-        {
-            v = max;
-        }
-
-        return v;
-    }
-
-//-----------------------------------------------------------------------------------
-    private float norm(float x, float y, float z)
-    {
-        float v = (float)Math.pow(x, 2) + (float)Math.pow(y, 2) + (float)Math.pow(z, 2);
-        return (float)Math.sqrt(v);
-    }
+// //-----------------------------------------------------------------------------------
+//     private void lowPassFiltering(float uX, float uY, float uZ)
+//     {
+//         float updateFreq = 30; // match this to your update speed
+//         float cutOffFreq = 0.9f;
+//         float timeRC = 1.0f / cutOffFreq;
+//         float dt = 1.0f / updateFreq;
+//         float filterConstant = timeRC / (dt + timeRC);
+//         float alpha = filterConstant;                 
+//         float kAccelerometerMinStep = 0.033f;
+//         float kAccelerometerNoiseAttenuation = 3.0f;
+
+//         float d = clamp((Math.abs(norm(mX, mY, mZ) - norm(uX, uY, uZ)) / kAccelerometerMinStep - 1.0f), MIN_VALUE, MAX_VALUE);
+//         alpha = d * filterConstant / kAccelerometerNoiseAttenuation + (1.0f - d) * filterConstant;
+
+//         mX = (float) (alpha * (mX + uX - lastX));
+//         mY = (float) (alpha * (mY + uY - lastY));
+//         mZ = (float) (alpha * (mZ + uZ - lastZ));
+
+//         lastX = mX;
+//         lastY = mY;
+//         lastZ = mZ;
+//     }
+
+// //-----------------------------------------------------------------------------------
+//     private float clamp(float v, float min, float max)
+//     {
+//         if (v <= min)
+//         {
+//             v = min;
+//         }
+//         else if (v >= max)
+//         {
+//             v = max;
+//         }
+
+//         return v;
+//     }
+
+// //-----------------------------------------------------------------------------------
+//     private float norm(float x, float y, float z)
+//     {
+//         float v = (float)Math.pow(x, 2) + (float)Math.pow(y, 2) + (float)Math.pow(z, 2);
+//         return (float)Math.sqrt(v);
+//     }
 }

+ 2 - 13
android/src/crown/android/CrownActivity.java

@@ -30,7 +30,6 @@ public class CrownActivity extends Activity
 
 	// Input attributes
 	private CrownTouch 			mTouchListener;
-	private CrownAccelerometer	mAccelerometerListener;
 
 	/**
 	*
@@ -49,9 +48,6 @@ public class CrownActivity extends Activity
 
 		// Init Input
 		mTouchListener = new CrownTouch();
-		mAccelerometerListener = new CrownAccelerometer();
-
-		Log.i(TAG, "onCreate called.");
     }
 
 	/**
@@ -60,7 +56,6 @@ public class CrownActivity extends Activity
 	public void onStart()
 	{
 		super.onStart();
-		Log.i(TAG, "onStart called.");
 	}
 
 	/**
@@ -69,7 +64,6 @@ public class CrownActivity extends Activity
 	public void onRestart()
 	{
 		super.onRestart();
-		Log.i(TAG, "onRestart called.");
 	}
 
 	/**
@@ -80,10 +74,8 @@ public class CrownActivity extends Activity
 		super.onResume();
         mView.onResume();
 		
-		Log.i(TAG, "onResume called.");
-
 		// init accelerometer
-		if (!mAccelerometerListener.startListening(this))
+		if (!CrownAccelerometer.startListening(this))
 		{
 			Log.i(TAG, "Device has no accelerometer. App terminated.");
 			finish();
@@ -97,7 +89,6 @@ public class CrownActivity extends Activity
 	{
 		super.onPause();
         mView.onPause();
-		Log.i(TAG, "onPause called.");
 	}
 
 	/**
@@ -106,10 +97,9 @@ public class CrownActivity extends Activity
 	public void onStop()
 	{
 		super.onStop();
-		Log.i(TAG, "onStop called.");
 
 		// stop accelerometer
-		mAccelerometerListener.stopListening();
+		CrownAccelerometer.stopListening();
 	}
 
 	/**
@@ -118,7 +108,6 @@ public class CrownActivity extends Activity
 	public void onDestroy()
 	{
 		super.onDestroy();
-		Log.i(TAG, "onDestroy called.");
 	}
 
 	/**

+ 14 - 3
samples/android/triangle.cpp

@@ -14,7 +14,7 @@ extern "C"
 	JNIEXPORT void JNICALL Java_crown_android_CrownLib_frame(JNIEnv* env, jobject obj);
 };
 
-class MainScene : public AccelerometerListener
+class MainScene : public AccelerometerListener, TouchListener
 {
 	
 public:
@@ -22,6 +22,7 @@ public:
 	MainScene()
 	{
 		get_input_manager()->register_accelerometer_listener(this);
+		get_input_manager()->register_touch_listener(this);
 
 		cam = new MovableCamera(Vec3::ZERO, true, 90.0f, 1.6f, true, 0.1f, 2.5f);
 
@@ -35,13 +36,23 @@ public:
 
 	void accelerometer_changed(const AccelerometerEvent& event)
 	{
-		Log::I("Accelerometer changed");
-
 		cam->SetRotation(event.x, event.y);
 	}
 
+	void touch_down(const TouchEvent& event)
+	{
+	}
+
+	void touch_move(const TouchEvent& event)
+	{
+	}
+
 	void draw_triangle()
 	{
+		uint32_t width;
+		uint32_t height;
+		os::get_render_window_metrics(width, height);
+
 		cam->Render();
 
 		static GLfloat vertices[] = {  -1.0f, -1.0f, -2.0f,

+ 1 - 1
src/Config.h

@@ -62,7 +62,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 	b) you can choose OpenGL|ES if and only if you are
 	   on a Linux or Android machine.
 */
-//#define CROWN_USE_OPENGL		//!< Whether to build with OpenGL
+// #define CROWN_USE_OPENGL		//!< Whether to build with OpenGL
 #define CROWN_USE_OPENGLES		//!< Whether to build with OpenGL|ES
 
 //#define CROWN_USE_WINDOWING	//!< Whether to build with windowing

+ 5 - 1
src/FPSSystem.cpp

@@ -19,7 +19,7 @@ FPSSystem::FPSSystem(MovableCamera* camera) :
 	m_down_pressed(false),
 	m_left_pressed(false)
 {
-//	get_input_manager()->register_keyboard_listener(this);
+	get_input_manager()->register_keyboard_listener(this);
 	get_input_manager()->register_accelerometer_listener(this);
 	//get_input_manager()->register_mouse_listener(this);
 	get_input_manager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
@@ -32,21 +32,25 @@ void FPSSystem::key_pressed(const KeyboardEvent& event)
 	switch (event.key)
 	{
 		case 'w':
+		case 'W':
 		{
 			m_up_pressed = true;
 			break;
 		}
 		case 'a':
+		case 'A':
 		{
 			m_left_pressed = true;
 			break;
 		}
 		case 's':
+		case 'S':
 		{
 			m_down_pressed = true;
 			break;
 		}
 		case 'd':
+		case 'D':
 		{
 			m_right_pressed = true;
 			break;

+ 0 - 8
src/os/android/AndroidOS.cpp

@@ -38,11 +38,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "AndroidOS.h"
 #include "Log.h"
 
-#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "crown", __VA_ARGS__))
-#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "crown", __VA_ARGS__))
-#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, "crown", __VA_ARGS__))
-#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "crown", __VA_ARGS__))
-
 namespace crown
 {
 namespace os
@@ -262,8 +257,6 @@ void get_render_window_metrics(uint32_t& width, uint32_t& height)
 {
 	width = window_width;
 	height = window_height;
-
-	Log::I("width=%d", width);
 }
 
 //-----------------------------------------------------------------------------
@@ -283,7 +276,6 @@ JNIEXPORT void JNICALL Java_crown_android_CrownLib_setRenderWindowMetrics(JNIEnv
 {
 	window_width = width;
 	window_height = height;
-	Log::I("setRenderWindowMetrics called. %d", (uint32_t)width);
 }
 
 } // namespace os

+ 17 - 5
src/os/linux/Input.cpp

@@ -28,6 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include <X11/XKBlib.h>
 #include "Keyboard.h"
 #include "OS.h"
+#include "Log.h"
 
 namespace crown
 {
@@ -168,6 +169,9 @@ void event_loop()
 {
 	XEvent event;
 
+	OSEventParameter data_button[4] = {0, 0, 0, 0};
+	OSEventParameter data_key[4] = {0, 0, 0, 0};
+
 	while (XPending(display))
 	{
 		XNextEvent(display, &event);
@@ -185,21 +189,27 @@ void event_loop()
 			{
 				OSEventType oset_type = event.type == ButtonPress ? OSET_BUTTON_PRESS : OSET_BUTTON_RELEASE;
 
+				data_button[0].int_value = event.xbutton.x;
+				data_button[1].int_value = event.xbutton.y;
+
 				switch (event.xbutton.button)
 				{
 					case Button1:
 					{
-						push_event(oset_type, event.xbutton.x, event.xbutton.y, 0, 0);
+						data_button[2].int_value = 0;
+						push_event(oset_type, data_button[0], data_button[1], data_button[2], data_button[3]);
 						break;
 					}
 					case Button2:
 					{
-						push_event(oset_type, event.xbutton.x, event.xbutton.y, 1, 0);
+						data_button[3].int_value = 1;
+						push_event(oset_type, data_button[0], data_button[1], data_button[2], data_button[3]);
 						break;
 					}
 					case Button3:
 					{
-						push_event(oset_type, event.xbutton.x, event.xbutton.y, 2, 0);
+						data_button[3].int_value = 2;
+						push_event(oset_type, data_button[0], data_button[1], data_button[2], data_button[3]);
 						break;
 					}
 				}
@@ -208,7 +218,7 @@ void event_loop()
 			}
 			case MotionNotify:
 			{
-				push_event(OSET_MOTION_NOTIFY, event.xbutton.x, event.xbutton.y, 0, 0);
+				push_event(OSET_MOTION_NOTIFY, data_button[0], data_button[1], data_button[2], data_button[3]);
 				break;
 			}
 			case KeyPress:
@@ -238,7 +248,9 @@ void event_loop()
 
 				OSEventType oset_type = event.type == KeyPress ? OSET_KEY_PRESS : OSET_KEY_RELEASE;
 
-				push_event(oset_type, kc, 0, 0, 0);
+				data_key[0].int_value = ((int32_t)kc);
+
+				push_event(oset_type, data_key[0], data_key[1], data_key[2], data_key[3]);
 
 //				// Text input part
 //				if (event.type == KeyPress && len > 0)