- more cleanup on music code

* removed some redundant functionality (e.g. Shift-F5 to change - use the console for that!)
* removed a few more leftover parts of the old music system
* savegames should not do more than resuming the music at the point of saving. (DN3D and RR only so far. Blood to be done.)
* handle music enabling/disabling in the backend, which simply knows better what to do. This was only working in the menu, so changing the CVAR had no effect.
This commit is contained in:
Christoph Oelckers 2019-11-28 03:18:58 +01:00
parent a59917b35d
commit 324056ad88
24 changed files with 93 additions and 165 deletions

View file

@ -1790,13 +1790,6 @@ void UpdateSoundToggle(CGameMenuItemZBool *pItem)
void UpdateMusicToggle(CGameMenuItemZBool *pItem)
{
mus_enabled = pItem->at20;
if (!MusicEnabled())
sndStopSong();
else
{
if (gGameStarted || gDemo.at1)
sndPlaySong("*", gGameOptions.zLevelSong, true);
}
}
void Update3DToggle(CGameMenuItemZBool *pItem)
@ -1847,9 +1840,6 @@ void SetSound(CGameMenuItemChain *pItem)
sndInit();
sfxInit();
if (mus_enabled && (gGameStarted || gDemo.at1))
sndPlaySong("*", gGameOptions.zLevelSong, true);
}
void PreDrawSound(CGameMenuItem *pItem)

View file

@ -100,7 +100,7 @@ extern UserConfig userConfig;
inline bool MusicEnabled()
{
return mus_enabled && !userConfig.nomusic;
return !userConfig.nomusic;
}
inline bool SoundEnabled()

View file

@ -129,7 +129,6 @@ CVARD(Bool, snd_enabled, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG, "enables/disables
CVARD(Bool, snd_tryformats, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG, "enables/disables automatic discovery of replacement sounds and music in .flac and .ogg formats")
CVARD(Bool, snd_doppler, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG, "enable/disable 3d sound")
CVARD(Bool, mus_enabled, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG, "enables/disables music")
CVARD(Bool, mus_restartonload, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG, "restart the music when loading a saved game with the same map or not") // only implemented for Blood - todo: generalize
CVARD(Bool, mus_redbook, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG|CVAR_FRONTEND_BLOOD, "enables/disables redbook audio (Blood only!)") // only Blood has assets for this.

View file

@ -140,6 +140,12 @@ CUSTOM_CVARD(Int, mus_volume, 255, CVAR_ARCHIVE|CVAR_GLOBALCONFIG, "controls mus
}
}
CUSTOM_CVARD(Bool, mus_enabled, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG, "enables/disables music")
{
if (self) S_RestartMusic();
else S_StopMusic(true);
}
//==========================================================================
//
// Callbacks for the music system.

View file

@ -68,6 +68,8 @@
#include "c_dispatch.h"
#include "gamecontrol.h"
#include "filereadermusicinterface.h"
#include "savegamehelp.h"
#include "sjson.h"
MusPlayingInfo mus_playing;
MusicAliasMap MusicAliases;
@ -75,6 +77,7 @@ MidiDeviceMap MidiDevices;
MusicVolumeMap MusicVolumes;
MusicAliasMap LevelMusicAliases;
bool MusicPaused;
static bool mus_blocked;
//==========================================================================
//
//
@ -190,6 +193,7 @@ void S_ResumeMusic ()
void S_UpdateMusic ()
{
mus_blocked = false;
if (mus_playing.handle != nullptr)
{
ZMusic_Update(mus_playing.handle);
@ -360,8 +364,8 @@ bool S_ChangeMusic(const char* musicname, int order, bool looping, bool force)
// shutdown old music
S_StopMusic (true);
// Just record it if volume is 0
if (mus_volume <= 0)
// Just record it if volume is 0 or music was disabled
if (mus_volume <= 0 || !mus_enabled)
{
mus_playing.loop = looping;
mus_playing.name = musicname;
@ -416,12 +420,11 @@ bool S_ChangeMusic(const char* musicname, int order, bool looping, bool force)
//
// S_RestartMusic
//
// Must only be called from snd_reset in i_sound.cpp!
//==========================================================================
void S_RestartMusic ()
{
if (!mus_playing.LastSong.IsEmpty())
if (!mus_playing.LastSong.IsEmpty() && mus_volume > 0 && mus_enabled)
{
FString song = mus_playing.LastSong;
mus_playing.LastSong = "";
@ -557,6 +560,7 @@ CCMD (stopmus)
static FString lastMusicLevel, lastMusic;
int Mus_Play(const char *mapname, const char *fn, bool loop)
{
if (mus_blocked) return 0;
// Store the requested names for resuming.
lastMusicLevel = mapname;
lastMusic = fn;
@ -588,6 +592,7 @@ int Mus_Play(const char *mapname, const char *fn, bool loop)
void Mus_Stop()
{
if (mus_blocked) return;
S_StopMusic(true);
}
@ -597,3 +602,64 @@ void Mus_SetPaused(bool on)
else S_ResumeMusic();
}
void MUS_Save()
{
FString music = mus_playing.name;
if (music.IsEmpty()) music = mus_playing.LastSong;
sjson_context* ctx = sjson_create_context(0, 0, NULL);
if (!ctx)
{
return;
}
sjson_node* root = sjson_mkobject(ctx);
sjson_put_string(ctx, root, "music", music);
sjson_put_int(ctx, root, "baseorder", mus_playing.baseorder);
sjson_put_bool(ctx, root, "loop", mus_playing.loop);
char* encoded = sjson_stringify(ctx, root, " ");
FileWriter* fil = WriteSavegameChunk("music.json");
if (!fil)
{
sjson_destroy_context(ctx);
return;
}
fil->Write(encoded, strlen(encoded));
sjson_free_string(ctx, encoded);
sjson_destroy_context(ctx);
}
bool MUS_Restore()
{
auto fil = ReadSavegameChunk("music.json");
if (!fil.isOpen())
{
return false;
}
auto text = fil.ReadPadded(1);
fil.Close();
if (text.Size() == 0)
{
return false;
}
sjson_context* ctx = sjson_create_context(0, 0, NULL);
sjson_node* root = sjson_decode(ctx, (const char*)text.Data());
mus_playing.LastSong = sjson_get_string(root, "music", "");
mus_playing.baseorder = sjson_get_int(root, "baseorder", 0);
mus_playing.loop = sjson_get_bool(root, "loop", true);
sjson_destroy_context(ctx);
mus_blocked = true; // this is to prevent scripts from resetting the music after it has been loaded from the savegame.
return true;
}
void MUS_ResumeSaved()
{
S_RestartMusic();
}

View file

@ -97,7 +97,11 @@ extern MusPlayingInfo mus_playing;
extern float relative_volume, saved_relative_volume;
void MUS_Save();
bool MUS_Restore();
// Note for later when the OPL player is ported.
// DN3D and related games use "d3dtimbr.tmb"
#endif

View file

@ -6,3 +6,4 @@ void Mus_Init();
int Mus_Play(const char *mapname, const char *fn, bool loop);
void Mus_Stop();
void Mus_SetPaused(bool on);
void MUS_ResumeSaved();

View file

@ -43,6 +43,7 @@
#include "filesystem/filesystem.h"
#include "statistics.h"
#include "secrets.h"
#include "s_music.h"
static CompositeSavegameWriter savewriter;
static FResourceFile *savereader;
@ -74,8 +75,10 @@ bool OpenSaveGameForRead(const char *name)
if (savereader != nullptr)
{
// Load system-side data from savegames.
ReadStatistics();
SECRET_Load();
MUS_Restore();
}
return savereader != nullptr;
@ -142,8 +145,10 @@ void G_WriteSaveHeader(const char *name, const char*mapname, const char *maptitl
sjson_free_string(ctx, encoded);
sjson_destroy_context(ctx);
// Handle system-side modules that need to persist data in savegames here, in a central place.
SaveStatistics();
SECRET_Save();
MUS_Save();
}
//=============================================================================

View file

@ -4411,11 +4411,6 @@ extern int G_StartRTS(int lumpNum, int localPlayer)
return 0;
}
void G_PrintCurrentMusic(void)
{
Bsnprintf(apStrings[QUOTE_MUSIC], MAXQUOTELEN, "Playing %s", g_mapInfo[g_musicIndex].musicfn);
P_DoQuote(QUOTE_MUSIC, g_player[myconnectindex].ps);
}
// Trying to sanitize the mess of options and the mess of variables the mess was stored in. (Did I say this was a total mess before...? >) )
// Hopefully this is more comprehensible, at least it neatly stores everything useful in a single linear value...
@ -4662,25 +4657,6 @@ void G_HandleLocalKeys(void)
{
if (SHIFTS_IS_PRESSED)
{
if (ridiculeNum == 5 && myplayer.fta > 0 && myplayer.ftq == QUOTE_MUSIC)
{
const unsigned int maxi = VOLUMEALL ? MUS_FIRST_SPECIAL : 6;
unsigned int const oldMusicIndex = g_musicIndex;
unsigned int MyMusicIndex = g_musicIndex;
do
{
++MyMusicIndex;
if (MyMusicIndex >= maxi)
MyMusicIndex = 0;
}
while (S_TryPlayLevelMusic(MyMusicIndex) && MyMusicIndex != oldMusicIndex);
G_PrintCurrentMusic();
return;
}
G_AddUserQuote(*CombatMacros[ridiculeNum-1]);
Net_SendTaunt(ridiculeNum);
@ -4712,21 +4688,6 @@ void G_HandleLocalKeys(void)
typebuf[0] = 0;
}
if (inputState.UnboundKeyPressed(sc_F5) && MusicEnabled())
{
map_t *const pMapInfo = &g_mapInfo[g_musicIndex];
char *const musicString = apStrings[QUOTE_MUSIC];
inputState.ClearKeyStatus(sc_F5);
if (pMapInfo->musicfn != NULL)
Bsnprintf(musicString, MAXQUOTELEN, "%s. Use SHIFT-F5 to change.", pMapInfo->musicfn);
else
musicString[0] = '\0';
P_DoQuote(QUOTE_MUSIC, g_player[myconnectindex].ps);
}
if ((buttonMap.ButtonDown(gamefunc_Quick_Save) || g_doQuickSave == 1) && (myplayer.gm & MODE_GAME))
{
buttonMap.ClearButton(gamefunc_Quick_Save);

View file

@ -481,11 +481,6 @@ static inline int G_GetViewscreenSizeShift(uspriteptr_t const spr)
#endif
}
extern void G_PrintCurrentMusic(void);
#ifdef LUNATIC
void El_SetCON(const char *conluacode);
#endif
EXTERN_INLINE_HEADER void G_SetStatusBarScale(int32_t sc);

View file

@ -1531,7 +1531,7 @@ int32_t __fastcall VM_GetUserdef(int32_t labelNum, int const lParm2)
case USERDEFS_MENUTITLE_PAL: labelNum = ud.menutitle_pal; break;
case USERDEFS_SLIDEBAR_PALSELECTED: labelNum = ud.slidebar_palselected; break;
case USERDEFS_SLIDEBAR_PALDISABLED: labelNum = ud.slidebar_paldisabled; break;
case USERDEFS_MUSIC_EPISODE: labelNum = ud.music_episode; break;
case USERDEFS_MUSIC_EPISODE: labelNum = ud.music_episode; break; // Problem: This info is utterly meaningless with the new music system.
case USERDEFS_MUSIC_LEVEL: labelNum = ud.music_level; break;
case USERDEFS_SHADOW_PAL: labelNum = ud.shadow_pal; break;
case USERDEFS_MENU_SCROLLBARTILENUM: labelNum = ud.menu_scrollbartilenum; break;

View file

@ -1765,9 +1765,6 @@ static void Menu_EntryLinkActivate(MenuEntry_t *entry)
FX_StopAllSounds();
S_ClearSoundLocks();
if (MusicEnabled())
S_RestartMusic();
}
else if (entry == &ME_SAVESETUP_CLEANUP)
{
@ -1831,7 +1828,6 @@ static int32_t Menu_EntryOptionModify(MenuEntry_t *entry, int32_t newOption)
S_PauseMusic(true);
else
{
S_RestartMusic();
S_PauseMusic(false);
}
}

View file

@ -332,9 +332,6 @@ static int osdcmd_restartsound(osdcmdptr_t UNUSED(parm))
FX_StopAllSounds();
S_ClearSoundLocks();
if (MusicEnabled())
S_RestartMusic();
return OSDCMD_OK;
}

View file

@ -32,6 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "version.h"
#include "savegamehelp.h"
#include "menu/menu.h"
#include "z_music.h"
BEGIN_DUKE_NS
@ -2110,29 +2111,10 @@ static void postloadplayer(int32_t savegamep)
//2.5
if (savegamep)
{
int32_t musicIdx = (ud.music_episode*MAXLEVELS) + ud.music_level;
Bmemset(gotpic, 0, sizeof(gotpic));
Bmemset(gotpic, 0, sizeof(gotpic));
S_ClearSoundLocks();
G_CacheMapData();
if (boardfilename[0] != 0 && ud.level_number == 7 && ud.volume_number == 0 && ud.music_level == USERMAPMUSICFAKELEVEL && ud.music_episode == USERMAPMUSICFAKEVOLUME)
{
char levname[BMAX_PATH];
G_SetupFilenameBasedMusic(levname, boardfilename);
}
if (g_mapInfo[musicIdx].musicfn != NULL && (musicIdx != g_musicIndex || different_user_map))
{
ud.music_episode = g_musicIndex / MAXLEVELS;
ud.music_level = g_musicIndex % MAXLEVELS;
S_PlayLevelMusicOrNothing(musicIdx);
}
else
S_ContinueLevelMusic();
if (MusicEnabled())
S_PauseMusic(false);
MUS_ResumeSaved();
g_player[myconnectindex].ps->gm = MODE_GAME;
ud.recstat = 0;

View file

@ -119,19 +119,6 @@ void S_PauseSounds(bool paused)
}
}
void S_RestartMusic(void)
{
// Fixme: This should be completely decided by the backend, not here.
if (ud.recstat != 2 && g_player[myconnectindex].ps->gm&MODE_GAME)
{
S_PlayLevelMusicOrNothing(g_musicIndex);
}
else if (G_GetLogoFlags() & LOGO_PLAYMUSIC)
{
S_PlaySpecialMusicOrNothing(MUS_INTRO);
}
}
void S_MenuSound(void)
{
static int SoundNum;

View file

@ -70,7 +70,6 @@ inline void S_ClearSoundLocks(void) {}
int32_t S_LoadSound(uint32_t num);
void cacheAllSounds(void);
void S_MenuSound(void);
void S_RestartMusic(void);
void S_PauseMusic(bool paused);
void S_PauseSounds(bool paused);
bool S_TryPlayLevelMusic(unsigned int m);

View file

@ -5956,12 +5956,6 @@ extern int G_StartRTS(int lumpNum, int localPlayer)
return 0;
}
void G_PrintCurrentMusic(void)
{
Bsnprintf(apStrings[QUOTE_MUSIC], MAXQUOTELEN, "Playing %s", g_mapInfo[g_musicIndex].musicfn);
P_DoQuote(QUOTE_MUSIC, g_player[myconnectindex].ps);
}
// Trying to sanitize the mess of options and the mess of variables the mess was stored in. (Did I say this was a total mess before...? >) )
// Hopefully this is more comprehensible, at least it neatly stores everything useful in a single linear value...
bool GameInterface::validate_hud(int layout)
@ -6206,24 +6200,6 @@ void G_HandleLocalKeys(void)
{
if (SHIFTS_IS_PRESSED)
{
if (ridiculeNum == 5 && g_player[myconnectindex].ps->fta > 0 && g_player[myconnectindex].ps->ftq == QUOTE_MUSIC)
{
const unsigned int maxi = VOLUMEALL ? MUS_FIRST_SPECIAL : 6;
unsigned int MyMusicIndex = g_musicIndex;
do
{
++MyMusicIndex;
if (MyMusicIndex >= maxi)
MyMusicIndex = 0;
}
while (S_TryPlayLevelMusic(MyMusicIndex));
G_PrintCurrentMusic();
return;
}
G_AddUserQuote(*CombatMacros[ridiculeNum-1]);
Net_SendTaunt(ridiculeNum);
pus = NUMPAGES;

View file

@ -474,7 +474,6 @@ static inline int G_GetViewscreenSizeShift(const uspritetype *tspr)
#endif
}
extern void G_PrintCurrentMusic(void);
EXTERN_INLINE_HEADER void G_SetStatusBarScale(int32_t sc);

View file

@ -3439,9 +3439,6 @@ static void Menu_EntryLinkActivate(MenuEntry_t *entry)
FX_StopAllSounds();
S_ClearSoundLocks();
if (MusicEnabled())
S_RestartMusic();
}
else if (entry == &ME_SAVESETUP_CLEANUP)
{
@ -3517,7 +3514,6 @@ static int32_t Menu_EntryOptionModify(MenuEntry_t *entry, int32_t newOption)
S_PauseMusic(true);
else
{
S_RestartMusic();
S_PauseMusic(false);
}
}

View file

@ -329,9 +329,6 @@ static int osdcmd_restartsound(osdcmdptr_t UNUSED(parm))
FX_StopAllSounds();
S_ClearSoundLocks();
if (MusicEnabled())
S_RestartMusic();
return OSDCMD_OK;
}

View file

@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "i_specialpaths.h"
#include "gamecontrol.h"
#include "version.h"
#include "z_music.h"
#include "savegamehelp.h"
BEGIN_RR_NS
@ -1687,24 +1688,10 @@ static void postloadplayer(int32_t savegamep)
//2.5
if (savegamep)
{
int32_t musicIdx = (ud.music_episode*MAXLEVELS) + ud.music_level;
Bmemset(gotpic, 0, sizeof(gotpic));
S_ClearSoundLocks();
G_CacheMapData();
if (boardfilename[0] != 0 && ud.level_number == 7 && ud.volume_number == 0 && ud.music_level == 7 && ud.music_episode == 0)
{
char levname[BMAX_PATH];
G_SetupFilenameBasedMusic(levname, boardfilename, ud.level_number);
}
if (g_mapInfo[musicIdx].musicfn != NULL && (musicIdx != g_musicIndex || different_user_map))
{
ud.music_episode = g_musicIndex / MAXLEVELS;
ud.music_level = g_musicIndex % MAXLEVELS;
S_PlayLevelMusicOrNothing(musicIdx);
}
MUS_ResumeSaved();
if (MusicEnabled())
S_PauseMusic(false);

View file

@ -108,19 +108,6 @@ void S_PauseSounds(bool paused)
}
}
void S_RestartMusic(void)
{
// Fixme: This should be completely decided by the backend, not here.
if (ud.recstat != 2 && g_player[myconnectindex].ps->gm&MODE_GAME)
{
S_PlayLevelMusicOrNothing(g_musicIndex);
}
else
{
S_PlaySpecialMusicOrNothing(MUS_INTRO);
}
}
void S_MenuSound(void)
{
static int SoundNum;

View file

@ -71,7 +71,6 @@ inline void S_ClearSoundLocks(void) {}
int32_t S_LoadSound(uint32_t num);
void S_PrecacheSounds(void);
void S_MenuSound(void);
void S_RestartMusic(void);
void S_PauseMusic(bool paused);
void S_PauseSounds(bool paused);
void S_PlayRRMusic(int newTrack = -1);

View file

@ -1189,7 +1189,6 @@ void MusicStartup(void)
else
{
buildprintf("Music error: %s\n", MUSIC_ErrorString(status));
mus_enabled = FALSE;
return;
}