فهرست منبع

Simplify CrownActivity and related stuff

Daniele Bartolini 12 سال پیش
والد
کامیت
00396ca791

+ 106 - 67
engine/os/android/AndroidDevice.cpp

@@ -31,6 +31,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "OsEventQueue.h"
 #include "Renderer.h"
 #include "Touch.h"
+#include "OsWindow.h"
 
 namespace crown
 {
@@ -41,6 +42,7 @@ public:
 
 	//-----------------------------------------------------------------------------
 	AndroidDevice()
+		: m_game_thread("game_thread")
 	{
 		#if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
 			m_fileserver = 1;
@@ -50,20 +52,41 @@ public:
 	//-----------------------------------------------------------------------------
 	int32_t run(int, char**)
 	{
-		process_events();
-		Device::frame();
-		m_touch->update();
+		m_game_thread.start(main_loop, (void*)this);
+
+		//while (true) {}
+		return 0;
+	}
+
+	//-----------------------------------------------------------------------------
+	int32_t loop()
+	{
+		Device::init();
+
+		while (is_running() && !process_events())
+		{
+			Device::frame();
+			m_touch->update();
+			m_keyboard->update();
+		}
+
+		Device::shutdown();
 		return 0;
 	}
 
+	//-----------------------------------------------------------------------------
+	static int32_t main_loop(void* thiz)
+	{
+		return ((AndroidDevice*) thiz)->loop();
+	}
+
 	//-----------------------------------------------------------------------------
 	bool process_events()
 	{
 		OsEvent event;
-		do
-		{
-			m_queue.pop_event(&event);
 
+		while (m_queue.pop_event(&event))
+		{
 			if (event.type == OsEvent::NONE) continue;
 
 			switch (event.type)
@@ -80,10 +103,40 @@ public:
 
 					break;
 				}
+				case OsEvent::KEYBOARD:
+				{
+					const OsKeyboardEvent& ev = event.keyboard;
+					m_keyboard->set_button_state(ev.button, ev.pressed);
+					Log::i("KEYBOARD EVENT RECEIVED");
+					break;
+				}
+				case OsEvent::METRICS:
+				{
+					const OsMetricsEvent& ev = event.metrics;
+					m_window->m_x = 0;
+					m_window->m_y = 0;
+					m_window->m_width = ev.width;
+					m_window->m_height = ev.height;
+					Log::i("METRICS EVENT RECEIVED");
+					break;
+				}
 				case OsEvent::EXIT:
 				{
+					Log::i("EXIT EVENT RECEIVED");
 					return true;
 				}
+				case OsEvent::PAUSE:
+				{
+					Log::i("PAUSE EVENT RECEIVED, pausing...");
+					pause();
+					break;
+				}
+				case OsEvent::RESUME:
+				{
+					Log::i("RESUME EVENT RECEIVED, resuming...");
+					unpause();
+					break;
+				}
 				default:
 				{
 					CE_FATAL("Unknown Os Event");
@@ -91,11 +144,16 @@ public:
 				}
 			}
 		}
-		while (event.type != OsEvent::NONE);
 
 		return false;
 	}
 
+	//-----------------------------------------------------------------------------
+	void push_keyboard_event(uint32_t modifier, KeyboardButton::Enum b, bool pressed)
+	{
+		m_queue.push_keyboard_event(modifier, b, pressed);
+	}
+
 	//-----------------------------------------------------------------------------
 	void push_touch_event(uint16_t x, uint16_t y, uint8_t pointer_id)
 	{
@@ -108,9 +166,33 @@ public:
 		m_queue.push_touch_event(x, y, pointer_id, pressed);
 	}
 
+	void push_metrics_event(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
+	{
+		m_queue.push_metrics_event(x, y, width, height);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_pause_event()
+	{
+		m_queue.push_pause_event();
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_resume_event()
+	{
+		m_queue.push_resume_event();
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_exit_event(int32_t code)
+	{
+		m_queue.push_exit_event(code);
+	}
+
 private:
 
 	OsEventQueue m_queue;
+	OsThread m_game_thread;
 };
 
 static AndroidDevice* g_engine;
@@ -133,98 +215,55 @@ extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_shutdownCrown(JNIE
 }
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_initDevice(JNIEnv* /*env*/, jobject /*obj*/)
-{
-	device()->init();
-}
-
-//-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_stopDevice(JNIEnv* /*env*/, jobject /*obj*/)
-{
-	device()->stop();
-}
-
-//-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_shutdownDevice(JNIEnv* /*env*/, jobject /*obj*/)
-{
-	device()->shutdown();
-}
-
-//-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pauseDevice(JNIEnv* /*env*/, jobject /*obj*/)
-{
-	device()->pause();
-}
-
-//-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_unpauseDevice(JNIEnv* /*env*/, jobject /*obj*/)
-{
-	device()->unpause();
-}
-
-//-----------------------------------------------------------------------------
-extern "C" JNIEXPORT bool JNICALL Java_crown_android_CrownLib_isDeviceInit(JNIEnv* /*env*/, jobject /*obj*/)
-{
-	return device()->is_init();
-}
-
-//-----------------------------------------------------------------------------
-extern "C" JNIEXPORT bool JNICALL Java_crown_android_CrownLib_isDeviceRunning(JNIEnv* /*env*/, jobject /*obj*/)
-{
-	return device()->is_running();
-}
-
-//-----------------------------------------------------------------------------
-extern "C" JNIEXPORT bool JNICALL Java_crown_android_CrownLib_isDevicePaused(JNIEnv* /*env*/, jobject /*obj*/)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_run(JNIEnv* /*env*/, jobject /*obj*/)
 {
-	return device()->is_paused();
+	g_engine->run(0, NULL);
 }
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_run(JNIEnv* /*env*/, jobject /*obj*/)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushKeyboardEvent(JNIEnv * /*env*/, jobject /*obj*/, jint modifier, jint b, jint pressed)
 {
-	g_engine->run(0, NULL);
+	g_engine->push_keyboard_event(modifier, (KeyboardButton::Enum) b, pressed);
 }
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_initRenderer(JNIEnv* /*env*/, jobject /*obj*/)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushTouchEventMove(JNIEnv * /*env*/, jobject /*obj*/, jint pointer_id, jint x, jint y)
 {
-	device()->renderer()->init();
+	g_engine->push_touch_event(x, y, pointer_id);
 }
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_shutdownRenderer(JNIEnv* /*env*/, jobject /*obj*/)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushTouchEventPointer(JNIEnv * /*env*/, jobject /*obj*/, jint pointer_id, jint x, jint y, jint pressed)
 {
-	device()->renderer()->shutdown();
+	g_engine->push_touch_event(x, y, pointer_id, pressed);
 }
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pauseSoundRenderer(JNIEnv* /*env*/, jobject /*obj*/)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushAccelerometerEvent(JNIEnv * /*env*/, jobject /*obj*/, jint type, jfloat x, jfloat y, jfloat z)
 {
-	//device()->sound_renderer()->pause();
 }
 
-//-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_unpauseSoundRenderer(JNIEnv* /*env*/, jobject /*obj*/)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushMetricsEvent(JNIEnv * /*env*/, jobject /*obj*/, jint x, jint y, jint width, jint height)
 {
-	//device()->sound_renderer()->unpause();
+	g_engine->push_metrics_event(x, y, width, height);
 }
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushTouchEventMove(JNIEnv * /*env*/, jobject /*obj*/, jint pointer_id, jint x, jint y)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushPauseEvent(JNIEnv * /*env*/, jobject /*obj*/)
 {
-	g_engine->push_touch_event(x, y, pointer_id);
+	g_engine->push_pause_event();
 }
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushTouchEventPointer(JNIEnv * /*env*/, jobject /*obj*/, jint pointer_id, jint x, jint y, jint pressed)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushResumeEvent(JNIEnv * /*env*/, jobject /*obj*/)
 {
-	g_engine->push_touch_event(x, y, pointer_id, pressed);
+	g_engine->push_resume_event();
 }
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushAccelerometerEvent(JNIEnv * /*env*/, jobject /*obj*/, jint type, jfloat x, jfloat y, jfloat z)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushExitEvent(JNIEnv * /*env*/, jobject /*obj*/, jint code)
 {
+	g_engine->push_exit_event(code);
 }
 
 } // namespace crown

+ 4 - 3
engine/os/android/OsWindow.cpp

@@ -36,10 +36,11 @@ ANativeWindow* g_android_window = NULL;
 
 //-----------------------------------------------------------------------------
 OsWindow::OsWindow()
-	: m_x(0), m_y(0), m_width(0), m_height(0)
+	: m_x(0)
+	, m_y(0)
+	, m_width(0)
+	, m_height(0)
 {
-	m_width = ANativeWindow_getWidth(g_android_window);
-	m_height = ANativeWindow_getHeight(g_android_window);
 }
 
 //-----------------------------------------------------------------------------

+ 1 - 1
engine/os/android/OsWindow.h

@@ -89,7 +89,7 @@ public:
 	/// Stub method, does nothing under Android.
 	void			frame();
 
-private:
+public:
 
 	uint32_t		m_x;
 	uint32_t		m_y;

+ 78 - 35
engine/os/android/java/CrownActivity.java

@@ -42,12 +42,8 @@ import android.view.View;
 import android.view.Surface;
 import android.view.SurfaceView;
 import android.view.SurfaceHolder;
+import android.view.KeyEvent;
 
-import crown.android.CrownEnum;
-
-/**
-*	BootStrap of Android Application
-*/
 public class CrownActivity extends Activity
 {
 	// Debug
@@ -55,15 +51,10 @@ public class CrownActivity extends Activity
 
 	// Resource attributes
     static AssetManager 		mAssetManager;
-
-	// Input attributes
-	private CrownTouch 			mTouch;
-	private CrownSensor			mSensor;
-
 	private CrownSurfaceView 	mView;
 
 
-//-----------------------------------------------------------------------------
+	//-----------------------------------------------------------------------------
     public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
@@ -79,53 +70,105 @@ public class CrownActivity extends Activity
 		mView = new CrownSurfaceView(this);
         setContentView(mView);
 
-		// Init Input
-		mTouch = new CrownTouch(this);
-		mSensor = new CrownSensor(this);
-
 		Log.i(TAG, "Crown Activity created");
     }
 
-//-----------------------------------------------------------------------------
+	//-----------------------------------------------------------------------------
 	public void onResume()
 	{
 		super.onResume();
-		
-		if (CrownLib.isDeviceInit())
-		{
-			CrownLib.unpauseSoundRenderer();
-		}
-		// init accelerometer
-		mSensor.startListening(this);
-
+		CrownLib.pushResumeEvent();
 		Log.i(TAG, "Crown Activity resumed");
 	}
 
-//-----------------------------------------------------------------------------
+	//-----------------------------------------------------------------------------
 	public void onPause()
 	{
 		super.onPause();
-
-		CrownLib.pauseSoundRenderer();
-		// stop accelerometer
-		mSensor.stopListening();
-
+		CrownLib.pushPauseEvent();
 		Log.i(TAG, "Crown Activity paused");
 	}
 
-//-----------------------------------------------------------------------------
+	//-----------------------------------------------------------------------------
 	public void onDestroy()
 	{
 		super.onDestroy();
-
-		CrownLib.shutdownDevice();
+		CrownLib.pushExitEvent(0);
 		CrownLib.shutdownCrown();
+		Log.i(TAG, "Crown Activity destroyed");
+	}
+
+	//-----------------------------------------------------------------------------
+	public void onBackPressed()
+	{
+		// Simulate ESCAPE key
+		CrownLib.pushKeyboardEvent(0, 0x1B, 1);
 	}
 
-//-----------------------------------------------------------------------------
+	//-----------------------------------------------------------------------------
+	@Override
+	public boolean onKeyDown(int keyCode, KeyEvent event)
+	{
+		if ((keyCode == KeyEvent.KEYCODE_BACK))
+		{
+			CrownLib.pushKeyboardEvent(0, 0x1B, 1);
+		}
+		return super.onKeyUp(keyCode, event);
+	}
+
+	//-----------------------------------------------------------------------------
+	@Override
+	public boolean onKeyUp(int keyCode, KeyEvent event)
+	{
+		if ((keyCode == KeyEvent.KEYCODE_BACK))
+		{
+			CrownLib.pushKeyboardEvent(0, 0x1B, 0);
+		}
+		return super.onKeyDown(keyCode, event);
+	}
+
+	//-----------------------------------------------------------------------------
 	public boolean onTouchEvent(MotionEvent event)
 	{
-		mTouch.onTouch(event);
+		final int pointerIndex = event.getActionIndex();
+		final int pointerCount = event.getPointerCount();
+
+		final int pointerId = event.getPointerId(pointerIndex);
+		final float x = event.getX(pointerIndex);
+		final float y = event.getY(pointerIndex);
+
+		final int actionMasked = event.getActionMasked();
+
+		switch (actionMasked) 
+		{	
+			case MotionEvent.ACTION_DOWN:
+			case MotionEvent.ACTION_POINTER_DOWN:
+			{
+				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.pushTouchEventPointer(pointerId, (int)x, (int)y, 0);
+				break;			
+			}
+			case MotionEvent.ACTION_MOVE:
+			{
+				for (int index = 0; index < pointerCount; index++)
+				{
+					CrownLib.pushTouchEventMove(event.getPointerId(index), (int)event.getX(index), (int)event.getY(index));
+				}
+				break;
+			}
+		}
+
         return super.onTouchEvent(event);
 	}
 }

+ 7 - 21
engine/os/android/java/CrownLib.java

@@ -40,17 +40,6 @@ public class CrownLib
 	// Crown functions
 	public static native void		initCrown();
 	public static native void		shutdownCrown();
-	
-	// Device functions
-	public static native void 		initDevice();
-	public static native void		shutdownDevice();
-	public static native void		pauseDevice();
-	public static native void		unpauseDevice();
-	public static native void		stopDevice();
-
-	public static native boolean 	isDeviceInit();
-	public static native boolean	isDeviceRunning();
-	public static native boolean	isDevicePaused();
 
 	public static native int		run();
 
@@ -61,16 +50,13 @@ public class CrownLib
 	public static native void		createWindow(Surface window);
 	public static native void		destroyWindow();
 
-	// Renderer functions
-	public static native void		initRenderer();
-	public static native void		shutdownRenderer();
-
-	// SoundRenderer functions
-	public static native void		pauseSoundRenderer();
-	public static native void		unpauseSoundRenderer();
-
-	// InputManager functions
+	// Os events
+	public static native void		pushKeyboardEvent(int modifier, int b, int pressed);
 	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);	
+	public static native void		pushMetricsEvent(int x, int y, int width, int height);
+	public static native void 		pushAccelerometerEvent(int type, float x, float y, float z);
+	public static native void		pushPauseEvent();
+	public static native void		pushResumeEvent();
+	public static native void		pushExitEvent(int code);
 }

+ 5 - 49
engine/os/android/java/CrownSurfaceView.java

@@ -37,47 +37,14 @@ public class CrownSurfaceView extends SurfaceView implements SurfaceHolder.Callb
 {
 	private final String TAG = "crown";
 
-	private CrownMainThread mThread;
-
 	private boolean mSurfaceCreated;
 
-//-----------------------------------------------------------------------------
+	//-----------------------------------------------------------------------------
 	public CrownSurfaceView(Context context)
 	{
 		super(context);
-
 		getHolder().addCallback(this);
-
 		setFocusable(true);
-
-		mSurfaceCreated = false;
-	}
-
-//-----------------------------------------------------------------------------
-	public boolean isSurfaceCreated()
-	{
-		return mSurfaceCreated;
-	}
-
-//-----------------------------------------------------------------------------
-	public void createThread(SurfaceHolder holder)
-	{
-		mThread = new CrownMainThread(holder);
-		mThread.start();
-	}
-
-//-----------------------------------------------------------------------------
-	public void destroyThread()
-	{
-        try
-        {
-        	CrownLib.pauseDevice();
-            mThread.join();
-        }
-        catch (InterruptedException e)
-        {
-            Log.e("crown", "terminateThread corrupts");
-        }     
 	}
 
 	//-----------------------------------------------------------------------------
@@ -85,13 +52,10 @@ public class CrownSurfaceView extends SurfaceView implements SurfaceHolder.Callb
 	public void surfaceCreated(SurfaceHolder holder) 
 	{
 		Log.d(TAG, "Crown Surface created");
+		mSurfaceCreated = true;
 
-		if (!mSurfaceCreated)
-		{
-			mSurfaceCreated = true;
-
-			createThread(holder);
-		}
+		CrownLib.createWindow(holder.getSurface());
+		CrownLib.run();
 	}
 
 	//-----------------------------------------------------------------------------
@@ -99,15 +63,6 @@ public class CrownSurfaceView extends SurfaceView implements SurfaceHolder.Callb
 	public void surfaceDestroyed(SurfaceHolder holder) 
 	{
 		mSurfaceCreated = false;
-
-		destroyThread();
-
-		CrownLib.pauseDevice();
-
-		CrownLib.destroyWindow();
-
-		CrownLib.shutdownRenderer();
-
 		Log.d(TAG, "Crown Surface destroyed");
 	}
 
@@ -115,6 +70,7 @@ public class CrownSurfaceView extends SurfaceView implements SurfaceHolder.Callb
 	@Override
 	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
 	{
+		CrownLib.pushMetricsEvent(0, 0, width, height);
 		Log.d(TAG, "Crown Surface changed");
 	}
 }