Переглянути джерело

Add input handling to AndroidDevice

Daniele Bartolini 12 роки тому
батько
коміт
a44809ccf7

+ 82 - 5
engine/os/android/AndroidDevice.cpp

@@ -25,11 +25,13 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include <jni.h>
+#include "Allocator.h"
 #include "Device.h"
+#include "Log.h"
+#include "OsEventQueue.h"
 #include "Renderer.h"
 #include "SoundRenderer.h"
-#include "Log.h"
-#include "Allocator.h"
+#include "Touch.h"
 
 namespace crown
 {
@@ -40,8 +42,67 @@ public:
 
 	int32_t run(int, char**)
 	{
-		// Do nothing, the game loop is java-side
+		process_events();
+		Device::frame();
+		m_touch->update();
+		return 0;
+	}
+
+	//-----------------------------------------------------------------------------
+	bool process_events()
+	{
+		OsEvent event;
+		do
+		{
+			m_queue.pop_event(&event);
+
+			if (event.type == OsEvent::NONE) continue;
+
+			switch (event.type)
+			{
+				case OsEvent::TOUCH:
+				{
+					const OsTouchEvent& ev = event.touch;
+					switch (ev.type)
+					{
+						case OsTouchEvent::POINTER: m_touch->set_pointer_state(ev.x, ev.y, ev.pointer_id, ev.pressed); break;
+						case OsTouchEvent::MOVE: m_touch->set_position(ev.pointer_id, ev.x, ev.y); break;
+						default: CE_FATAL("Oops, unknown touch event type"); break;
+					}
+
+					break;
+				}
+				case OsEvent::EXIT:
+				{
+					return true;
+				}
+				default:
+				{
+					CE_FATAL("Unknown Os Event");
+					break;
+				}
+			}
+		}
+		while (event.type != OsEvent::NONE);
+
+		return false;
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_touch_event(uint16_t x, uint16_t y, uint8_t pointer_id)
+	{
+		m_queue.push_touch_event(x, y, pointer_id);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_touch_event(uint16_t x, uint16_t y, uint8_t pointer_id, bool pressed)
+	{
+		m_queue.push_touch_event(x, y, pointer_id, pressed);
 	}
+
+private:
+
+	OsEventQueue m_queue;
 };
 
 static AndroidDevice* g_engine;
@@ -112,9 +173,9 @@ extern "C" JNIEXPORT bool JNICALL Java_crown_android_CrownLib_isDevicePaused(JNI
 }
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_frame(JNIEnv* /*env*/, jobject /*obj*/)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_run(JNIEnv* /*env*/, jobject /*obj*/)
 {
-	device()->frame();
+	g_engine->run(0, NULL);
 }
 
 //-----------------------------------------------------------------------------
@@ -139,7 +200,23 @@ extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pauseSoundRenderer
 extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_unpauseSoundRenderer(JNIEnv* /*env*/, jobject /*obj*/)
 {
 	device()->sound_renderer()->unpause();
+}
+
+//-----------------------------------------------------------------------------
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushTouchEventMove(JNIEnv * /*env*/, jobject /*obj*/, jint pointer_id, jint x, jint y)
+{
+	g_engine->push_touch_event(x, y, pointer_id);
+}
 
+//-----------------------------------------------------------------------------
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushTouchEventPointer(JNIEnv * /*env*/, jobject /*obj*/, jint pointer_id, jint x, jint y, jint pressed)
+{
+	g_engine->push_touch_event(x, y, pointer_id, pressed);
+}
+
+//-----------------------------------------------------------------------------
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushAccelerometerEvent(JNIEnv * /*env*/, jobject /*obj*/, jint type, jfloat x, jfloat y, jfloat z)
+{
 }
 
 } // namespace crown

+ 0 - 25
engine/os/android/AndroidOS.cpp

@@ -309,30 +309,5 @@ void execute_process(const char* args[])
 }
 
 } // namespace os
-
-//-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushTouchEvent(JNIEnv * /*env*/, jobject /*obj*/, jint type, jint pointer_id, jint x, jint y)
-{	
-	OsTouchEvent event;
-
-	event.pointer_id = pointer_id;
-	event.x = x;
-	event.y = y;
-
-	//os_event_buffer()->push_event((OsEventType)type, &event, sizeof(OsTouchEvent));
-}
-
-//-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushAccelerometerEvent(JNIEnv * /*env*/, jobject /*obj*/, jint type, jfloat x, jfloat y, jfloat z)
-{
-	OsAccelerometerEvent event;
-
-	event.x = x;
-	event.y = y;
-	event.z = z;
-
-	//os_event_buffer()->push_event((OsEventType)type, &event, sizeof(OsAccelerometerEvent));
-}
-
 } // namespace crown
 

+ 3 - 2
engine/os/android/java/CrownLib.java

@@ -52,7 +52,7 @@ public class CrownLib
 	public static native boolean	isDeviceRunning();
 	public static native boolean	isDevicePaused();
 
-	public static native void 		frame();
+	public static native int		run();
 
 	// AssetManager functions
 	public static native void 		initAssetManager(AssetManager assetManager);
@@ -70,6 +70,7 @@ public class CrownLib
 	public static native void		unpauseSoundRenderer();
 
 	// InputManager functions
-	public static native void 		pushTouchEvent(int type, int pointer_id, int x, int y);
+	public static native void 		pushTouchEventMove(int pointer_id, int x, int y);
+	public static native void		pushTouchEventPointer(int pointer_id, int x, int y, int pressed);
 	public static native void 		pushAccelerometerEvent(int type, float x, float y, float z);	
 }

+ 1 - 1
engine/os/android/java/CrownMainThread.java

@@ -62,7 +62,7 @@ public class CrownMainThread extends Thread
 
 		while (CrownLib.isDeviceRunning() && !CrownLib.isDevicePaused())
 		{
-			CrownLib.frame();
+			CrownLib.run();
 		}
 	}
 }

+ 7 - 6
engine/os/android/java/CrownTouch.java

@@ -62,26 +62,27 @@ public class CrownTouch
 			case MotionEvent.ACTION_DOWN:
 			case MotionEvent.ACTION_POINTER_DOWN:
 			{
-				CrownLib.pushTouchEvent(CrownEnum.OSET_TOUCH_DOWN, pointerId, (int)x, (int)y);
+				CrownLib.pushTouchEventPointer(pointerId, (int) x, (int) y, 1);
 				break;			
 			}
-
 			case MotionEvent.ACTION_UP:
 			case MotionEvent.ACTION_POINTER_UP:
+			{
+				CrownLib.pushTouchEventPointer(pointerId, (int) x, (int) y, 0);
+				break;
+			}
 			case MotionEvent.ACTION_OUTSIDE:
 			case MotionEvent.ACTION_CANCEL:
 			{
-				CrownLib.pushTouchEvent(CrownEnum.OSET_TOUCH_UP, pointerId, (int)x, (int)y);
+				CrownLib.pushTouchEventPointer(pointerId, (int)x, (int)y, 0);
 				break;			
 			}
-			
 			case MotionEvent.ACTION_MOVE:
 			{
 				for (int index = 0; index < pointerCount; index++)
 				{
-					CrownLib.pushTouchEvent(CrownEnum.OSET_TOUCH_MOVE, event.getPointerId(index), (int)event.getX(index), (int)event.getY(index));
+					CrownLib.pushTouchEventMove(event.getPointerId(index), (int)event.getX(index), (int)event.getY(index));
 				}
-
 				break;
 			}
 		}