From b9681ccc26f6831702fe67cfb68e41aff77d8710 Mon Sep 17 00:00:00 2001 From: Karol Nowak Date: Sat, 17 Feb 2007 14:57:30 +0000 Subject: [PATCH] Possibly fixed a deadlock in sound code, removed warnings, improved comments. --- src/sound.cpp | 8 ++++---- src/soundsource.cpp | 7 +++++-- src/soundsource.hpp | 5 +++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/sound.cpp b/src/sound.cpp index bb3cb2ca348..f3324b1e5e4 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -589,8 +589,6 @@ void play_sound(const std::string& files, int channel) } } - threading::lock l(channel_mutex); - // the insertion will fail if there is already an element in the cache std::pair< std::map< std::string, Mix_Chunk * >::iterator, bool > it = sound_cache.insert(std::make_pair(file, (Mix_Chunk *)0)); @@ -622,12 +620,14 @@ void play_sound(const std::string& files, int channel) //FIXME: in worst case it can play on bell channel(0), nothing happend // only sound can have another volume than others sounds const int res = Mix_PlayChannel(channel, cache, 0); - channel_chunks[res] = cache; + + threading::lock l(channel_mutex); if(res < 0) { channel_chunks[res] = 0; ERR_AUDIO << "error playing sound effect: " << Mix_GetError() << "\n"; } - + else + channel_chunks[res] = cache; } } diff --git a/src/soundsource.cpp b/src/soundsource.cpp index 236c484ba97..94e1581b7b3 100644 --- a/src/soundsource.cpp +++ b/src/soundsource.cpp @@ -29,7 +29,9 @@ int calculate_volume(int x, int y, const display &disp) int dx = area.w / 2 - x; dx *= dx; int dy = area.h / 2 - y; dy *= dy; - return maximum(0, 256.0 * (sqrt(dx + dy) / (sqrt(area.w*area.w + area.h * area.h) / 2))); + // An obscure formula to calculate SDL_Mixer's "distance" based on the source's + // distance from screen's center + return maximum(0, 128 * static_cast(sqrt(dx + dy) / (sqrt(area.w*area.w + area.h * area.h)))); } } // end of namespace @@ -113,7 +115,8 @@ void manager::add_location(const std::string &name, const gamemap::location &loc manager::positional_source::positional_source(const std::string &files, int min_delay, int chance, bool play_fogged) : _last_played(0), _min_delay(min_delay), _chance(chance), - _play_fogged(play_fogged), _files(files), _id(last_id++) + _id(last_id++), _play_fogged(play_fogged), _visible(false), + _files(files) { } diff --git a/src/soundsource.hpp b/src/soundsource.hpp index 4eb2d03449c..f7908ad539f 100644 --- a/src/soundsource.hpp +++ b/src/soundsource.hpp @@ -48,10 +48,11 @@ class manager : public events::observer { public: // min_delay is a minimum time in seconds, which must pass before - // this sound source can be played again + // this sound source can be played again if it remains visible // // chance is a chance ;-) (in %) that the sound source will emit - // sound every second + // sound every second after the delay has passed or once the source + // becomes visible positional_source(const std::string &files, int min_delay, int chance, bool play_fogged = false); void update(unsigned int time, const display &disp);