Fix resumemusic/musicfadein not working

It seems like they were unfinished. This commit makes them properly
work.

When a track is stopped with stopmusic() or musicfadeout(),
resumemusic() will resume from where the track stopped. musicfadein()
does the same but does it with a gradual fade instead of suddenly
playing it at full volume.

I changed several interfaces around for this. First, setting currentsong
to -1 when music is stopped is handled in the hook callback that gets
called by SDL_mixer whenever the music stops. Otherwise, it'd be
problematic if currentsong was set to -1 when the song starts fading out
instead of when the song actually ends.

Also, music.play() has a few optional arguments now, to reduce the
copying-and-pasting of music code.

Lastly, we have to roll our own tracker of music length by using
SDL_GetPerformanceCounter(), because there's no way to get the music
position if a song fades out. (We could implicitly keep the music
position if we abruptly stopped the song using Mix_PauseMusic(), and
resume it using Mix_ResumeMusic(), but ignoring the fact that those two
functions are also used on the unfocus-pause (which, as it turns out, is
basically a non-issue because the unfocus-pause can use some other
functions), there's no equivalent for fading out, i.e. there's no
"fade out and pause when it fully fades out" function in SDL_mixer.) And
then we have to account for the unfocus-pause in our manual tracker.

Other than that, these commands are now fully functional.
This commit is contained in:
Misa
2020-06-27 01:31:09 -07:00
committed by Ethan Lee
parent 2662cd4d06
commit facb079b35
5 changed files with 56 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
#include "KeyPoll.h"
#include "Graphics.h"
#include "Music.h"
#include <stdio.h>
#include <string.h>
#include <utf8/unchecked.h>
@@ -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 */