diff --git a/panda/src/audiotraits/fmodAudioManager.cxx b/panda/src/audiotraits/fmodAudioManager.cxx index eaae30483e..6fbd4e1a04 100644 --- a/panda/src/audiotraits/fmodAudioManager.cxx +++ b/panda/src/audiotraits/fmodAudioManager.cxx @@ -27,12 +27,11 @@ #include "fmodAudioManager.h" #include "fmodAudioSound.h" #include "nullAudioSound.h" +#include "virtualFileSystem.h" #include #include #include -#include -using std::cerr; PT(AudioManager) Create_AudioManager() { audio_debug("Create_AudioManager() Fmod."); @@ -131,6 +130,13 @@ get_sound(const string &file_name) { assert(is_valid()); Filename path = file_name; + if (use_vfs) { + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + vfs->resolve_filename(path, get_sound_path()); + } else { + path.resolve_filename(get_sound_path()); + } + audio_debug(" resolved file_name is '"<data), - flags, entry->size); + flags, entry->size); } if (stream == NULL) { audio_error("FmodAudioManager::get_sound failed."); @@ -224,7 +230,7 @@ uncache_sound(const string& file_name) { // purged right now! SoundCacheEntry *entry = &(*itor).second; if (entry->refcount == 0) { - audio_debug("FmodAudioManager::dec_refcount: purging "<data; _sounds.erase(itor); @@ -244,17 +250,22 @@ void FmodAudioManager:: clear_cache() { // Mark all cache entries as stale. Delete those which already have // refcounts of zero. + SoundMap::iterator itor = _sounds.begin(); - for( ; itor != _sounds.end(); ++itor) { + + // Have to use a while loop, not a for loop, since we don't want to + // increment itor in the case in which we delete an entry. + while (itor != _sounds.end()) { SoundCacheEntry *entry = &(*itor).second; if (entry->refcount == 0) { - audio_debug("FmodAudioManager: purging "<< (*itor).first + audio_debug("FmodAudioManager::clear_cache: purging "<< (*itor).first << " from the cache."); delete [] entry->data; _sounds.erase(itor); itor = _sounds.begin(); } else { entry->stale = true; + ++itor; } } } @@ -394,7 +405,7 @@ dec_refcount(const string& file_name) { audio_debug("FmodAudioManager: "<refcount); if (entry->refcount == 0 && entry->stale) { - audio_debug("FmodAudioManager: purging "<data; _sounds.erase(itor); } @@ -406,11 +417,12 @@ dec_refcount(const string& file_name) { //////////////////////////////////////////////////////////////////// // Function: FmodAudioManager::load // Access: Private -// Description: Loads the specified file into memory. -// Returns NULL if an error occurs. +// Description: Loads the specified file into memory. Returns a +// newly-allocated buffer, and stores the size of the +// buffer in size. Returns NULL if an error occurs. //////////////////////////////////////////////////////////////////// void* FmodAudioManager:: -load(const Filename& filename, const size_t size) const { +load(const Filename& filename, size_t &size) const { // Check file type (based on filename suffix string suffix = filename.get_extension(); std::transform(suffix.begin(), suffix.end(), suffix.begin(), tolower); @@ -427,56 +439,60 @@ load(const Filename& filename, const size_t size) const { } // open the file. - string os_filename = filename.to_os_specific(); - FILE *audioFile = fopen(os_filename.c_str(), "rb"); - if (!audioFile) { - audio_error("File "<exists(filename)) { + audio_error("File " << filename << " does not exist."); + return NULL; + } + + audioFile = vfs->open_read_file(binary_filename); + + } else { + if (!filename.exists()) { + audio_error("File " << filename << " does not exist."); + return NULL; + } + + audioFile = new ifstream; + if (!binary_filename.open_read(*(ifstream *)audioFile)) { + delete audioFile; + audioFile = NULL; + } + } + + if (audioFile == (istream *)NULL) { + // Unable to open. + audio_error("Unable to read " << filename << "."); return NULL; } + + // Determine the file size. + audioFile->seekg(0, ios::end); + size = (size_t)audioFile->tellg(); + audioFile->seekg(0, ios::beg); // Read the entire file into memory. char *buffer = new char[size]; if (buffer == NULL) { audio_error("out-of-memory error while loading "<read(buffer, size); + if (!(*audioFile)) { audio_error("Read error while loading "<