From: Tobias Markus Date: Sun, 1 Feb 2015 18:53:59 +0000 (+0100) Subject: Pause music when pressing ESC during a level and resume it when exiting the pause... X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=73cb1e168850018eaa821a42b30bb221dc5186fe;p=supertux.git Pause music when pressing ESC during a level and resume it when exiting the pause menu --- diff --git a/src/audio/openal_sound_source.cpp b/src/audio/openal_sound_source.cpp index b30b44521..ceec5bf85 100644 --- a/src/audio/openal_sound_source.cpp +++ b/src/audio/openal_sound_source.cpp @@ -56,6 +56,32 @@ OpenALSoundSource::playing() } void +OpenALSoundSource::pause() +{ + alSourcePause(source); + SoundManager::check_al_error("Couldn't pause audio source: "); +} + +void +OpenALSoundSource::resume() +{ + if( !this->paused() ) + { + return; + } + + this->play(); +} + +bool +OpenALSoundSource::paused() +{ + ALint state = AL_PAUSED; + alGetSourcei(source, AL_SOURCE_STATE, &state); + return state == AL_PAUSED; +} + +void OpenALSoundSource::update() { } diff --git a/src/audio/openal_sound_source.hpp b/src/audio/openal_sound_source.hpp index 8725d59b9..6a14393b9 100644 --- a/src/audio/openal_sound_source.hpp +++ b/src/audio/openal_sound_source.hpp @@ -29,7 +29,10 @@ public: virtual void play(); virtual void stop(); + virtual void pause(); + virtual void resume(); virtual bool playing(); + virtual bool paused(); virtual void update(); diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index d00c93ca1..3d38f6cdb 100644 --- a/src/audio/sound_manager.cpp +++ b/src/audio/sound_manager.cpp @@ -301,6 +301,24 @@ SoundManager::play_music(const std::string& filename, bool fade) } void +SoundManager::pause_music() +{ + if(music_source) + { + music_source->pause(); + } +} + +void +SoundManager::resume_music() +{ + if(music_source) + { + music_source->resume(); + } +} + +void SoundManager::set_listener_position(const Vector& pos) { static Uint32 lastticks = SDL_GetTicks(); diff --git a/src/audio/sound_manager.hpp b/src/audio/sound_manager.hpp index 8c699d4a5..801b02980 100644 --- a/src/audio/sound_manager.hpp +++ b/src/audio/sound_manager.hpp @@ -64,6 +64,8 @@ public: void enable_music(bool music_enabled); void play_music(const std::string& filename, bool fade = false); + void pause_music(); + void resume_music(); void stop_music(float fadetime = 0); bool is_music_enabled() { return music_enabled; } diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index efe4c78d1..d64995b06 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -249,6 +249,7 @@ GameSession::toggle_pause() speed_before_pause = ScreenManager::current()->get_speed(); ScreenManager::current()->set_speed(0); MenuManager::instance().set_menu(MenuStorage::GAME_MENU); + SoundManager::current()->pause_music(); game_pause = true; } @@ -440,6 +441,7 @@ GameSession::update(float elapsed_time) // Unpause the game if the menu has been closed if (game_pause && !MenuManager::instance().is_active()) { ScreenManager::current()->set_speed(speed_before_pause); + SoundManager::current()->resume_music(); game_pause = false; }