From dab96b810691c26e29fef92d88c828a311be3e9d Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 3 Feb 2021 17:11:46 -0500 Subject: Added running sounds --- src/mixer.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'src/mixer.cpp') diff --git a/src/mixer.cpp b/src/mixer.cpp index bfbedcd..bdcb7ce 100644 --- a/src/mixer.cpp +++ b/src/mixer.cpp @@ -1,6 +1,29 @@ #include "mixer.h" -void Mixer::playSound(std::string filename) { +void Mixer::playSound(std::string_view filename) { + Mix_Chunk* chunk = getChunkByFilename(std::string(filename)); + + if (Mix_PlayChannel(-1, chunk, 0) == -1) { + throw mix_error(); + } +} + +int Mixer::loopSound(std::string_view filename) { + Mix_Chunk* chunk = getChunkByFilename(std::string(filename)); + + int ret = Mix_PlayChannel(-1, chunk, -1); + if (ret == -1) { + throw mix_error(); + } + + return ret; +} + +void Mixer::stopChannel(int channel) { + Mix_HaltChannel(channel); +} + +Mix_Chunk* Mixer::getChunkByFilename(std::string filename) { if (!sounds_.count(filename)) { Mix_Chunk* sample = Mix_LoadWAV(filename.c_str()); if (!sample) { @@ -10,7 +33,5 @@ void Mixer::playSound(std::string filename) { sounds_[filename] = chunk_ptr(sample); } - if (Mix_PlayChannel(-1, sounds_[filename].get(), 0) == -1) { - throw mix_error(); - } + return sounds_[filename].get(); } -- cgit 1.4.1