From e4d6455ffe7440d2c0cb1a3c64c89546e00487fb Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 13 Oct 2020 17:05:58 +1100 Subject: [PATCH] Android: Fix immediately exiting if try to start game with invalid IP/port. Also clicking 'quit game' returns to main menu instead of exiting. --- src/Game.c | 24 ++---------------------- src/Http.c | 3 --- src/Launcher.c | 11 ++++++++++- src/TexturePack.c | 1 + src/Window.c | 24 ++++++++++++++++++++++++ 5 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/Game.c b/src/Game.c index 66d789832..be7ceed3c 100644 --- a/src/Game.c +++ b/src/Game.c @@ -670,33 +670,13 @@ static void Game_RunLoop(void) { #endif #ifdef CC_BUILD_ANDROID -static cc_bool winCreated; -static void OnWindowCreated(void* obj) { winCreated = true; } - +extern cc_bool Window_RemakeSurface(void); static cc_bool SwitchToGame() { - JNIEnv* env; - JavaGetCurrentEnv(env); - /* Reset components */ Platform_LogConst("undoing components"); Drawer2D_Component.Free(); //Http_Component.Free(); - - /* Force window to be destroyed and re-created */ - /* (see comments in setupForGame for why this has to be done) */ - JavaCallVoid(env, "setupForGame", "()V", NULL); - Event_Register_(&WindowEvents.Created, NULL, OnWindowCreated); - Platform_LogConst("Entering wait for window loop.."); - - /* Loop until window gets created async */ - while (WindowInfo.Exists && !winCreated) { - Window_ProcessEvents(); - Thread_Sleep(10); - } - - Platform_LogConst("OK I'm starting the game.."); - Event_Unregister_(&WindowEvents.Created, NULL, OnWindowCreated); - return winCreated; + return Window_RemakeSurface(); } #endif diff --git a/src/Http.c b/src/Http.c index 3adeb4bbf..4a649e4e6 100644 --- a/src/Http.c +++ b/src/Http.c @@ -1145,9 +1145,6 @@ void Http_UrlEncodeUrl(String* dst, const String* src) { *-----------------------------------------------------Http component------------------------------------------------------* *#########################################################################################################################*/ static void OnInit(void) { -#ifdef CC_BUILD_ANDROID - if (workerThread) return; -#endif Http_WorkerInit(); ScheduledTask_Add(30, Http_CleanCacheTask); RequestList_Init(&pendingReqs); diff --git a/src/Launcher.c b/src/Launcher.c index 4e4ee8d56..43f4d959c 100644 --- a/src/Launcher.c +++ b/src/Launcher.c @@ -308,7 +308,16 @@ void Launcher_Run(void) { #ifdef CC_BUILD_ANDROID extern int Program_Run(int argc, char** argv); - if (Launcher_ShouldExit) Program_Run(0, NULL); + extern cc_bool Window_RemakeSurface(void); + + if (Launcher_ShouldExit) { + Launcher_ShouldExit = false; + Http_Component.Free(); + + Program_Run(0, NULL); + Window_RemakeSurface(); + Launcher_Run(); + } #endif if (Launcher_ShouldUpdate) { const char* action; diff --git a/src/TexturePack.c b/src/TexturePack.c index 8e4419452..24fd49873 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -114,6 +114,7 @@ GfxResourceID Atlas2D_LoadTile(TextureLoc texLoc) { static void Atlas2D_Free(void) { Mem_Free(Atlas2D.Bmp.scan0); Atlas2D.Bmp.scan0 = NULL; + Atlas2D.RowsCount = 0; } static void Atlas1D_Free(void) { diff --git a/src/Window.c b/src/Window.c index 101e53aba..de604147d 100644 --- a/src/Window.c +++ b/src/Window.c @@ -3789,6 +3789,30 @@ void Window_Create(int width, int height) { /* actual window creation is done when processSurfaceCreated is received */ } +static cc_bool winCreated; +static void OnWindowCreated(void* obj) { winCreated = true; } +cc_bool Window_RemakeSurface(void) { + JNIEnv* env; + JavaGetCurrentEnv(env); + winCreated = false; + + /* Force window to be destroyed and re-created */ + /* (see comments in setupForGame for why this has to be done) */ + JavaCallVoid(env, "setupForGame", "()V", NULL); + Event_Register_(&WindowEvents.Created, NULL, OnWindowCreated); + Platform_LogConst("Entering wait for window exist loop.."); + + /* Loop until window gets created async */ + while (WindowInfo.Exists && !winCreated) { + Window_ProcessEvents(); + Thread_Sleep(10); + } + + Platform_LogConst("OK window created.."); + Event_Unregister_(&WindowEvents.Created, NULL, OnWindowCreated); + return winCreated; +} + void Window_SetTitle(const String* title) { /* TODO: Implement this somehow */ /* Maybe https://stackoverflow.com/questions/2198410/how-to-change-title-of-activity-in-android */