Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

soundList.cpp

Go to the documentation of this file.
00001 
00005 /*
00006     This file is part of Advanced Strategic Command; http://www.asc-hq.de
00007     Copyright (C) 1994-2005  Martin Bickel  
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU General Public License for more details.
00018 
00019     You should have received a copy of the GNU General Public License
00020     along with this program; see the file COPYING. If not, write to the
00021     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00022     Boston, MA  02111-1307  USA
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" ); // : sound = %x, active = %d \n", int(snd), int(_active) );
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    instance = new SoundList;
00075    displayLogMessage ( 4, "  SoundList::init() : starting initialize ..." );
00076    instance->initialize ( );
00077    displayLogMessage ( 4, "  completed\n" );
00078 
00079 }
00080 
00081 void SoundList::readLine( PropertyContainer& pc, const ASCString& name, SoundList::Sample sample, int subtype )
00082 {
00083    vector<ASCString> labels;
00084    vector<ASCString> files;
00085    if ( pc.find ( name + ".files" ))
00086       pc.addStringArray ( name + ".files", files );
00087    if ( pc.find ( name + ".labels" ))
00088       pc.addStringArray ( name + ".labels", labels );
00089 
00090    SoundAssignment s;
00091    if ( files.size() && !files[0].empty() )
00092       s.defaultSound = getSound( files[0] );
00093    else
00094       s.defaultSound = NULL;
00095 
00096    s.sample = sample;
00097    s.subType = subtype;
00098 
00099    for( int i = 0; i < labels.size() && i < files.size() ; i++ )
00100       s.snd[ copytoLower(labels[i]) ] = getSound( files[i] );
00101 
00102    soundAssignments.push_back ( s );
00103 }
00104 
00105 void SoundList::initialize(  )
00106 {
00107    TextPropertyGroup* tpg = NULL;
00108    {
00109       tnfilestream s ( "sounds.asctxt", tnstream::reading );
00110 
00111       TextFormatParser tfp ( &s );
00112       tpg = tfp.run();
00113    }
00114    auto_ptr<TextPropertyGroup> atpg ( tpg );
00115 
00116    PropertyReadingContainer pc ( "sounds", tpg );
00117 
00118    pc.openBracket("shoot");
00119    dataLoaderTicker();
00120     readLine( pc, "MACHINEGUN", SoundList::shooting, 6 );
00121    dataLoaderTicker();
00122     readLine( pc, "CRUISEMISSILE", SoundList::shooting, 0 );
00123    dataLoaderTicker();
00124     readLine( pc, "MINE", SoundList::shooting, 1 );
00125    dataLoaderTicker();
00126     readLine( pc, "BOMB", SoundList::shooting, 2 );
00127    dataLoaderTicker();
00128     readLine( pc, "LARGEMISSILE", SoundList::shooting, 3 );
00129    dataLoaderTicker();
00130     readLine( pc, "SMALLMISSILE", SoundList::shooting, 4 );
00131    dataLoaderTicker();
00132     readLine( pc, "TORPEDO", SoundList::shooting, 5 );
00133    dataLoaderTicker();
00134     readLine( pc, "CANNON", SoundList::shooting, 7 );
00135    dataLoaderTicker();
00136     readLine( pc, "LASER", SoundList::shooting, 10);
00137    pc.closeBracket();
00138 
00139    dataLoaderTicker();
00140    pc.openBracket("move");
00141     readLine( pc, "default", SoundList::moving, 0 );
00142    dataLoaderTicker();
00143     readLine( pc, "LIGHT_TRACKED_VEHICLE", SoundList::moving, 1 );
00144    dataLoaderTicker();
00145     readLine( pc, "MEDIUM_TRACKED_VEHICLE", SoundList::moving, 2 );
00146    dataLoaderTicker();
00147     readLine( pc, "HEAVY_TRACKED_VEHICLE", SoundList::moving, 3 );
00148    dataLoaderTicker();
00149     readLine( pc, "LIGHT_WHEELED_VEHICLE", SoundList::moving, 4 );
00150    dataLoaderTicker();
00151     readLine( pc, "MEDIUM_WHEELED_VEHICLE", SoundList::moving, 5 );
00152    dataLoaderTicker();
00153     readLine( pc, "HEAVY_WHEELED_VEHICLE", SoundList::moving, 6 );
00154    dataLoaderTicker();
00155     readLine( pc, "TROOPER", SoundList::moving, 7 );
00156    dataLoaderTicker();
00157     readLine( pc, "RAIL_VEHICLE", SoundList::moving, 8 );
00158    dataLoaderTicker();
00159     readLine( pc, "MEDIUM_AIRCRAFT", SoundList::moving, 9 );
00160    dataLoaderTicker();
00161     readLine( pc, "MEDIUM_SHIP", SoundList::moving, 10 );
00162    dataLoaderTicker();
00163     readLine( pc, "TURRET", SoundList::moving, 11 );
00164    dataLoaderTicker();
00165     readLine( pc, "LIGHT_AIRCRAFT", SoundList::moving, 12 );
00166    dataLoaderTicker();
00167     readLine( pc, "HEAVY_AIRCRAFT", SoundList::moving, 13 );
00168    dataLoaderTicker();
00169     readLine( pc, "LIGHT_SHIP", SoundList::moving, 14 );
00170    dataLoaderTicker();
00171     readLine( pc, "HEAVY_SHIP", SoundList::moving, 15 );
00172    dataLoaderTicker();
00173     readLine( pc, "HELICOPTER", SoundList::moving, 16 );
00174    dataLoaderTicker();
00175    pc.closeBracket();
00176 
00177    pc.openBracket("UserInterface");
00178     readLine( pc, "ACKNOWLEDGE", SoundList::menu_ack );
00179    pc.closeBracket();
00180    dataLoaderTicker();
00181    readLine( pc, "CONQUER_BUILDING", SoundList::conquer_building );
00182    dataLoaderTicker();
00183    readLine( pc, "UNIT_EXPLODES", SoundList::unitExplodes );
00184    dataLoaderTicker();
00185    readLine( pc, "BUILDING_COLLAPSES", SoundList::buildingCollapses );
00186    dataLoaderTicker();
00187    readLine( pc, "REFUEL", SoundList::refuel );
00188    dataLoaderTicker();
00189    readLine( pc, "REPAIR", SoundList::repair );
00190    dataLoaderTicker();
00191    readLine( pc, "JUMPDRIVE", SoundList::jumpdrive );
00192 
00193 }
00194 
00195 Sound* SoundList::getSound( const ASCString& filename )
00196 {
00197    if ( SoundSystem::getInstance()->isOff() )
00198       return NULL;
00199 
00200    displayLogMessage ( 5, " SoundList::getSound(1) : trying to acquire handle for sound %s \n", filename.c_str() );
00201 
00202    if ( soundFiles.find ( filename ) == soundFiles.end() ) {
00203      displayLogMessage ( 5, " Sound has not been loaded ...\n" );
00204 
00205      Sound* s;
00206      if( filename.find(':') != ASCString::npos ) {
00207         ASCString primary = filename.substr( 0, filename.find(':'));
00208         ASCString secondary = filename.substr( filename.find(':')+1 );
00209         displayLogMessage ( 8, " this is a multipart sound\n" );
00210         s = new Sound( primary, secondary );
00211      } else {
00212         s = new Sound ( filename );
00213      }
00214 
00215       dataLoaderTicker();
00216 
00217 
00218      soundFiles[filename] = s;
00219      if ( s != NULL )
00220         displayLogMessage ( 5, " loading sound completed\n" );
00221      else
00222         displayLogMessage ( 5, " loading sound failed\n" );
00223 
00224      return s;
00225   } else
00226      return soundFiles[filename];
00227 }
00228 
00229 
00230 Sound* SoundList::getSound( Sample snd, int subType, const ASCString& label, int height )
00231 {
00232    if ( SoundSystem::getInstance()->isOff() )
00233       return NULL;
00234 
00235    if ( label.find ( "." ) != ASCString::npos ) {
00236       return getSound ( label );
00237    } else {
00238       ASCString newlabel = copytoLower(label);
00239 
00240       for ( vector<SoundAssignment>::iterator i = soundAssignments.begin(); i != soundAssignments.end(); i++ )
00241          if ( snd == i->sample && subType == i->subType ) {
00242             if ( height >= 0 && height <= 7 )
00243                if ( i->snd.find( heightTags[height] ) != i->snd.end() ) {
00244                        displayLogMessage ( 10, ASCString(" SoundList::getSound(2) : heightlabel ") + heightTags[height] + " found, returning matching sound \n" );
00245                   return i->snd[heightTags[height]];
00246                }
00247 
00248             if ( newlabel.empty() || i->snd.find( newlabel ) == i->snd.end() ) {
00249                 displayLogMessage ( 10, " SoundList::getSound(2) : label " + label + " not found, returning default sound \n" );
00250                if ( !i->defaultSound )
00251                 displayLogMessage ( 10, "  SoundList::getSound(2) : no default sound registered !!!! \n" );
00252                return i->defaultSound;
00253             } else {
00254                     displayLogMessage ( 10, " SoundList::getSound(2) : label " + label + " found, returning matching sound \n" );
00255                return i->snd[newlabel];
00256             }
00257          }
00258    }
00259 
00260         displayLogMessage ( 10, " SoundList::getSound(2) : sound not found, returning NULL \n" );
00261    return NULL;
00262 }
00263 
00264 Sound* SoundList::playSound( Sample snd, int subType , bool looping, const ASCString& label  )
00265 {
00266    Sound* sound = getSound ( snd, subType, label );
00267    if ( !sound )
00268       return NULL;
00269 
00270    if ( looping )
00271       sound->playLoop();
00272    else
00273       sound->play();
00274 
00275    return sound;
00276 }
00277 
00278 SoundList::~SoundList()
00279 {
00280    for ( SoundFiles::iterator i = soundFiles.begin(); i != soundFiles.end(); i++ )
00281       delete i->second;
00282 }
00283 

Generated on Tue Jun 24 01:27:52 2008 for Advanced Strategic Command by  doxygen 1.4.2