Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fade out and pause music on death and resume on restart of level, fixes
  • Loading branch information
tobbi committed Mar 16, 2015
1 parent 96b4e53 commit 8e52a5b
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 19 deletions.
18 changes: 12 additions & 6 deletions src/audio/sound_manager.cpp
Expand Up @@ -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();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/audio/sound_manager.hpp
Expand Up @@ -64,8 +64,8 @@ class SoundManager : public Currenton<SoundManager>

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; }
Expand Down
9 changes: 6 additions & 3 deletions src/audio/stream_sound_source.cpp
Expand Up @@ -79,18 +79,21 @@ 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);
fade_state = NoFading;
} 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);
Expand Down
2 changes: 1 addition & 1 deletion src/audio/stream_sound_source.hpp
Expand Up @@ -29,7 +29,7 @@ class StreamSoundSource : public OpenALSoundSource

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
Expand Down
2 changes: 1 addition & 1 deletion src/object/player.cpp
Expand Up @@ -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);
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/supertux/game_session.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/supertux/game_session.hpp
Expand Up @@ -79,7 +79,7 @@ class GameSession : public Screen,
* 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();
Expand Down
6 changes: 6 additions & 0 deletions src/supertux/sector.cpp
Expand Up @@ -1497,6 +1497,12 @@ Sector::play_music(MusicType type)
}
}

void
Sector::resume_music()
{
SoundManager::current()->resume_music(1.5f);
}

MusicType
Sector::get_music_type()
{
Expand Down
1 change: 1 addition & 0 deletions src/supertux/sector.hpp
Expand Up @@ -108,6 +108,7 @@ class Sector : public scripting::SSector,
bool inside(const Rectf& rectangle) const;

void play_music(MusicType musictype);
void resume_music();
MusicType get_music_type();

int get_active_bullets()
Expand Down

6 comments on commit 8e52a5b

@HybridDog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if I'm in on the overworld, then fall into a cave and the music changes and then I die and respawn on the overworld?
Do I then hear the cave music at the wrong place?

@tobbi
Copy link
Member Author

@tobbi tobbi commented on 8e52a5b Mar 17, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting use case. I will check....

@tobbi
Copy link
Member Author

@tobbi tobbi commented on 8e52a5b Mar 17, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this is currently the case. This needs fixing!

@tobbi
Copy link
Member Author

@tobbi tobbi commented on 8e52a5b Mar 17, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix is in 589f20b and should be synchronized in a few hours.

@HybridDog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks
I really like that music doesn't start from the beginning the whole time.

If I press ESC, the music begins very quiet after respawning although it didn't become so quiet before respawning because by pressing ESC you can respawn earlier.

@tobbi
Copy link
Member Author

@tobbi tobbi commented on 8e52a5b Mar 20, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah, I will put this on the TODO list as well. Don't know what to do, probably save the last volume and re-use it somehow. But I'll think of something.

Please sign in to comment.