Skip to content

Commit

Permalink
bugfix: reimplement game reloading by delaying one tick, avoid script…
Browse files Browse the repository at this point in the history
… result affect reloaded content

refix sdlpal#165
Thanks @PalAlexx report and test
  • Loading branch information
palxex committed Jan 27, 2021
1 parent 5384e71 commit dbd2a4a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 57 deletions.
44 changes: 1 addition & 43 deletions game.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,6 @@

#include "main.h"

static VOID
PAL_GameStart(
VOID
)
/*++
Purpose:
Do some initialization work when game starts (new game or load game).
Parameters:
None.
Return value:
None.
--*/
{
PAL_SetLoadFlags(kLoadScene | kLoadPlayerSprite);

if (!gpGlobals->fEnteringScene)
{
//
// Fade in music if the player has loaded an old game.
//
AUDIO_PlayMusic(gpGlobals->wNumMusic, TRUE, 1);
}

gpGlobals->fNeedToFadeIn = TRUE;
gpGlobals->dwFrameNum = 0;
}

VOID
PAL_GameMain(
VOID
Expand Down Expand Up @@ -85,23 +52,14 @@ PAL_GameMain(
//
// Initialize game data and set the flags to load the game resources.
//
PAL_InitGameData(gpGlobals->bCurrentSaveSlot);
PAL_ReloadInNextTick(gpGlobals->bCurrentSaveSlot);

//
// Run the main game loop.
//
dwTime = SDL_GetTicks();
while (TRUE)
{
//
// Do some initialization at game start.
//
if (gpGlobals->fGameStart)
{
PAL_GameStart();
gpGlobals->fGameStart = FALSE;
}

//
// Load the game resources if needed.
//
Expand Down
28 changes: 26 additions & 2 deletions global.c
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,32 @@ PAL_SaveGame(
PAL_SaveGame_DOS(iSaveSlot, wSavedTimes);
}

VOID
PAL_ReloadInNextTick(
INT iSaveSlot
)
/*++
Purpose:
Reload the game IN NEXT TICK, avoid reentrant problems.
Parameters:
[IN] iSaveSlot - Slot of saved game.
Return value:
None.
--*/
{
gpGlobals->bCurrentSaveSlot = (BYTE)iSaveSlot;
PAL_SetLoadFlags(kLoadGlobalData | kLoadScene | kLoadPlayerSprite);
gpGlobals->fEnteringScene = TRUE;
gpGlobals->fNeedToFadeIn = TRUE;
gpGlobals->dwFrameNum = 0;
}

VOID
PAL_InitGameData(
INT iSaveSlot
Expand Down Expand Up @@ -919,8 +945,6 @@ PAL_InitGameData(
PAL_LoadDefaultGame();
}

gpGlobals->fGameStart = TRUE;
gpGlobals->fNeedToFadeIn = FALSE;
gpGlobals->iCurInvMenuItem = 0;
gpGlobals->fInBattle = FALSE;

Expand Down
6 changes: 5 additions & 1 deletion global.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,6 @@ typedef struct tagGLOBALVARS
int iCurPlayingRNG; // current playing RNG animation
BYTE bCurrentSaveSlot; // current save slot (1-5)
BOOL fInMainGame; // TRUE if in main game
BOOL fGameStart; // TRUE if the has just started
BOOL fEnteringScene; // TRUE if entering a new scene
BOOL fNeedToFadeIn; // TRUE if need to fade in when drawing scene
BOOL fInBattle; // TRUE if in battle
Expand Down Expand Up @@ -580,6 +579,11 @@ PAL_InitGameData(
INT iSaveSlot
);

VOID
PAL_ReloadInNextTick(
INT iSaveSlot
);

INT
PAL_CountItem(
WORD wObjectID
Expand Down
10 changes: 4 additions & 6 deletions play.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ PAL_GameUpdate(
gpGlobals->fEnteringScene = FALSE;

i = gpGlobals->wNumScene - 1;
wResult = PAL_RunTriggerScript(gpGlobals->g.rgScene[i].wScriptOnEnter, 0xFFFF);
if (!gpGlobals->fGameStart)
gpGlobals->g.rgScene[i].wScriptOnEnter = wResult;
gpGlobals->g.rgScene[i].wScriptOnEnter = PAL_RunTriggerScript(gpGlobals->g.rgScene[i].wScriptOnEnter, 0xFFFF);

if (gpGlobals->fEnteringScene || gpGlobals->fGameStart)
if (gpGlobals->fEnteringScene)
{
//
// Don't go further as we're switching to another scene
Expand Down Expand Up @@ -166,7 +164,7 @@ PAL_GameUpdate(

PAL_ClearKeyState();

if (gpGlobals->fEnteringScene || gpGlobals->fGameStart)
if (gpGlobals->fEnteringScene)
{
//
// Don't go further on scene switching
Expand All @@ -193,7 +191,7 @@ PAL_GameUpdate(
if (wScriptEntry != 0)
{
p->wAutoScript = PAL_RunAutoScript(wScriptEntry, wEventObjectID);
if (gpGlobals->fEnteringScene || gpGlobals->fGameStart)
if (gpGlobals->fEnteringScene)
{
//
// Don't go further on scene switching
Expand Down
9 changes: 9 additions & 0 deletions res.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ PAL_LoadResources(
return;
}

//
// Load global data
//
if (gpResources->bLoadFlags & kLoadGlobalData)
{
PAL_InitGameData(gpGlobals->bCurrentSaveSlot);
AUDIO_PlayMusic(gpGlobals->wNumMusic, TRUE, 1);
}

//
// Load scene
//
Expand Down
5 changes: 3 additions & 2 deletions res.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@

typedef enum tagLOADRESFLAG
{
kLoadScene = (1 << 0), // load a scene
kLoadPlayerSprite = (1 << 1), // load player sprites
kLoadGlobalData = (1 << 0), // load global data
kLoadScene = (1 << 1), // load a scene
kLoadPlayerSprite = (1 << 2), // load player sprites
} LOADRESFLAG, *LPLOADRESFLAG;

PAL_C_LINKAGE_BEGIN
Expand Down
4 changes: 2 additions & 2 deletions script.c
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,7 @@ PAL_InterpretInstruction(
// Load the last saved game
//
PAL_FadeOut(1);
PAL_InitGameData(gpGlobals->bCurrentSaveSlot);
PAL_ReloadInNextTick(gpGlobals->bCurrentSaveSlot);
return 0; // don't go further

case 0x004F:
Expand Down Expand Up @@ -3268,7 +3268,7 @@ PAL_RunTriggerScript(
}
else
{
wScriptEntry++;
wScriptEntry++;
}
break;

Expand Down
2 changes: 1 addition & 1 deletion uigame.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ PAL_SystemMenu(
{
AUDIO_PlayMusic(0, FALSE, 1);
PAL_FadeOut(1);
PAL_InitGameData(iSlot);
PAL_ReloadInNextTick(iSlot);
}
break;

Expand Down

0 comments on commit dbd2a4a

Please sign in to comment.