From e72610a3dc0e7e1d18deb6375daa8bd5ef1e420f Mon Sep 17 00:00:00 2001
From: mazmazz <mar.marcoz@outlook.com>
Date: Fri, 14 Sep 2018 12:47:33 -0400
Subject: [PATCH] Separate StopMusic and UnloadMusic

---
 src/s_sound.c         | 12 ++++-------
 src/sdl/mixer_sound.c | 50 ++++++++++++++++++-------------------------
 2 files changed, 25 insertions(+), 37 deletions(-)

diff --git a/src/s_sound.c b/src/s_sound.c
index 13b8beac10..2b8e8e7215 100644
--- a/src/s_sound.c
+++ b/src/s_sound.c
@@ -1344,11 +1344,13 @@ static boolean S_LoadMusic(const char *mname)
 static void S_UnloadMusic(void)
 {
 	I_UnloadSong();
-	music_name[0] = 0;
+
 #ifndef HAVE_SDL //SDL uses RWOPS
 	Z_ChangeTag(music_data, PU_CACHE);
 #endif
 	music_data = NULL;
+
+	music_name[0] = 0;
 	music_flags = 0;
 	music_looping = false;
 }
@@ -1413,13 +1415,7 @@ void S_StopMusic(void)
 
 	S_SpeedMusic(1.0f);
 	I_StopSong();
-	I_UnloadSong();
-
-#ifndef HAVE_SDL //SDL uses RWOPS
-	Z_ChangeTag(music_data, PU_CACHE);
-#endif
-
-	music_name[0] = 0;
+	S_UnloadMusic(); // for now, stopping also means you unload the song
 }
 
 //
diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c
index dd00d78f73..99a379ea2f 100644
--- a/src/sdl/mixer_sound.c
+++ b/src/sdl/mixer_sound.c
@@ -484,19 +484,7 @@ FUNCMATH void I_InitMusic(void)
 
 void I_ShutdownMusic(void)
 {
-#ifdef HAVE_LIBGME
-	if (gme)
-	{
-		Mix_HookMusic(NULL, NULL);
-		gme_delete(gme);
-		gme = NULL;
-	}
-#endif
-	if (!music)
-		return;
-	Mix_HookMusicFinished(NULL);
-	Mix_FreeMusic(music);
-	music = NULL;
+	I_UnloadSong();
 }
 
 /// ------------------------
@@ -565,10 +553,8 @@ boolean I_SetSongSpeed(float speed)
 
 boolean I_LoadSong(char *data, size_t len)
 {
-	I_Assert(!music);
-#ifdef HAVE_LIBGME
-	I_Assert(!gme);
-#endif
+	if (music || gme)
+		I_UnloadSong();
 
 #ifdef HAVE_LIBGME
 	if ((UINT8)data[0] == 0x1F
@@ -710,11 +696,20 @@ boolean I_LoadSong(char *data, size_t len)
 
 void I_UnloadSong(void)
 {
-	// \todo unhook looper
-	//var_cleanup();
-	//Mix_FreeMusic(music);
-	//music = NULL;
 	I_StopSong();
+
+#ifdef HAVE_LIBGME
+	if (gme)
+	{
+		gme_delete(gme);
+		gme = NULL;
+	}
+#endif
+	if (music)
+	{
+		Mix_FreeMusic(music);
+		music = NULL;
+	}
 }
 
 boolean I_PlaySong(boolean looping)
@@ -750,17 +745,14 @@ void I_StopSong(void)
 	if (gme)
 	{
 		Mix_HookMusic(NULL, NULL);
-		gme_delete(gme);
-		gme = NULL;
 		current_track = -1;
-		return;
 	}
 #endif
-	if (!music)
-		return;
-	Mix_HookMusicFinished(NULL);
-	Mix_FreeMusic(music);
-	music = NULL;
+	if (music)
+	{
+		Mix_HookMusicFinished(NULL);
+		Mix_HaltMusic();
+	}
 }
 
 void I_PauseSong(void)
-- 
GitLab