1 // ambient_sound.cpp basti_
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.
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.
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
23 #include "ambient_sound.h"
24 #include "object_factory.h"
25 #include "lisp/lisp.h"
28 AmbientSound::AmbientSound(const lisp::Lisp& lisp)
42 if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
43 std::cerr << "No Position in ambient_sound" << std::endl;
46 lisp.get("width" , dimension.x);
47 lisp.get("height", dimension.y);
49 lisp.get("distance_factor",distance_factor);
50 lisp.get("distance_bias" ,distance_bias );
51 lisp.get("sample" ,sample );
52 lisp.get("volume" ,maximumvolume );
54 // set dimension to zero if smaller than 64, which is default size in flexlay
56 if ((dimension.x <= 64) || (dimension.y <= 64)) {
61 // square all distances (saves us a sqrt later)
63 distance_bias*=distance_bias;
64 distance_factor*=distance_factor;
66 // set default silence_distance
68 if (distance_factor == 0)
69 silence_distance = 10e99;
71 silence_distance = 1/distance_factor;
73 lisp.get("silence_distance",silence_distance);
75 playing=-1; // not playing at the beginning
79 AmbientSound::~AmbientSound() {
84 AmbientSound::hit(Player& )
89 AmbientSound::stop_playing() {
91 Mix_HaltChannel(playing);
97 AmbientSound::start_playing()
99 playing=sound_manager->play_sound(sample,-1);
100 Mix_Volume(playing,0);
101 currentvolume=targetvolume=1e-20;
105 AmbientSound::update(float deltat)
114 px=Sector::current()->player->get_pos().x;
115 py=Sector::current()->player->get_pos().y;
117 // Relate to which point in the area
119 rx=px<position.x?position.x:
120 (px<position.x+dimension.x?px:position.x+dimension.x);
121 ry=py<position.y?position.y:
122 (py<position.y+dimension.y?py:position.y+dimension.y);
124 // calculate square of distance
126 float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
128 sqrdistance-=distance_bias;
130 // inside the bias: full volume (distance 0)
135 // calculate target volume - will never become 0
137 targetvolume=1/(1+sqrdistance*distance_factor);
139 float rise=targetvolume/currentvolume;
141 // rise/fall half life?
143 currentvolume*=pow(rise,deltat*10);
144 currentvolume += 1e-6; // volume is at least 1e-6 (0 would never rise)
149 Mix_Volume(playing,(int)(currentvolume*maximumvolume*MIX_MAX_VOLUME));
151 if (sqrdistance>=silence_distance && currentvolume<1e-3)
155 if (sqrdistance<silence_distance) {
159 else // set a reasonable latency
160 latency=(int)(0.001/distance_factor);
161 //(int)(10*((sqrdistance-silence_distance)/silence_distance));
165 // heuristically measured "good" latency maximum
167 // if (latency>0.001/distance_factor)
172 AmbientSound::draw(DrawingContext &)
176 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");