Advanced Strategic Command
soundList.cpp
Go to the documentation of this file.
1 
5 /*
6  This file is part of Advanced Strategic Command; http://www.asc-hq.de
7  Copyright (C) 1994-2010 Martin Bickel
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; see the file COPYING. If not, write to the
21  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22  Boston, MA 02111-1307 USA
23 */
24 
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 
29 #ifndef _DOS_
30  #include "global.h"
31  #include <SDL.h>
32 #endif
33 
34 #include "soundList.h"
35 #include "basestrm.h"
36 #include "sgstream.h"
37 #include "stringtokenizer.h"
38 #include "textfiletags.h"
39 
40 SoundList* SoundList::instance = NULL;
41 
42 
44  : sound( snd ), active ( _active )
45 {
46  displayLogMessage ( 10, " Instantiating SoundLoopManager \n" ); // : sound = %x, active = %d \n", int(snd), int(_active) );
47 }
48 
49 
50 void SoundLoopManager :: activate ( int dummy )
51 {
52  if ( !active && sound ) {
53  sound->playLoop();
54  displayLogMessage ( 10, " SoundLoopManager: playing sound\n" );
55  }
56  active = true;
57 }
58 
59 
61 {
62  if ( !instance )
63  fatalError("SoundList::getInstance() - Soundlist not initialized");
64 
65  return *instance;
66 }
67 
68 
70 {
71  if ( instance )
72  fatalError("SoundList::init() - Soundlist already initialized");
73 
74  static SoundList myList;
75  instance = &myList;
76  displayLogMessage ( 4, " SoundList::init() : starting initialize ..." );
77  instance->initialize ( );
78  displayLogMessage ( 4, " completed\n" );
79 
80 }
81 
82 void SoundList::readLine( PropertyContainer& pc, const ASCString& name, SoundList::Sample sample, int subtype )
83 {
84  vector<ASCString> labels;
85  vector<ASCString> files;
86  if ( pc.find ( name + ".files" ))
87  pc.addStringArray ( name + ".files", files );
88  if ( pc.find ( name + ".labels" ))
89  pc.addStringArray ( name + ".labels", labels );
90 
91  SoundAssignment s;
92  if ( files.size() && !files[0].empty() )
93  s.defaultSound = getSound( files[0] );
94  else
95  s.defaultSound = NULL;
96 
97  s.sample = sample;
98  s.subType = subtype;
99 
100  for( int i = 0; i < labels.size() && i < files.size() ; i++ )
101  s.snd[ copytoLower(labels[i]) ] = getSound( files[i] );
102 
103  soundAssignments.push_back ( s );
104 }
105 
106 void SoundList::initialize( )
107 {
108  TextPropertyGroup* tpg = NULL;
109  {
110  tnfilestream s ( "sounds.asctxt", tnstream::reading );
111 
112  TextFormatParser tfp ( &s );
113  tpg = tfp.run();
114  }
115  auto_ptr<TextPropertyGroup> atpg ( tpg );
116 
117  PropertyReadingContainer pc ( "sounds", tpg );
118 
119  pc.openBracket("shoot");
121  readLine( pc, "MACHINEGUN", SoundList::shooting, 6 );
123  readLine( pc, "CRUISEMISSILE", SoundList::shooting, 0 );
125  readLine( pc, "MINE", SoundList::shooting, 1 );
127  readLine( pc, "BOMB", SoundList::shooting, 2 );
129  readLine( pc, "LARGEMISSILE", SoundList::shooting, 3 );
131  readLine( pc, "SMALLMISSILE", SoundList::shooting, 4 );
133  readLine( pc, "TORPEDO", SoundList::shooting, 5 );
135  readLine( pc, "CANNON", SoundList::shooting, 7 );
137  readLine( pc, "LASER", SoundList::shooting, 10);
138  pc.closeBracket();
139 
141  pc.openBracket("move");
142  readLine( pc, "default", SoundList::moving, 0 );
144  readLine( pc, "LIGHT_TRACKED_VEHICLE", SoundList::moving, 1 );
146  readLine( pc, "MEDIUM_TRACKED_VEHICLE", SoundList::moving, 2 );
148  readLine( pc, "HEAVY_TRACKED_VEHICLE", SoundList::moving, 3 );
150  readLine( pc, "LIGHT_WHEELED_VEHICLE", SoundList::moving, 4 );
152  readLine( pc, "MEDIUM_WHEELED_VEHICLE", SoundList::moving, 5 );
154  readLine( pc, "HEAVY_WHEELED_VEHICLE", SoundList::moving, 6 );
156  readLine( pc, "TROOPER", SoundList::moving, 7 );
158  readLine( pc, "RAIL_VEHICLE", SoundList::moving, 8 );
160  readLine( pc, "MEDIUM_AIRCRAFT", SoundList::moving, 9 );
162  readLine( pc, "MEDIUM_SHIP", SoundList::moving, 10 );
164  readLine( pc, "TURRET", SoundList::moving, 11 );
166  readLine( pc, "LIGHT_AIRCRAFT", SoundList::moving, 12 );
168  readLine( pc, "HEAVY_AIRCRAFT", SoundList::moving, 13 );
170  readLine( pc, "LIGHT_SHIP", SoundList::moving, 14 );
172  readLine( pc, "HEAVY_SHIP", SoundList::moving, 15 );
174  readLine( pc, "HELICOPTER", SoundList::moving, 16 );
176  pc.closeBracket();
177 
178  pc.openBracket("UserInterface");
179  readLine( pc, "ACKNOWLEDGE", SoundList::menu_ack );
180  pc.closeBracket();
182  readLine( pc, "CONQUER_BUILDING", SoundList::conquer_building );
184  readLine( pc, "UNIT_EXPLODES", SoundList::unitExplodes );
186  readLine( pc, "BUILDING_COLLAPSES", SoundList::buildingCollapses );
188  readLine( pc, "REFUEL", SoundList::refuel );
190  readLine( pc, "REPAIR", SoundList::repair );
192  readLine( pc, "JUMPDRIVE", SoundList::jumpdrive );
193 
194 }
195 
196 Sound* SoundList::getSound( const ASCString& filename )
197 {
198  if ( SoundSystem::getInstance()->isOff() )
199  return NULL;
200 
201  displayLogMessage ( 5, " SoundList::getSound(1) : trying to acquire handle for sound %s \n", filename.c_str() );
202 
203  if ( soundFiles.find ( filename ) == soundFiles.end() ) {
204  displayLogMessage ( 5, " Sound has not been loaded ...\n" );
205 
206  Sound* s;
207  if( filename.find(':') != ASCString::npos ) {
208  ASCString primary = filename.substr( 0, filename.find(':'));
209  ASCString secondary = filename.substr( filename.find(':')+1 );
210  displayLogMessage ( 8, " this is a multipart sound\n" );
211  s = new Sound( primary, secondary );
212  } else {
213  s = new Sound ( filename );
214  }
215 
217 
218 
219  soundFiles[filename] = s;
220  if ( s != NULL )
221  displayLogMessage ( 5, " loading sound completed\n" );
222  else
223  displayLogMessage ( 5, " loading sound failed\n" );
224 
225  return s;
226  } else
227  return soundFiles[filename];
228 }
229 
230 
231 Sound* SoundList::getSound( Sample snd, int subType, const ASCString& label, int height )
232 {
233  if ( SoundSystem::getInstance()->isOff() )
234  return NULL;
235 
236  if ( label.find ( "." ) != ASCString::npos ) {
237  return getSound ( label );
238  } else {
239  ASCString newlabel = copytoLower(label);
240 
241  for ( vector<SoundAssignment>::iterator i = soundAssignments.begin(); i != soundAssignments.end(); i++ )
242  if ( snd == i->sample && subType == i->subType ) {
243  if ( height >= 0 && height <= 7 )
244  if ( i->snd.find( heightTags[height] ) != i->snd.end() ) {
245  displayLogMessage ( 10, ASCString(" SoundList::getSound(2) : heightlabel ") + heightTags[height] + " found, returning matching sound \n" );
246  return i->snd[heightTags[height]];
247  }
248 
249  if ( newlabel.empty() || i->snd.find( newlabel ) == i->snd.end() ) {
250  displayLogMessage ( 10, " SoundList::getSound(2) : label " + label + " not found, returning default sound \n" );
251  if ( !i->defaultSound )
252  displayLogMessage ( 10, " SoundList::getSound(2) : no default sound registered !!!! \n" );
253  return i->defaultSound;
254  } else {
255  displayLogMessage ( 10, " SoundList::getSound(2) : label " + label + " found, returning matching sound \n" );
256  return i->snd[newlabel];
257  }
258  }
259  }
260 
261  displayLogMessage ( 10, " SoundList::getSound(2) : sound not found, returning NULL \n" );
262  return NULL;
263 }
264 
265 Sound* SoundList::playSound( Sample snd, int subType , bool looping, const ASCString& label )
266 {
267  Sound* sound = getSound ( snd, subType, label );
268  if ( !sound )
269  return NULL;
270 
271  if ( looping )
272  sound->playLoop();
273  else
274  sound->play();
275 
276  return sound;
277 }
278 
280 {
281  for ( SoundFiles::iterator i = soundFiles.begin(); i != soundFiles.end(); i++ )
282  delete i->second;
283 }
284 
void play(void)
Definition: sound.cpp:453
Definition: sound.h:20
sigc::signal< void > dataLoaderTicker
static SoundList & getInstance()
Definition: soundList.cpp:60
This class provides all the sounds required by the games user interface.
Definition: soundList.h:35
void activate(int dummy)
Definition: soundList.cpp:50
Global platform dependant definitions. This file just branches to the platform specific files in thei...
SoundLoopManager(Sound *snd, bool _active=true)
Definition: soundList.cpp:43
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
static void init()
Definition: soundList.cpp:69
void addStringArray(const ASCString &name, vector< ASCString > &property)
Sound * getSound(Sample snd, int subType=0, const ASCString &label="", int height=-1)
Definition: soundList.cpp:231
static SoundSystem * getInstance()
Definition: sound.h:127
A simple string tokenizer.
void displayLogMessage(int msgVerbosity, const char *message,...)
The collection of all sounds used by ASC.
The interface for the various streams that ASC offers, like file and memory streams.
Sound(const ASCString &filename, int fadeIn=0)
Create a Sound from the .wav file specified by filename.
Definition: sound.cpp:384
const char * heightTags[choehenstufennum]
The IO for many basic classes and structurs of ACS.
Parses a .ASCTXT file and returns a TextPropertyGroup.
Sound * playSound(Sample snd, int subType=0, bool looping=false, const ASCString &label="")
Definition: soundList.cpp:265
Class that stores all the (preparsed) entries of an .ASCTXT file.
virtual void closeBracket()
void playLoop()
Definition: sound.cpp:462
virtual void openBracket(const ASCString &name)
ASCString copytoLower(const ASCString &s)
bool find(const ASCString &name)
void fatalError(const ASCString &string)