]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
Merge branch 'durk/traffic'
[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     SGPath path( globals->get_fg_root() );
73     if (path_str.empty()) {
74         SG_LOG(SG_GENERAL, SG_ALERT, "No path in /sim/sound/path");
75         return;
76     }
77
78     path.append(path_str.c_str());
79     SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
80            << " from " << path.str());
81
82     SGPropertyNode root;
83     try {
84         readProperties(path.str(), &root);
85     } catch (const sg_exception &) {
86         SG_LOG(SG_GENERAL, SG_ALERT,
87                "Error reading file '" << path.str() << '\'');
88         return;
89     }
90
91     node = root.getNode("fx");
92     if(node) {
93         for (int i = 0; i < node->nChildren(); ++i) {
94             SGXmlSound *sound = new SGXmlSound();
95   
96             try {
97                 sound->init(globals->get_props(), node->getChild(i), this,
98                             _avionics, globals->get_fg_root());
99   
100                 _sound.push_back(sound);
101             } catch ( sg_exception &e ) {
102                 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
103                 delete sound;
104             }
105         }
106     }
107 }
108
109
110 void
111 FGFX::reinit()
112 {
113     _sound.clear();
114     init();
115 };
116
117
118 void
119 FGFX::update (double dt)
120 {
121     bool active = _avionics_external->getBoolValue() ||
122                   _internal->getBoolValue();
123
124     if ( active && _avionics_enabled->getBoolValue() )
125         _avionics->resume(); // no-op if already in resumed state
126     else
127         _avionics->suspend();
128     _avionics->set_volume( _avionics_volume->getFloatValue() );
129
130     if ( _enabled->getBoolValue() ) {
131         set_volume( _volume->getDoubleValue() );
132         resume();
133
134         // update sound effects if not paused
135         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
136             _sound[i]->update(dt);
137         }
138
139         SGSampleGroup::update(dt);
140     }
141     else
142         suspend();
143 }
144
145 // end of fg_fx.cxx