diff --git a/desktop_version/src/KeyPoll.cpp b/desktop_version/src/KeyPoll.cpp index 697ca615..b22467b2 100644 --- a/desktop_version/src/KeyPoll.cpp +++ b/desktop_version/src/KeyPoll.cpp @@ -56,8 +56,6 @@ KeyPoll::KeyPoll(void) linealreadyemptykludge = false; - pauseStart = 0; - isActive = true; } @@ -287,6 +285,8 @@ void KeyPoll::Poll(void) if (!game.disablepause) { isActive = true; + music.resume(); + music.resumeef(); } if (!useFullscreenSpaces) { @@ -300,16 +300,13 @@ void KeyPoll::Poll(void) } } SDL_DisableScreenSaver(); - if (!game.disablepause && Mix_PlayingMusic()) - { - // Correct songStart for how long we were paused - music.songStart += SDL_GetPerformanceCounter() - pauseStart; - } break; case SDL_WINDOWEVENT_FOCUS_LOST: if (!game.disablepause) { isActive = false; + music.pause(); + music.pauseef(); } if (!useFullscreenSpaces) { @@ -321,10 +318,6 @@ void KeyPoll::Poll(void) ); } SDL_EnableScreenSaver(); - if (!game.disablepause) - { - pauseStart = SDL_GetPerformanceCounter(); - } break; /* Mouse Focus */ diff --git a/desktop_version/src/KeyPoll.h b/desktop_version/src/KeyPoll.h index 2c5c7532..b8851b46 100644 --- a/desktop_version/src/KeyPoll.h +++ b/desktop_version/src/KeyPoll.h @@ -68,8 +68,6 @@ public: bool linealreadyemptykludge; - Uint64 pauseStart; - private: std::map controllers; std::map buttonmap; diff --git a/desktop_version/src/Music.cpp b/desktop_version/src/Music.cpp index bff2d3ff..e7e24ffd 100644 --- a/desktop_version/src/Music.cpp +++ b/desktop_version/src/Music.cpp @@ -9,26 +9,19 @@ #include "Map.h" #include "UtilityClass.h" -static void songend(void); - musicclass::musicclass(void) { safeToProcessMusic= false; m_doFadeInVol = false; - musicVolume = MIX_MAX_VOLUME; + m_doFadeOutVol = false; + musicVolume = 0; FadeVolAmountPerFrame = 0; currentsong = 0; nicechange = -1; nicefade = false; - resumesong = 0; quick_fade = true; - songStart = 0; - songEnd = 0; - - Mix_HookMusicFinished(&songend); - usingmmmmmm = false; } @@ -142,14 +135,6 @@ void musicclass::init(void) } } -static void songend(void) -{ - extern musicclass music; - music.songEnd = SDL_GetPerformanceCounter(); - music.resumesong = music.currentsong; - music.currentsong = -1; -} - void musicclass::destroy(void) { for (size_t i = 0; i < soundTracks.size(); ++i) @@ -172,7 +157,7 @@ void musicclass::destroy(void) mmmmmm_blob.clear(); } -void musicclass::play(int t, const double position_sec /*= 0.0*/, const int fadein_ms /*= 3000*/) +void musicclass::play(int t) { if (mmmmmm && usingmmmmmm) { @@ -193,9 +178,8 @@ void musicclass::play(int t, const double position_sec /*= 0.0*/, const int fade } safeToProcessMusic = true; - musicVolume = MIX_MAX_VOLUME; - if (currentsong == t && Mix_FadingMusic() != MIX_FADING_OUT) + if (currentsong == t && !m_doFadeOutVol) { return; } @@ -217,14 +201,18 @@ void musicclass::play(int t, const double position_sec /*= 0.0*/, const int fade if (currentsong == 0 || currentsong == 7 || (!map.custommode && (currentsong == 0+num_mmmmmm_tracks || currentsong == 7+num_mmmmmm_tracks))) { // Level Complete theme, no fade in or repeat - if (Mix_FadeInMusicPos(musicTracks[t].m_music, 0, 0, position_sec) == -1) + if (Mix_PlayMusic(musicTracks[t].m_music, 0) == -1) { - printf("Mix_FadeInMusicPos: %s\n", Mix_GetError()); + printf("Mix_PlayMusic: %s\n", Mix_GetError()); + } + else + { + musicVolume = MIX_MAX_VOLUME; } } else { - if (Mix_FadingMusic() == MIX_FADING_OUT) + if (m_doFadeOutVol) { // We're already fading out nicechange = t; @@ -233,35 +221,39 @@ void musicclass::play(int t, const double position_sec /*= 0.0*/, const int fade if (quick_fade) { - Mix_FadeOutMusic(500); // fade out quicker + fadeMusicVolumeOut(500); // fade out quicker } else { quick_fade = true; } } - else if (Mix_FadeInMusicPos(musicTracks[t].m_music, -1, fadein_ms, position_sec) == -1) + else if (Mix_PlayMusic(musicTracks[t].m_music, -1) == -1) { - printf("Mix_FadeInMusicPos: %s\n", Mix_GetError()); + printf("Mix_PlayMusic: %s\n", Mix_GetError()); + } + else + { + fadeMusicVolumeIn(3000); + musicVolume = 0; } } - - songStart = SDL_GetPerformanceCounter(); } -void musicclass::resume(const int fadein_ms /*= 0*/) +void musicclass::resume() { - const double offset = static_cast(songEnd - songStart); - const double frequency = static_cast(SDL_GetPerformanceFrequency()); + Mix_ResumeMusic(); +} - const double position_sec = offset / frequency; - - play(resumesong, position_sec, fadein_ms); +void musicclass::resumefade(const int fadein_ms) +{ + resume(); + fadeMusicVolumeIn(fadein_ms); } void musicclass::fadein(void) { - resume(3000); // 3000 ms fadein + resumefade(3000); // 3000 ms fadein } void musicclass::pause(void) @@ -271,7 +263,8 @@ void musicclass::pause(void) void musicclass::haltdasmusik(void) { - Mix_HaltMusic(); + /* Just pauses music. This is intended. */ + pause(); } void musicclass::silencedasmusik(void) @@ -292,13 +285,20 @@ void musicclass::setfadeamount(const int fade_ms) void musicclass::fadeMusicVolumeIn(int ms) { m_doFadeInVol = true; + m_doFadeOutVol = false; setfadeamount(ms); } +void musicclass::fadeMusicVolumeOut(const int fadeout_ms) +{ + m_doFadeInVol = false; + m_doFadeOutVol = true; + setfadeamount(fadeout_ms); +} + void musicclass::fadeout(const bool quick_fade_ /*= true*/) { - Mix_FadeOutMusic(2000); - resumesong = currentsong; + fadeMusicVolumeOut(2000); quick_fade = quick_fade_; } @@ -311,6 +311,17 @@ void musicclass::processmusicfadein(void) } } +void musicclass::processmusicfadeout(void) +{ + musicVolume -= FadeVolAmountPerFrame; + if (musicVolume < 0) + { + musicVolume = 0; + m_doFadeOutVol = false; + pause(); + } +} + void musicclass::processmusic(void) { if(!safeToProcessMusic) @@ -318,7 +329,7 @@ void musicclass::processmusic(void) return; } - if (nicefade && Mix_PlayingMusic() == 0) + if (nicefade && Mix_PausedMusic() == 1) { play(nicechange); nicechange = -1; @@ -329,6 +340,11 @@ void musicclass::processmusic(void) { processmusicfadein(); } + + if (m_doFadeOutVol) + { + processmusicfadeout(); + } } diff --git a/desktop_version/src/Music.h b/desktop_version/src/Music.h index 28b2914d..a03bcce1 100644 --- a/desktop_version/src/Music.h +++ b/desktop_version/src/Music.h @@ -15,23 +15,25 @@ public: void init(void); void destroy(void); - void play(int t, const double position_sec = 0.0, const int fadein_ms = 3000); - void resume(const int fadein_ms = 0); + void play(int t); + void resume(); + void resumefade(const int fadein_ms); void pause(void); void haltdasmusik(void); void silencedasmusik(void); void setfadeamount(const int fade_ms); void fadeMusicVolumeIn(int ms); + void fadeMusicVolumeOut(const int fadeout_ms); void fadeout(const bool quick_fade_ = true); void fadein(void); void processmusicfadein(void); + void processmusicfadeout(void); void processmusic(void); void niceplay(int t); void changemusicarea(int x, int y); int currentsong; - int resumesong; void playef(int t); void pauseef(void); @@ -46,6 +48,7 @@ public: bool nicefade; bool m_doFadeInVol; + bool m_doFadeOutVol; int FadeVolAmountPerFrame; int musicVolume; @@ -59,9 +62,6 @@ public: binaryBlob mmmmmm_blob; int num_pppppp_tracks; int num_mmmmmm_tracks; - - Uint64 songStart; - Uint64 songEnd; }; #ifndef MUSIC_DEFINITION diff --git a/desktop_version/src/Script.cpp b/desktop_version/src/Script.cpp index 1ee14250..476eae91 100644 --- a/desktop_version/src/Script.cpp +++ b/desktop_version/src/Script.cpp @@ -284,7 +284,7 @@ void scriptclass::run(void) } if (words[0] == "resumemusic") { - music.resume(); + music.resumefade(0); } if (words[0] == "musicfadeout") { diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index 2ed8693a..501b6f2e 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -705,9 +705,6 @@ static enum LoopCode loop_begin(void) static void unfocused_run(void) { - Mix_Pause(-1); - Mix_PauseMusic(); - if (!game.blackout) { ClearSurface(graphics.backBuffer); @@ -726,8 +723,7 @@ static void unfocused_run(void) static void focused_begin(void) { - Mix_Resume(-1); - Mix_ResumeMusic(); + /* no-op. */ } static void focused_end(void)