00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028
00029 #ifndef _DOS_
00030 #include "global.h"
00031 #include <SDL.h>
00032 #endif
00033
00034 #include "soundList.h"
00035 #include "basestrm.h"
00036 #include "sgstream.h"
00037 #include "stringtokenizer.h"
00038 #include "textfiletags.h"
00039
00040 SoundList* SoundList::instance = NULL;
00041
00042
00043 SoundLoopManager :: SoundLoopManager ( Sound* snd, bool _active )
00044 : sound( snd ), active ( _active )
00045 {
00046 displayLogMessage ( 10, " Instantiating SoundLoopManager \n" );
00047 }
00048
00049
00050 void SoundLoopManager :: activate ( int dummy )
00051 {
00052 if ( !active && sound ) {
00053 sound->playLoop();
00054 displayLogMessage ( 10, " SoundLoopManager: playing sound\n" );
00055 }
00056 active = true;
00057 }
00058
00059
00060 SoundList& SoundList::getInstance()
00061 {
00062 if ( !instance )
00063 fatalError("SoundList::getInstance() - Soundlist not initialized");
00064
00065 return *instance;
00066 }
00067
00068
00069 void SoundList::init( )
00070 {
00071 if ( instance )
00072 fatalError("SoundList::init() - Soundlist already initialized");
00073
00074 static SoundList myList;
00075 instance = &myList;
00076 displayLogMessage ( 4, " SoundList::init() : starting initialize ..." );
00077 instance->initialize ( );
00078 displayLogMessage ( 4, " completed\n" );
00079
00080 }
00081
00082 void SoundList::readLine( PropertyContainer& pc, const ASCString& name, SoundList::Sample sample, int subtype )
00083 {
00084 vector<ASCString> labels;
00085 vector<ASCString> files;
00086 if ( pc.find ( name + ".files" ))
00087 pc.addStringArray ( name + ".files", files );
00088 if ( pc.find ( name + ".labels" ))
00089 pc.addStringArray ( name + ".labels", labels );
00090
00091 SoundAssignment s;
00092 if ( files.size() && !files[0].empty() )
00093 s.defaultSound = getSound( files[0] );
00094 else
00095 s.defaultSound = NULL;
00096
00097 s.sample = sample;
00098 s.subType = subtype;
00099
00100 for( int i = 0; i < labels.size() && i < files.size() ; i++ )
00101 s.snd[ copytoLower(labels[i]) ] = getSound( files[i] );
00102
00103 soundAssignments.push_back ( s );
00104 }
00105
00106 void SoundList::initialize( )
00107 {
00108 TextPropertyGroup* tpg = NULL;
00109 {
00110 tnfilestream s ( "sounds.asctxt", tnstream::reading );
00111
00112 TextFormatParser tfp ( &s );
00113 tpg = tfp.run();
00114 }
00115 auto_ptr<TextPropertyGroup> atpg ( tpg );
00116
00117 PropertyReadingContainer pc ( "sounds", tpg );
00118
00119 pc.openBracket("shoot");
00120 dataLoaderTicker();
00121 readLine( pc, "MACHINEGUN", SoundList::shooting, 6 );
00122 dataLoaderTicker();
00123 readLine( pc, "CRUISEMISSILE", SoundList::shooting, 0 );
00124 dataLoaderTicker();
00125 readLine( pc, "MINE", SoundList::shooting, 1 );
00126 dataLoaderTicker();
00127 readLine( pc, "BOMB", SoundList::shooting, 2 );
00128 dataLoaderTicker();
00129 readLine( pc, "LARGEMISSILE", SoundList::shooting, 3 );
00130 dataLoaderTicker();
00131 readLine( pc, "SMALLMISSILE", SoundList::shooting, 4 );
00132 dataLoaderTicker();
00133 readLine( pc, "TORPEDO", SoundList::shooting, 5 );
00134 dataLoaderTicker();
00135 readLine( pc, "CANNON", SoundList::shooting, 7 );
00136 dataLoaderTicker();
00137 readLine( pc, "LASER", SoundList::shooting, 10);
00138 pc.closeBracket();
00139
00140 dataLoaderTicker();
00141 pc.openBracket("move");
00142 readLine( pc, "default", SoundList::moving, 0 );
00143 dataLoaderTicker();
00144 readLine( pc, "LIGHT_TRACKED_VEHICLE", SoundList::moving, 1 );
00145 dataLoaderTicker();
00146 readLine( pc, "MEDIUM_TRACKED_VEHICLE", SoundList::moving, 2 );
00147 dataLoaderTicker();
00148 readLine( pc, "HEAVY_TRACKED_VEHICLE", SoundList::moving, 3 );
00149 dataLoaderTicker();
00150 readLine( pc, "LIGHT_WHEELED_VEHICLE", SoundList::moving, 4 );
00151 dataLoaderTicker();
00152 readLine( pc, "MEDIUM_WHEELED_VEHICLE", SoundList::moving, 5 );
00153 dataLoaderTicker();
00154 readLine( pc, "HEAVY_WHEELED_VEHICLE", SoundList::moving, 6 );
00155 dataLoaderTicker();
00156 readLine( pc, "TROOPER", SoundList::moving, 7 );
00157 dataLoaderTicker();
00158 readLine( pc, "RAIL_VEHICLE", SoundList::moving, 8 );
00159 dataLoaderTicker();
00160 readLine( pc, "MEDIUM_AIRCRAFT", SoundList::moving, 9 );
00161 dataLoaderTicker();
00162 readLine( pc, "MEDIUM_SHIP", SoundList::moving, 10 );
00163 dataLoaderTicker();
00164 readLine( pc, "TURRET", SoundList::moving, 11 );
00165 dataLoaderTicker();
00166 readLine( pc, "LIGHT_AIRCRAFT", SoundList::moving, 12 );
00167 dataLoaderTicker();
00168 readLine( pc, "HEAVY_AIRCRAFT", SoundList::moving, 13 );
00169 dataLoaderTicker();
00170 readLine( pc, "LIGHT_SHIP", SoundList::moving, 14 );
00171 dataLoaderTicker();
00172 readLine( pc, "HEAVY_SHIP", SoundList::moving, 15 );
00173 dataLoaderTicker();
00174 readLine( pc, "HELICOPTER", SoundList::moving, 16 );
00175 dataLoaderTicker();
00176 pc.closeBracket();
00177
00178 pc.openBracket("UserInterface");
00179 readLine( pc, "ACKNOWLEDGE", SoundList::menu_ack );
00180 pc.closeBracket();
00181 dataLoaderTicker();
00182 readLine( pc, "CONQUER_BUILDING", SoundList::conquer_building );
00183 dataLoaderTicker();
00184 readLine( pc, "UNIT_EXPLODES", SoundList::unitExplodes );
00185 dataLoaderTicker();
00186 readLine( pc, "BUILDING_COLLAPSES", SoundList::buildingCollapses );
00187 dataLoaderTicker();
00188 readLine( pc, "REFUEL", SoundList::refuel );
00189 dataLoaderTicker();
00190 readLine( pc, "REPAIR", SoundList::repair );
00191 dataLoaderTicker();
00192 readLine( pc, "JUMPDRIVE", SoundList::jumpdrive );
00193
00194 }
00195
00196 Sound* SoundList::getSound( const ASCString& filename )
00197 {
00198 if ( SoundSystem::getInstance()->isOff() )
00199 return NULL;
00200
00201 displayLogMessage ( 5, " SoundList::getSound(1) : trying to acquire handle for sound %s \n", filename.c_str() );
00202
00203 if ( soundFiles.find ( filename ) == soundFiles.end() ) {
00204 displayLogMessage ( 5, " Sound has not been loaded ...\n" );
00205
00206 Sound* s;
00207 if( filename.find(':') != ASCString::npos ) {
00208 ASCString primary = filename.substr( 0, filename.find(':'));
00209 ASCString secondary = filename.substr( filename.find(':')+1 );
00210 displayLogMessage ( 8, " this is a multipart sound\n" );
00211 s = new Sound( primary, secondary );
00212 } else {
00213 s = new Sound ( filename );
00214 }
00215
00216 dataLoaderTicker();
00217
00218
00219 soundFiles[filename] = s;
00220 if ( s != NULL )
00221 displayLogMessage ( 5, " loading sound completed\n" );
00222 else
00223 displayLogMessage ( 5, " loading sound failed\n" );
00224
00225 return s;
00226 } else
00227 return soundFiles[filename];
00228 }
00229
00230
00231 Sound* SoundList::getSound( Sample snd, int subType, const ASCString& label, int height )
00232 {
00233 if ( SoundSystem::getInstance()->isOff() )
00234 return NULL;
00235
00236 if ( label.find ( "." ) != ASCString::npos ) {
00237 return getSound ( label );
00238 } else {
00239 ASCString newlabel = copytoLower(label);
00240
00241 for ( vector<SoundAssignment>::iterator i = soundAssignments.begin(); i != soundAssignments.end(); i++ )
00242 if ( snd == i->sample && subType == i->subType ) {
00243 if ( height >= 0 && height <= 7 )
00244 if ( i->snd.find( heightTags[height] ) != i->snd.end() ) {
00245 displayLogMessage ( 10, ASCString(" SoundList::getSound(2) : heightlabel ") + heightTags[height] + " found, returning matching sound \n" );
00246 return i->snd[heightTags[height]];
00247 }
00248
00249 if ( newlabel.empty() || i->snd.find( newlabel ) == i->snd.end() ) {
00250 displayLogMessage ( 10, " SoundList::getSound(2) : label " + label + " not found, returning default sound \n" );
00251 if ( !i->defaultSound )
00252 displayLogMessage ( 10, " SoundList::getSound(2) : no default sound registered !!!! \n" );
00253 return i->defaultSound;
00254 } else {
00255 displayLogMessage ( 10, " SoundList::getSound(2) : label " + label + " found, returning matching sound \n" );
00256 return i->snd[newlabel];
00257 }
00258 }
00259 }
00260
00261 displayLogMessage ( 10, " SoundList::getSound(2) : sound not found, returning NULL \n" );
00262 return NULL;
00263 }
00264
00265 Sound* SoundList::playSound( Sample snd, int subType , bool looping, const ASCString& label )
00266 {
00267 Sound* sound = getSound ( snd, subType, label );
00268 if ( !sound )
00269 return NULL;
00270
00271 if ( looping )
00272 sound->playLoop();
00273 else
00274 sound->play();
00275
00276 return sound;
00277 }
00278
00279 SoundList::~SoundList()
00280 {
00281 for ( SoundFiles::iterator i = soundFiles.begin(); i != soundFiles.end(); i++ )
00282 delete i->second;
00283 }
00284