1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-23 01:59:43 +01:00

Re-fix resumemusic/musicfadein once again

So it looks like facb079b35 (PR #316) had
a few issues.

The SDL performance counter doesn't really work that well. Testing
reveals that unfocusing and focusing the game again results in
the resumemusic() script command resuming the track at the wrong time.
Even when not unfocusing the game at all, stopping a track and resuming
it resumes it at the wrong time. (Only disabling the unfocus pause fixes
this.)

Furthermore, there's also the fact that the SDL performance counter
keeps incrementing when the game is paused under GDB. So... yeah.

Instead of dealing with the SDL performance counter, I'm just going to
pause and resume the music directly (so the stopmusic() script command
just pauses the music instead). As a result, we no longer can keep
constantly calling Mix_PauseMusic() or Mix_ResumeMusic() when focused or
unfocused, so I've moved those calls to happen directly when the
relevant SDL events are received (the constant calls were originally in
VCE, and whoever added them (I'm pretty sure it was Leo) was not the
sharpest tool in the shed...).

And we are going to switch over to using our own fade system instead of
the SDL mixer fade system. In fact, we were already using our own fade
system for fadeins after collecting a trinket or a custom level
crewmate, but we were still using the mixer system for the rest. This is
an inconsistency that I am glad to correct, so we're also doing our own
fadeouts now.

There is, however, an issue with the fade system where the length it
goes for is inaccurate, because it's based on a volume-per-frame second
calculation that gets truncated. But that's an issue to fix later - at
least what I'm doing right now makes resumemusic() and musicfadein()
work better than before.
This commit is contained in:
Misa 2021-04-02 12:56:25 -07:00 committed by Ethan Lee
parent 6d3a73c540
commit 510ec07021
6 changed files with 67 additions and 64 deletions

View file

@ -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 */

View file

@ -68,8 +68,6 @@ public:
bool linealreadyemptykludge;
Uint64 pauseStart;
private:
std::map<SDL_JoystickID, SDL_GameController*> controllers;
std::map<SDL_GameControllerButton, bool> buttonmap;

View file

@ -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<double>(songEnd - songStart);
const double frequency = static_cast<double>(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();
}
}

View file

@ -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

View file

@ -284,7 +284,7 @@ void scriptclass::run(void)
}
if (words[0] == "resumemusic")
{
music.resume();
music.resumefade(0);
}
if (words[0] == "musicfadeout")
{

View file

@ -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)