another bit of refactoring, trying to be backwards compatible without breaking more

This commit is contained in:
Tim Felgentreff 2022-08-09 17:53:58 +02:00
parent be4a1b45b2
commit 9ed69f7df1
8 changed files with 41 additions and 39 deletions

View file

@ -191,9 +191,6 @@ extern void InitSoundClient();
// music.cpp
/// Check if music is finished and play the next song
extern void CheckMusicFinished(int force = 0);
/// Initialize music
extern void InitMusic();
@ -201,7 +198,7 @@ extern void InitMusic();
extern void CallbackMusicEnable();
/// Skip the next music stopped callback invocation
extern void CallbackMusicSkip();
extern void CallbackMusicDisable();
/// Turn music stopped callback on and trigger it immediately
extern void CallbackMusicTrigger();

View file

@ -91,7 +91,7 @@ void SetMusicFinishedCallback(void (*callback)());
/// Play a music file
extern int PlayMusic(const std::string &file);
/// Stop music playing
extern void StopMusic(bool fade = true);
extern void StopMusic();
/// Set music volume
extern void SetMusicVolume(int volume);
/// Get music volume

View file

@ -55,17 +55,39 @@
/// flag is set when a MusicFinishedCallback was enqueued in the event loop and should be handled, and unset when the handler has run.
static std::atomic_flag MusicFinishedEventQueued = ATOMIC_FLAG_INIT;
static volatile bool IsCallbackEnabled = false;
/*----------------------------------------------------------------------------
-- Functions
----------------------------------------------------------------------------*/
/**
** Check if music is finished and play the next song
*/
static void CheckMusicFinished()
{
if (SoundEnabled() && IsMusicEnabled()) {
lua_getglobal(Lua, "MusicStopped");
if (!lua_isfunction(Lua, -1)) {
fprintf(stderr, "No MusicStopped function in Lua\n");
} else {
DebugPrint("Calling MusicStopped callback at %ul\n" _C_ SDL_GetTicks());
LuaCall(0, 1);
}
}
// clear the flag after handling the event, so the next event can be enqueued
MusicFinishedEventQueued.clear();
}
/**
** Callback for when music has finished
** Note: we are in the sdl audio thread, so dispatch an event to the main event loop
*/
static void MusicFinishedCallback()
{
if (!IsCallbackEnabled) {
return;
}
if (MusicFinishedEventQueued.test_and_set()) {
// don't queue more than one of these events at a time
return;
@ -81,44 +103,27 @@ static void MusicFinishedCallback()
}
}
/**
** Check if music is finished and play the next song
*/
void CheckMusicFinished(int force)
{
if (SoundEnabled() && IsMusicEnabled() && MusicFinishedEventQueued.test_and_set()) {
lua_getglobal(Lua, "MusicStopped");
if (!lua_isfunction(Lua, -1)) {
fprintf(stderr, "No MusicStopped function in Lua\n");
} else {
LuaCall(0, 1);
}
}
// clear the flag after handling the event, so the next event can be enqueued
MusicFinishedEventQueued.clear();
}
/**
** Init music
*/
void InitMusic()
{
SetMusicFinishedCallback(MusicFinishedCallback);
CallbackMusicEnable();
#ifdef USE_FLUIDSYNTH
InitFluidSynth();
#endif
}
void CallbackMusicEnable() {
MusicFinishedEventQueued.clear();
IsCallbackEnabled = true;
}
void CallbackMusicSkip() {
MusicFinishedEventQueued.test_and_set();
void CallbackMusicDisable() {
IsCallbackEnabled = false;
}
void CallbackMusicTrigger() {
MusicFinishedEventQueued.clear();
MusicFinishedCallback();
}

View file

@ -360,7 +360,7 @@ static Mix_Music *currentMusic = NULL;
static Mix_Music *LoadMusic(const char *name)
{
if (currentMusic) {
StopMusic(false);
StopMusic();
Mix_FreeMusic(currentMusic);
}
currentMusic = Mix_LoadMUS(name);
@ -584,17 +584,13 @@ int PlayMusic(const std::string &file)
/**
** Stop the current playing music. This does not trigger the music finished callback.
*/
void StopMusic(bool fade)
void StopMusic()
{
CallbackMusicSkip();
if (External_Stop()) {
return;
}
if (fade) {
Mix_FadeOutMusic(200);
} else {
CallbackMusicDisable();
if (!External_Stop()) {
Mix_HaltMusic();
}
CallbackMusicEnable();
}
/**

View file

@ -193,7 +193,10 @@ void ShowTitleScreens()
Video.ClearScreen();
}
Invalidate();
CallbackMusicTrigger();
CallbackMusicEnable();
if (!IsMusicPlaying()) {
CallbackMusicTrigger();
}
}

View file

@ -16,7 +16,7 @@ end
$]
extern int PlayMusic(const std::string name);
extern void StopMusic(bool fade = true);
extern void StopMusic();
extern bool IsMusicPlaying();
extern int SetChannelVolume(int channel, int volume);

View file

@ -256,7 +256,7 @@ static void UiToggleSound()
} else {
SetEffectsEnabled(true);
SetMusicEnabled(true);
CheckMusicFinished(true);
CallbackMusicTrigger();
}
}

View file

@ -366,7 +366,7 @@ int PlayMovie(const std::string &name)
int prevMusicVolume = GetMusicVolume();
SetMusicVolume(GetEffectsVolume());
PlayMusic(filename);
CallbackMusicSkip(); // do not run the lua callback when the video audio finished
CallbackMusicDisable(); // do not run the lua callback when the video audio finished
EventCallback callbacks;
@ -418,6 +418,7 @@ int PlayMovie(const std::string &name)
StopMusic();
SetMusicVolume(prevMusicVolume);
SDL_DestroyTexture(yuv_overlay);
CallbackMusicEnable();
OggFree(&data);
f.close();