Merge pull request #674 from jyrkive/mute-in-background

Mute the music when the game loses focus
This commit is contained in:
Charles Dang 2016-07-30 16:49:21 +11:00 committed by GitHub
commit 56e2a733e5
8 changed files with 89 additions and 0 deletions

View file

@ -61,6 +61,40 @@
[/grid]
#enddef
#define _GUI_AUTO_MUTE_MUSIC_CHECKBOX
[row]
[column]
horizontal_grow = true
[grid]
[row]
[column]
{_GUI_PREFERENCES_CHECKBOX_ALIGN_SPACER}
[/column]
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_grow = true
[toggle_button]
id = sound_toggle_stop_music_in_background
label = _ "Pause music on focus loss"
tooltip = _ "Pause the music when you switch to any other window"
[/toggle_button]
[/column]
[column]
grow_factor = 1
[spacer]
[/spacer]
[/column]
[/row]
[/grid]
[/column]
[/row]
#enddef
#define _GUI_PREFERENCES_SOUND_GRID
[row]
[column]
@ -84,6 +118,8 @@
[/column]
[/row]
{_GUI_AUTO_MUTE_MUSIC_CHECKBOX}
[row]
[column]
horizontal_grow = true
@ -121,4 +157,5 @@
[/layer]
#undef _GUI_PREFERENCES_SOUND_GRID
#under _GUI_AUTO_MUTE_MUSIC_CHECKBOX
#undef _GUI_SOUND_SLIDER_CONTROL

View file

@ -111,6 +111,7 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts, const char
main_event_context_(),
hotkey_manager_(),
music_thinker_(),
music_muter_(),
test_scenario_("test"),
screenshot_map_(),
screenshot_filename_(),

View file

@ -113,6 +113,7 @@ private:
const events::event_context main_event_context_;
const hotkey::manager hotkey_manager_;
sound::music_thinker music_thinker_;
sound::music_muter music_muter_;
std::string test_scenario_;

View file

@ -622,6 +622,11 @@ void tpreferences::initialize_members(twindow& window)
music_on(), music_volume(),
bind_void(set_music, _1), set_music_volume, window);
setup_single_toggle("sound_toggle_stop_music_in_background",
stop_music_in_background(),
set_stop_music_in_background,
window);
/* TURN BELL */
setup_toggle_slider_pair("sound_toggle_bell", "sound_volume_bell",
turn_bell(), bell_volume(),

View file

@ -680,6 +680,16 @@ bool set_music(bool ison) {
return true;
}
bool stop_music_in_background()
{
return get("stop_music_in_background", false);
}
void set_stop_music_in_background(bool ison)
{
preferences::set("stop_music_in_background", ison);
}
namespace {
double scroll = 0.2;
}

View file

@ -127,6 +127,9 @@ namespace preferences {
int music_volume();
void set_music_volume(int vol);
bool stop_music_in_background();
void set_stop_music_in_background(bool ison);
bool turn_bell();
bool set_turn_bell(bool ison);

View file

@ -618,6 +618,30 @@ void music_thinker::process(events::pump_info &info) {
}
}
music_muter::music_muter() :
events::sdl_handler(false)
{
join_global();
}
void music_muter::handle_window_event(const SDL_Event& event)
{
if (preferences::stop_music_in_background() && preferences::music_on())
{
if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED)
{
Mix_ResumeMusic();
}
else if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST)
{
if (Mix_PlayingMusic())
{
Mix_PauseMusic();
}
}
}
}
void commit_music_changes()
{
played_before.clear();

View file

@ -85,6 +85,14 @@ class music_thinker : public events::pump_monitor {
void process(events::pump_info &info);
};
// A class to mute music when the game is in background
class music_muter : public events::sdl_handler {
public:
music_muter();
void handle_event(const SDL_Event&) override {}
void handle_window_event(const SDL_Event& event) override;
};
// Save music playlist for snapshot
void write_music_play_list(config& snapshot);