Przeglądaj źródła

remove android main thread (java) and add many functions to android env

mikymod 12 lat temu
rodzic
commit
9aa37f583b

+ 37 - 6
engine/os/android/AndroidDevice.cpp

@@ -26,12 +26,13 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include <jni.h>
 #include "Device.h"
+#include "Renderer.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_init(JNIEnv* /*env*/, jobject /*obj*/)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_initDevice(JNIEnv* /*env*/, jobject /*obj*/)
 {
 	const char* argv[] = { "crown-android" };
 
@@ -39,27 +40,57 @@ extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_init(JNIEnv* /*env
 }
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_shutdown(JNIEnv* /*env*/, jobject /*obj*/)
+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_frame(JNIEnv* /*env*/, jobject /*obj*/)
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pauseDevice(JNIEnv* /*env*/, jobject /*obj*/)
 {
-	device()->frame();
+	device()->pause();
 }
 
 //-----------------------------------------------------------------------------
-extern "C" JNIEXPORT bool JNICALL Java_crown_android_CrownLib_isInit(JNIEnv* /*env*/, jobject /*obj*/)
+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_isRunning(JNIEnv* /*env*/, jobject /*obj*/)
+extern "C" JNIEXPORT bool JNICALL Java_crown_android_CrownLib_isDeviceRunning(JNIEnv* /*env*/, jobject /*obj*/)
 {
 	return device()->is_running();
 }
 
+//-----------------------------------------------------------------------------
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_frame(JNIEnv* /*env*/, jobject /*obj*/)
+{
+	device()->frame();
+}
+
+//-----------------------------------------------------------------------------
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_initRenderer(JNIEnv* /*env*/, jobject /*obj*/)
+{
+	device()->renderer()->init();
+}
+
+//-----------------------------------------------------------------------------
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_shutdownRenderer(JNIEnv* /*env*/, jobject /*obj*/)
+{
+	device()->renderer()->shutdown();
+}
+
 } // namespace crown

+ 10 - 8
engine/os/android/AndroidOS.cpp

@@ -131,19 +131,21 @@ bool exists(const char* path)
 //-----------------------------------------------------------------------------
 bool is_directory(const char* path)
 {
-	struct stat info;
-	memset(&info, 0, sizeof(struct stat));
-	lstat(path, &info);
-	return ((S_ISDIR(info.st_mode)) != 0 && (S_ISLNK(info.st_mode) == 0));
+	// struct stat info;
+	// memset(&info, 0, sizeof(struct stat));
+	// lstat(path, &info);
+	// return ((S_ISDIR(info.st_mode)) != 0 && (S_ISLNK(info.st_mode) == 0));
+	return true;
 }
 
 //-----------------------------------------------------------------------------
 bool is_file(const char* path)
 {
-	struct stat info;
-	memset(&info, 0, sizeof(struct stat));
-	lstat(path, &info);
-	return ((S_ISREG(info.st_mode) != 0) && (S_ISLNK(info.st_mode) == 0));
+	// struct stat info;
+	// memset(&info, 0, sizeof(struct stat));
+	// lstat(path, &info);
+	// return ((S_ISREG(info.st_mode) != 0) && (S_ISLNK(info.st_mode) == 0));
+	return true;
 }
 
 //-----------------------------------------------------------------------------

+ 2 - 2
engine/os/android/ApkFilesystem.cpp

@@ -59,13 +59,13 @@ void ApkFilesystem::close(File* file)
 //-----------------------------------------------------------------------------
 bool ApkFilesystem::is_directory(const char* path)
 {
-	return false;
+	return true;
 }
 
 //-----------------------------------------------------------------------------
 bool ApkFilesystem::is_file(const char* path)
 {
-	return false;
+	return true;
 }
 
 //-----------------------------------------------------------------------------

+ 25 - 20
engine/os/android/CrownActivity.java

@@ -38,6 +38,7 @@ import android.hardware.SensorManager;
 import android.content.Context;
 import android.widget.Toast;
 import android.content.res.AssetManager;
+import android.view.View;
 import android.view.Surface;
 import android.view.SurfaceView;
 import android.view.SurfaceHolder;
@@ -51,7 +52,7 @@ public class CrownActivity extends Activity
 {
 
 	// Debug
-	public static String TAG = "CrownActivity";
+	public static String TAG = "crown";
 
 	// Resource attributes
     static AssetManager 		mAssetManager;
@@ -68,6 +69,8 @@ public class CrownActivity extends Activity
     {
         super.onCreate(savedInstanceState);
 
+		Log.i(TAG, "Crown Activity created");
+
 		// init AssetManager
 		mAssetManager = getAssets();
 		CrownLib.initAssetManager(mAssetManager);
@@ -86,30 +89,29 @@ public class CrownActivity extends Activity
 	{
 		super.onStart();
 
-	}
-
-//-----------------------------------------------------------------------------
-	public void onRestart()
-	{
-		super.onRestart();
+		Log.i(TAG, "Crown Activity started");
 	}
 
 //-----------------------------------------------------------------------------
 	public void onResume()
 	{
 		super.onResume();
-		
+
 		// init accelerometer
-		if (!mSensor.startListening(this))
-		{
-			finish();
-		}
+		mSensor.startListening(this);
+
+		Log.i(TAG, "Crown Activity resumed");
 	}
 
 //-----------------------------------------------------------------------------
 	public void onPause()
 	{
 		super.onPause();
+
+		// stop accelerometer
+		mSensor.stopListening();
+
+		Log.i(TAG, "Crown Activity paused");
 	}
 
 //-----------------------------------------------------------------------------
@@ -117,14 +119,23 @@ public class CrownActivity extends Activity
 	{
 		super.onStop();
 
-		// stop accelerometer
-		mSensor.stopListening();
+		Log.i(TAG, "Crown Activity stopped");
+	}
+
+//-----------------------------------------------------------------------------
+	public void onRestart()
+	{
+		super.onRestart();
 	}
 
 //-----------------------------------------------------------------------------
 	public void onDestroy()
 	{
 		super.onDestroy();
+
+		CrownLib.stopDevice();
+
+		Log.i(TAG, "Crown Activity destroyed");
 	}
 
 //-----------------------------------------------------------------------------
@@ -133,10 +144,4 @@ public class CrownActivity extends Activity
 		mTouch.onTouch(event);
         return super.onTouchEvent(event);
 	}
-
-//-----------------------------------------------------------------------------
-	public boolean hasMultiTouchSupport(Context context)
-	{
-		return context.getPackageManager().hasSystemFeature("android.hardware.touchscreen.multitouch");
-	}
 }

+ 17 - 8
engine/os/android/CrownLib.java

@@ -38,20 +38,29 @@ public class CrownLib
 	}
 	
 	// Device functions
-	public static native void 		init();
+	public static native void 		initDevice();
+	public static native void		stopDevice();
+	public static native void		pauseDevice();
+	public static native void		unpauseDevice();
+	public static native void 		shutdownDevice();
+
+	public static native boolean 	isDeviceInit();
+	public static native boolean	isDeviceRunning();
+
 	public static native void 		frame();
-	public static native void 		shutdown();
-	public static native boolean 	isInit();
-	public static native boolean	isRunning();
 
 	// AssetManager functions
 	public static native void 		initAssetManager(AssetManager assetManager);
 
-	// InputManager functions
-	public static native void 		pushIntEvent(int type, int a, int b, int c, int d);
-	public static native void 		pushFloatEvent(int type, float a, float b, float c, float d);
-
 	// Window functions
 	public static native void		setWindow(Surface window);
 	public static native void 		setDisplaySize(int width, int height);
+
+	// Renderer functions
+	public static native void		initRenderer();
+	public static native void		shutdownRenderer();
+
+	// InputManager functions
+	public static native void 		pushIntEvent(int type, int a, int b, int c, int d);
+	public static native void 		pushFloatEvent(int type, float a, float b, float c, float d);	
 }

+ 22 - 13
engine/os/android/CrownSurfaceView.java

@@ -31,18 +31,18 @@ import android.view.Surface;
 import android.view.SurfaceView;
 import android.view.SurfaceHolder;
 
+import android.util.Log;
+
 public class CrownSurfaceView extends SurfaceView implements SurfaceHolder.Callback
 {
-	private MainThread mMainThread;
+	private final String TAG = "crown";
 
 	//-----------------------------------------------------------------------------
 	public CrownSurfaceView(Context context)
 	{
 		super(context);
 
-		this.getHolder().addCallback(this);
-
-		mMainThread = new MainThread(getHolder(), this);
+		getHolder().addCallback(this);
 
 		setFocusable(true);
 	}
@@ -51,26 +51,35 @@ public class CrownSurfaceView extends SurfaceView implements SurfaceHolder.Callb
 	@Override
 	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
 	{
+		Log.d(TAG, "Crown Surface changed");
 	}
 
 	//-----------------------------------------------------------------------------
 	@Override
 	public void surfaceCreated(SurfaceHolder holder) 
 	{
-		mMainThread.start();
+		Log.d(TAG, "Crown Surface created");
+
+		CrownLib.setWindow(getHolder().getSurface());
+
+		if (!CrownLib.isDeviceInit())
+		{
+			CrownLib.initDevice();
+		}
+		else
+		{
+			CrownLib.initRenderer();
+			CrownLib.unpauseDevice();
+		}
 	}
 
 	//-----------------------------------------------------------------------------
 	@Override
 	public void surfaceDestroyed(SurfaceHolder holder) 
 	{
-		try
-		{
-			mMainThread.join();
-		}
-		catch (InterruptedException e)
-		{
-			e.printStackTrace();
-		}
+		Log.d(TAG, "Crown Surface destroyed");
+		
+		CrownLib.pauseDevice();
+		CrownLib.shutdownRenderer();
 	}
 }

+ 0 - 58
engine/os/android/MainThread.java

@@ -1,58 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-package crown.android;
-
-import android.view.SurfaceHolder;
-
-public class MainThread extends Thread
-{
-	private SurfaceHolder mHolder;
-	private CrownSurfaceView mView;
-
-	public MainThread(SurfaceHolder holder, CrownSurfaceView view)
-	{
-		super();
-
-		mHolder = holder;
-		mView = view;
-	}
-
-	// This is the classic main() replacement for Android
-	@Override
-	public void run()
-	{
-		CrownLib.setWindow(mHolder.getSurface());
-		CrownLib.init();
-
-		while (CrownLib.isRunning())
-		{
-			CrownLib.frame();
-		}
-
-		CrownLib.shutdown();
-	}
-}

+ 9 - 13
engine/os/android/OsWindow.cpp

@@ -24,41 +24,37 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include <android/native_window_jni.h>
-
 #include "OsWindow.h"
 #include "GLContext.h"
 #include "Assert.h"
+#include "Log.h"
 
 namespace crown
 {
 
-static ANativeWindow* window = NULL;
+static ANativeWindow* g_android_window = NULL;
 
 //-----------------------------------------------------------------------------
 OsWindow::OsWindow(uint32_t /*width*/, uint32_t /*height*/, uint32_t /*parent*/) :
-	m_window(NULL),
 	m_x(0),
 	m_y(0),
 	m_width(0),
 	m_height(0)
 {
-	m_window = window;
 
-	m_width = ANativeWindow_getWidth(m_window);
-	m_height = ANativeWindow_getHeight(m_window);
+	m_width = ANativeWindow_getWidth(g_android_window);
+	m_height = ANativeWindow_getHeight(g_android_window);
 
-    set_android_window(m_window);
+    set_android_window(g_android_window);
 }
 
 //-----------------------------------------------------------------------------
 OsWindow::~OsWindow()
 {
-	if (m_window)
+	if (g_android_window)
 	{
-		ANativeWindow_release(m_window);
+		ANativeWindow_release(g_android_window);
 	}
-
 }
 
 //-----------------------------------------------------------------------------
@@ -129,7 +125,7 @@ void OsWindow::set_title(const char* /*title*/)
 //-----------------------------------------------------------------------------
 void OsWindow::frame()
 {
-	// Implemented Java-side
+	// Log::i("window width: %d", ANativeWindow_getWidth(g_android_window));
 }
 
 //-----------------------------------------------------------------------------
@@ -137,7 +133,7 @@ extern "C" void Java_crown_android_CrownLib_setWindow(JNIEnv *env, jclass /*claz
 {
     // obtain a native window from a Java surface
 	CE_ASSERT(surface != 0, "Unable to get Android window");
-    window = ANativeWindow_fromSurface(env, surface);
+    g_android_window = ANativeWindow_fromSurface(env, surface);
 }
 
 } // namespace crown

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

@@ -28,6 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include <sys/types.h>
 #include <android/native_window.h>
+#include <android/native_window_jni.h>
 
 namespace crown
 {
@@ -81,8 +82,6 @@ public:
 
 private:
 
-	ANativeWindow*	m_window;
-
 	uint32_t		m_x;
 	uint32_t		m_y;
 	uint32_t		m_width;

+ 5 - 0
engine/renderers/gles/egl/GLContext.cpp

@@ -28,6 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Assert.h"
 #include "GLContext.h"
+#include "Log.h"
 
 namespace crown
 {
@@ -82,6 +83,8 @@ void GLContext::create_context()
 	surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)awindow, NULL);
 
     eglMakeCurrent(display, surface, surface, context);
+
+    Log::i("EGL context created");
 }
 
 //-----------------------------------------------------------------------------
@@ -90,6 +93,8 @@ void GLContext::destroy_context()
  	eglDestroyContext(display, context);
  	eglDestroySurface(display, surface);
  	eglTerminate(display);
+
+    Log::i("EGL context destroyed");
 }
 
 //-----------------------------------------------------------------------------