Pause the music when the game loses focus

This commit is contained in:
Jyrki Vesterinen 2016-06-30 12:58:18 +03:00
parent 41145a783c
commit bf510e7886
4 changed files with 34 additions and 0 deletions

View file

@ -112,6 +112,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

@ -114,6 +114,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

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