Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mixer constructor taking a musicCompleteHandler #1257

Merged
merged 9 commits into from
Mar 3, 2025
6 changes: 6 additions & 0 deletions NAS2D/Mixer/Mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
using namespace NAS2D;


Mixer::Mixer(Delegate<void()> musicCompleteHandler)
{
mMusicComplete.connect(musicCompleteHandler);
}


Mixer::~Mixer()
{
}
Expand Down
7 changes: 5 additions & 2 deletions NAS2D/Mixer/Mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "../Duration.h"
#include "../Signal/Signal.h"
#include "../Signal/Delegate.h"


namespace NAS2D
Expand All @@ -28,11 +29,13 @@ namespace NAS2D
public:
Mixer() = default;
Mixer(const Mixer&) = default;
Mixer& operator=(const Mixer&) = default;
Mixer(Mixer&&) = default;
Mixer& operator=(Mixer&&) = default;
explicit Mixer(Delegate<void()> musicCompleteHandler);
virtual ~Mixer();

Mixer& operator=(const Mixer&) = default;
Mixer& operator=(Mixer&&) = default;

public:
virtual void playSound(const Sound& sound) = 0;
virtual void stopSound() = 0;
Expand Down
40 changes: 29 additions & 11 deletions NAS2D/Mixer/MixerSDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ using namespace NAS2D;

namespace
{
// ==================================================================================
// INTEROP WITH SDL2_MIXER
// ==================================================================================
// Global so it can be accessed without capturing `this`
Signal<> musicFinished;
// ==================================================================================


constexpr int AudioVolumeMin = 0;
constexpr int AudioVolumeMax = 128;

Expand All @@ -58,6 +50,18 @@ namespace

constexpr int AudioBufferSizeMin = 256;
constexpr int AudioBufferSizeMax = 4096;

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


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


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

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


MixerSDL::MixerSDL(const Options& options, Delegate<void()> musicCompleteHandler) :
MixerSDL(options)
{
mMusicComplete.connect(musicCompleteHandler);
}


MixerSDL::MixerSDL(Delegate<void()> musicCompleteHandler) :
MixerSDL()
{
mMusicComplete.connect(musicCompleteHandler);
}


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

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

SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
Expand Down
9 changes: 6 additions & 3 deletions NAS2D/Mixer/MixerSDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ namespace NAS2D
static void WriteConfigurationOptions(const Options& options);

MixerSDL();
MixerSDL(const Options& options);
MixerSDL(const MixerSDL&) = delete;
MixerSDL& operator=(const MixerSDL&) = delete;
MixerSDL(MixerSDL&&) = default;
MixerSDL& operator=(MixerSDL&&) = default;
explicit MixerSDL(const Options& options);
explicit MixerSDL(const Options& options, Delegate<void()> musicCompleteHandler);
explicit MixerSDL(Delegate<void()> musicCompleteHandler);
~MixerSDL() override;

MixerSDL& operator=(const MixerSDL&) = delete;
MixerSDL& operator=(MixerSDL&&) = default;

void playSound(const Sound& sound) override;
void stopSound() override;
void pauseSound() override;
Expand Down