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)
38 if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
39 std::cerr << "No Position in ambient_sound" << std::endl;
41 lisp.get("distance_factor",distance_factor);
42 lisp.get("distance_bias" ,distance_bias );
43 lisp.get("sample" ,sample );
44 lisp.get("volume" ,maximumvolume );
46 distance_bias*=distance_bias;
47 distance_factor*=distance_factor;
49 if (distance_factor == 0)
50 silence_distance = 10e99;
52 silence_distance = 1/distance_factor;
54 lisp.get("silence_distance",silence_distance);
56 playing=-1; // not playing at the beginning
61 AmbientSound::~AmbientSound() {
66 AmbientSound::hit(Player& )
71 AmbientSound::stop_playing() {
73 Mix_HaltChannel(playing);
79 AmbientSound::start_playing()
81 playing=sound_manager->play_sound(sample,-1);
82 Mix_Volume(playing,0);
83 currentvolume=targetvolume=1e-20;
87 AmbientSound::update(float deltat)
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;
95 sqrdistance-=distance_bias;
100 targetvolume=1/(1+sqrdistance*distance_factor);
101 float rise=targetvolume/currentvolume;
102 currentvolume*=pow(rise,deltat*10);
103 currentvolume += 1e-30;
106 Mix_Volume(playing,(int)(currentvolume*maximumvolume*MIX_MAX_VOLUME));
107 if (sqrdistance>=silence_distance && currentvolume<0.05)
111 if (sqrdistance<silence_distance) {
116 latency=(int)(10*((sqrdistance-silence_distance)/silence_distance));
120 if (latency>0.001/distance_factor)
121 latency=(int)(0.001/distance_factor);
125 AmbientSound::draw(DrawingContext &)
129 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");