Back to index

supertuxkart  0.5+dfsg1
sound_manager.cpp
Go to the documentation of this file.
00001 //  $Id$
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 Patrick Ammann <pammann@aro.ch>
00005 //  Copyright (C) 2008 Patrick Ammann <pammann@aro.ch>, Joerg Henrichs
00006 //
00007 //  This program is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU General Public License
00009 //  as published by the Free Software Foundation; either version 2
00010 //  of the License, or (at your option) any later version.
00011 //
00012 //  This program is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU General Public License
00018 //  along with this program; if not, write to the Free Software
00019 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 #include <assert.h>
00022 #include <fstream>
00023 
00024 #include "sound_manager.hpp"
00025 #include "user_config.hpp"
00026 #include "string_utils.hpp"
00027 #include "gui/font.hpp"
00028 #include "file_manager.hpp"
00029 
00030 #ifdef __APPLE__
00031 #  include <OpenAL/al.h>
00032 #else
00033 #  include <AL/al.h>
00034 #endif
00035 #include <AL/alut.h>
00036 
00037 #include "music_ogg.hpp"
00038 #include "sfx_openal.hpp"
00039 
00040 #include "translation.hpp"
00041 #if defined(WIN32) && !defined(__CYGWIN__)
00042 #  define strcasecmp _strcmpi
00043 #endif
00044 SoundManager* sound_manager= NULL;
00045 
00046 SoundManager::SoundManager() : m_sfxs(NUM_SOUNDS)
00047 {
00048     m_current_music= NULL;
00049     if(alutInit(0, NULL) == AL_TRUE)  // init OpenAL sound system
00050         m_initialized = true;
00051     else
00052     {
00053         fprintf(stderr, "WARNING: Could not initialize the ALUT based sound.\n");
00054         m_initialized = false;
00055     }
00056 
00057     alGetError(); //Called here to clear any non-important errors found
00058 
00059     if (m_initialized)
00060     {
00061         // must be in sync with enumSoundSFX
00062         m_sfxs[SOUND_UGH          ] = new SFXImpl("ugh.wav");
00063         m_sfxs[SOUND_WINNER       ] = new SFXImpl("radio/grandprix_winner.wav");
00064         m_sfxs[SOUND_GRAB         ] = new SFXImpl("tintagel/grab_collectable.wav");
00065         m_sfxs[SOUND_CRASH        ] = new SFXImpl("tintagel/crash.wav");
00066         m_sfxs[SOUND_SHOT         ] = new SFXImpl("radio/shot.wav");
00067         m_sfxs[SOUND_EXPLOSION    ] = new SFXImpl("explosion.wav");
00068         m_sfxs[SOUND_BZZT         ] = new SFXImpl("bzzt.wav");
00069         m_sfxs[SOUND_BEEP         ] = new SFXImpl("radio/horn.wav");
00070         m_sfxs[SOUND_USE_ANVIL    ] = new SFXImpl("radio/slap.wav");
00071         m_sfxs[SOUND_USE_PARACHUTE] = new SFXImpl("radio/squeaky.wav");
00072         m_sfxs[SOUND_WEE          ] = new SFXImpl("wee.wav"); 
00073         m_sfxs[SOUND_BACK_MENU    ] = new SFXImpl("tintagel/deselect_option.wav");
00074         m_sfxs[SOUND_SELECT_MENU  ] = new SFXImpl("tintagel/select_option.wav"); 
00075         m_sfxs[SOUND_MOVE_MENU    ] = new SFXImpl("tintagel/move_option.wav");
00076         m_sfxs[SOUND_FULL         ] = new SFXImpl("tintagel/energy_bar_full.wav");
00077         m_sfxs[SOUND_PRESTART     ] = new SFXImpl("tintagel/pre_start_race.wav");
00078         m_sfxs[SOUND_START        ] = new SFXImpl("tintagel/start_race.wav");
00079         m_sfxs[SOUND_MISSILE_LOCK ] = new SFXImpl("radio/radarping.wav");
00080     }
00081 
00082     loadMusicInformation();
00083 }  // SoundManager
00084 
00085 //-----------------------------------------------------------------------------
00086 SoundManager::~SoundManager()
00087 {
00088     // SFX cleanup
00089     for(SFXsType::iterator it= m_sfxs.begin(); it != m_sfxs.end(); it++)
00090     {
00091         delete *it;
00092     }
00093     m_sfxs.empty();
00094 
00095     if(m_initialized)
00096     {
00097         alutExit();
00098     }
00099 }   // ~SoundManager
00100 
00101 //-----------------------------------------------------------------------------
00102 void SoundManager::loadMusicInformation()
00103 {
00104     // Load music files from data/music, and dirs defined in SUPERTUXKART_MUSIC_PATH
00105     std::vector<std::string> allMusicDirs=file_manager->getMusicDirs();
00106     for(std::vector<std::string>::iterator dir=allMusicDirs.begin();
00107                                            dir!=allMusicDirs.end(); dir++)
00108     {
00109         loadMusicFromOneDir(*dir);
00110     }   // for dir
00111 }   // loadMusicInformation
00112 
00113 //-----------------------------------------------------------------------------
00114 void SoundManager::loadMusicFromOneDir(const std::string& dir)
00115 {
00116     std::set<std::string> files;
00117     file_manager->listFiles(files, dir, /*is_full_path*/ true,
00118                             /*make_full_path*/ true);
00119     for(std::set<std::string>::iterator i = files.begin(); i != files.end(); ++i)
00120     {
00121         if(StringUtils::extension(*i)!="music") continue;
00122         m_allMusic[StringUtils::basename(*i)] = new MusicInformation(*i);
00123     }   // for i
00124 } // loadMusicFromOneDir
00125 
00126 //-----------------------------------------------------------------------------
00127 void SoundManager::addMusicToTracks()
00128 {
00129     for(std::map<std::string,MusicInformation*>::iterator i=m_allMusic.begin();
00130                                                           i!=m_allMusic.end(); i++)
00131     {
00132         if(!i->second) 
00133         {
00134             fprintf(stderr, "Can't find music file '%s' - ignored.\n",
00135                     i->first.c_str());
00136             continue;
00137         }
00138         i->second->addMusicToTracks();
00139     }
00140 }   // addMusicToTracks
00141 
00142 //-----------------------------------------------------------------------------
00143 MusicInformation* SoundManager::getMusicInformation(const std::string& filename)
00144 {
00145     if(filename=="")
00146     {
00147         return NULL;
00148     }
00149     const std::string basename = StringUtils::basename(filename);
00150     MusicInformation* mi = m_allMusic[basename];
00151     if(!mi)
00152     {
00153         mi = new MusicInformation(filename);
00154         m_allMusic[basename] = mi;
00155     }
00156     return mi;
00157 }   // SoundManager
00158 //-----------------------------------------------------------------------------
00159 void SoundManager::playSfx(unsigned int id)
00160 {
00161     if(!user_config->doSFX() || !m_initialized) return;
00162 
00163     assert(id>=0 && id<m_sfxs.size() && m_sfxs[id]);
00164     m_sfxs[id]->play();
00165 
00166 }   // playSfx
00167 
00168 //-----------------------------------------------------------------------------
00169 void SoundManager::startMusic(MusicInformation* mi)
00170 {
00171     m_current_music = mi;
00172     
00173     if(!mi || !user_config->doMusic() || !m_initialized) return;
00174     
00175     mi->startMusic();
00176 }   // startMusic
00177 
00178 //-----------------------------------------------------------------------------
00179 void SoundManager::stopMusic()
00180 {
00181     if(m_current_music) m_current_music->stopMusic();
00182 }   // stopMusic
00183 
00184 //-----------------------------------------------------------------------------