[Super-tux-commit] supertux/src music_manager.cpp,NONE,1.1 music_manager.h,NONE,1.1 musicref.cpp,NON
Brought to you by:
wkendrick
From: Ingo R. <gr...@us...> - 2004-04-24 14:49:18
|
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17682 Modified Files: Makefile.am gameloop.cpp level.cpp level.h resources.cpp resources.h setup.cpp sound.cpp sound.h title.cpp world.cpp worldmap.cpp worldmap.h Added Files: music_manager.cpp music_manager.h musicref.cpp musicref.h Log Message: - patch from MatzeB to make music handling easier and more stable Index: resources.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/resources.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- resources.h 22 Apr 2004 21:24:31 -0000 1.7 +++ resources.h 24 Apr 2004 14:49:04 -0000 1.8 @@ -20,7 +20,10 @@ #ifndef SUPERTUX_RESOURCES_H #define SUPERTUX_RESOURCES_H +#include "musicref.h" + class SpriteManager; +class MusicManager; extern Surface* img_waves[3]; extern Surface* img_water; @@ -32,7 +35,10 @@ extern Surface* img_super_bkgd; extern Surface* img_red_glow; +extern MusicRef herring_song; + extern SpriteManager* sprite_manager; +extern MusicManager* music_manager; void loadshared(); void unloadshared(); Index: Makefile.am =================================================================== RCS file: /cvsroot/super-tux/supertux/src/Makefile.am,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- Makefile.am 17 Apr 2004 10:48:03 -0000 1.18 +++ Makefile.am 24 Apr 2004 14:49:03 -0000 1.19 @@ -69,6 +69,10 @@ sprite.h \ sprite.cpp \ sprite_manager.cpp \ -sprite_manager.h +sprite_manager.h \ +music_manager.cpp \ +music_manager.h \ +musicref.cpp \ +musicref.h # EOF # Index: sound.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/sound.cpp,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- sound.cpp 22 Apr 2004 21:51:19 -0000 1.12 +++ sound.cpp 24 Apr 2004 14:49:04 -0000 1.13 @@ -24,9 +24,9 @@ #include "setup.h" /*global variable*/ -bool use_sound; /* handle sound on/off menu and command-line option */ -bool use_music; /* handle music on/off menu and command-line option */ -bool audio_device; /* != 0: available and initialized */ +bool use_sound = true; /* handle sound on/off menu and command-line option */ +bool use_music = true; /* handle music on/off menu and command-line option */ +bool audio_device = true; /* != 0: available and initialized */ int current_music; char * soundfilenames[NUM_SOUNDS] = { @@ -55,8 +55,6 @@ #include <SDL_mixer.h> Mix_Chunk * sounds[NUM_SOUNDS]; -Mix_Music * herring_song = 0; -Mix_Music * current_song = 0; /* --- OPEN THE AUDIO DEVICE --- */ @@ -158,47 +156,3 @@ } } -void halt_music(void) -{ - if (!use_music || !audio_device) - return; - - Mix_HaltMusic(); - current_song = 0; -} - - -void play_music(Mix_Music *music) -{ - if (!audio_device) - return; - if(music == current_song) - return; - - if (use_music && Mix_PlayMusic(music, -1) < 0) - st_abort("Couldn't play music: ", Mix_GetError()); - - current_song = music; -} - - -void free_music(Mix_Music *music) -{ - if(!audio_device) - return; - - Mix_FreeMusic( music ); -} - -void enable_music(bool enable) -{ - if(!audio_device) - return; - - use_music = enable; - if(!use_music) - Mix_HaltMusic(); - else - Mix_PlayMusic(current_song, -1); -} - Index: worldmap.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/worldmap.cpp,v retrieving revision 1.45 retrieving revision 1.46 diff -u -d -r1.45 -r1.46 --- worldmap.cpp 24 Apr 2004 09:32:32 -0000 1.45 +++ worldmap.cpp 24 Apr 2004 14:49:04 -0000 1.46 @@ -28,6 +28,7 @@ #include "gameloop.h" #include "setup.h" #include "worldmap.h" +#include "resources.h" namespace WorldMapNS { @@ -283,7 +284,6 @@ name = "<no file>"; music = "SALCON.MOD"; - song = 0; load_map(); } @@ -555,8 +555,6 @@ level->y == tux->get_tile_pos().y) { std::cout << "Enter the current level: " << level->name << std::endl;; - halt_music(); - GameSession session(datadir + "levels/" + level->name, 1, ST_GL_LOAD_LEVEL_FILE); @@ -582,7 +580,7 @@ break; } - play_music(song); + music_manager->play_music(song); Menu::set_current(0); if (!savegame_file.empty()) savegame(savegame_file); @@ -725,11 +723,8 @@ quit = false; - song = load_song(datadir + "/music/" + music); - if(!song) - st_abort("Couldn't load song ", music.c_str()); - - play_music(song); + song = music_manager->load_music(datadir + "/music/" + music); + music_manager->play_music(song); while(!quit) { Point tux_pos = tux->get_pos(); @@ -758,9 +753,6 @@ SDL_Delay(20); } - - halt_music(); - free_music(song); } void Index: sound.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/sound.h,v retrieving revision 1.25 retrieving revision 1.26 diff -u -d -r1.25 -r1.26 --- sound.h 22 Apr 2004 21:51:19 -0000 1.25 +++ sound.h 24 Apr 2004 14:49:04 -0000 1.26 @@ -78,7 +78,6 @@ /* variables for stocking the sound and music */ extern Mix_Chunk* sounds[NUM_SOUNDS]; -extern Mix_Music* herring_song; /* functions handling the sound and music */ int open_audio(int frequency, Uint16 format, int channels, int chunksize); @@ -88,10 +87,4 @@ void free_chunk(Mix_Chunk*chunk); void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker); -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*/ --- NEW FILE: music_manager.cpp --- // $Id: music_manager.cpp,v 1.1 2004/04/24 14:49:03 grumbel Exp $ // // SuperTux // Copyright (C) 2004 Ingo Ruhnke <gr...@gm...> // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include <assert.h> #include "music_manager.h" #include "musicref.h" #include "sound.h" #include "setup.h" MusicManager::MusicManager() : music_enabled(true) { } MusicManager::~MusicManager() { if(audio_device) Mix_HaltMusic(); } MusicRef MusicManager::load_music(const std::string& file) { if(!audio_device) return MusicRef(0); if(!exists_music(file)) st_abort("Couldn't load musicfile ", file.c_str()); std::map<std::string, MusicResource>::iterator i = musics.find(file); assert(i != musics.end()); return MusicRef(& (i->second)); } bool MusicManager::exists_music(const std::string& file) { if(!audio_device) return true; // song already loaded? std::map<std::string, MusicResource>::iterator i = musics.find(file); if(i != musics.end()) { return true; } Mix_Music* song = Mix_LoadMUS(file.c_str()); if(song == 0) return false; // insert into music list std::pair<std::map<std::string, MusicResource>::iterator, bool> result = musics.insert( std::make_pair<std::string, MusicResource> (file, MusicResource())); MusicResource& resource = result.first->second; resource.manager = this; resource.music = song; return true; } void MusicManager::free_music(MusicResource* ) { // TODO free music, currently we can't do this since SDL_mixer seems to have // some bugs if you load/free alot of mod files. } void MusicManager::play_music(const MusicRef& musicref) { if(!audio_device) return; if(musicref.music == 0 || current_music == musicref.music) return; if(current_music) current_music->refcount--; current_music = musicref.music; current_music->refcount++; if(music_enabled) Mix_PlayMusic(current_music->music, -1); } void MusicManager::halt_music() { if(!audio_device) return; Mix_HaltMusic(); if(current_music) { current_music->refcount--; if(current_music->refcount == 0) free_music(current_music); current_music = 0; } } void MusicManager::enable_music(bool enable) { if(!audio_device) return; if(enable == music_enabled) return; music_enabled = enable; if(music_enabled == false) { Mix_HaltMusic(); } else { Mix_PlayMusic(current_music->music, -1); } } MusicManager::MusicResource::~MusicResource() { // buggy SDL_mixer :-/ // Mix_FreeMusic(music); } Index: worldmap.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/worldmap.h,v retrieving revision 1.21 retrieving revision 1.22 diff -u -d -r1.21 -r1.22 --- worldmap.h 21 Apr 2004 11:17:31 -0000 1.21 +++ worldmap.h 24 Apr 2004 14:49:04 -0000 1.22 @@ -23,7 +23,7 @@ #include <vector> #include <string> -#include <SDL_mixer.h> +#include "musicref.h" namespace WorldMapNS { @@ -153,7 +153,7 @@ typedef std::vector<Level> Levels; Levels levels; - Mix_Music* song; + MusicRef song; Direction input_direction; bool enter_level; Index: gameloop.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/gameloop.cpp,v retrieving revision 1.98 retrieving revision 1.99 diff -u -d -r1.98 -r1.99 --- gameloop.cpp 24 Apr 2004 09:32:31 -0000 1.98 +++ gameloop.cpp 24 Apr 2004 14:49:03 -0000 1.99 @@ -53,6 +53,7 @@ #include "tile.h" #include "particlesystem.h" #include "resources.h" +#include "music_manager.h" GameSession* GameSession::current_ = 0; @@ -141,6 +142,8 @@ void GameSession::levelintro(void) { + music_manager->halt_music(); + char str[60]; /* Level Intro: */ clearscreen(0, 0, 0); @@ -380,7 +383,7 @@ { end_sequenze = true; last_x_pos = -1; - halt_music(); + music_manager->halt_music(); } else { --- NEW FILE: musicref.cpp --- // $Id: musicref.cpp,v 1.1 2004/04/24 14:49:03 grumbel Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2000 Bill Kendrick <bi...@ne...> // Copyright (C) 2004 Matthias Braun <ma...@br...> // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "musicref.h" MusicRef::MusicRef() : music(0) { } MusicRef::MusicRef(MusicManager::MusicResource* newmusic) : music(newmusic) { if(music) music->refcount++; } MusicRef::~MusicRef() { if(music) { music->refcount--; if(music->refcount == 0) music->manager->free_music(music); } } MusicRef::MusicRef(const MusicRef& other) : music(other.music) { if(music) music->refcount++; } MusicRef& MusicRef::operator =(const MusicRef& other) { MusicManager::MusicResource* oldres = music; music = other.music; if(music) music->refcount++; if(oldres) { oldres->refcount--; if(oldres->refcount == 0) music->manager->free_music(music); } return *this; } Index: setup.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/setup.cpp,v retrieving revision 1.52 retrieving revision 1.53 diff -u -d -r1.52 -r1.53 --- setup.cpp 24 Apr 2004 09:32:32 -0000 1.52 +++ setup.cpp 24 Apr 2004 14:49:04 -0000 1.53 @@ -49,6 +49,7 @@ #include "configfile.h" #include "scene.h" #include "worldmap.h" +#include "resources.h" #include "player.h" @@ -537,10 +538,7 @@ use_sound = !use_sound; break; case MNID_MUSIC: - if(use_music != options_menu->item[MNID_MUSIC].toggled) - { - enable_music(options_menu->item[MNID_MUSIC].toggled); - } + music_manager->enable_music(options_menu->item[MNID_MUSIC].toggled); break; case MNID_SHOWFPS: if(show_fps != options_menu->item[MNID_SHOWFPS].toggled) --- NEW FILE: musicref.h --- // $Id: musicref.h,v 1.1 2004/04/24 14:49:04 grumbel Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Matthias Braun <ma...@br...> // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef HEADER_MUSIC_RESOURCE_H #define HEADER_MUSIC_RESOURCE_H #include "music_manager.h" /** This class holds a reference to a music file and maintains a correct * refcount for that file. */ class MusicRef { public: MusicRef(); MusicRef(const MusicRef& other); ~MusicRef(); MusicRef& operator= (const MusicRef& other); private: friend class MusicManager; MusicRef(MusicManager::MusicResource* music); MusicManager::MusicResource* music; }; #endif Index: level.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/level.h,v retrieving revision 1.36 retrieving revision 1.37 diff -u -d -r1.36 -r1.37 --- level.h 24 Apr 2004 12:11:10 -0000 1.36 +++ level.h 24 Apr 2004 14:49:03 -0000 1.37 @@ -25,6 +25,7 @@ #include "texture.h" #include "badguy.h" #include "lispreader.h" +#include "musicref.h" class Tile; @@ -68,8 +69,8 @@ { public: Surface* img_bkgd; - Mix_Music* level_song; - Mix_Music* level_song_fast; + MusicRef level_song; + MusicRef level_song_fast; std::string name; std::string author; @@ -119,8 +120,8 @@ void load_song(); void free_song(); - Mix_Music* get_level_music(); - Mix_Music* get_level_music_fast(); + MusicRef get_level_music(); + MusicRef get_level_music_fast(); void save(const char* subset, int level); --- NEW FILE: music_manager.h --- // $Id: music_manager.h,v 1.1 2004/04/24 14:49:03 grumbel Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2000 Bill Kendrick <bi...@ne...> // Copyright (C) 2004 Duong-Khang NGUYEN <neo...@us...> // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef HEADER_MUSIC_MANAGER_H #define HEADER_MUSIC_MANAGER_H #include <SDL_mixer.h> #include <string> #include <map> class MusicRef; /** This class manages a list of music resources and is responsible for playing * the music. */ class MusicManager { public: MusicManager(); ~MusicManager(); MusicRef load_music(const std::string& file); bool exists_music(const std::string& filename); void play_music(const MusicRef& music); void halt_music(); void enable_music(bool enable); private: friend class MusicRef; class MusicResource { public: ~MusicResource(); MusicManager* manager; Mix_Music* music; int refcount; }; void free_music(MusicResource* music); std::map<std::string, MusicResource> musics; MusicResource* current_music; bool music_enabled; }; #endif Index: title.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/title.cpp,v retrieving revision 1.58 retrieving revision 1.59 diff -u -d -r1.58 -r1.59 --- title.cpp 24 Apr 2004 09:32:32 -0000 1.58 +++ title.cpp 24 Apr 2004 14:49:04 -0000 1.59 @@ -139,7 +139,6 @@ { index -= 1; // FIXME: Hack std::cout << "Sarting level: " << index << std::endl; - halt_music(); GameSession session(current_contrib_subset, index, ST_GL_PLAY); session.run(); Menu::set_current(main_menu); @@ -308,7 +307,6 @@ generate_contrib_menu(); break; case MNID_LEVELEDITOR: - halt_music(); leveleditor(1); Menu::set_current(main_menu); break; Index: world.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/world.cpp,v retrieving revision 1.39 retrieving revision 1.40 diff -u -d -r1.39 -r1.40 --- world.cpp 22 Apr 2004 19:15:22 -0000 1.39 +++ world.cpp 24 Apr 2004 14:49:04 -0000 1.40 @@ -76,7 +76,6 @@ for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i) delete *i; - halt_music(); // just to be sure (because levelmusic is freed now) delete level; } @@ -428,16 +427,16 @@ currentmusic = musictype; switch(currentmusic) { case HURRYUP_MUSIC: - ::play_music(get_level()->get_level_music_fast()); + music_manager->play_music(get_level()->get_level_music_fast()); break; case LEVEL_MUSIC: - ::play_music(get_level()->get_level_music()); + music_manager->play_music(get_level()->get_level_music()); break; case HERRING_MUSIC: - ::play_music(herring_song); + music_manager->play_music(herring_song); break; default: - ::halt_music(); + music_manager->halt_music(); break; } } Index: level.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/level.cpp,v retrieving revision 1.47 retrieving revision 1.48 diff -u -d -r1.47 -r1.48 --- level.cpp 24 Apr 2004 12:11:10 -0000 1.47 +++ level.cpp 24 Apr 2004 14:49:03 -0000 1.48 @@ -31,6 +31,8 @@ #include "scene.h" #include "tile.h" #include "lispreader.h" +#include "resources.h" +#include "music_manager.h" using namespace std; @@ -199,18 +201,18 @@ } Level::Level() - : img_bkgd(0), level_song(0), level_song_fast(0) + : img_bkgd(0) { } Level::Level(const std::string& subset, int level) - : img_bkgd(0), level_song(0), level_song_fast(0) + : img_bkgd(0) { load(subset, level); } Level::Level(const std::string& filename) - : img_bkgd(0), level_song(0), level_song_fast(0) + : img_bkgd(0) { load(filename); } @@ -218,7 +220,6 @@ Level::~Level() { free_gfx(); - free_song(); } void @@ -698,29 +699,13 @@ } } -void -Level::free_song(void) -{ - if(level_song_fast != level_song) { - free_music(level_song_fast); - level_song_fast = 0; - } - - free_music(level_song); - level_song = 0; -} - void Level::load_song() { - free_song(); - char* song_path; char* song_subtitle; - level_song = ::load_song(datadir + "/music/" + song_title); - if(!level_song) - st_abort("Couldn't load song: " , song_title.c_str()); + level_song = music_manager->load_music(datadir + "/music/" + song_title); song_path = (char *) malloc(sizeof(char) * datadir.length() + strlen(song_title.c_str()) + 8 + 5); @@ -728,21 +713,22 @@ strcpy(strstr(song_subtitle, "."), "\0"); sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(), song_subtitle, strstr(song_title.c_str(), ".")); - level_song_fast = ::load_song(song_path); - if(!level_song_fast) { + if(!music_manager->exists_music(song_path)) { level_song_fast = level_song; + } else { + level_song_fast = music_manager->load_music(song_path); } free(song_subtitle); free(song_path); } -Mix_Music* +MusicRef Level::get_level_music() { return level_song; } -Mix_Music* +MusicRef Level::get_level_music_fast() { return level_song_fast; Index: resources.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/resources.cpp,v retrieving revision 1.20 retrieving revision 1.21 diff -u -d -r1.20 -r1.21 --- resources.cpp 24 Apr 2004 13:12:12 -0000 1.20 +++ resources.cpp 24 Apr 2004 14:49:04 -0000 1.21 @@ -36,7 +36,10 @@ Surface* img_red_glow; +MusicRef herring_song; + SpriteManager* sprite_manager = 0; +MusicManager* music_manager = 0; /* Load graphics/sounds shared between all levels: */ void loadshared() @@ -44,6 +47,7 @@ int i; sprite_manager = new SpriteManager(datadir + "/supertux.strf"); + music_manager = new MusicManager(); /* Tuxes: */ smalltux_star = sprite_manager->load("smalltux-star"); @@ -270,17 +274,13 @@ sounds[i] = load_sound(datadir + soundfilenames[i]); /* Herring song */ - herring_song = load_song(datadir + "/music/SALCON.MOD"); - if(!herring_song) - st_abort("Couldn't load song ", "/music/SALCON.MOD"); + herring_song = music_manager->load_music(datadir + "/music/SALCON.MOD"); } /* Free shared data: */ void unloadshared(void) { - delete sprite_manager; - int i; free_special_gfx(); @@ -330,8 +330,10 @@ for (i = 0; i < NUM_SOUNDS; i++) free_chunk(sounds[i]); - /* free the herring song */ - free_music( herring_song ); + delete sprite_manager; + sprite_manager = 0; + delete music_manager; + music_manager = 0; } /* EOF */ |