From 0ac4f63c89227daf2cc29b5298d9fcfa5cda3d73 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 20 Nov 2026 21:02:54 -0600 Subject: [PATCH 1/3] ESP32: a save loaded before the title screen opens static.jkl first; pause-menu load closes the engine menus A save loaded from the tab5-emu pause menu while the intro was still playing crashed in sithKeyFrame_GetByIdx (load fault at +0x20, lw a3,364(a4) with a4 = sithWorld_g_pStaticWorld = NULL; core dump: a4=0, a3=0xffff7fff, a5=0x22, i.e. static keyframe 0x8022 on the first track of the first DSS_PUPPETSTATUS). The static world (static.jkl) and the inventory bins (items.dat) are loaded by jkMain_TitleShow, which runs after the intro and before any menu can start a level; the pause menu's load switches VIDEO -> UNK -> GAMEPLAY directly, so the level and the save's messages were processed without them. Loading in a session that already had a level (or that had shown the title) never hit this. - jkMain_GameplayShow: if no static world is open yet, open static.jkl and load items.dat, as jkMain_TitleShow and Main_StartupDedicated's autostart do. A no-op on every path that went through the title. - jk_esp_engine_load: before the title the Players dialog has not read a profile either (controls have no bindings): read the remembered player's profile, or fall back to the default controls. - jk_esp_engine_load at an engine menu (main menu, Players dialog, Single Player, Setup, the in-game Esc menu): the level switch only happens once no modal menu is open, and on RETRO targets a menu does not close by itself for jkGuiRend_thing_four, so the load waited for the user to leave the menus (and New Game replaced it). jkGuiRend_ESP32_bCloseMenus makes every menu return -1 and jkGuiMain_Show leave its loop until jkMain_GuiAdvance performs the switch. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- src/Gui/jkGUIMain.c | 6 ++++++ src/Gui/jkGUIRend.c | 14 ++++++++++++++ src/Gui/jkGUIRend.h | 5 +++++ src/Main/jkMain.c | 18 ++++++++++++++++++ src/Platform/ESP32/jk_esp_save.c | 28 +++++++++++++++++++++++++++- 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/Gui/jkGUIMain.c b/src/Gui/jkGUIMain.c index 8523f8752..173547fa8 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); diff --git a/src/Gui/jkGUIRend.c b/src/Gui/jkGUIRend.c index 4e4df11ed..587b7539f 100644 --- a/src/Gui/jkGUIRend.c +++ b/src/Gui/jkGUIRend.c @@ -36,6 +36,16 @@ 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) and jkGuiMain_Show leaves its loop; cleared by +// jkMain_GuiAdvance when the switch happens. +int jkGuiRend_ESP32_bCloseMenus = 0; #endif static tVBuffer* jkGuiRend_menuBuffer = NULL; static tVBuffer *jkGuiRend_texture_dword_8561E8 = NULL; @@ -343,6 +353,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, diff --git a/src/Gui/jkGUIRend.h b/src/Gui/jkGUIRend.h index aa563c716..9fe360d6b 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 (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/Main/jkMain.c b/src/Main/jkMain.c index 38a278179..543060ffd 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 ) @@ -556,6 +559,21 @@ void jkMain_GameplayShow(int a1, int a2) jkGuiTitle_ShowLoading(jkMain_aLevelJklFname, 0); } + // 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. + if ( !sithWorld_g_pStaticWorld ) + { + stdPlatform_Printf("OpenJKDF2: %s: no title screen yet, opening static.jkl\n", __func__); + sithOpenStatic("static.jkl"); + jkHudInv_InitItems(); + } + // MOTS added: // jkEpisode_Shutdown v3 = 0; // Added diff --git a/src/Platform/ESP32/jk_esp_save.c b/src/Platform/ESP32/jk_esp_save.c index 638f7d906..3a212c269 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,33 @@ 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_GameplayShow 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...) + // 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 From 6aa7123728a5ffee2c1f6535f75d7822c17a2063 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 20 Nov 2026 21:30:55 -0600 Subject: [PATCH 2/3] ESP32: closing the menus for a load no longer hangs the Force / tally / new player screens; static.jkl before the episode GOB; quit at the main menu returns Review follow-ups to the fresh-load fix (0ac4f63c): - jkGuiRend_ESP32_bCloseMenus made every menu return -1, but the Force and single-player tally screens answer -1 with an "abort?" Yes/No (which then also returned -1, i.e. No) and reopened, and the new player dialog reopens on -1 while no profile exists: a load at Esc > Jedi Powers, the end-of-level tally or star screen, the dark/light choice or a first-run Players dialog spun forever and Quit could not end it. Those screens now return while the flag is set (the new player dialog with the default controls, since no profile was read). - jkMain_EndLevelScreenShow, jkMain_ChoiceShow and the cutscenes menu fall back to the main menu on -1, which replaced the pending load (the cart reported success): skipped while the flag is set. - jkMain_VideoLeave starts the next level when an inter-level cutscene (VIDEO3 / VIDEO4) ends, and jkMain_GuiAdvance takes that state over the one it is switching to: a load during one was replaced by the next level. Skipped when switching to JK_GAMEMODE_UNK (only ever a load). - static.jkl and items.dat for a level reached without the title are now opened in jkMain_UnkShow, with the title's loading screen, before jkMain_UnkTick mounts the save's episode GOB: jkRes searches the episode directory first, so in jkMain_GameplayShow the session's static world could pick up files from that episode. GameplayShow keeps the guard as a fallback. - The flag is reset in jkGuiRend_Shutdown, so it cannot reach the next launch. - A quit from outside the engine (Window_MessageLoop) also sets it: only cancelling the active menu left jkGuiMain_Show reopening the main menu, so a quit at JK's main menu never returned and jk::deinit waited forever. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- src/Gui/jkGUIForce.c | 7 +++ src/Gui/jkGUIMain.c | 5 +++ src/Gui/jkGUIPlayer.c | 16 +++++++ src/Gui/jkGUIRend.c | 13 +++++- src/Gui/jkGUIRend.h | 2 +- src/Gui/jkGUISingleTally.c | 7 +++ src/Main/jkMain.c | 73 +++++++++++++++++++++++++------ src/Platform/ESP32/Window_ESP32.c | 6 +++ src/Platform/ESP32/jk_esp_save.c | 7 +-- 9 files changed, 116 insertions(+), 20 deletions(-) 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 173547fa8..3a6d04a27 100644 --- a/src/Gui/jkGUIMain.c +++ b/src/Gui/jkGUIMain.c @@ -284,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 587b7539f..712ec6e0d 100644 --- a/src/Gui/jkGUIRend.c +++ b/src/Gui/jkGUIRend.c @@ -43,8 +43,12 @@ void jkGuiRend_ESP32_CancelActiveMenu(void) // 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) and jkGuiMain_Show leaves its loop; cleared by -// jkMain_GuiAdvance when the switch happens. +// 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; @@ -558,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 9fe360d6b..2b9f3513d 100644 --- a/src/Gui/jkGUIRend.h +++ b/src/Gui/jkGUIRend.h @@ -85,7 +85,7 @@ 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 (see jkGUIRend.c) +// 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 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 543060ffd..2a47a005b 100644 --- a/src/Main/jkMain.c +++ b/src/Main/jkMain.c @@ -496,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; @@ -513,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 @@ -559,20 +585,11 @@ void jkMain_GameplayShow(int a1, int a2) jkGuiTitle_ShowLoading(jkMain_aLevelJklFname, 0); } - // 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. - if ( !sithWorld_g_pStaticWorld ) - { - stdPlatform_Printf("OpenJKDF2: %s: no title screen yet, opening static.jkl\n", __func__); - sithOpenStatic("static.jkl"); - jkHudInv_InitItems(); - } +#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 @@ -906,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; @@ -929,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 } @@ -1504,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 3a212c269..a16e154ba 100644 --- a/src/Platform/ESP32/jk_esp_save.c +++ b/src/Platform/ESP32/jk_esp_save.c @@ -80,7 +80,7 @@ int jk_esp_engine_load(const char* path) } if (!sithWorld_g_pStaticWorld) { // Still in the intro: the engine has not shown its title (which - // reads static.jkl: jkMain_GameplayShow now does that for a level + // 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 @@ -98,8 +98,9 @@ int jk_esp_engine_load(const char* path) jkSmack_gameMode = 1; // "load a save" flavour of the level switch 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...) - // close them, as the engine's own Load does by returning from them. + // 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; From 6a715074f21b18e8c954e172f06b5934bd3b0b06 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 20 Nov 2026 22:11:54 -0600 Subject: [PATCH 3/3] ESP32: the COG lexer's buffer and the mixer's buffer can be freed with the engine Both are made once per process: the lexer's 16 KB input buffer by the first COG parse (during the first level load) and the software mixer's chunk buffer by the first stdSound_Startup. In tab5-emu the engine runs in a process that outlives it, so they stayed in the middle of the PSRAM heap after the game: the first Jedi Knight session split the largest free block from 18.4 MB to 9.5-12.5 MB for the rest of the boot. - sithCogParse_ESP32_ReleaseLexer() deletes the lexer's buffer and resets yy_init; yyrestart() makes a new buffer when there is none, as newer flex does (this one passed the NULL buffer on). - stdSound_ESP32_Release() frees the mix buffer and clears stdSound_bInitted so the next startup makes a new one. The port calls both once the engine and its audio task have stopped. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- src/Cog/lex.yy.c | 18 ++++++++++++++++++ src/Platform/ESP32/stdSound.c | 11 +++++++++++ 2 files changed, 29 insertions(+) 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/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) {