From 862b715003e4e5d8a383100d15bd8786ecb50ba0 Mon Sep 17 00:00:00 2001 From: Ted John Date: Mon, 3 Oct 2016 18:14:34 +0100 Subject: [PATCH] Fix #4522: Theme music doesn't stop when connected to paused server Refactor audio initialisation and stop all music and sounds when the screen mode changes. --- src/audio/audio.c | 26 +++++++++++++++++++++++--- src/audio/audio.h | 2 ++ src/editor.c | 5 +++-- src/game.c | 2 +- src/openrct2.c | 21 +++++++-------------- src/rct2.c | 5 ----- src/rct2.h | 2 ++ src/scenario.c | 4 ++++ src/title.c | 6 ++---- 9 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/audio/audio.c b/src/audio/audio.c index 6cb2e71362..a1f4095d69 100644 --- a/src/audio/audio.c +++ b/src/audio/audio.c @@ -121,11 +121,22 @@ void audio_stop_channel(void **channel); void audio_init() { int result = SDL_Init(SDL_INIT_AUDIO); - if (result >= 0) + if (result < 0) { + log_error("SDL_Init %s", SDL_GetError()); return; + } - log_fatal("SDL_Init %s", SDL_GetError()); - exit(-1); + if (str_is_null_or_empty(gConfigSound.device)) { + Mixer_Init(NULL); + gAudioCurrentDevice = 0; + } else { + Mixer_Init(gConfigSound.device); + for (int i = 0; i < gAudioDeviceCount; i++) { + if (strcmp(gAudioDevices[i].name, gConfigSound.device) == 0) { + gAudioCurrentDevice = i; + } + } + } } void audio_quit() @@ -296,6 +307,15 @@ void audio_stop_ride_music() } } +void audio_stop_all_music_and_sounds() +{ + audio_stop_title_music(); + audio_stop_vehicle_sounds(); + audio_stop_ride_music(); + audio_stop_crowd_sound(); + audio_stop_rain_sound(); +} + void audio_stop_crowd_sound() { audio_stop_channel(&gCrowdSoundChannel); diff --git a/src/audio/audio.h b/src/audio/audio.h index ca530c2d87..77a289a3a9 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -281,4 +281,6 @@ void audio_toggle_all_sounds(); */ void audio_unpause_sounds(); +void audio_stop_all_music_and_sounds(); + #endif diff --git a/src/editor.c b/src/editor.c index 30f725d341..c6f9c267c1 100644 --- a/src/editor.c +++ b/src/editor.c @@ -84,8 +84,7 @@ void editor_load() { rct_window *mainWindow; - audio_pause_sounds(); - audio_unpause_sounds(); + audio_stop_all_music_and_sounds(); object_manager_unload_all_objects(); object_list_load(); map_init(150); @@ -168,6 +167,7 @@ void trackdesigner_load() { rct_window *mainWindow; + audio_stop_all_music_and_sounds(); gScreenFlags = SCREEN_FLAGS_TRACK_DESIGNER; gScreenAge = 0; @@ -207,6 +207,7 @@ void trackmanager_load() { rct_window *mainWindow; + audio_stop_all_music_and_sounds(); gScreenFlags = SCREEN_FLAGS_TRACK_MANAGER; gScreenAge = 0; diff --git a/src/game.c b/src/game.c index 8788b2e70d..fc332dc45a 100644 --- a/src/game.c +++ b/src/game.c @@ -289,7 +289,6 @@ void game_update() // Update the game one or more times for (i = 0; i < numUpdates; i++) { game_logic_update(); - audio_start_title_music(); if (gGameSpeed > 1) continue; @@ -853,6 +852,7 @@ void game_load_init() rct_window *mainWindow; gScreenFlags = SCREEN_FLAGS_PLAYING; + audio_stop_all_music_and_sounds(); viewport_init_all(); game_create_windows(); mainWindow = window_get_main(); diff --git a/src/openrct2.c b/src/openrct2.c index c7ccbe3d06..0579ccf3bc 100644 --- a/src/openrct2.c +++ b/src/openrct2.c @@ -233,6 +233,13 @@ bool openrct2_initialise() // return false; // } + if (!rct2_init_directories()) { + return false; + } + if (!rct2_startup_checks()) { + return false; + } + if (!gOpenRCT2Headless) { audio_init(); audio_populate_devices(); @@ -260,20 +267,6 @@ bool openrct2_initialise() chat_init(); openrct2_copy_original_user_files_over(); - - // TODO move to audio initialise function - if (str_is_null_or_empty(gConfigSound.device)) { - Mixer_Init(NULL); - gAudioCurrentDevice = 0; - } else { - Mixer_Init(gConfigSound.device); - for (int i = 0; i < gAudioDeviceCount; i++) { - if (strcmp(gAudioDevices[i].name, gConfigSound.device) == 0) { - gAudioCurrentDevice = i; - } - } - } - return true; } diff --git a/src/rct2.c b/src/rct2.c index ba521e4a56..0effa52fe1 100644 --- a/src/rct2.c +++ b/src/rct2.c @@ -151,11 +151,6 @@ bool rct2_init() gScenarioTicks = 0; util_srand((unsigned int)time(0)); - if (!rct2_init_directories()) - return false; - - if (!rct2_startup_checks()) - return false; config_reset_shortcut_keys(); config_shortcut_keys_load(); diff --git a/src/rct2.h b/src/rct2.h index fc25e97275..6ba631454a 100644 --- a/src/rct2.h +++ b/src/rct2.h @@ -288,6 +288,8 @@ extern char gRCT2AddressObjectDataPath[]; extern char gRCT2AddressTracksPath[]; bool rct2_init(); +int rct2_init_directories(); +int rct2_startup_checks(); void rct2_dispose(); void rct2_update(); void substitute_path(char *dest, const char *path, const char *filename); diff --git a/src/scenario.c b/src/scenario.c index 4ba785c2eb..fa536bfe92 100644 --- a/src/scenario.c +++ b/src/scenario.c @@ -14,6 +14,7 @@ *****************************************************************************/ #pragma endregion +#include "audio/audio.h" #include "cheats.h" #include "config.h" #include "game.h" @@ -150,7 +151,10 @@ void scenario_begin() { rct_window *mainWindow; + audio_stop_title_music(); + gScreenFlags = SCREEN_FLAGS_PLAYING; + audio_stop_all_music_and_sounds(); viewport_init_all(); game_create_windows(); mainWindow = window_get_main(); diff --git a/src/title.c b/src/title.c index 3972c7a272..db5244ff1a 100644 --- a/src/title.c +++ b/src/title.c @@ -125,15 +125,14 @@ void title_load() window_staff_list_init_vars(); map_update_tile_pointers(); reset_sprite_spatial_index(); - audio_stop_ride_music(); - audio_stop_crowd_sound(); - //stop_other_sounds(); + audio_stop_all_music_and_sounds(); viewport_init_all(); news_item_init_queue(); window_main_open(); title_create_windows(); title_init_showcase(); gfx_invalidate_screen(); + audio_start_title_music(); gScreenAge = 0; if (gOpenRCT2ShowChangelog) { @@ -529,7 +528,6 @@ void title_update() for (i = 0; i < numUpdates; i++) { game_logic_update(); } - audio_start_title_music(); } gInputFlags &= ~INPUT_FLAG_VIEWPORT_SCROLLING;