00001 #include "SdlArs.h" 00002 00003 namespace Ars 00004 { 00005 static int cnt = 0; 00006 00007 Sounds::Sounds( void ) 00008 : loop( 0 ) , ticks( -1 ) , channel( 0 ) , wave( 0 ) , id( ++cnt ) 00009 { 00010 } 00011 00012 Sounds::~Sounds( void ) 00013 { 00014 if( wave ) 00015 Mix_FreeChunk( wave ); 00016 wave = 0; 00017 } 00018 00019 void Sounds::Configure( const int aChannel , const int nr , const int aTicks ) 00020 { 00021 if( nr != -2 ) 00022 loop = nr; 00023 if( aChannel != -2 ) 00024 channel = aChannel; 00025 if( aTicks != -2 ) 00026 ticks = aTicks; 00027 } 00028 00029 void Sounds::Play( const int aChannel , const int nr , const int aTicks ) 00030 { 00031 Configure( aChannel , nr , aTicks ); 00032 if( wave ) 00033 Mix_PlayChannelTimed( channel , wave , loop , ticks ); 00034 } 00035 00036 const bool Sounds::Load( const string &aSection , const string &akey , string aCfgFile ) 00037 { 00038 if( aCfgFile.empty() ) 00039 aCfgFile = Cfg::GetCfg().GetCfgFile( "Paths" , "sounds" ); 00040 Cfg::Handler &h = Cfg::GetCfg( aCfgFile ); 00041 00042 return Load( h.GetCfgFile( aSection , akey ) ); 00043 } 00044 00045 const bool Sounds::Load( const string &aFilename ) 00046 { 00047 if( wave ) 00048 Mix_FreeChunk( wave ); 00049 00050 wave = Mix_LoadWAV( aFilename.c_str() ); 00051 return wave != 0; 00052 } 00053 00054 std::map<icstring , Sounds *> soundList; 00055 00056 Sounds emptySound; 00057 00058 Sounds &GetSound( const icstring &name ) 00059 { 00060 std::map<icstring , Sounds *>::iterator pos = soundList.find( name ); 00061 00062 if( pos == soundList.end() ) 00063 return emptySound; 00064 return *pos->second; 00065 } 00066 Sounds &AddSound( const icstring &name , const string &aSection , const string &akey , string aCfgFile ) 00067 { 00068 if( GetSound( name ) == emptySound ) 00069 { 00070 Sounds *s = new Sounds(); 00071 s->Load( aSection , akey , aCfgFile ); 00072 soundList.insert( make_pair( name , s ) ); 00073 } 00074 return GetSound( name ); 00075 } 00076 00077 Sounds &AddSound( const icstring &name , const string &aFilename ) 00078 { 00079 if( !GetSound( name ) ) 00080 { 00081 Sounds *s = new Sounds(); 00082 s->Load( aFilename ); 00083 soundList.insert( make_pair( name , s ) ); 00084 } 00085 return GetSound( name ); 00086 } 00087 00088 void UnloadSounds( void ) 00089 { 00090 for( std::map<icstring , Sounds *>::iterator it = soundList.begin() ; it != soundList.end() ; ++it ) 00091 delete it->second; 00092 soundList.clear(); 00093 } 00094 00095 void Preload( const icstring &aSection ) 00096 { 00097 Cfg::Handler &h = Cfg::GetCfg( "@sounds" ); 00098 Rsrc res( Cfg::GetCfg().GetCfgString( "Paths" , "Sounds" ) ); 00099 string path = h.GetPath(); 00100 00101 icstring tmp = icstring( aSection.c_str() ); 00102 tmp = res.FindCombination( tmp ); 00103 icstring snd = strip( tmp ); 00104 00105 while( !snd.empty() ) 00106 { 00107 icstring sndDef = Rsrc::GetList( snd ); 00108 if( !sndDef.empty() ) 00109 { 00110 icstring name = Rsrc::GetId( sndDef ); 00111 string fname = path + Rsrc::GetStr( sndDef ).c_str(); 00112 AddSound( name , fname ); 00113 } 00114 } 00115 } 00116 00117 }