]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
httpd: update mongoose and websockets
[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 #include <Main/globals.hxx>
36
37 #include <simgear/props/props.hxx>
38 #include <simgear/props/props_io.hxx>
39 #include <simgear/misc/sg_path.hxx>
40 #include <simgear/sound/soundmgr_openal.hxx>
41 #include <simgear/sound/xmlsound.hxx>
42
43 FGFX::FGFX ( const std::string &refname, SGPropertyNode *props ) :
44     _props( props )
45 {
46     if (!props) {
47         _is_aimodel = false;
48         _props = globals->get_props();
49         _enabled = fgGetNode("/sim/sound/effects/enabled", true);
50         _volume = fgGetNode("/sim/sound/effects/volume", true);
51     } else {
52         _is_aimodel = true;
53         _enabled = _props->getNode("/sim/sound/aimodels/enabled", true);
54         _enabled->setBoolValue(fgGetBool("/sim/sound/effects/enabled"));
55          _volume = _props->getNode("/sim/sound/aimodels/volume", true);
56         _volume->setFloatValue(fgGetFloat("/sim/sound/effects/volume"));
57     }
58
59     _avionics_enabled = _props->getNode("sim/sound/avionics/enabled", true);
60     _avionics_volume = _props->getNode("sim/sound/avionics/volume", true);
61     _avionics_ext = _props->getNode("sim/sound/avionics/external-view", true);
62     _internal = _props->getNode("sim/current-view/internal", true);
63
64     _smgr = globals->get_soundmgr();
65     if (!_smgr) {
66       return;
67     }
68   
69     _refname = refname;
70     _smgr->add(this, refname);
71
72     if (!_is_aimodel)
73     {
74         _avionics = _smgr->find("avionics", true);
75         _avionics->tie_to_listener();
76     }
77 }
78
79 void FGFX::unbind()
80 {
81     if (_smgr)
82     {
83         _smgr->remove(_refname);
84     }
85 }
86
87 FGFX::~FGFX ()
88 {
89     for (unsigned int i = 0; i < _sound.size(); i++ ) {
90         delete _sound[i];
91     }
92     _sound.clear();
93 }
94
95
96 void
97 FGFX::init()
98 {
99     if (!_smgr) {
100         return;
101     }
102   
103     SGPropertyNode *node = _props->getNode("sim/sound", true);
104
105     std::string path_str = node->getStringValue("path");
106     if (path_str.empty()) {
107         SG_LOG(SG_SOUND, SG_ALERT, "No path in sim/sound/path");
108         return;
109     }
110     
111     SGPath path = globals->resolve_aircraft_path(path_str);
112     if (path.isNull())
113     {
114         SG_LOG(SG_SOUND, SG_ALERT,
115                "File not found: '" << path_str);
116         return;
117     }
118     SG_LOG(SG_SOUND, SG_INFO, "Reading sound " << node->getName()
119            << " from " << path.str());
120
121     SGPropertyNode root;
122     try {
123         readProperties(path.str(), &root);
124     } catch (const sg_exception &) {
125         SG_LOG(SG_SOUND, SG_ALERT,
126                "Error reading file '" << path.str() << '\'');
127         return;
128     }
129
130     node = root.getNode("fx");
131     if(node) {
132         for (int i = 0; i < node->nChildren(); ++i) {
133             SGXmlSound *soundfx = new SGXmlSound();
134   
135             try {
136                 soundfx->init( _props, node->getChild(i), this, _avionics,
137                                path.dir() );
138                 _sound.push_back( soundfx );
139             } catch ( sg_exception &e ) {
140                 SG_LOG(SG_SOUND, SG_ALERT, e.getFormattedMessage());
141                 delete soundfx;
142             }
143         }
144     }
145 }
146
147
148 void
149 FGFX::reinit()
150 {
151     for ( unsigned int i = 0; i < _sound.size(); i++ ) {
152         delete _sound[i];
153     }
154     _sound.clear();
155     init();
156 }
157
158
159 void
160 FGFX::update (double dt)
161 {
162     if (!_smgr) {
163         return;
164     }
165       
166     if ( _enabled->getBoolValue() ) {
167         if ( _avionics_enabled->getBoolValue())
168         {
169             if (_avionics_ext->getBoolValue() || _internal->getBoolValue()) {
170                 // avionics sound is enabled
171                 _avionics->resume(); // no-op if already in resumed state
172                 _avionics->set_volume( _avionics_volume->getFloatValue() );
173             }
174             else
175                 _avionics->suspend();
176         }
177
178         set_volume( _volume->getDoubleValue() );
179         resume();
180
181         // update sound effects if not paused
182         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
183             _sound[i]->update(dt);
184         }
185
186         SGSampleGroup::update(dt);
187     }
188     else
189         suspend();
190 }
191
192 // end of fg_fx.cxx