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