diff --git a/game.c b/game.c index 619c38e..23d173d 100644 --- a/game.c +++ b/game.c @@ -21,6 +21,47 @@ #include "main.h" +#ifdef __EMSCRIPTEN__ +#include + +// +// wasmga.me: the play page keeps one snapshot of the game to resume from, as +// a saved game in a slot the menus don't show. Its "saved times" field holds +// the slot the player last used, which the menus and game over go back to. +// +#define WG_SNAPSHOT_SLOT 99 +static BOOL wg_snapshot_wanted, wg_resume_wanted; +static int wg_snapshots; + +// +// Asks for a snapshot, written at the start of the next frame the party is +// walking around (not in a battle, dialogue or menu). wg_snapshot_count() +// goes up once it is written. +// +EMSCRIPTEN_KEEPALIVE void wg_request_snapshot(void) { wg_snapshot_wanted = TRUE; } +EMSCRIPTEN_KEEPALIVE int wg_snapshot_count(void) { return wg_snapshots; } + +// +// Called before the game starts: skips the opening screens and menu and loads +// the snapshot. +// +EMSCRIPTEN_KEEPALIVE void wg_resume_at_start(void) { wg_resume_wanted = TRUE; } +BOOL wg_resuming(void) { return wg_resume_wanted; } + +static BYTE wg_last_slot(void) +{ + FILE *fp = UTIL_OpenFileAtPath(gConfig.pszSavePath, PAL_va(0, "%d.rpg", WG_SNAPSHOT_SLOT)); + WORD w = 0; + if (fp != NULL) + { + if (fread(&w, sizeof(WORD), 1, fp) != 1) w = 0; + fclose(fp); + } + w = SDL_SwapLE16(w); + return (BYTE)(w <= 5 ? w : 0); +} +#endif + VOID PAL_GameMain( VOID @@ -45,7 +86,11 @@ PAL_GameMain( // // Show the opening menu. // +#ifdef __EMSCRIPTEN__ + gpGlobals->bCurrentSaveSlot = wg_resume_wanted ? WG_SNAPSHOT_SLOT : (BYTE)PAL_OpeningMenu(); +#else gpGlobals->bCurrentSaveSlot = (BYTE)PAL_OpeningMenu(); +#endif gpGlobals->fInMainGame = TRUE; // @@ -64,6 +109,19 @@ PAL_GameMain( // PAL_LoadResources(); +#ifdef __EMSCRIPTEN__ + if (gpGlobals->bCurrentSaveSlot == WG_SNAPSHOT_SLOT) + { + gpGlobals->bCurrentSaveSlot = wg_last_slot(); + } + if (wg_snapshot_wanted && !gpGlobals->fEnteringScene) + { + wg_snapshot_wanted = FALSE; + PAL_SaveGame(WG_SNAPSHOT_SLOT, gpGlobals->bCurrentSaveSlot); + wg_snapshots++; + } +#endif + // // Clear the input state of previous frame. // diff --git a/main.c b/main.c index c442e8b..2673dc8 100644 --- a/main.c +++ b/main.c @@ -542,8 +542,15 @@ main( // // Show the trademark screen and splash screen // - PAL_TrademarkScreen(); - PAL_SplashScreen(); +#ifdef __EMSCRIPTEN__ + // wasmga.me: resuming from a snapshot goes straight back into the game (game.c) + extern BOOL wg_resuming(void); + if (!wg_resuming()) +#endif + { + PAL_TrademarkScreen(); + PAL_SplashScreen(); + } // // Run the main game routine