Possibly fixed a deadlock in sound code, removed warnings, improved comments.

This commit is contained in:
Karol Nowak 2007-02-17 14:57:30 +00:00
parent e6e4c6fe34
commit b9681ccc26
3 changed files with 12 additions and 8 deletions

View File

@ -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;
}
}

View File

@ -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<int>(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<int>(0, 128 * static_cast<int>(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)
{
}

View File

@ -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);