Ensure sample indices are initialized also with sound disabled

The assertion of #8262 can be reproduced when sound is disabled or failed to be initialized, as the sample indices where not being initialized properly in these cases. It is still necessary to initialized them so sounds can be loaded in the editor also when sound is disabled.

The potential thread-safety issues of the `CSound::AllocSample` function are not yet resolved so the issue remains open.
This commit is contained in:
Robert Müller 2024-06-18 22:53:36 +02:00
parent 1457f42620
commit efa069ef80

View file

@ -207,6 +207,18 @@ int CSound::Init()
m_pGraphics = Kernel()->RequestInterface<IEngineGraphics>(); m_pGraphics = Kernel()->RequestInterface<IEngineGraphics>();
m_pStorage = Kernel()->RequestInterface<IStorage>(); m_pStorage = Kernel()->RequestInterface<IStorage>();
// Initialize sample indices. We always need them to load sounds in
// the editor even if sound is disabled or failed to be enabled.
m_FirstFreeSampleIndex = 0;
for(size_t i = 0; i < std::size(m_aSamples) - 1; ++i)
{
m_aSamples[i].m_Index = i;
m_aSamples[i].m_NextFreeSampleIndex = i + 1;
m_aSamples[i].m_pData = nullptr;
}
m_aSamples[std::size(m_aSamples) - 1].m_Index = std::size(m_aSamples) - 1;
m_aSamples[std::size(m_aSamples) - 1].m_NextFreeSampleIndex = SAMPLE_INDEX_FULL;
if(!g_Config.m_SndEnable) if(!g_Config.m_SndEnable)
return 0; return 0;
@ -228,7 +240,6 @@ int CSound::Init()
// Open the audio device and start playing sound! // Open the audio device and start playing sound!
m_Device = SDL_OpenAudioDevice(nullptr, 0, &Format, &FormatOut, 0); m_Device = SDL_OpenAudioDevice(nullptr, 0, &Format, &FormatOut, 0);
if(m_Device == 0) if(m_Device == 0)
{ {
dbg_msg("sound", "unable to open audio: %s", SDL_GetError()); dbg_msg("sound", "unable to open audio: %s", SDL_GetError());
@ -243,15 +254,6 @@ int CSound::Init()
#endif #endif
m_pMixBuffer = (int *)calloc(m_MaxFrames * 2, sizeof(int)); m_pMixBuffer = (int *)calloc(m_MaxFrames * 2, sizeof(int));
m_FirstFreeSampleIndex = 0;
for(size_t i = 0; i < std::size(m_aSamples) - 1; ++i)
{
m_aSamples[i].m_Index = i;
m_aSamples[i].m_NextFreeSampleIndex = i + 1;
}
m_aSamples[std::size(m_aSamples) - 1].m_Index = std::size(m_aSamples) - 1;
m_aSamples[std::size(m_aSamples) - 1].m_NextFreeSampleIndex = SAMPLE_INDEX_FULL;
SDL_PauseAudioDevice(m_Device, 0); SDL_PauseAudioDevice(m_Device, 0);
m_SoundEnabled = true; m_SoundEnabled = true;