diff --git a/desktop_version/src/KeyPoll.cpp b/desktop_version/src/KeyPoll.cpp index 5e7ec8a3..38c6553a 100644 --- a/desktop_version/src/KeyPoll.cpp +++ b/desktop_version/src/KeyPoll.cpp @@ -1,5 +1,6 @@ #include "KeyPoll.h" #include "Graphics.h" +#include "Music.h" #include #include #include @@ -54,6 +55,8 @@ KeyPoll::KeyPoll() } linealreadyemptykludge = false; + + pauseStart = 0; } void KeyPoll::enabletextentry() @@ -254,6 +257,11 @@ void KeyPoll::Poll() } } SDL_DisableScreenSaver(); + if (Mix_PlayingMusic()) + { + // Correct songStart for how long we were paused + music.songStart += SDL_GetPerformanceCounter() - pauseStart; + } break; case SDL_WINDOWEVENT_FOCUS_LOST: isActive = false; @@ -267,6 +275,7 @@ void KeyPoll::Poll() ); } SDL_EnableScreenSaver(); + pauseStart = SDL_GetPerformanceCounter(); break; /* Mouse Focus */ diff --git a/desktop_version/src/KeyPoll.h b/desktop_version/src/KeyPoll.h index c4cbfe69..a2654e07 100644 --- a/desktop_version/src/KeyPoll.h +++ b/desktop_version/src/KeyPoll.h @@ -76,6 +76,8 @@ 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 25432762..6a17b1b7 100644 --- a/desktop_version/src/Music.cpp +++ b/desktop_version/src/Music.cpp @@ -4,6 +4,8 @@ #include "BinaryBlob.h" #include "Map.h" +void songend(); + void musicclass::init() { for (size_t i = 0; i < soundTracks.size(); ++i) { @@ -227,9 +229,20 @@ void musicclass::init() volume = 0.0f; fadeoutqueuesong = -1; dontquickfade = false; + + songStart = 0; + songEnd = 0; + + Mix_HookMusicFinished(&songend); } -void musicclass::play(int t) +void songend() +{ + music.songEnd = SDL_GetPerformanceCounter(); + music.currentsong = -1; +} + +void musicclass::play(int t, const double position_sec /*= 0.0*/, const int fadein_ms /*= 3000*/) { t = (t % 16); @@ -250,9 +263,9 @@ void musicclass::play(int t) if (currentsong == 0 || currentsong == 7 || (!map.custommode && (currentsong == 16 || currentsong == 23))) { // Level Complete theme, no fade in or repeat - if(Mix_FadeInMusic(musicTracks[t].m_music, 0, 0)==-1) + if(Mix_FadeInMusicPos(musicTracks[t].m_music, 0, 0, position_sec)==-1) { - printf("Mix_PlayMusic: %s\n", Mix_GetError()); + printf("Mix_FadeInMusicPos: %s\n", Mix_GetError()); } } else @@ -266,11 +279,13 @@ void musicclass::play(int t) else dontquickfade = false; } - else if(Mix_FadeInMusic(musicTracks[t].m_music, -1, 3000)==-1) + else if(Mix_FadeInMusicPos(musicTracks[t].m_music, -1, fadein_ms, position_sec)==-1) { - printf("Mix_FadeInMusic: %s\n", Mix_GetError()); + printf("Mix_FadeInMusicPos: %s\n", Mix_GetError()); } } + + songStart = SDL_GetPerformanceCounter(); } else { @@ -279,10 +294,25 @@ void musicclass::play(int t) } } +void musicclass::resume(const int fadein_ms /*= 0*/) +{ + const double offset = static_cast(songEnd - songStart); + const double frequency = static_cast(SDL_GetPerformanceFrequency()); + + const double position_sec = offset / frequency; + + play(resumesong, position_sec, fadein_ms); +} + +void musicclass::fadein() +{ + resume(3000); // 3000 ms fadein +} + void musicclass::haltdasmusik() { Mix_HaltMusic(); - currentsong = -1; + resumesong = currentsong; } void musicclass::silencedasmusik() @@ -300,7 +330,7 @@ void musicclass::fadeMusicVolumeIn(int ms) void musicclass::fadeout() { Mix_FadeOutMusic(2000); - currentsong = -1; + resumesong = currentsong; } void musicclass::processmusicfadein() diff --git a/desktop_version/src/Music.h b/desktop_version/src/Music.h index ead32835..d681f61c 100644 --- a/desktop_version/src/Music.h +++ b/desktop_version/src/Music.h @@ -13,11 +13,13 @@ class musicclass public: void init(); - void play(int t); + void play(int t, const double position_sec = 0.0, const int fadein_ms = 3000); + void resume(const int fadein_ms = 0); void haltdasmusik(); void silencedasmusik(); void fadeMusicVolumeIn(int ms); void fadeout(); + void fadein(); void processmusicfadein(); void processmusic(); void niceplay(int t); @@ -53,6 +55,9 @@ public: bool usingmmmmmm; binaryBlob musicReadBlob; + + Uint64 songStart; + Uint64 songEnd; }; extern musicclass music; diff --git a/desktop_version/src/Script.cpp b/desktop_version/src/Script.cpp index f8e6026a..32389eb6 100644 --- a/desktop_version/src/Script.cpp +++ b/desktop_version/src/Script.cpp @@ -260,7 +260,7 @@ void scriptclass::run() } if (words[0] == "resumemusic") { - music.play(music.resumesong); + music.resume(); } if (words[0] == "musicfadeout") { @@ -269,8 +269,7 @@ void scriptclass::run() } if (words[0] == "musicfadein") { - music.musicfadein = 90; - //if(!game.muted) music.fadeMusicVolumeIn(3000); + music.fadein(); } if (words[0] == "trinketscriptmusic") {