Skip to content

Commit

Permalink
Fix memory leak screens not terminating
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Nov 30, 2023
1 parent cb89d8b commit 81b1974
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 64 deletions.
9 changes: 2 additions & 7 deletions src/game_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2014, 2016-2018, 2021-2022 Cong Xu
Copyright (c) 2014, 2016-2018, 2021-2023 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -385,13 +385,12 @@ void LoopRunnerChange(LoopRunner *l, GameLoopData *newData)
void LoopRunnerPush(LoopRunner *l, GameLoopData *newData)
{
CArrayPushBack(&l->Loops, &newData);
newData->IsUsed = true;
}
void LoopRunnerPop(LoopRunner *l)
{
GameLoopData *data = GetCurrentLoop(l);
GameLoopOnExit(data);
data->IsUsed = false;
GameLoopTerminate(data);
CArrayDelete(&l->Loops, l->Loops.size - 1);
}

Expand Down Expand Up @@ -420,10 +419,6 @@ static void GameLoopOnExit(GameLoopData *data)
{
data->OnExit(data);
}
if (!data->IsUsed)
{
GameLoopTerminate(data);
}
data->HasExited = true;
data->HasEntered = false;
}
Expand Down
3 changes: 1 addition & 2 deletions src/game_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2014, 2017-2018, 2022 Cong Xu
Copyright (c) 2014, 2017-2018, 2022-2023 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -63,7 +63,6 @@ typedef struct sGameLoopData
bool HasEntered;
bool HasExited;
bool HasDrawnFirst;
bool IsUsed;
bool DrawParent;
} GameLoopData;

Expand Down
13 changes: 7 additions & 6 deletions src/hiscores.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,18 @@ static void HighScoresScreenTerminate(GameLoopData *data)
}
static GameLoopResult HighScoresScreenUpdate(GameLoopData *data, LoopRunner *l)
{
HighScoresScreenData *hData = data->Data;
// Make copy before popping loop
HighScoresScreenData hData = *(HighScoresScreenData *)data->Data;
LoopRunnerPop(l);
if (!IsPVP(hData->co->Entry.Mode) &&
if (!IsPVP(hData.co->Entry.Mode) &&
GetNumPlayers(PLAYER_ANY, false, true) > 0)
{
LoadHighScores();
bool allTime = false;
bool todays = false;
CA_FOREACH(PlayerData, p, gPlayerDatas)
const bool isPlayerComplete =
(!hData->co->IsQuit && !p->survived) || hData->co->IsComplete;
(!hData.co->IsQuit && !p->survived) || hData.co->IsComplete;
if (isPlayerComplete && p->IsLocal && IsPlayerHuman(p))
{
EnterHighScore(p);
Expand All @@ -121,18 +122,18 @@ static GameLoopResult HighScoresScreenUpdate(GameLoopData *data, LoopRunner *l)
{
p->missions++;
}
p->lastMission = hData->co->MissionIndex;
p->lastMission = hData.co->MissionIndex;
CA_FOREACH_END()
SaveHighScores();

// Show high scores screen if high enough
if (todays)
{
LoopRunnerPush(l, DisplayTodaysHighScores(hData->g));
LoopRunnerPush(l, DisplayTodaysHighScores(hData.g));
}
if (allTime)
{
LoopRunnerPush(l, DisplayAllTimeHighScores(hData->g));
LoopRunnerPush(l, DisplayAllTimeHighScores(hData.g));
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/loading_screens.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,21 @@ static GameLoopResult LoopUpdate(GameLoopData *data, LoopRunner *l)
sData->count++;
if (complete)
{
// Remove current loop, but copy its data before destroying it
ScreenLoadingData sDataCopy = *sData;
sData = NULL;
LoopRunnerPop(l);
if (sData->ascending)
if (sDataCopy.ascending)
{
if (sData->removeParent)
if (sDataCopy.removeParent)
{
LoopRunnerPop(l);
}
if (sData->nextLoop)
if (sDataCopy.nextLoop)
{
LoopRunnerPush(l, sData->nextLoop);
LoopRunnerPush(l, sDataCopy.nextLoop);
// Show a loading screen with tiles animating out
LoopRunnerPush(l, ScreenLoading(sData->loadingText, false, NULL, false));
LoopRunnerPush(l, ScreenLoading(sDataCopy.loadingText, false, NULL, false));
}
}
return UPDATE_RESULT_OK;
Expand Down
84 changes: 40 additions & 44 deletions src/prep.c
Original file line number Diff line number Diff line change
Expand Up @@ -684,57 +684,55 @@ static GameLoopResult GameOptionsUpdate(GameLoopData *data, LoopRunner *l)
MissionOptionsTerminate(&gMission);
CampaignUnload(&gCampaign);
LoopRunnerPop(l);
return UPDATE_RESULT_OK;
}
if (!ConfigApply(&gConfig, NULL))
{
LOG(LM_MAIN, LL_ERROR,
"Failed to apply config; reset to last used");
ConfigResetChanged(&gConfig);
}
else
{
if (!ConfigApply(&gConfig, NULL))
{
LOG(LM_MAIN, LL_ERROR,
"Failed to apply config; reset to last used");
ConfigResetChanged(&gConfig);
}
else
// Save options for later
ConfigSave(&gConfig, GetConfigFilePath(CONFIG_FILE));
}

// Set allowed weapons
// First check if the player has unwittingly disabled all weapons
// if so, enable all weapons
bool allDisabled = true;
for (int i = 0, j = 0; i < (int)gData->allowed.size; i++, j++)
{
const bool *allowed = CArrayGet(&gData->allowed, i);
if (*allowed)
{
// Save options for later
ConfigSave(&gConfig, GetConfigFilePath(CONFIG_FILE));
allDisabled = false;
break;
}

// Set allowed weapons
// First check if the player has unwittingly disabled all weapons
// if so, enable all weapons
bool allDisabled = true;
}
if (!allDisabled)
{
for (int i = 0, j = 0; i < (int)gData->allowed.size; i++, j++)
{
const bool *allowed = CArrayGet(&gData->allowed, i);
if (*allowed)
if (!*allowed)
{
allDisabled = false;
break;
}
}
if (!allDisabled)
{
for (int i = 0, j = 0; i < (int)gData->allowed.size; i++, j++)
{
const bool *allowed = CArrayGet(&gData->allowed, i);
if (!*allowed)
{
CArrayDelete(&gMission.Weapons, j);
j--;
}
CArrayDelete(&gMission.Weapons, j);
j--;
}
}
}

gCampaign.OptionsSet = true;
gCampaign.OptionsSet = true;

// If enabled, start net server
if (!gCampaign.IsClient && ConfigGetBool(&gConfig, "StartServer"))
{
NetServerOpen(&gNetServer);
}
LoopRunnerPush(
l, ScreenMissionBriefing(&gCampaign.Setting, &gMission));
// If enabled, start net server
if (!gCampaign.IsClient && ConfigGetBool(&gConfig, "StartServer"))
{
NetServerOpen(&gNetServer);
}
LoopRunnerPush(
l, ScreenMissionBriefing(&gCampaign.Setting, &gMission));
return UPDATE_RESULT_OK;
}
return UPDATE_RESULT_DRAW;
Expand Down Expand Up @@ -795,13 +793,11 @@ static GameLoopResult CheckGameStart(void *data, LoopRunner *l)
if (!gCampaign.IsLoaded)
{
LoopRunnerPop(l);
return UPDATE_RESULT_OK;
}
else
{
LoopRunnerPush(
l, ScreenLoading(
"Starting game...", true,
RunGame(&gCampaign, &gMission, &gMap), true));
}
LoopRunnerPush(
l, ScreenLoading(
"Starting game...", true,
RunGame(&gCampaign, &gMission, &gMap), true));
return UPDATE_RESULT_OK;
}

0 comments on commit 81b1974

Please sign in to comment.