doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Sound.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code 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 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code 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 Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #include "../idlib/precompiled.h"
30 #pragma hdrstop
31 
32 #include "Game_local.h"
33 
34 /*
35 ===============================================================================
36 
37  SOUND
38 
39 ===============================================================================
40 */
41 
42 const idEventDef EV_Speaker_On( "On", NULL );
43 const idEventDef EV_Speaker_Off( "Off", NULL );
44 const idEventDef EV_Speaker_Timer( "<timer>", NULL );
45 
47  EVENT( EV_Activate, idSound::Event_Trigger )
48  EVENT( EV_Speaker_On, idSound::Event_On )
49  EVENT( EV_Speaker_Off, idSound::Event_Off )
50  EVENT( EV_Speaker_Timer, idSound::Event_Timer )
52 
53 
54 /*
55 ================
56 idSound::idSound
57 ================
58 */
59 idSound::idSound( void ) {
60  lastSoundVol = 0.0f;
61  soundVol = 0.0f;
62  shakeTranslate.Zero();
63  shakeRotate.Zero();
64  random = 0.0f;
65  wait = 0.0f;
66  timerOn = false;
67  playingUntilTime = 0;
68 }
69 
70 /*
71 ================
72 idSound::Save
73 ================
74 */
75 void idSound::Save( idSaveGame *savefile ) const {
76  savefile->WriteFloat( lastSoundVol );
77  savefile->WriteFloat( soundVol );
78  savefile->WriteFloat( random );
79  savefile->WriteFloat( wait );
80  savefile->WriteBool( timerOn );
81  savefile->WriteVec3( shakeTranslate );
82  savefile->WriteAngles( shakeRotate );
83  savefile->WriteInt( playingUntilTime );
84 }
85 
86 /*
87 ================
88 idSound::Restore
89 ================
90 */
91 void idSound::Restore( idRestoreGame *savefile ) {
92  savefile->ReadFloat( lastSoundVol );
93  savefile->ReadFloat( soundVol );
94  savefile->ReadFloat( random );
95  savefile->ReadFloat( wait );
96  savefile->ReadBool( timerOn );
97  savefile->ReadVec3( shakeTranslate );
98  savefile->ReadAngles( shakeRotate );
99  savefile->ReadInt( playingUntilTime );
100 }
101 
102 /*
103 ================
104 idSound::Spawn
105 ================
106 */
107 void idSound::Spawn( void ) {
108  spawnArgs.GetVector( "move", "0 0 0", shakeTranslate );
109  spawnArgs.GetAngles( "rotate", "0 0 0", shakeRotate );
110  spawnArgs.GetFloat( "random", "0", random );
111  spawnArgs.GetFloat( "wait", "0", wait );
112 
113  if ( ( wait > 0.0f ) && ( random >= wait ) ) {
114  random = wait - 0.001;
115  gameLocal.Warning( "speaker '%s' at (%s) has random >= wait", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
116  }
117 
118  soundVol = 0.0f;
119  lastSoundVol = 0.0f;
120 
121  if ( ( shakeRotate != ang_zero ) || ( shakeTranslate != vec3_zero ) ) {
123  }
124 
125  if ( !refSound.waitfortrigger && ( wait > 0.0f ) ) {
126  timerOn = true;
128  } else {
129  timerOn = false;
130  }
131 }
132 
133 /*
134 ================
135 idSound::Event_Trigger
136 
137 this will toggle the idle idSound on and off
138 ================
139 */
140 void idSound::Event_Trigger( idEntity *activator ) {
141  if ( wait > 0.0f ) {
142  if ( timerOn ) {
143  timerOn = false;
145  } else {
146  timerOn = true;
147  DoSound( true );
149  }
150  } else {
151  if ( gameLocal.isMultiplayer ) {
153  DoSound( false );
154  } else {
155  DoSound( true );
156  }
157  } else {
159  DoSound( false );
160  } else {
161  DoSound( true );
162  }
163  }
164  }
165 }
166 
167 /*
168 ================
169 idSound::Event_Timer
170 ================
171 */
172 void idSound::Event_Timer( void ) {
173  DoSound( true );
175 }
176 
177 /*
178 ================
179 idSound::Think
180 ================
181 */
182 void idSound::Think( void ) {
183  idAngles ang;
184 
185  // run physics
186  RunPhysics();
187 
188  // clear out our update visuals think flag since we never call Present
190 }
191 
192 /*
193 ===============
194 idSound::UpdateChangableSpawnArgs
195 ===============
196 */
198 
200 
201  if ( source ) {
202  FreeSoundEmitter( true );
203  spawnArgs.Copy( *source );
206  refSound.referenceSound = saveRef;
207 
208  idVec3 origin;
209  idMat3 axis;
210 
211  if ( GetPhysicsToSoundTransform( origin, axis ) ) {
212  refSound.origin = GetPhysics()->GetOrigin() + origin * axis;
213  } else {
215  }
216 
217  spawnArgs.GetFloat( "random", "0", random );
218  spawnArgs.GetFloat( "wait", "0", wait );
219 
220  if ( ( wait > 0.0f ) && ( random >= wait ) ) {
221  random = wait - 0.001;
222  gameLocal.Warning( "speaker '%s' at (%s) has random >= wait", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
223  }
224 
225  if ( !refSound.waitfortrigger && ( wait > 0.0f ) ) {
226  timerOn = true;
227  DoSound( false );
231  // start it if it isn't already playing, and we aren't waitForTrigger
232  DoSound( true );
233  timerOn = false;
234  }
235  }
236 }
237 
238 /*
239 ===============
240 idSound::SetSound
241 ===============
242 */
243 void idSound::SetSound( const char *sound, int channel ) {
244  const idSoundShader *shader = declManager->FindSound( sound );
245  if ( shader != refSound.shader ) {
246  FreeSoundEmitter( true );
247  }
249  refSound.shader = shader;
250  // start it if it isn't already playing, and we aren't waitForTrigger
252  DoSound( true );
253  }
254 }
255 
256 /*
257 ================
258 idSound::DoSound
259 ================
260 */
261 void idSound::DoSound( bool play ) {
262  if ( play ) {
265  } else {
266  StopSound( SND_CHANNEL_ANY, true );
267  playingUntilTime = 0;
268  }
269 }
270 
271 /*
272 ================
273 idSound::Event_On
274 ================
275 */
276 void idSound::Event_On( void ) {
277  if ( wait > 0.0f ) {
278  timerOn = true;
280  }
281  DoSound( true );
282 }
283 
284 /*
285 ================
286 idSound::Event_Off
287 ================
288 */
289 void idSound::Event_Off( void ) {
290  if ( timerOn ) {
291  timerOn = false;
293  }
294  DoSound( false );
295 }
296 
297 /*
298 ===============
299 idSound::ShowEditingDialog
300 ===============
301 */
304 }
305 
virtual const idVec3 & GetOrigin(int id=0) const =0
void FreeSoundEmitter(bool immediate)
Definition: Entity.cpp:1787
float GetFloat(const char *key, const char *defaultString="0") const
Definition: Dict.h:248
bool PostEventSec(const idEventDef *ev, float time)
Definition: Class.cpp:747
virtual void InitTool(const toolFlag_t tool, const idDict *dict)=0
bool waitfortrigger
Definition: Game.h:217
virtual const idSoundShader * FindSound(const char *name, bool makeDefault=true)=0
virtual bool CurrentlyPlaying(void) const =0
virtual void UpdateChangeableSpawnArgs(const idDict *source)
Definition: Sound.cpp:197
const idEventDef EV_Activate("activate","e")
const idEventDef EV_Speaker_Timer("<timer>", NULL)
idRandom random
Definition: Game_local.h:291
Definition: Sound.h:40
bool isMultiplayer
Definition: Game_local.h:325
idAngles GetAngles(const char *key, const char *defaultString=NULL) const
Definition: Dict.h:278
void DoSound(bool play)
Definition: Sound.cpp:261
Definition: Vector.h:316
void ReadBool(bool &value)
Definition: SaveGame.cpp:976
idAngles ang_zero(0.0f, 0.0f, 0.0f)
float lastSoundVol
Definition: Sound.h:60
idDict spawnArgs
Definition: Entity.h:122
const idEventDef EV_Speaker_Off("Off", NULL)
void WriteVec3(const idVec3 &vec)
Definition: SaveGame.cpp:253
void Copy(const idDict &other)
Definition: Dict.cpp:70
#define EVENT(event, function)
Definition: Class.h:53
void WriteBool(const bool value)
Definition: SaveGame.cpp:222
idGameEdit * gameEdit
Definition: GameEdit.cpp:668
void void void Warning(const char *fmt,...) const id_attribute((format(printf
Definition: Game_local.cpp:735
GLsizei GLsizei GLcharARB * source
Definition: glext.h:3633
void Event_On(void)
Definition: Sound.cpp:276
bool StartSoundShader(const idSoundShader *shader, const s_channelType channel, int soundShaderFlags, bool broadcast, int *length)
Definition: Entity.cpp:1656
void Think(void)
Definition: Sound.cpp:182
float wait
Definition: Sound.h:63
virtual bool GetPhysicsToSoundTransform(idVec3 &origin, idMat3 &axis)
Definition: Entity.cpp:2853
void Event_Trigger(idEntity *activator)
Definition: Sound.cpp:140
idAngles shakeRotate
Definition: Sound.h:66
idPhysics * GetPhysics(void) const
Definition: Entity.cpp:2607
bool timerOn
Definition: Sound.h:64
void ReadFloat(float &value)
Definition: SaveGame.cpp:967
void SetSound(const char *sound, int channel=SND_CHANNEL_ANY)
Definition: Sound.cpp:243
idSoundEmitter * referenceSound
Definition: Game.h:209
#define vec3_zero
Definition: Vector.h:390
void WriteFloat(const float value)
Definition: SaveGame.cpp:213
idCommon * common
Definition: Common.cpp:206
void Event_Timer(void)
Definition: Sound.cpp:172
virtual void UpdateChangeableSpawnArgs(const idDict *source)
Definition: Entity.cpp:377
Definition: Dict.h:65
idVec3 origin
Definition: Game.h:211
#define NULL
Definition: Lib.h:88
const idEventDef EV_Speaker_On("On", NULL)
idVec3 GetVector(const char *key, const char *defaultString=NULL) const
Definition: Dict.h:260
float soundVol
Definition: Sound.h:61
void CancelEvents(const idEventDef *ev)
Definition: Class.cpp:619
const idSoundShader * shader
Definition: Game.h:214
idGameLocal gameLocal
Definition: Game_local.cpp:64
bool RunPhysics(void)
Definition: Entity.cpp:2616
#define END_CLASS
Definition: Class.h:54
virtual void ShowEditingDialog(void)
Definition: Sound.cpp:302
const char * ToString(int precision=2) const
Definition: Vector.cpp:221
void WriteInt(const int value)
Definition: SaveGame.cpp:168
idDeclManager * declManager
void Save(idSaveGame *savefile) const
Definition: Sound.cpp:75
Definition: Matrix.h:333
void WriteAngles(const idAngles &angles)
Definition: SaveGame.cpp:318
soundShaderParms_t parms
Definition: Game.h:218
tuple f
Definition: idal.py:89
const GLcharARB * name
Definition: glext.h:3629
void Event_Off(void)
Definition: Sound.cpp:289
#define CLASS_DECLARATION(nameofsuperclass, nameofclass)
Definition: Class.h:110
void ReadVec3(idVec3 &vec)
Definition: SaveGame.cpp:1011
void ReadAngles(idAngles &angles)
Definition: SaveGame.cpp:1073
void BecomeActive(int flags)
Definition: Entity.cpp:995
refSound_t refSound
Definition: Entity.h:373
idVec3 shakeTranslate
Definition: Sound.h:65
virtual void ParseSpawnArgsToRefSound(const idDict *args, refSound_t *refSound)
Definition: Entity.cpp:324
float random
Definition: Sound.h:62
int playingUntilTime
Definition: Sound.h:67
void BecomeInactive(int flags)
Definition: Entity.cpp:1025
void ReadInt(int &value)
Definition: SaveGame.cpp:922
float CRandomFloat(void)
Definition: Random.h:86
void StopSound(const s_channelType channel, bool broadcast)
Definition: Entity.cpp:1713
int soundShaderFlags
Definition: sound.h:65
void Spawn(void)
Definition: Sound.cpp:107
void Restore(idRestoreGame *savefile)
Definition: Sound.cpp:91