From 098d1afb50f5adf8db561b363a9b641cc40e8b71 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 17 Sep 2025 01:04:23 +0200 Subject: [PATCH] add P_FreeLevelState + extra stuff for D_ClearState this just takes out the cleanup code from P_SetupLevel so it can be called from within D_ClearState and all the mapdata will be freed when going back to the main menu Not super crazy and really necessary in kart, but why not tbh this is mostly based on what RR does --- src/d_main.c | 21 +++++++++-- src/g_demo.c | 10 ++++- src/g_demo.h | 1 + src/hardware/hw_cache.c | 3 +- src/p_setup.c | 81 +++++++++++++++++++++++++---------------- src/p_setup.h | 1 + 6 files changed, 79 insertions(+), 38 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index a6a38d413..691b431a9 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -982,6 +982,7 @@ void D_ClearState(void) // (otherwise the game still thinks we're playing!) SV_StopServer(); SV_ResetServer(); + serverlistultimatecount = 0; for (i = 0; i < MAXPLAYERS; i++) CL_ClearPlayer(i); @@ -997,8 +998,11 @@ void D_ClearState(void) // reset modeattacking modeattacking = ATTACKING_NONE; - // empty maptol so mario/etc sounds don't play in sound test when they shouldn't + // empty some other semi-important state maptol = 0; + nextmapoverride = 0; + skipstats = 0; + gamemap = 1; gameaction = ga_nothing; memset(displayplayers, 0, sizeof(displayplayers)); @@ -1007,16 +1011,26 @@ void D_ClearState(void) gametype = GT_RACE; // SRB2kart paused = false; - netgame = false; // title menu shouldnt be a netgame lmao - // clear cmd building stuff memset(gamekeydown, 0, sizeof(gamekeydown)); memset(joyxmove, 0, sizeof(joyxmove)); memset(joyymove, 0, sizeof(joyymove)); mousex = mousey = 0; + + // Reset the palette + if (rendermode != render_none) + V_SetPaletteLump("PLAYPAL"); + G_ResetAllDeviceRumbles(); + S_StopSounds(); S_ResetKeepAndSpecialMus(); // just in case + + P_FreeLevelState(); + + netgame = false; // title menu shouldnt be a netgame lmao + G_SetGamestate(GS_NULL); + wipegamestate = GS_NULL; } // @@ -1024,6 +1038,7 @@ void D_ClearState(void) // void D_StartTitle(void) { + demo.title = false; D_ClearState(); M_ClearMenus(true); F_StartTitleScreen(); diff --git a/src/g_demo.c b/src/g_demo.c index f7e4e2c93..c298c5756 100644 --- a/src/g_demo.c +++ b/src/g_demo.c @@ -3864,8 +3864,8 @@ static void G_StopTimingDemo(void) D_StartTitle(); } - -boolean G_CheckDemoStatus(void) +// Clean up all ghosts +void G_FreeGhosts(void) { while (ghosts) { @@ -3874,6 +3874,12 @@ boolean G_CheckDemoStatus(void) ghosts = next; } ghosts = NULL; +} + + +boolean G_CheckDemoStatus(void) +{ + G_FreeGhosts(); // DO NOT end metal sonic demos here diff --git a/src/g_demo.h b/src/g_demo.h index de1495e91..905094579 100644 --- a/src/g_demo.h +++ b/src/g_demo.h @@ -174,6 +174,7 @@ void G_ResetDemoRecording(void); void G_DoPlayDemo(char *defdemoname); void G_TimeDemo(const char *name); void G_AddGhost(char *defdemoname); +void G_FreeGhosts(void); void G_UpdateStaffGhostName(lumpnum_t l); void G_DoPlayMetal(void); void G_DoneLevelLoad(void); diff --git a/src/hardware/hw_cache.c b/src/hardware/hw_cache.c index 467a801e1..d44ac4f82 100644 --- a/src/hardware/hw_cache.c +++ b/src/hardware/hw_cache.c @@ -1391,8 +1391,7 @@ UINT32 HWR_GetLightTableID(extracolormap_t *colormap) // call become invalid and must not be used. void HWR_ClearLightTables(void) { - if (vid.glstate == VID_GL_LIBRARY_LOADED) - GL_ClearLightTables(); + GL_ClearLightTables(); } #endif //HWRENDER diff --git a/src/p_setup.c b/src/p_setup.c index becc760aa..cb7d100d0 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -2686,6 +2686,55 @@ static void P_SetupPlayer(void) } } +void P_FreeLevelState(void) +{ + sector_t *ss; + + if (numsectors) + { + LUA_InvalidateLevel(); + + for (ss = sectors; sectors+numsectors != ss; ss++) + { + Z_Free(ss->attached); + Z_Free(ss->attachedsolid); + } + + // This is the simplest guard against double frees. + // No valid map has zero sectors. Or, come to think + // of it, less than two in general! ~toast 310525 + numsectors = 0; + } + + // Clear pointers that would be left dangling by the purge + R_FlushTranslationColormapCache(); + +#ifdef HWRENDER + // Free GPU textures before freeing patches. + if (vid.glstate == VID_GL_LIBRARY_LOADED) + { + if (rendermode == render_opengl) + HWR_ClearAllTextures(); + + // Delete light table textures + HWR_ClearLightTables(); + } +#endif + + G_FreeGhosts(); // ghosts are allocated with PU_LEVEL + + Patch_FreeTag(PU_PATCH_LOWPRIORITY); + //Patch_FreeTag(PU_PATCH_ROTATED); // we keep those ty! + Z_FreeTags(PU_LEVEL, PU_PURGELEVEL - 1); + + Y_VoteClear(); + +#if defined (WALLSPLATS) || defined (FLOORSPLATS) + // clear the splats from previous level + R_ClearLevelSplats(); +#endif +} + /** Loads a level from a lump or external wad. * * \param fromnetsave If true, skip some stuff because we're loading a netgame snapshot. @@ -2698,7 +2747,6 @@ boolean P_SetupLevel(boolean fromnetsave, boolean reloadinggamestate) // Map header should always be in place at this point INT32 i; boolean ranspecialwipe = false; - sector_t *ss; lumpnum_t encoreLump = LUMPERROR; UINT8 levelfadecol; @@ -2804,36 +2852,7 @@ boolean P_SetupLevel(boolean fromnetsave, boolean reloadinggamestate) I_UpdateNoVsync(); }*/ - LUA_InvalidateLevel(); - - for (ss = sectors; sectors+numsectors != ss; ss++) - { - Z_Free(ss->attached); - Z_Free(ss->attachedsolid); - } - - // Clear pointers that would be left dangling by the purge - R_FlushTranslationColormapCache(); - -#ifdef HWRENDER - // Free GPU textures before freeing patches. - if (rendermode == render_opengl && (vid.glstate == VID_GL_LIBRARY_LOADED)) - HWR_ClearAllTextures(); - - // Delete light table textures - HWR_ClearLightTables(); -#endif - - Patch_FreeTag(PU_PATCH_LOWPRIORITY); - //Patch_FreeTag(PU_PATCH_ROTATED); // we keep those ty! - Z_FreeTags(PU_LEVEL, PU_PURGELEVEL - 1); - - Y_VoteClear(); - -#if defined (WALLSPLATS) || defined (FLOORSPLATS) - // clear the splats from previous level - R_ClearLevelSplats(); -#endif + P_FreeLevelState(); R_InitializeLevelInterpolators(); diff --git a/src/p_setup.h b/src/p_setup.h index dc656cf4a..02f3238c6 100644 --- a/src/p_setup.h +++ b/src/p_setup.h @@ -67,6 +67,7 @@ extern line_t *spawnlines; extern side_t *spawnsides; void P_SetupLevelSky(INT32 skynum, boolean global); +void P_FreeLevelState(void); boolean P_SetupLevel(boolean fromnetsave, boolean reloadinggamestate); #ifdef HWRENDER