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