changed worldmap format a bit to be more consistent with level format
[supertux.git] / src / object / ambient_sound.cpp
1 // ambient_sound.cpp   basti_
2 //
3 //  SuperTux
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License
7 //  as published by the Free Software Foundation; either version 2
8 //  of the License, or (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 // 
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 //  02111-1307, USA.
19 #include <config.h>
20
21 #include <math.h>
22
23 #include "ambient_sound.h"
24 #include "object_factory.h"
25 #include "lisp/lisp.h"
26 #include "sector.h"
27
28 AmbientSound::AmbientSound(const lisp::Lisp& lisp)
29 {
30
31   position.x=0;
32   position.y=0;
33   distance_factor=0;
34   distance_bias=0;
35   maximumvolume=1;
36   sample="";
37
38   if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
39     std::cerr << "No Position in ambient_sound"  << std::endl;
40   }
41   lisp.get("distance_factor",distance_factor);
42   lisp.get("distance_bias"  ,distance_bias  );
43   lisp.get("sample"         ,sample         );
44   lisp.get("volume"         ,maximumvolume  );
45
46   distance_bias*=distance_bias;
47   distance_factor*=distance_factor;
48
49   if (distance_factor == 0)
50     silence_distance = 10e99;
51   else
52     silence_distance = 1/distance_factor;
53   
54   lisp.get("silence_distance",silence_distance);
55
56   playing=-1; // not playing at the beginning
57   latency=0;
58
59 }
60
61 AmbientSound::~AmbientSound() {
62   stop_playing();
63 }
64
65 void
66 AmbientSound::hit(Player& )
67 {
68 }
69
70 void
71 AmbientSound::stop_playing() {
72   if (playing>=0) {
73     Mix_HaltChannel(playing);
74     playing=-1;
75   }
76 }
77
78 void
79 AmbientSound::start_playing()
80 {
81   playing=sound_manager->play_sound(sample,-1);
82   Mix_Volume(playing,0);
83   currentvolume=targetvolume=1e-20;
84 }
85
86 void
87 AmbientSound::update(float deltat) 
88 {
89   if (latency--<=0) {
90
91     float dx=Sector::current()->player->get_pos().x-position.x;
92     float dy=Sector::current()->player->get_pos().y-position.y;
93     float sqrdistance=dx*dx+dy*dy;
94     
95     sqrdistance-=distance_bias;
96     
97     if (sqrdistance<0)
98       sqrdistance=0;
99     
100     targetvolume=1/(1+sqrdistance*distance_factor);
101     float rise=targetvolume/currentvolume;
102     currentvolume*=pow(rise,deltat*10);
103     currentvolume += 1e-30;
104
105     if (playing>=0) {
106       Mix_Volume(playing,(int)(currentvolume*maximumvolume*MIX_MAX_VOLUME));
107       if (sqrdistance>=silence_distance && currentvolume<0.05)
108         stop_playing();
109       latency=0;
110     } else {
111       if (sqrdistance<silence_distance) {
112         start_playing();
113         latency=0;
114       }
115       else 
116         latency=(int)(10*((sqrdistance-silence_distance)/silence_distance));
117     }
118
119   }
120   if (latency>0.001/distance_factor)
121     latency=(int)(0.001/distance_factor);
122 }
123
124 void
125 AmbientSound::draw(DrawingContext &) 
126 {
127 }
128
129 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");