]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
allow sound effects in the configuration file to be added to the 'avionics' sample...
[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 }
52
53
54 FGFX::~FGFX ()
55 {
56     for (unsigned int i = 0; i < _sound.size(); i++ ) {
57         delete _sound[i];
58     }
59     _sound.clear();
60 }
61
62
63 void
64 FGFX::init()
65 {
66     SGPropertyNode *node = fgGetNode("/sim/sound", true);
67
68     string path_str = node->getStringValue("path");
69     SGPath path( globals->get_fg_root() );
70     if (path_str.empty()) {
71         SG_LOG(SG_GENERAL, SG_ALERT, "No path in /sim/sound/path");
72         return;
73     }
74
75     path.append(path_str.c_str());
76     SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
77            << " from " << path.str());
78
79     SGPropertyNode root;
80     try {
81         readProperties(path.str(), &root);
82     } catch (const sg_exception &) {
83         SG_LOG(SG_GENERAL, SG_ALERT,
84                "Error reading file '" << path.str() << '\'');
85         return;
86     }
87
88     node = root.getNode("fx");
89     if(node) {
90         for (int i = 0; i < node->nChildren(); ++i) {
91             SGXmlSound *sound = new SGXmlSound();
92   
93             try {
94                 sound->init(globals->get_props(), node->getChild(i), this,
95                             _avionics, globals->get_fg_root());
96   
97                 _sound.push_back(sound);
98             } catch ( sg_exception &e ) {
99                 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
100                 delete sound;
101             }
102         }
103     }
104 }
105
106
107 void
108 FGFX::reinit()
109 {
110     _sound.clear();
111     init();
112 };
113
114
115 void
116 FGFX::update (double dt)
117 {
118     bool new_pause = _pause->getBoolValue();
119     if ( new_pause != last_pause ) {
120         if ( new_pause ) {
121             suspend();
122         } else {
123             resume();
124         }
125         last_pause = new_pause;
126     }
127
128     if ( !new_pause ) {
129         double volume = _volume->getDoubleValue();
130         if ( volume != last_volume ) {
131             set_volume( volume );        
132             last_volume = volume;
133         }
134
135         // update sound effects if not paused
136         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
137             _sound[i]->update(dt);
138         }
139
140         SGSampleGroup::update(dt);
141     }
142 }
143
144 // end of fg_fx.cxx