From: Ingo Ruhnke Date: Wed, 21 Apr 2004 23:38:14 +0000 (+0000) Subject: - music patch from MatzeB, should fix crash on level exit too worldmap X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=ebd0b704b1b446edad786135877ae74d5670949a;p=supertux.git - music patch from MatzeB, should fix crash on level exit too worldmap SVN-Revision: 619 --- diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 229e8fdea..2b689b8e5 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -122,7 +122,8 @@ GameSession::restart_level() } time_left.init(true); - start_timers(); + start_timers(); + world->play_music(LEVEL_MUSIC); } GameSession::~GameSession() @@ -484,9 +485,6 @@ GameSession::run() clearscreen(0, 0, 0); updatescreen(); - /* Play music: */ - play_current_music(); - // Eat unneeded events SDL_Event event; while (SDL_PollEvent(&event)) {} @@ -557,19 +555,25 @@ GameSession::run() } /* Handle time: */ - if (time_left.check()) + if (!time_left.check() && tux->dying == DYING_NOT) + tux->kill(KILL); + + /* Handle music: */ + if(tux->invincible_timer.check()) { - /* are we low on time ? */ - if (time_left.get_left() < TIME_WARNING - && (get_current_music() != HURRYUP_MUSIC)) /* play the fast music */ - { - set_current_music(HURRYUP_MUSIC); - play_current_music(); - } + if(world->get_music_type() != HERRING_MUSIC) + world->play_music(HERRING_MUSIC); } - else if(tux->dying == DYING_NOT) + /* are we low on time ? */ + else if (time_left.get_left() < TIME_WARNING + && (world->get_music_type() == LEVEL_MUSIC)) { - tux->kill(KILL); + world->play_music(HURRYUP_MUSIC); + } + /* or just normal music? */ + else if(world->get_music_type() != LEVEL_MUSIC) + { + world->play_music(LEVEL_MUSIC); } /* Calculate frames per second */ @@ -586,11 +590,8 @@ GameSession::run() } } - halt_music(); - - world->get_level()->free_gfx(); - world->get_level()->cleanup(); - world->get_level()->free_song(); + delete world; + world = 0; return exit_status; } diff --git a/src/level.cpp b/src/level.cpp index f722ede55..f0700c91a 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -199,19 +199,28 @@ void st_subset::free() } Level::Level() + : level_song(0), level_song_fast(0) { } Level::Level(const std::string& subset, int level) + : level_song(0), level_song_fast(0) { load(subset, level); } Level::Level(const std::string& filename) + : level_song(0), level_song_fast(0) { load(filename); } +Level::~Level() +{ + free_gfx(); + free_song(); +} + void Level::init_defaults() { @@ -688,12 +697,16 @@ void Level::free_song(void) { free_music(level_song); + level_song = 0; free_music(level_song_fast); + level_song_fast = 0; } void Level::load_song() { + free_song(); + char* song_path; char* song_subtitle; @@ -710,6 +723,18 @@ Level::load_song() free(song_path); } +Mix_Music* +Level::get_level_music() +{ + return level_song; +} + +Mix_Music* +Level::get_level_music_fast() +{ + return level_song_fast; +} + unsigned int Level::gettileid(float x, float y) { diff --git a/src/level.h b/src/level.h index 6ed8f36e6..9be4fd9af 100644 --- a/src/level.h +++ b/src/level.h @@ -68,6 +68,8 @@ class Level { public: Surface* img_bkgd; + Mix_Music* level_song; + Mix_Music* level_song_fast; std::string name; std::string author; @@ -95,6 +97,7 @@ class Level Level(); Level(const std::string& subset, int level); Level(const std::string& filename); + ~Level(); /** Will the Level structure with default values */ void init_defaults(); @@ -115,6 +118,8 @@ class Level void load_song(); void free_song(); + Mix_Music* get_level_music(); + Mix_Music* get_level_music_fast(); void save(const char* subset, int level); diff --git a/src/player.cpp b/src/player.cpp index 7fb220605..6b4db8269 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -276,36 +276,8 @@ Player::action(double frame_ratio) } } - /* ---- DONE HANDLING TUX! --- */ - /* Handle invincibility timer: */ - if (get_current_music() == HERRING_MUSIC && !invincible_timer.check()) - { - /* - no, we are no more invincible - or we were not in invincible mode - but are we in hurry ? - */ - - // FIXME: Move this to gamesession - if (GameSession::current()->time_left.get_left() < TIME_WARNING) - { - /* yes, we are in hurry - stop the herring_song, prepare to play the correct - fast level_song ! - */ - set_current_music(HURRYUP_MUSIC); - } - else - { - set_current_music(LEVEL_MUSIC); - } - - /* start playing it */ - play_current_music(); - } - // check some timers skidding_timer.check(); invincible_timer.check(); diff --git a/src/scene.cpp b/src/scene.cpp index d78df474d..de44f220e 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -25,7 +25,8 @@ PlayerStatus player_status; PlayerStatus::PlayerStatus() : score(0), distros(0), - lives(3) + lives(3), + score_multiplier(1) { } diff --git a/src/setup.cpp b/src/setup.cpp index 3cf3a9186..6aa3991ca 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -520,22 +520,7 @@ void process_options_menu(void) case 5: if(use_music != options_menu->item[5].toggled) { - if(use_music) - { - if(playing_music()) - { - halt_music(); - } - use_music = false; - } - else - { - use_music = true; - if (!playing_music()) - { - play_current_music(); - } - } + enable_music(options_menu->item[5].toggled); } break; case 6: @@ -1000,6 +985,7 @@ void parseargs(int argc, char * argv[]) /* Disable the compiled in sound feature */ printf("Sounds disabled \n"); use_sound = false; + audio_device = false; } else if (strcmp(argv[i], "--disable-music") == 0) { diff --git a/src/sound.cpp b/src/sound.cpp index f5dd9150c..340d01934 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -54,7 +54,8 @@ char * soundfilenames[NUM_SOUNDS] = { #include Mix_Chunk * sounds[NUM_SOUNDS]; -Mix_Music * level_song, * level_song_fast, * herring_song; +Mix_Music * herring_song = 0; +Mix_Music * current_song = 0; /* --- OPEN THE AUDIO DEVICE --- */ @@ -113,13 +114,17 @@ Mix_Chunk * load_sound(const std::string& file) Mix_Music * load_song(const std::string& file) { + if(!audio_device) + return 0; + Mix_Music * sng; sng = Mix_LoadMUS(file.c_str()); /* printf message and abort if there is an initialized audio device */ - if ((sng == NULL) && audio_device) + if (sng == NULL) st_abort("Can't load", file); + return (sng); } @@ -161,88 +166,42 @@ void free_chunk(Mix_Chunk *chunk) } } - -int playing_music(void) +void halt_music(void) { - if (use_music) - { - return Mix_PlayingMusic(); - } - else - { - /* we are in --disable-music we can't be playing music */ - return 0; - } + if (!use_music || !audio_device) + return; + + Mix_HaltMusic(); + current_song = 0; } -int halt_music(void) +void play_music(Mix_Music *music) { - if (use_music && audio_device) - { - return Mix_HaltMusic(); - } - else - { - return 0; - } -} + if (!audio_device) + return; + if (use_music && Mix_PlayMusic(music, -1) < 0) + st_abort("Couldn't play music: ", Mix_GetError()); -int play_music(Mix_Music *music, int loops) -{ - if (use_music && audio_device) - { - DEBUG_MSG(__PRETTY_FUNCTION__); - return Mix_PlayMusic(music, loops); - } - else - { - /* return error since you're trying to play music in --disable-sound mode */ - return -1; - } + current_song = music; } void free_music(Mix_Music *music) { - if ( music != NULL ) - { - DEBUG_MSG(__PRETTY_FUNCTION__); - Mix_FreeMusic( music ); - music = NULL; - } + Mix_FreeMusic( music ); } - int get_current_music() - { - return current_music; - } - - void set_current_music(int music) - { - current_music = music; - } - - void play_current_music() - { - if(playing_music()) - halt_music(); - - switch(current_music) - { - case LEVEL_MUSIC: - play_music(level_song, -1); // -1 to play forever - break; - case HERRING_MUSIC: - play_music(herring_song, -1); - break; - case HURRYUP_MUSIC: - play_music(level_song_fast, -1); - break; - case NO_MUSIC: // keep the compiler happy for the moment :-) - {} - /*default:*/ - } - /* use halt_music whenever you want to stop it */ + +void enable_music(bool enable) +{ + if(!audio_device) + return; + + use_music = enable; + if(!use_music) + Mix_HaltMusic(); + else + Mix_PlayMusic(current_song, -1); } diff --git a/src/sound.h b/src/sound.h index fec9de7a9..095e3c4d6 100644 --- a/src/sound.h +++ b/src/sound.h @@ -77,8 +77,6 @@ extern char* soundfilenames[NUM_SOUNDS]; /* variables for stocking the sound and music */ extern Mix_Chunk* sounds[NUM_SOUNDS]; -extern Mix_Music* level_song; -extern Mix_Music* level_song_fast; extern Mix_Music* herring_song; /* functions handling the sound and music */ @@ -86,17 +84,13 @@ int open_audio(int frequency, Uint16 format, int channels, int chunksize); void close_audio( void ); Mix_Chunk * load_sound(const std::string& file); -void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker); -Mix_Music * load_song(const std::string& file); - -int playing_music(void); -int halt_music(void); -int play_music(Mix_Music*music, int loops); -void free_music(Mix_Music*music); void free_chunk(Mix_Chunk*chunk); +void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker); -int get_current_music(); -void set_current_music(int music); -void play_current_music(); +Mix_Music* load_song(const std::string& file); +void free_music(Mix_Music* music); +void halt_music(void); +void enable_music(bool enable); +void play_music(Mix_Music* music); #endif /*SUPERTUX_SOUND_H*/ diff --git a/src/special.cpp b/src/special.cpp index 227251270..0bb4a1ae9 100644 --- a/src/special.cpp +++ b/src/special.cpp @@ -311,12 +311,7 @@ Upgrade::collision(void* p_c_object, int c_object) { play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER); pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME); - /* play the herring song ^^ */ - if (get_current_music() != HURRYUP_MUSIC) - { - set_current_music(HERRING_MUSIC); - play_current_music(); - } + World::current()->play_music(HERRING_MUSIC); } else if (kind == UPGRADE_1UP) { diff --git a/src/world.cpp b/src/world.cpp index e9c5bb194..4b6b62e57 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -68,10 +68,13 @@ World::World(const std::string& subset, int level_nr) activate_bad_guys(); activate_particle_systems(); get_level()->load_song(); + + play_music(LEVEL_MUSIC); } World::~World() { + halt_music(); // just to be sure (because levelmusic is freed now) delete level; } @@ -87,7 +90,7 @@ World::set_defaults() distro_counter = 0; /* set current song/music */ - set_current_music(LEVEL_MUSIC); + currentmusic = LEVEL_MUSIC; } void @@ -409,6 +412,32 @@ World::add_bullet(float x, float y, float xm, Direction dir) play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER); } +void +World::play_music(int musictype) +{ + currentmusic = musictype; + switch(currentmusic) { + case HURRYUP_MUSIC: + ::play_music(get_level()->get_level_music_fast()); + break; + case LEVEL_MUSIC: + ::play_music(get_level()->get_level_music()); + break; + case HERRING_MUSIC: + ::play_music(herring_song); + break; + default: + ::halt_music(); + break; + } +} + +int +World::get_music_type() +{ + return currentmusic; +} + /* Break a brick: */ void World::trybreakbrick(float x, float y, bool small) diff --git a/src/world.h b/src/world.h index ea2761023..ddbedd48f 100644 --- a/src/world.h +++ b/src/world.h @@ -55,6 +55,8 @@ class World int distro_counter; bool counting_distros; + int currentmusic; + static World* current_; public: static World* current() { return current_; } @@ -73,6 +75,10 @@ class World void draw(); void action(double frame_ratio); + void play_music(int musictype); + int get_music_type(); + + /** Checks for all possible collisions. And calls the collision_handlers, which the collision_objects provide for this case (or not). */ diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 7e3f00c52..24bc53b5b 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -582,7 +582,7 @@ WorldMap::update() break; } - play_music(song, 1); + play_music(song); Menu::set_current(0); if (!savegame_file.empty()) savegame(savegame_file); @@ -726,7 +726,7 @@ WorldMap::display() quit = false; song = load_song(datadir + "/music/" + music); - play_music(song, 1); + play_music(song); while(!quit) { Point tux_pos = tux->get_pos();