diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-27 12:05:45 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-27 12:05:45 -0500 |
| commit | 1656242563d14fa564bab8d4bc40054ab8998553 (patch) | |
| tree | 872589408ac7aef54b1959dc53b946fbed9dfcaa /src/mixer.cpp | |
| parent | e644f5c0d769989bd3b0665312c6949d76f5e388 (diff) | |
| download | tanetane-1656242563d14fa564bab8d4bc40054ab8998553.tar.gz tanetane-1656242563d14fa564bab8d4bc40054ab8998553.tar.bz2 tanetane-1656242563d14fa564bab8d4bc40054ab8998553.zip | |
Added background music (defined on a per-map basis)
Diffstat (limited to 'src/mixer.cpp')
| -rw-r--r-- | src/mixer.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
| diff --git a/src/mixer.cpp b/src/mixer.cpp index bdcb7ce..37c2177 100644 --- a/src/mixer.cpp +++ b/src/mixer.cpp | |||
| @@ -23,6 +23,49 @@ void Mixer::stopChannel(int channel) { | |||
| 23 | Mix_HaltChannel(channel); | 23 | Mix_HaltChannel(channel); |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | void Mixer::playMusic(std::string_view name, int ms) { | ||
| 27 | Mix_Music* song = getMusicByName(name); | ||
| 28 | int ret; | ||
| 29 | |||
| 30 | if (ms == 0) { | ||
| 31 | ret = Mix_PlayMusic(song, -1); | ||
| 32 | } else { | ||
| 33 | ret = Mix_FadeInMusic(song, -1, ms); | ||
| 34 | } | ||
| 35 | |||
| 36 | if (ret == -1) { | ||
| 37 | throw mix_error(); | ||
| 38 | } | ||
| 39 | |||
| 40 | playingTrack_ = name; | ||
| 41 | } | ||
| 42 | |||
| 43 | void Mixer::fadeoutMusic(int ms) { | ||
| 44 | if (Mix_FadeOutMusic(ms) == 0) { | ||
| 45 | throw mix_error(); | ||
| 46 | } | ||
| 47 | |||
| 48 | playingTrack_ = ""; | ||
| 49 | } | ||
| 50 | |||
| 51 | void Mixer::muteMusic() { | ||
| 52 | Mix_VolumeMusic(0); | ||
| 53 | musicMuted_ = true; | ||
| 54 | } | ||
| 55 | |||
| 56 | void Mixer::unmuteMusic() { | ||
| 57 | Mix_VolumeMusic(musicVolume_); | ||
| 58 | musicMuted_ = false; | ||
| 59 | } | ||
| 60 | |||
| 61 | void Mixer::pauseMusic() { | ||
| 62 | Mix_PauseMusic(); | ||
| 63 | } | ||
| 64 | |||
| 65 | void Mixer::unpauseMusic() { | ||
| 66 | Mix_ResumeMusic(); | ||
| 67 | } | ||
| 68 | |||
| 26 | Mix_Chunk* Mixer::getChunkByFilename(std::string filename) { | 69 | Mix_Chunk* Mixer::getChunkByFilename(std::string filename) { |
| 27 | if (!sounds_.count(filename)) { | 70 | if (!sounds_.count(filename)) { |
| 28 | Mix_Chunk* sample = Mix_LoadWAV(filename.c_str()); | 71 | Mix_Chunk* sample = Mix_LoadWAV(filename.c_str()); |
| @@ -35,3 +78,18 @@ Mix_Chunk* Mixer::getChunkByFilename(std::string filename) { | |||
| 35 | 78 | ||
| 36 | return sounds_[filename].get(); | 79 | return sounds_[filename].get(); |
| 37 | } | 80 | } |
| 81 | |||
| 82 | Mix_Music* Mixer::getMusicByName(std::string_view name) { | ||
| 83 | std::string filename = "../res/music/" + std::string(name) + ".wav"; | ||
| 84 | |||
| 85 | if (!music_.count(filename)) { | ||
| 86 | Mix_Music* song = Mix_LoadMUS(filename.c_str()); | ||
| 87 | if (!song) { | ||
| 88 | throw mix_error(); | ||
| 89 | } | ||
| 90 | |||
| 91 | music_[filename] = music_ptr(song); | ||
| 92 | } | ||
| 93 | |||
| 94 | return music_[filename].get(); | ||
| 95 | } | ||
