Pause music when pressing ESC during a level and resume it when exiting the pause...
authorTobias Markus <tobbi@mozilla-uk.org>
Sun, 1 Feb 2015 18:53:59 +0000 (19:53 +0100)
committerTobias Markus <tobbi@mozilla-uk.org>
Sun, 1 Feb 2015 18:53:59 +0000 (19:53 +0100)
src/audio/openal_sound_source.cpp
src/audio/openal_sound_source.hpp
src/audio/sound_manager.cpp
src/audio/sound_manager.hpp
src/supertux/game_session.cpp

index b30b445..ceec5bf 100644 (file)
@@ -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()
 {
 }
index 8725d59..6a14393 100644 (file)
@@ -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();
 
index d00c93c..3d38f6c 100644 (file)
@@ -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();
index 8c699d4..801b029 100644 (file)
@@ -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; }
index efe4c78..d64995b 100644 (file)
@@ -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;
   }