]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
Merge branch 'next' of D:\Git_New\flightgear into next
[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     _enabled( fgGetNode("/sim/sound/effects/enabled", true) ),
43     _volume( fgGetNode("/sim/sound/effects/volume", true) ),
44     _avionics_enabled( fgGetNode("/sim/sound/avionics/enabled", true) ),
45     _avionics_volume( fgGetNode("/sim/sound/avionics/volume", true) ),
46     _avionics_external( fgGetNode("/sim/sound/avionics/external-view", true) ),
47     _internal( fgGetNode("/sim/current-view/internal", true) )
48 {
49     SGSampleGroup::_smgr = smgr;
50     SGSampleGroup::_refname = refname;
51     SGSampleGroup::_smgr->add(this, refname);
52     _avionics = _smgr->find("avionics", true);
53     _avionics->tie_to_listener();
54 }
55
56
57 FGFX::~FGFX ()
58 {
59     for (unsigned int i = 0; i < _sound.size(); i++ ) {
60         delete _sound[i];
61     }
62     _sound.clear();
63 }
64
65
66 void
67 FGFX::init()
68 {
69     SGPropertyNode *node = fgGetNode("/sim/sound", true);
70
71     string path_str = node->getStringValue("path");
72     if (path_str.empty()) {
73         SG_LOG(SG_GENERAL, SG_ALERT, "No path in /sim/sound/path");
74         return;
75     }
76     
77     SGPath path = globals->resolve_aircraft_path(path_str);
78     SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
79            << " from " << path.str());
80
81     SGPropertyNode root;
82     try {
83         readProperties(path.str(), &root);
84     } catch (const sg_exception &) {
85         SG_LOG(SG_GENERAL, SG_ALERT,
86                "Error reading file '" << path.str() << '\'');
87         return;
88     }
89
90     node = root.getNode("fx");
91     if(node) {
92         for (int i = 0; i < node->nChildren(); ++i) {
93             SGXmlSound *sound = new SGXmlSound();
94   
95             try {
96                 sound->init(globals->get_props(), node->getChild(i), this,
97                             _avionics, path.dir());
98   
99                 _sound.push_back(sound);
100             } catch ( sg_exception &e ) {
101                 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
102                 delete sound;
103             }
104         }
105     }
106 }
107
108
109 void
110 FGFX::reinit()
111 {
112     _sound.clear();
113     init();
114 };
115
116
117 void
118 FGFX::update (double dt)
119 {
120     bool active = _avionics_external->getBoolValue() ||
121                   _internal->getBoolValue();
122
123     if ( active && _avionics_enabled->getBoolValue() )
124         _avionics->resume(); // no-op if already in resumed state
125     else
126         _avionics->suspend();
127     _avionics->set_volume( _avionics_volume->getFloatValue() );
128
129     if ( _enabled->getBoolValue() ) {
130         set_volume( _volume->getDoubleValue() );
131         resume();
132
133         // update sound effects if not paused
134         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
135             _sound[i]->update(dt);
136         }
137
138         SGSampleGroup::update(dt);
139     }
140     else
141         suspend();
142 }
143
144 // end of fg_fx.cxx