Skip to content

Commit

Permalink
Use Delegate for global musicFinished handler
Browse files Browse the repository at this point in the history
The way the `MixerSDL` constructor and destructor use this field, there can only ever reliably be a single listener.
  • Loading branch information
DanRStevens committed Mar 3, 2025
1 parent d21f222 commit 2e519f7
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions NAS2D/Mixer/MixerSDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ namespace
constexpr int AudioBufferSizeMax = 4096;

// Global so it can be accessed without capturing `this`
Signal<> musicFinished;
Delegate<void()> musicFinished;


void onMusicFinished()
{
musicFinished();
if (musicFinished)
{
musicFinished();
}
}
}

Expand Down Expand Up @@ -119,7 +122,7 @@ MixerSDL::MixerSDL(const Options& options)
soundVolume(options.sfxVolume);
musicVolume(options.musicVolume);

musicFinished.connect({this, &MixerSDL::onMusicFinished});
musicFinished = Delegate{this, &MixerSDL::onMusicFinished};
Mix_HookMusicFinished(&::onMusicFinished);
}

Expand All @@ -145,7 +148,7 @@ MixerSDL::~MixerSDL()
Mix_CloseAudio();

Mix_HookMusicFinished(nullptr);
musicFinished.disconnect({this, &MixerSDL::onMusicFinished});
musicFinished.clear();

SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
Expand Down

0 comments on commit 2e519f7

Please sign in to comment.