diff --git a/android/app/src/main/java/com/classicube/MainActivity.java b/android/app/src/main/java/com/classicube/MainActivity.java index 56573c378..536689ff7 100644 --- a/android/app/src/main/java/com/classicube/MainActivity.java +++ b/android/app/src/main/java/com/classicube/MainActivity.java @@ -193,6 +193,8 @@ public class MainActivity extends Activity { HACK_avoidFileUriExposedErrors(); if (!gameRunning) startGameAsync(); + // TODO rethink to avoid this + if (gameRunning) updateInstance(); super.onCreate(savedInstanceState); } @@ -382,6 +384,7 @@ public class MainActivity extends Activity { native void processOnLowMemory(); native void runGameAsync(); + native void updateInstance(); // ====================================== // --------------- VIEWS ---------------- diff --git a/src/Platform_Android.c b/src/Platform_Android.c index 72c9f1a1f..bf510f365 100644 --- a/src/Platform_Android.c +++ b/src/Platform_Android.c @@ -198,4 +198,45 @@ void JavaCall_String_String(const char* name, const cc_string* arg, cc_string* d ReturnString(env, obj, dst); (*env)->DeleteLocalRef(env, args[0].l); } + + +/*########################################################################################################################* +*----------------------------------------------------Initialisation-------------------------------------------------------* +*#########################################################################################################################*/ +extern void android_main(void); +static void JNICALL java_updateInstance(JNIEnv* env, jobject instance) { + Platform_LogConst("App instance updated!"); + App_Instance = (*env)->NewGlobalRef(env, instance); + /* TODO: Do we actually need to remove that global ref later? */ +} + +/* Called eventually by the activity java class to actually start the game */ +static void JNICALL java_runGameAsync(JNIEnv* env, jobject instance) { + void* thread; + java_updateInstance(env, instance); + + Platform_LogConst("Running game async!"); + /* The game must be run on a separate thread, as blocking the */ + /* main UI thread will cause a 'App not responding..' messagebox */ + thread = Thread_Start(android_main); + Thread_Detach(thread); +} +static const JNINativeMethod methods[] = { + { "updateInstance", "()V", java_updateInstance }, + { "runGameAsync", "()V", java_runGameAsync } +}; + +/* This method is automatically called by the Java VM when the */ +/* activity java class calls 'System.loadLibrary("classicube");' */ +CC_API jint JNI_OnLoad(JavaVM* vm, void* reserved) { + jclass klass; + JNIEnv* env; + VM_Ptr = vm; + JavaGetCurrentEnv(env); + + klass = (*env)->FindClass(env, "com/classicube/MainActivity"); + App_Class = (*env)->NewGlobalRef(env, klass); + JavaRegisterNatives(env, methods); + return JNI_VERSION_1_4; +} #endif diff --git a/src/Program.c b/src/Program.c index dfae2dfa6..6d86a5d78 100644 --- a/src/Program.c +++ b/src/Program.c @@ -123,42 +123,13 @@ int main_real(int argc, char** argv) { #elif defined CC_BUILD_ANDROID /* ClassiCube is just a native library on android, */ /* unlike other platforms where it is the executable. */ -/* (activity java class is responsible for kickstarting the game) */ -static void android_main(void) { +/* (activity java class is responsible for kickstarting the game, + see Platform_Android.c for the code that actually calls this) */ +void android_main(void) { Platform_LogConst("Main loop started!"); SetupProgram(0, NULL); for (;;) { RunProgram(0, NULL); } } - -/* Called eventually by the activity java class to actually start the game */ -static void JNICALL java_runGameAsync(JNIEnv* env, jobject instance) { - void* thread; - App_Instance = (*env)->NewGlobalRef(env, instance); - /* TODO: Do we actually need to remove that global ref later? */ - - Platform_LogConst("Running game async!"); - /* The game must be run on a separate thread, as blocking the */ - /* main UI thread will cause a 'App not responding..' messagebox */ - thread = Thread_Start(android_main); - Thread_Detach(thread); -} -static const JNINativeMethod methods[] = { - { "runGameAsync", "()V", java_runGameAsync } -}; - -/* This method is automatically called by the Java VM when the */ -/* activity java class calls 'System.loadLibrary("classicube");' */ -CC_API jint JNI_OnLoad(JavaVM* vm, void* reserved) { - jclass klass; - JNIEnv* env; - VM_Ptr = vm; - JavaGetCurrentEnv(env); - - klass = (*env)->FindClass(env, "com/classicube/MainActivity"); - App_Class = (*env)->NewGlobalRef(env, klass); - JavaRegisterNatives(env, methods); - return JNI_VERSION_1_4; -} #else /* NOTE: main_real is used for when compiling with MingW without linking to startup files. */ /* Normally, the final code produced for "main" is our "main" combined with crt's main */ diff --git a/src/Window_Android.c b/src/Window_Android.c index 51a1ead69..ed9a3ad13 100644 --- a/src/Window_Android.c +++ b/src/Window_Android.c @@ -261,7 +261,7 @@ void Window_Init(void) { DisplayInfo.ScaleY = JavaICall_Float(env, JAVA_getDpiY, NULL); } -static void Window_RemakeSurface(void) { +static void RemakeWindowSurface(void) { JNIEnv* env; JavaGetCurrentEnv(env); winCreated = false; @@ -272,6 +272,7 @@ static void Window_RemakeSurface(void) { Platform_LogConst("Entering wait for window exist loop.."); /* Loop until window gets created by main UI thread */ + /* (i.e. until processSurfaceCreated is received) */ while (!winCreated) { Window_ProcessEvents(); Thread_Sleep(10); @@ -282,8 +283,7 @@ static void Window_RemakeSurface(void) { static void DoCreateWindow(void) { WindowInfo.Exists = true; - /* actual window creation is done when processSurfaceCreated is received */ - Window_RemakeSurface(); + RemakeWindowSurface(); /* always start as fullscreen */ Window_EnterFullscreen(); }