- SoundManager doesn't need Effects for now - ambient sound volume
[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   sample="";
36
37   if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
38     std::cerr << "No Position in ambient_sound"  << std::endl;
39   }
40   lisp.get("distance_factor",distance_factor);
41   lisp.get("distance_bias"  ,distance_bias  );
42   lisp.get("sample"         ,sample         );
43
44   if (distance_factor == 0)
45     silence_distance = 10e99;
46   else
47     silence_distance = distance_factor*10e2;
48
49   playing=0;
50   startPlaying();
51 }
52
53
54 void
55 AmbientSound::hit(Player& )
56 {
57 }
58
59 void
60 AmbientSound::startPlaying()
61 {
62   playing=sound_manager->play_sound(sample,-1);
63   Mix_Volume(playing,0);
64   currentvolume=targetvolume=1e-20;
65 }
66
67 void
68 AmbientSound::action(float) 
69 {
70   float dx=Sector::current()->player->get_pos().x-position.x;
71   float dy=Sector::current()->player->get_pos().y-position.y;
72   float distance=sqrt(dx*dx+dy*dy);
73
74   distance-=distance_bias;
75
76   if (distance<0)
77     distance=0;
78
79   targetvolume=1/(1+distance*distance_factor);
80   float rise=targetvolume/currentvolume;
81   currentvolume*=pow(rise,.05);
82   currentvolume += 1e-30;
83   //  std::cout << currentvolume << " " << targetvolume << std::endl;
84   Mix_Volume(playing,(int)(currentvolume*MIX_MAX_VOLUME));
85
86 }
87
88 void
89 AmbientSound::draw(DrawingContext &) 
90 {
91 }
92
93 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");