]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
42b622108aa4f060c09130e78a66304ded142c96
[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     SGSoundMgr *smgr = globals->get_soundmgr();
65     if (!smgr) {
66       return;
67     }
68   
69     SGSampleGroup::_smgr = smgr;
70     SGSampleGroup::_refname = refname;
71     SGSampleGroup::_smgr->add(this, refname);
72
73     if (!_is_aimodel)
74     {
75         _avionics = _smgr->find("avionics", true);
76         _avionics->tie_to_listener();
77     }
78 }
79
80
81 FGFX::~FGFX ()
82 {
83     for (unsigned int i = 0; i < _sound.size(); i++ ) {
84         delete _sound[i];
85     }
86     _sound.clear();
87 }
88
89
90 void
91 FGFX::init()
92 {
93     if (!_smgr) {
94         return;
95     }
96   
97     SGPropertyNode *node = _props->getNode("sim/sound", true);
98
99     std::string path_str = node->getStringValue("path");
100     if (path_str.empty()) {
101         SG_LOG(SG_SOUND, SG_ALERT, "No path in sim/sound/path");
102         return;
103     }
104     
105     SGPath path = globals->resolve_aircraft_path(path_str);
106     if (path.isNull())
107     {
108         SG_LOG(SG_SOUND, SG_ALERT,
109                "File not found: '" << path_str);
110         return;
111     }
112     SG_LOG(SG_SOUND, SG_INFO, "Reading sound " << node->getName()
113            << " from " << path.str());
114
115     SGPropertyNode root;
116     try {
117         readProperties(path.str(), &root);
118     } catch (const sg_exception &) {
119         SG_LOG(SG_SOUND, SG_ALERT,
120                "Error reading file '" << path.str() << '\'');
121         return;
122     }
123
124     node = root.getNode("fx");
125     if(node) {
126         for (int i = 0; i < node->nChildren(); ++i) {
127             SGXmlSound *soundfx = new SGXmlSound();
128   
129             try {
130                 soundfx->init( _props, node->getChild(i), this, _avionics,
131                                path.dir() );
132                 _sound.push_back( soundfx );
133             } catch ( sg_exception &e ) {
134                 SG_LOG(SG_SOUND, SG_ALERT, e.getFormattedMessage());
135                 delete soundfx;
136             }
137         }
138     }
139 }
140
141
142 void
143 FGFX::reinit()
144 {
145     for ( unsigned int i = 0; i < _sound.size(); i++ ) {
146         delete _sound[i];
147     }
148     _sound.clear();
149     init();
150 };
151
152
153 void
154 FGFX::update (double dt)
155 {
156     if (!_smgr) {
157         return;
158     }
159       
160     if ( _enabled->getBoolValue() ) {
161         if ( _avionics_enabled->getBoolValue())
162         {
163             if (_avionics_ext->getBoolValue() || _internal->getBoolValue()) {
164                 // avionics sound is enabled
165                 _avionics->resume(); // no-op if already in resumed state
166                 _avionics->set_volume( _avionics_volume->getFloatValue() );
167             }
168             else
169                 _avionics->suspend();
170         }
171
172         set_volume( _volume->getDoubleValue() );
173         resume();
174
175         // update sound effects if not paused
176         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
177             _sound[i]->update(dt);
178         }
179
180         SGSampleGroup::update(dt);
181     }
182     else
183         suspend();
184 }
185
186 // end of fg_fx.cxx