Fade out and pause music on death and resume on restart of level, fixes #1064
authorTobias Markus <tobbi@mozilla-uk.org>
Mon, 16 Mar 2015 20:51:35 +0000 (21:51 +0100)
committerTobias Markus <tobbi@mozilla-uk.org>
Mon, 16 Mar 2015 20:52:31 +0000 (21:52 +0100)
src/audio/sound_manager.cpp
src/audio/sound_manager.hpp
src/audio/stream_sound_source.cpp
src/audio/stream_sound_source.hpp
src/object/player.cpp
src/supertux/game_session.cpp
src/supertux/game_session.hpp
src/supertux/sector.cpp
src/supertux/sector.hpp

index 3d38f6c..ace96c1 100644 (file)
@@ -301,19 +301,25 @@ SoundManager::play_music(const std::string& filename, bool fade)
 }
 
 void
-SoundManager::pause_music()
+SoundManager::pause_music(float fadetime)
 {
-  if(music_source)
-  {
+  if(fadetime > 0) {
+    if(music_source
+       && music_source->get_fade_state() != StreamSoundSource::FadingPause)
+      music_source->set_fading(StreamSoundSource::FadingPause, fadetime);
+  } else {
     music_source->pause();
   }
 }
 
 void
-SoundManager::resume_music()
+SoundManager::resume_music(float fadetime)
 {
-  if(music_source)
-  {
+  if(fadetime > 0) {
+    if(music_source
+       && music_source->get_fade_state() != StreamSoundSource::FadingResume)
+      music_source->set_fading(StreamSoundSource::FadingResume, fadetime);
+  } else {
     music_source->resume();
   }
 }
index 801b029..7056b0d 100644 (file)
@@ -64,8 +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 pause_music(float fadetime = 0);
+  void resume_music(float fadetime = 0);
   void stop_music(float fadetime = 0);
 
   bool is_music_enabled() { return music_enabled; }
index e813b7d..e51a2e5 100644 (file)
@@ -79,7 +79,7 @@ StreamSoundSource::update()
     play();
   }
 
-  if(fade_state == FadingOn) {
+  if(fade_state == FadingOn || fade_state == FadingResume) {
     float time = real_time - fade_start_time;
     if(time >= fade_time) {
       set_gain(1.0);
@@ -87,10 +87,13 @@ StreamSoundSource::update()
     } else {
       set_gain(time / fade_time);
     }
-  } else if(fade_state == FadingOff) {
+  } else if(fade_state == FadingOff || fade_state == FadingPause) {
     float time = real_time - fade_start_time;
     if(time >= fade_time) {
-      stop();
+      if(fade_state == FadingOff)
+        stop();
+      else
+        pause();
       fade_state = NoFading;
     } else {
       set_gain( (fade_time-time) / fade_time);
index d840066..9e8ac9a 100644 (file)
@@ -29,7 +29,7 @@ public:
 
   void set_sound_file(std::unique_ptr<SoundFile> newfile);
 
-  enum FadeState { NoFading, FadingOn, FadingOff };
+  enum FadeState { NoFading, FadingOn, FadingOff, FadingPause, FadingResume };
 
   void set_fading(FadeState state, float fadetime);
   FadeState get_fade_state() const
index ce1c8ce..93c1094 100644 (file)
@@ -1526,7 +1526,7 @@ Player::kill(bool completely)
 
     // TODO: need nice way to handle players dying in co-op mode
     Sector::current()->effect->fade_out(3.0);
-    SoundManager::current()->stop_music(3.0);
+    SoundManager::current()->pause_music(3.0);
   }
 }
 
index 57e48e4..da864e2 100644 (file)
@@ -82,7 +82,7 @@ GameSession::GameSession(const std::string& levelfile_, Savegame& savegame, Stat
 }
 
 int
-GameSession::restart_level()
+GameSession::restart_level(bool after_death)
 {
     PlayerStatus* currentStatus = m_savegame.get_player_status();
     coins_at_start = currentStatus->coins;
@@ -130,9 +130,13 @@ GameSession::restart_level()
     ScreenManager::current()->pop_screen();
     return (-1);
   }
-
-  SoundManager::current()->stop_music();
-  currentsector->play_music(LEVEL_MUSIC);
+  if(after_death == true) {
+    currentsector->resume_music();
+  }
+  else {
+    SoundManager::current()->stop_music();
+    currentsector->play_music(LEVEL_MUSIC);
+  }
 
   if(capture_file != "") {
     int newSeed=0;               // next run uses a new seed
@@ -371,7 +375,7 @@ GameSession::check_end_conditions()
   if(end_sequence && end_sequence->is_done()) {
     finish(true);
   } else if (!end_sequence && tux->is_dead()) {
-    restart_level();
+    restart_level(true);
   }
 }
 
index 4609f8b..0499e2d 100644 (file)
@@ -79,7 +79,7 @@ public:
    * resources for the current level/world
    */
   std::string get_working_directory();
-  int restart_level();
+  int restart_level(bool after_death = false);
 
   void toggle_pause();
   void abort_level();
index c9879c9..146db5a 100644 (file)
@@ -1497,6 +1497,12 @@ Sector::play_music(MusicType type)
   }
 }
 
+void
+Sector::resume_music()
+{
+  SoundManager::current()->resume_music(1.5f);
+}
+
 MusicType
 Sector::get_music_type()
 {
index 885a817..e21d1d4 100644 (file)
@@ -108,6 +108,7 @@ public:
   bool inside(const Rectf& rectangle) const;
 
   void play_music(MusicType musictype);
+  void resume_music();
   MusicType get_music_type();
 
   int get_active_bullets()