fade bgm on stage transition

This commit is contained in:
Andrei Alexeyev 2017-10-08 09:01:52 +03:00
parent 2bb2c8b54a
commit ff26d13496
No known key found for this signature in database
GPG key ID: 363707CD4C7FE8A4
5 changed files with 34 additions and 13 deletions

View file

@ -39,6 +39,7 @@ bool audio_backend_music_is_paused(void);
bool audio_backend_music_is_playing(void);
void audio_backend_music_resume(void);
void audio_backend_music_stop(void);
void audio_backend_music_fade(double fadetime);
void audio_backend_music_pause(void);
bool audio_backend_music_play(void *impl);
bool audio_backend_sound_play(void *impl, AudioBackendSoundGroup group);
@ -68,6 +69,7 @@ Music* get_music(const char *music);
void start_bgm(const char *name);
void stop_bgm(bool force);
void fade_bgm(void);
void resume_bgm(void);
void save_bgm(void); // XXX: this is broken
void restore_bgm(void); // XXX: this is broken

View file

@ -162,7 +162,7 @@ void resume_bgm(void) {
start_bgm(current_bgm.name); // In most cases it just unpauses existing music.
}
void stop_bgm(bool force) {
static void stop_bgm_internal(bool pause, bool fade) {
if(!current_bgm.name) {
return;
}
@ -170,18 +170,29 @@ void stop_bgm(bool force) {
current_bgm.started_at = -1;
if(audio_backend_music_is_playing() && !audio_backend_music_is_paused()) {
if(force) {
audio_backend_music_stop();
} else {
if(pause) {
audio_backend_music_pause();
log_debug("BGM paused");
} else if(fade) {
audio_backend_music_fade((FPS * FADE_TIME) / 2000.0);
log_debug("BGM fading out");
} else {
audio_backend_music_stop();
log_debug("BGM stopped");
}
log_info("BGM stopped");
} else {
log_info("No BGM was playing");
log_debug("No BGM was playing");
}
}
void stop_bgm(bool force) {
stop_bgm_internal(!force, false);
}
void fade_bgm(void) {
stop_bgm_internal(false, true);
}
void save_bgm(void) {
// XXX: this is broken
stralloc(&saved_bgm, current_bgm.name);

View file

@ -190,12 +190,19 @@ void audio_backend_music_stop(void) {
}
}
void audio_backend_music_fade(double fadetime) {
if(mixer_loaded) {
Mix_HookMusicFinished(NULL);
Mix_FadeOutMusic(1000 * fadetime);
}
}
bool audio_backend_music_is_paused(void) {
return mixer_loaded && Mix_PausedMusic();
}
bool audio_backend_music_is_playing(void) {
return mixer_loaded && Mix_PlayingMusic();
return mixer_loaded && Mix_PlayingMusic() && Mix_FadingMusic() != MIX_FADING_OUT;
}
void audio_backend_music_resume(void) {
@ -220,6 +227,9 @@ bool audio_backend_music_play(void *impl) {
Mix_Music *mmus;
int loops;
Mix_HookMusicFinished(NULL);
Mix_HaltMusic();
if(imus->intro) {
next_loop = imus->loop;
mmus = imus->intro;

View file

@ -17,10 +17,11 @@ bool audio_backend_music_is_paused(void) { return false; }
bool audio_backend_music_is_playing(void) { return false; }
void audio_backend_music_resume(void) {}
void audio_backend_music_stop(void) {}
void audio_backend_music_fade(double fadetime) {}
void audio_backend_music_pause(void) {}
bool audio_backend_music_play(void *impl) { return false; }
bool audio_backend_sound_play(void *impl, AudioBackendSoundGroup group) { return false; }
bool audio_backend_sound_loop(void *impl) { return false; }
bool audio_backend_sound_loop(void *impl, AudioBackendSoundGroup group) { return false; }
bool audio_backend_sound_stop_loop(void *impl) { return false; }
bool audio_backend_sound_pause_all(AudioBackendSoundGroup group) { return false; }
bool audio_backend_sound_resume_all(AudioBackendSoundGroup group) { return false; }

View file

@ -435,6 +435,7 @@ void stage_finish(int gameover) {
assert(global.game_over != GAMEOVER_TRANSITIONING);
global.game_over = GAMEOVER_TRANSITIONING;
set_transition_callback(TransFadeBlack, FADE_TIME, FADE_TIME*2, stage_finalize, (void*)(intptr_t)gameover);
fade_bgm();
if(global.replaymode == REPLAY_PLAY) {
return;
@ -648,10 +649,6 @@ void stage_loop(StageInfo *stage) {
fpscounter_reset(&global.fps);
loop_at_fps(stage_frame, stage_fpslimit_condition, &fstate, FPS);
if(global.game_over == GAMEOVER_RESTART && global.stage->type != STAGE_SPELL) {
stop_bgm(true);
}
if(global.replaymode == REPLAY_RECORD) {
replay_stage_event(global.replay_stage, global.frames, EV_OVER, 0);
}