From 8ce272c794b607243fe093de1f2a844c8e40957d Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Tue, 10 Oct 2023 22:36:23 +0200 Subject: [PATCH] Use `std::unique_ptr` for `SoundChannel::Unit`. --- src/sound/sound_server.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/sound/sound_server.cpp b/src/sound/sound_server.cpp index 8362d68a80..ac071b8eee 100644 --- a/src/sound/sound_server.cpp +++ b/src/sound/sound_server.cpp @@ -237,7 +237,7 @@ static bool External_Volume(int volume, int oldVolume) { /// Channels for sound effects and unit speech struct SoundChannel { - Origin *Unit = nullptr; /// pointer to unit, who plays the sound, if any + std::unique_ptr Unit; /// pointer to unit, who plays the sound, if any void (*FinishedCallback)(int channel) = nullptr; /// Callback for when a sample finishes playing }; @@ -288,7 +288,6 @@ static void ChannelFinished(int channel) event.user.data1 = (void*) Channels[channel].FinishedCallback; SDL_PeepEvents(&event, 1, SDL_ADDEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT); } - delete Channels[channel].Unit; Channels[channel].Unit = nullptr; } @@ -483,7 +482,7 @@ static int PlaySample(Mix_Chunk *sample, Origin *origin, void (*callback)(int ch DebugPrint("play sample %d\n", sample->volume); #ifdef DYNAMIC_LOAD if (sample->allocated == 0xcafebeef) { - char *name = (char*)(sample->abuf); + const char *name = (const char*)(sample->abuf); Mix_Chunk *loadedSample = ForceLoadSample(name); if (loadedSample) { memcpy(sample, loadedSample, sizeof(Mix_Chunk)); @@ -498,10 +497,10 @@ static int PlaySample(Mix_Chunk *sample, Origin *origin, void (*callback)(int ch if (channel >= 0 && channel < MaxChannels) { Channels[channel].FinishedCallback = callback; if (origin && origin->Base) { - Origin *source = new Origin; + auto source = std::make_unique(); source->Base = origin->Base; source->Id = origin->Id; - Channels[channel].Unit = source; + Channels[channel].Unit = std::move(source); } } } @@ -677,7 +676,7 @@ bool SoundEnabled() ** ** @return false if failure, true if everything ok. */ -static int InitSdlSound() +static bool InitSdlSound() { fs::path timidityCfg(StratagusLibPath); timidityCfg = timidityCfg / "timidity" / "timidity.cfg";