Pause the music when the game loses focus
This commit is contained in:
parent
41145a783c
commit
bf510e7886
4 changed files with 34 additions and 0 deletions
|
@ -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_(),
|
||||
|
|
|
@ -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_;
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue