diff --git a/src/Cog/lex.yy.c b/src/Cog/lex.yy.c index bafb1d566..8a0428611 100644 --- a/src/Cog/lex.yy.c +++ b/src/Cog/lex.yy.c @@ -1174,6 +1174,11 @@ FILE *input_file; #endif { +#ifdef TARGET_ESP32 + /* (as newer flex does: sithCogParse_ESP32_ReleaseLexer may have freed it) */ + if ( ! yy_current_buffer ) + yy_current_buffer = yy_create_buffer( (FILE*)yyin, YY_BUF_SIZE ); +#endif yy_init_buffer( yy_current_buffer, input_file ); yy_load_buffer_state(); } @@ -1303,3 +1308,16 @@ FILE *file; } # line 89 "cog.l" +#ifdef TARGET_ESP32 +/* ESP32: the lexer's input buffer (YY_BUF_SIZE) is made by the first parse + * and kept for the process. The port frees it when the engine has stopped: + * made during the first level load, it otherwise stays in the middle of the + * PSRAM heap after the game and splits its largest free block. The next parse + * makes a new one (yyrestart / yylex). */ +void sithCogParse_ESP32_ReleaseLexer(void) +{ + if ( yy_current_buffer ) + yy_delete_buffer( yy_current_buffer ); + yy_init = 1; +} +#endif diff --git a/src/Gui/jkGUIForce.c b/src/Gui/jkGUIForce.c index 9db678cef..874fc09ca 100644 --- a/src/Gui/jkGUIForce.c +++ b/src/Gui/jkGUIForce.c @@ -649,6 +649,13 @@ int jkGuiForce_Show(int bCanSpendStars, int isMulti, int a4, char16_t* a5, int * clicked = jkGuiRend_DisplayAndReturnClicked(jkGuiForce_pMenu); if ( clicked == -1 ) { +#ifdef TARGET_ESP32 + // Added: closing the menus for a pending load or a quit (see + // jkGuiRend_ESP32_bCloseMenus): the Yes/No below returns -1 too, + // so this screen would reopen forever + if ( jkGuiRend_ESP32_bCloseMenus ) + break; +#endif if ( !jkGuiDialog_YesNoDialog(jkStrings_GetUniStringWithFallback("GUI_ABORT_GAME"), jkStrings_GetUniStringWithFallback("GUI_CONFIRM_ABORTCD")) ) continue; } diff --git a/src/Gui/jkGUIMain.c b/src/Gui/jkGUIMain.c index 8523f8752..3a6d04a27 100644 --- a/src/Gui/jkGUIMain.c +++ b/src/Gui/jkGUIMain.c @@ -156,6 +156,12 @@ void jkGuiMain_Show() do { if (g_should_exit) return; // Added +#ifdef TARGET_ESP32 + // Added: a level switch requested from outside the menus (the + // tab5-emu pause menu loading a save) waits for this loop to end; + // leave it the way starting a game from the menus does + if (jkGuiRend_ESP32_bCloseMenus) break; +#endif jkGuiRend_MenuSetEscapeKeyShortcutElement(&jkGuiMain_menu, &jkGuiMain_elements[2]); v1 = jkGuiRend_DisplayAndReturnClicked(&jkGuiMain_menu); @@ -278,6 +284,11 @@ void jkGuiMain_ShowCutscenes() } } while ( v4 != -1 ); +#ifdef TARGET_ESP32 + // Added: closed for a pending load (see jkGuiRend_ESP32_bCloseMenus): + // going back to the main menu would replace that switch + if ( !jkGuiRend_ESP32_bCloseMenus ) +#endif jkMain_MenuReturn(); LABEL_17: v7 = 0; diff --git a/src/Gui/jkGUIPlayer.c b/src/Gui/jkGUIPlayer.c index 42f44716e..1c6b3d11c 100644 --- a/src/Gui/jkGUIPlayer.c +++ b/src/Gui/jkGUIPlayer.c @@ -21,6 +21,9 @@ #include "General/stdFnames.h" #include "General/stdFileUtil.h" #include "World/jkPlayer.h" +#ifdef TARGET_ESP32 +#include "Devices/sithControl.h" +#endif static int jkGuiPlayer_bInitted = 0; @@ -350,7 +353,20 @@ void jkGuiPlayer_ShowNewPlayer(int a1) jkPlayer_CreateConf(jkGuiPlayer_awTmp_555D28); } if ( v7 < 0 ) + { +#ifdef TARGET_ESP32 + // Added: closing the menus for a pending load or a quit (see + // jkGuiRend_ESP32_bCloseMenus). With no profile to go back to + // (v15) this dialog reopens until one is created, i.e. forever; + // leave with the default controls instead, since no profile was + // read. With profiles, going back to the list reads the selected + // one (its -1 case) and leaves. + if ( jkGuiRend_ESP32_bCloseMenus && v15 ) + sithControl_DefaultInit(); + else +#endif v14 = 1; + } } while ( v14 ); jkGuiRend_DarrayFree(&a1a); diff --git a/src/Gui/jkGUIRend.c b/src/Gui/jkGUIRend.c index 4e4df11ed..712ec6e0d 100644 --- a/src/Gui/jkGUIRend.c +++ b/src/Gui/jkGUIRend.c @@ -36,6 +36,20 @@ void jkGuiRend_ESP32_CancelActiveMenu(void) if (jkGuiRend_activeMenu && !jkGuiRend_activeMenu->lastClicked) jkGuiRend_activeMenu->lastClicked = -1; } + +// tab5-emu: set when the platform asks for a GUI state switch while a modal +// menu is open (the pause menu loading a save at the engine's main menu). +// jkMain_GuiAdvance switches only once no menu is open, and on this target a +// menu does not close by itself for that (the original's jkGuiRend_thing_four +// cancel is off for TARGET_RETRO_HOMEBREW, see below), so the load would wait +// for the user to leave the menus -- and New Game would replace it. While set, +// every menu returns -1 (back), the screens that would ask "abort?" or reopen +// themselves on -1 (Force, tally, new player) return instead, jkGuiMain_Show +// leaves its loop, and the state handlers that fall back to the main menu on +// -1 keep the pending switch; cleared by jkMain_GuiAdvance when the switch +// happens. Also set by a quit from outside the engine (Window_MessageLoop): +// the same loops are what kept a quit at the main menu from returning. +int jkGuiRend_ESP32_bCloseMenus = 0; #endif static tVBuffer* jkGuiRend_menuBuffer = NULL; static tVBuffer *jkGuiRend_texture_dword_8561E8 = NULL; @@ -343,6 +357,10 @@ int32_t jkGuiRend_DisplayAndReturnClicked(jkGuiMenu *menu) while ( !menu->lastClicked ) { msgret = Window_MessageLoop(); +#ifdef TARGET_ESP32 + if ( jkGuiRend_ESP32_bCloseMenus ) + menu->lastClicked = -1; +#endif if ( jkGuiRend_thing_four && jkGuiRend_thing_five ) { // Added: this makes the menu that appears when pressing ESC in jkGUISingleTally flicker, @@ -544,6 +562,11 @@ void jkGuiRend_Shutdown() jkGuiRend_thing_five = 0; jkGuiRend_thing_four = 0; +#ifdef TARGET_ESP32 + // Added: a quit (or a quit right after a load at a menu) ends the session + // before jkMain_GuiAdvance clears it; the next launch starts without it + jkGuiRend_ESP32_bCloseMenus = 0; +#endif jkGuiRend_bIsSurfaceValid = 0; jkGuiRend_bInitted = 0; jkGuiRend_bOpen = 0; diff --git a/src/Gui/jkGUIRend.h b/src/Gui/jkGUIRend.h index aa563c716..2b9f3513d 100644 --- a/src/Gui/jkGUIRend.h +++ b/src/Gui/jkGUIRend.h @@ -84,6 +84,11 @@ extern int32_t jkGuiRend_thing_five; extern int32_t jkGuiRend_thing_four; +#ifdef TARGET_ESP32 +// tab5-emu: close the open modal menus for a pending GUI state switch or a quit (see jkGUIRend.c) +extern int jkGuiRend_ESP32_bCloseMenus; +#endif + void jkGuiRend_CopyVBuffer(jkGuiMenu *menu, rdRect *rect); void jkGuiRend_SetPalette(uint8_t* pal); void jkGuiRend_DrawRect(tVBuffer *vbuf, rdRect *rect, __int16 color); diff --git a/src/Gui/jkGUISingleTally.c b/src/Gui/jkGUISingleTally.c index b24ca1806..a7db6b852 100644 --- a/src/Gui/jkGUISingleTally.c +++ b/src/Gui/jkGUISingleTally.c @@ -62,6 +62,13 @@ int jkGuiSingleTally_Show() ret = jkGuiRend_DisplayAndReturnClicked(&jkGuiSingleTally_menu); if ( ret == -1 ) { +#ifdef TARGET_ESP32 + // Added: closing the menus for a pending load or a quit (see + // jkGuiRend_ESP32_bCloseMenus): the Yes/No below returns -1 too, + // so this screen would reopen forever + if ( jkGuiRend_ESP32_bCloseMenus ) + break; +#endif if ( !jkGuiDialog_YesNoDialog(jkStrings_GetUniStringWithFallback("GUI_ABORT_GAME"), jkStrings_GetUniStringWithFallback("GUI_CONFIRM_ABORTCD")) ) continue; } diff --git a/src/Main/jkMain.c b/src/Main/jkMain.c index 38a278179..2a47a005b 100644 --- a/src/Main/jkMain.c +++ b/src/Main/jkMain.c @@ -302,6 +302,9 @@ void jkMain_GuiAdvance() if ( jkSmack_stopTick && !jkGuiRend_thing_five ) { jkGuiRend_thing_four = 0; +#ifdef TARGET_ESP32 + jkGuiRend_ESP32_bCloseMenus = 0; // Added: the switch it waited for happens now +#endif v4 = jkSmack_currentGuiState; v5 = jkMain_aGuiStateFuncs[jkSmack_currentGuiState].leaveFunc; if ( v5 ) @@ -493,6 +496,12 @@ void jkMain_EndLevelScreenShow(int a1, int a2) } } +#ifdef TARGET_ESP32 + // Added: the tally / Force screen was closed for a pending load (see + // jkGuiRend_ESP32_bCloseMenus); the main menu would replace that switch + if ( jkGuiRend_ESP32_bCloseMenus ) + return; +#endif if ( jkGuiRend_thing_five ) jkGuiRend_thing_four = 1; jkSmack_stopTick = 1; @@ -510,6 +519,26 @@ void jkMain_EndLevelScreenLeave(int a1, int a2) ; } +#ifdef TARGET_ESP32 +// Added: a level needs the static world (static.jkl: the player's templates, +// models, puppets and their keyframes) and the inventory bins (items.dat), +// which jkMain_TitleShow loads before any menu can start one. A level reached +// without the title (the tab5-emu pause menu loading a save while the intro +// is still playing) got neither, and the save's first static keyframe (id +// 0x8000 | n) dereferenced a NULL sithWorld_g_pStaticWorld in +// sithKeyFrame_GetByIdx. Same as the title screen and +// Main_StartupDedicated's level autostart. A no-op once the title has run. +static int jkMain_ESP32_OpenStaticIfNoTitle(const char* pCaller) +{ + if ( sithWorld_g_pStaticWorld ) + return 0; + stdPlatform_Printf("OpenJKDF2: %s: no title screen yet, opening static.jkl\n", pCaller); + sithOpenStatic("static.jkl"); + jkHudInv_InitItems(); + return 1; +} +#endif + void jkMain_GameplayShow(int a1, int a2) { signed int level_loaded; // esi @@ -556,6 +585,12 @@ void jkMain_GameplayShow(int a1, int a2) jkGuiTitle_ShowLoading(jkMain_aLevelJklFname, 0); } +#ifdef TARGET_ESP32 + // Added: fallback for a level reached without the title by any other + // way than jkMain_UnkShow (which opens the static world itself) + jkMain_ESP32_OpenStaticIfNoTitle(__func__); +#endif + // MOTS added: // jkEpisode_Shutdown v3 = 0; // Added @@ -888,6 +923,12 @@ void jkMain_ChoiceShow(int a1, int a2) if ( jkGuiForce_Show(0, 0.0, 1, 0, &v1, 1) == -1 ) { +#ifdef TARGET_ESP32 + // Added: closed for a pending load (see jkGuiRend_ESP32_bCloseMenus); + // the main menu would replace that switch + if ( jkGuiRend_ESP32_bCloseMenus ) + return; +#endif if ( jkGuiRend_thing_five ) jkGuiRend_thing_four = 1; jkSmack_stopTick = 1; @@ -911,6 +952,20 @@ void jkMain_ChoiceLeave(int a1, int a2) void jkMain_UnkShow(int a1, int a2) { +#ifdef TARGET_ESP32 + // Added: a save loaded before the title screen (from the intro, see + // jkMain_ESP32_OpenStaticIfNoTitle). Opened here, before jkMain_UnkTick + // mounts the save's episode GOB, so static.jkl resolves its files the way + // the title does (jkRes searches the episode directory before the + // resource GOBs, and the static world stays for the whole session), with + // the title's loading screen; the cutscene is closed by now. + if ( !sithWorld_g_pStaticWorld ) + { + jkGuiTitle_ShowLoadingStatic(); + jkMain_ESP32_OpenStaticIfNoTitle(__func__); + jkGuiTitle_LoadingFinalize(); + } +#endif jkPlayer_SetAmmoMaximums(0); // MOTS added } @@ -1486,6 +1541,14 @@ void jkMain_VideoLeave(int a1, int a2) // sithTime_Resume(); jkCutscene_sub_421410(); +#ifdef TARGET_ESP32 + // Added: a save loaded during an inter-level cutscene (the tab5-emu pause + // menu; the switch to JK_GAMEMODE_UNK is only ever a load): starting the + // next level here would replace it, as jkMain_GuiAdvance takes the state + // this sets over the one it is switching to + if ( a2 == JK_GAMEMODE_UNK ) + return; +#endif if ( a1 == JK_GAMEMODE_VIDEO3 || a1 == JK_GAMEMODE_VIDEO4 ) jkMain_StartNextLevelInEpisode(0, 1); } diff --git a/src/Platform/ESP32/Window_ESP32.c b/src/Platform/ESP32/Window_ESP32.c index 5e7244267..71a3e88fb 100644 --- a/src/Platform/ESP32/Window_ESP32.c +++ b/src/Platform/ESP32/Window_ESP32.c @@ -263,6 +263,12 @@ int Window_MessageLoop() Window_bFlipRequested = 1; // redraw after the pause } if (jk_esp_quit_requested) { + // Cancelling only the active menu is not enough: the main menu (and + // the Force / tally / new player screens) reopen on -1, so a quit at + // JK's main menu never got back to the frame loop and jk::deinit + // waited forever. The flag makes them return (see jkGUIRend.c); it is + // reset with the GUI at shutdown. + jkGuiRend_ESP32_bCloseMenus = 1; jkGuiRend_ESP32_CancelActiveMenu(); return 0; } diff --git a/src/Platform/ESP32/jk_esp_save.c b/src/Platform/ESP32/jk_esp_save.c index 638f7d906..a16e154ba 100644 --- a/src/Platform/ESP32/jk_esp_save.c +++ b/src/Platform/ESP32/jk_esp_save.c @@ -6,6 +6,8 @@ #include "Main/jkMain.h" #include "Main/jkSmack.h" #include "Gui/jkGUITitle.h" +#include "Gui/jkGUIRend.h" +#include "Devices/sithControl.h" #include "World/jkPlayer.h" #include "World/sithWorld.h" #include "Gameplay/sithPlayer.h" @@ -76,9 +78,34 @@ int jk_esp_engine_load(const char* path) for (char16_t* p = header.saveName; *p; p++) { if (*p == u'~') { label = p + 1; break; } } + if (!sithWorld_g_pStaticWorld) { + // Still in the intro: the engine has not shown its title (which + // reads static.jkl: jkMain_UnkShow now does that for a level + // reached without it) nor its Players dialog, which reads the + // player's profile (controls, options). Read the one the dialog + // would offer (the last player, from the registry); without one the + // controls have no bindings, so fall back to the defaults. + char name[32]; + stdString_WcharToChar(name, jkPlayer_playerShortName, 31); + name[31] = 0; + jk_esp_log("load: no title yet, reading player profile '%s'", name); + if (!jkPlayer_ReadConf(jkPlayer_playerShortName)) { + jk_esp_log("load: no player profile, default controls"); + sithControl_DefaultInit(); + } + } jk_esp_log("load: switching to episode '%s' level '%s' for %s", header.episodeName, header.jklName, path); jkSmack_gameMode = 1; // "load a save" flavour of the level switch - return jkMain_sub_4034D0(header.episodeName, (char*)path, header.jklName, label); + int ok = jkMain_sub_4034D0(header.episodeName, (char*)path, header.jklName, label); + // The switch happens once no engine menu is open (jkMain_GuiAdvance); + // at the main menu (or the Players dialog, Single Player, Setup, the Esc + // menu, a tally or Force screen...) close them, as the engine's own Load + // does by returning from them. + if (ok && jkGuiRend_thing_five) { + jk_esp_log("load: closing the engine's menus first"); + jkGuiRend_ESP32_bCloseMenus = 1; + } + return ok; } // Restart the current level. Like the engine's ESC menu "restart": restore diff --git a/src/Platform/ESP32/stdSound.c b/src/Platform/ESP32/stdSound.c index 0efa1c107..3b66fa876 100644 --- a/src/Platform/ESP32/stdSound.c +++ b/src/Platform/ESP32/stdSound.c @@ -283,6 +283,17 @@ void stdSound_ESP32_Pump(void) } } +// The port, once the engine and its audio task have stopped: the mix buffer is +// made once per process; free it so it is not left in the middle of the PSRAM +// heap after the game (the next startup makes a new one) +void stdSound_ESP32_Release(void) +{ + free(stdSound_pMixBuf); + stdSound_pMixBuf = NULL; + stdSound_bInitted = 0; + stdSound_pendingOff = stdSound_pendingLen = 0; +} + // Periodic diagnostics (called from the engine's frame report) void stdSound_ESP32_Report(void) {