]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
Tie samplegroup 'avionics' to the listener, just in case no other device is created...
[flightgear.git] / src / Sound / fg_fx.cxx
1 // fg_fx.cxx -- Sound effect management class implementation
2 //
3 // Started by David Megginson, October 2001
4 // (Reuses some code from main.cxx, probably by Curtis Olson)
5 //
6 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef _MSC_VER
25 #pragma warning (disable: 4786)
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include "fg_fx.hxx"
33
34 #include <Main/fg_props.hxx>
35
36 #include <simgear/props/props.hxx>
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/sound/soundmgr_openal.hxx>
39 #include <simgear/sound/xmlsound.hxx>
40
41 FGFX::FGFX ( SGSoundMgr *smgr, const string &refname ) :
42     last_pause( false ),
43     last_volume( 0.0 ),
44     _pause( fgGetNode("/sim/sound/pause") ),
45     _volume( fgGetNode("/sim/sound/volume") )
46 {
47     SGSampleGroup::_smgr = smgr;
48     SGSampleGroup::_refname = refname;
49     SGSampleGroup::_smgr->add(this, refname);
50     _avionics = _smgr->find("avionics", true);
51     _avionics->tie_to_listener();
52 }
53
54
55 FGFX::~FGFX ()
56 {
57     for (unsigned int i = 0; i < _sound.size(); i++ ) {
58         delete _sound[i];
59     }
60     _sound.clear();
61 }
62
63
64 void
65 FGFX::init()
66 {
67     SGPropertyNode *node = fgGetNode("/sim/sound", true);
68
69     string path_str = node->getStringValue("path");
70     SGPath path( globals->get_fg_root() );
71     if (path_str.empty()) {
72         SG_LOG(SG_GENERAL, SG_ALERT, "No path in /sim/sound/path");
73         return;
74     }
75
76     path.append(path_str.c_str());
77     SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
78            << " from " << path.str());
79
80     SGPropertyNode root;
81     try {
82         readProperties(path.str(), &root);
83     } catch (const sg_exception &) {
84         SG_LOG(SG_GENERAL, SG_ALERT,
85                "Error reading file '" << path.str() << '\'');
86         return;
87     }
88
89     node = root.getNode("fx");
90     if(node) {
91         for (int i = 0; i < node->nChildren(); ++i) {
92             SGXmlSound *sound = new SGXmlSound();
93   
94             try {
95                 sound->init(globals->get_props(), node->getChild(i), this,
96                             _avionics, globals->get_fg_root());
97   
98                 _sound.push_back(sound);
99             } catch ( sg_exception &e ) {
100                 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
101                 delete sound;
102             }
103         }
104     }
105 }
106
107
108 void
109 FGFX::reinit()
110 {
111     _sound.clear();
112     init();
113 };
114
115
116 void
117 FGFX::update (double dt)
118 {
119     bool new_pause = _pause->getBoolValue();
120     if ( new_pause != last_pause ) {
121         if ( new_pause ) {
122             suspend();
123         } else {
124             resume();
125         }
126         last_pause = new_pause;
127     }
128
129     if ( !new_pause ) {
130         double volume = _volume->getDoubleValue();
131         if ( volume != last_volume ) {
132             set_volume( volume );        
133             last_volume = volume;
134         }
135
136         // update sound effects if not paused
137         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
138             _sound[i]->update(dt);
139         }
140
141         SGSampleGroup::update(dt);
142     }
143 }
144
145 // end of fg_fx.cxx