31e3f3a0eb1741b98baecc513a021f3b2270eca0
[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 /***
29  *  Ambient Sound Source, beta version. Features:
30  *
31  *  - "disc" like structure. Full volume up to some distance
32  *    (distance_bias) to the source, then fading proportional to
33  *    inverse square distance
34  *  
35  *  This is experimental, clicks sometimes and still leaks mem 
36  *
37  *      basti_ 
38  */
39
40 AmbientSound::AmbientSound(const lisp::Lisp& lisp)
41 {
42
43   //  position=pos;
44   lisp.get("x", position.x);
45   lisp.get("y", position.y);
46   lisp.get("distance_factor",distance_factor);
47   lisp.get("distance_bias"  ,distance_bias  );
48   lisp.get("sample"         ,sample         );
49
50   if (distance_factor == 0)
51     silence_distance = 10e99;
52   else
53     silence_distance = distance_factor*10e2;
54
55   volume_ptr=NULL;
56   playing=0;
57   startPlaying();
58 }
59
60
61 void
62 AmbientSound::hit(Player& )
63 {
64 }
65
66 void
67 AmbientSound::startPlaying()
68 {
69   playing=sound_manager->play_sound(sample,-1);
70   volume_ptr=new float[2];
71   volume_ptr[0]=0;
72   volume_ptr[1]=0;
73   sound_manager->register_effect(playing,&SoundManager::volume_adjust,
74                                  NULL,(void *)volume_ptr);
75 }
76
77 void
78 AmbientSound::action(float) 
79 {
80   float dx=Sector::current()->player->get_pos().x-position.x;
81   float dy=Sector::current()->player->get_pos().y-position.y;
82   float distance=sqrt(dx*dx+dy*dy);
83
84   distance-=distance_bias;
85
86   if (distance<0)
87     distance=0;
88
89   volume_ptr[0]=1/(1+distance*distance_factor); // inverse square of distance
90
91 }
92
93 void
94 AmbientSound::draw(DrawingContext &dc) 
95 {
96   return;
97   sprite->draw(dc,position,1);
98
99 }
100
101 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");