]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
62973b1aa967a1a6275a63b7be04175940124dad
[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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24 #ifdef _MSC_VER
25 #pragma warning (disable: 4786)
26 #endif
27
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/structure/exception.hxx>
30 #ifdef __BORLANDC__
31 #  define exception c_exception
32 #endif
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/props/props.hxx>
35 #include <simgear/sound/xmlsound.hxx>
36
37 #include <Main/fg_props.hxx>
38
39 #include "fg_fx.hxx"
40
41
42 FGFX::FGFX () :
43     _volume( fgGetNode("/sim/sound/volume") ),
44     _pause( fgGetNode("/sim/sound/pause") ),
45     last_pause( true ),
46     last_volume( 0.0 )
47 {
48 }
49
50 FGFX::~FGFX ()
51 {
52    _sound.clear();
53 }
54
55 void
56 FGFX::init()
57 {
58    SGPropertyNode *node = fgGetNode("/sim/sound", true);
59    int i;
60
61    string path_str = node->getStringValue("path");
62    SGPath path( globals->get_fg_root() );
63    if (path_str.empty()) {
64       SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
65       return;
66    }
67
68    path.append(path_str.c_str());
69    SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
70           << " from " << path.str());
71
72    SGPropertyNode root;
73    try {
74       readProperties(path.str(), &root);
75    } catch (const sg_exception &e) {
76       SG_LOG(SG_GENERAL, SG_ALERT,
77        "Incorrect path specified in configuration file");
78       return;
79    }
80
81    node = root.getNode("fx");
82    for (i = 0; i < node->nChildren(); i++) {
83       SGXmlSound *sound = new SGXmlSound();
84
85       sound->init(globals->get_props(), node->getChild(i),
86                   globals->get_soundmgr(), globals->get_fg_root());
87
88       _sound.push_back(sound);
89    }
90 }
91
92 void
93 FGFX::reinit()
94 {
95    _sound.clear();
96    init();
97 };
98
99 void
100 FGFX::bind ()
101 {
102 }
103
104 void
105 FGFX::unbind ()
106 {
107 }
108
109 void
110 FGFX::update (double dt)
111 {
112     // command sound manger
113     bool pause = _pause->getBoolValue();
114     if ( pause != last_pause ) {
115         if ( pause ) {
116             globals->get_soundmgr()->pause();
117         } else {
118             globals->get_soundmgr()->resume();
119         }
120         last_pause = pause;
121     }
122
123     double volume = _volume->getDoubleValue();
124     if ( volume != last_volume ) {
125         globals->get_soundmgr()->set_volume( volume );        
126         last_volume = volume;
127     }
128
129     if ( !pause ) {
130         // update sound effects if not paused
131         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
132             _sound[i]->update(dt);
133         }
134     }
135 }
136
137 // end of fg_fx.cxx