diff --git a/source/common/gamecontrol.cpp b/source/common/gamecontrol.cpp index b5dff8451..653b07f25 100644 --- a/source/common/gamecontrol.cpp +++ b/source/common/gamecontrol.cpp @@ -273,6 +273,13 @@ void I_StartupJoysticks(); void I_ShutdownInput(); int RunGame(); +void ShutdownSystem() +{ + Mus_Stop(); + if (soundEngine) delete soundEngine; + I_ShutdownInput(); +} + int GameMain() { // Set up the console before anything else so that it can receive text. @@ -295,6 +302,8 @@ int GameMain() } catch (const std::runtime_error & err) { + // shut down critical systems before showing a message box. + ShutdownSystem(); wm_msgbox("Error", "%s", err.what()); return 3; } @@ -303,7 +312,7 @@ int GameMain() // Just let the rest of the function execute. r = exit.Reason(); } - I_ShutdownInput(); + ShutdownSystem(); G_SaveConfig(); #ifndef NETCODE_DISABLE if (gHaveNetworking) enet_deinitialize(); @@ -550,7 +559,7 @@ void CONFIG_InitMouseAndController() CCMD(snd_reset) { Mus_Stop(); - soundEngine->Reset(); + if (soundEngine) soundEngine->Reset(); MUS_ResumeSaved(); } diff --git a/source/common/sound/s_sound.cpp b/source/common/sound/s_sound.cpp index 618652a12..bab2bb214 100644 --- a/source/common/sound/s_sound.cpp +++ b/source/common/sound/s_sound.cpp @@ -1705,4 +1705,4 @@ void SoundEngine::AddRandomSound(int Owner, TArray list) S_sfx[Owner].link = index; S_sfx[Owner].bRandomHeader = true; S_sfx[Owner].NearLimit = -1; -} +} \ No newline at end of file diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index e60fe0b52..77f4d0e66 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -5807,6 +5807,7 @@ int GameInterface::app_main() { I_Error("app_main: There was a problem initializing the Build engine: %s\n", engineerrstr); } + S_InitSound(); g_logFlushWindow = 0; diff --git a/source/duke3d/src/gamedef.cpp b/source/duke3d/src/gamedef.cpp index 65b892fb5..71362aaac 100644 --- a/source/duke3d/src/gamedef.cpp +++ b/source/duke3d/src/gamedef.cpp @@ -2255,6 +2255,7 @@ static void scriptUpdateOpcodeForVariableType(intptr_t *ins) static bool C_ParseCommand(bool loop /*= false*/) { int32_t i, j=0, k=0, tw; + TArray buffer; do { @@ -4960,10 +4961,10 @@ repeatcase: i = 0; { - TArray build; + buffer.Clear(); while (*textptr != 0x0a && *textptr != 0x0d && *textptr != 0) { - build.Push(*textptr); + buffer.Push(*textptr); textptr++, i++; if (EDUKE32_PREDICT_FALSE(*textptr != 0x0a && *textptr != 0x0d && ispecial(*textptr))) { @@ -4974,8 +4975,8 @@ repeatcase: break; } } - build.Push(0); - C_CON_SetButtonAlias(j, build.Data()); + buffer.Push(0); + C_CON_SetButtonAlias(j, buffer.Data()); } continue; @@ -5246,7 +5247,7 @@ repeatcase: scriptSkipSpaces(); - TArray buffer; + buffer.Clear(); while (*textptr != 0x0a && *textptr != 0x0d && *textptr != 0) { buffer.Push(*textptr); @@ -5362,7 +5363,6 @@ repeatcase: case CON_DEFINESOUND: { - FString filename; int ps, pe, vo, pr, m; float volume; @@ -5390,7 +5390,7 @@ repeatcase: i = 0; C_SkipComments(); - TArray buffer; + buffer.Clear(); if (*textptr == '\"') { @@ -5421,7 +5421,7 @@ repeatcase: vo = g_scriptPtr[-1]; g_scriptPtr -= 5; - int res = S_DefineSound(k, filename, ps, pe, pr, m, vo, 1.f); + int res = S_DefineSound(k, buffer.Data(), ps, pe, pr, m, vo, 1.f); volume = 1.f; diff --git a/source/duke3d/src/sounds.cpp b/source/duke3d/src/sounds.cpp index ce85391c6..7e64c8f45 100644 --- a/source/duke3d/src/sounds.cpp +++ b/source/duke3d/src/sounds.cpp @@ -47,6 +47,11 @@ public: } }; +void S_InitSound() +{ + soundEngine = new DukeSoundEngine; +} + //========================================================================== // // This is to avoid hardscoding the dependency on Wads into the sound engine @@ -78,10 +83,12 @@ void S_PauseSounds(bool paused) void cacheAllSounds(void) { - for (int i=0, j=0; i < MAXSOUNDS; ++i) + auto& sfx = soundEngine->GetSounds(); + int i = 0; + for(auto &snd : sfx) { - soundEngine->CacheSound(i); - if ((i&31) == 0) + soundEngine->CacheSound(&snd); + if (((++i)&31) == 0) gameHandleEvents(); } } diff --git a/source/duke3d/src/sounds.h b/source/duke3d/src/sounds.h index 9111a4c48..d30c9576c 100644 --- a/source/duke3d/src/sounds.h +++ b/source/duke3d/src/sounds.h @@ -66,6 +66,7 @@ void S_Update(void); void S_ChangeSoundPitch(int soundNum, int spriteNum, int pitchoffset); int S_GetUserFlags(int sndnum); int S_DefineSound(unsigned index, const char* filename, int ps, int pe, int pr, int m, int vo, float vol); +void S_InitSound(); static inline bool S_IsAmbientSFX(int spriteNum) {