00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "SoundProcessor.h"
00018
00019 #ifdef __COMPILE_WITH_SFML_AUDIO__
00020
00021 #include "../core/GameManager.h"
00022 #include <algorithm>
00023
00024
00025
00026
00027 SoundProcessor::SoundProcessor(AssetGroup *parent)
00028 : AssetProcessor(parent, "sounds"), mIsReloading(false)
00029 {
00030 }
00031
00032
00033 SoundProcessor::~SoundProcessor()
00034 {
00035 removeAssets();
00036 }
00037
00038
00039 bool SoundProcessor::getSound(const std::string &fileName, c8 *& buffer, long &length)
00040 {
00041 #ifdef WIN32
00042 std::string tmp = fileName;
00043 std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
00044 std::map<std::string, std::pair<c8*, long> >::iterator it = mSounds.find(tmp);
00045 #else
00046 std::map<std::string, std::pair<c8*, long> >::iterator it = mSounds.find(fileName);
00047 #endif
00048
00049 if(it != mSounds.end())
00050 {
00051 buffer = (it->second).first;
00052 length = (it->second).second;
00053 return true;
00054 }
00055
00056 return false;
00057 }
00058
00059
00060 bool SoundProcessor::loadAsset(const std::string &fileName)
00061 {
00062
00063 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00064
00065
00066 std::string workingDir = (fileSystem->getWorkingDirectory()).c_str();
00067
00068 if(!fileSystem->changeWorkingDirectoryTo( (pParent->getBaseDir()).c_str() ) )
00069 return false;
00070
00071 if(!fileSystem->changeWorkingDirectoryTo("./sounds/"))
00072 return false;
00073
00074
00075 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
00076
00077 if(file)
00078 {
00079
00080 long fSize = file->getSize();
00081 c8 *fBuffer = new c8[fSize+1];
00082 memset(fBuffer, 0, fSize+1);
00083
00084 file->read(fBuffer, fSize);
00085 file->drop();
00086
00087
00088 mSounds[fileName] = std::pair<c8*, long>(fBuffer, fSize);
00089 cout << "Loaded sound: " << fileName << "\n";
00090
00091
00092 if(mIsReloading)
00093 pParent->emitEvent(std::string("sounds/") + fileName, &mSounds[fileName]);
00094
00095 else
00096 pParent->createEventSlot(std::string("sounds/") + fileName);
00097 }
00098
00099
00100 fileSystem->changeWorkingDirectoryTo(workingDir.c_str());
00101
00102 return (file) ? true : false;
00103 }
00104
00105
00106 void SoundProcessor::loadAssets()
00107 {
00108
00109 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00110
00111
00112 std::string workingDir = (fileSystem->getWorkingDirectory()).c_str();
00113
00114 if(!fileSystem->changeWorkingDirectoryTo( (pParent->getBaseDir()).c_str() ) )
00115 return;
00116
00117 if(!fileSystem->changeWorkingDirectoryTo("./sounds/"))
00118 return;
00119
00120
00121 IFileList *fileList = fileSystem->createFileList();
00122
00123 for(u32 i = 0; i < fileList->getFileCount(); i++)
00124 {
00125
00126 std::string fileName = (fileList->getFileName(i)).c_str();
00127
00128 if(fileName == "..")
00129 continue;
00130
00131
00132 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
00133
00134 if(file)
00135 {
00136
00137 long fSize = file->getSize();
00138 c8 *fBuffer = new c8[fSize+1];
00139 memset(fBuffer, 0, fSize+1);
00140
00141 file->read(fBuffer, fSize);
00142 file->drop();
00143
00144
00145 mSounds[fileName] = std::pair<c8*, long>(fBuffer, fSize);
00146 cout << "Loaded sound: " << fileName << "\n";
00147
00148
00149 if(mIsReloading)
00150 pParent->emitEvent(std::string("sounds/") + fileName, &mSounds[fileName]);
00151
00152 else
00153 pParent->createEventSlot(std::string("sounds/") + fileName);
00154 }
00155 }
00156
00157
00158 fileList->drop();
00159
00160 fileSystem->changeWorkingDirectoryTo(workingDir.c_str());
00161 }
00162
00163
00164 bool SoundProcessor::reloadAsset(const std::string &fileName)
00165 {
00166 mIsReloading = true;
00167
00168 removeAsset(fileName);
00169 bool succeeded = loadAsset(fileName);
00170
00171 mIsReloading = false;
00172
00173 return succeeded;
00174 }
00175
00176
00177 void SoundProcessor::reloadAssets()
00178 {
00179 mIsReloading = true;
00180
00181 removeAssets();
00182 loadAssets();
00183
00184 mIsReloading = false;
00185 }
00186
00187
00188 bool SoundProcessor::removeAsset(const std::string &fileName)
00189 {
00190
00191 #ifdef WIN32
00192 std::string tmp = fileName;
00193 std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
00194 std::map<std::string, std::pair<c8*, long> >::iterator it = mSounds.find(tmp);
00195 #else
00196 std::map<std::string, std::pair<c8*, long> >::iterator it = mSounds.find(fileName);
00197 #endif
00198
00199
00200 if(it != mSounds.end())
00201 {
00202
00203 if(!mIsReloading)
00204 {
00205 std::string slotName = std::string("sounds/") + it->first;
00206 std::pair<c8*, long> emptySound = std::pair<c8*, long>(NULL, 0);
00207
00208 pParent->emitEvent(slotName, &emptySound);
00209 pParent->removeEventSlot(slotName);
00210 }
00211
00212
00213 c8 *fBuffer = (it->second).first;
00214 delete [] fBuffer;
00215 mSounds.erase(it);
00216
00217 return true;
00218 }
00219
00220
00221 else return false;
00222 }
00223
00224
00225 void SoundProcessor::removeAssets()
00226 {
00227
00228 std::map<std::string, std::pair<c8*, long> >::iterator it;
00229
00230 for(it = mSounds.begin(); it != mSounds.end(); it++)
00231 {
00232
00233 std::string slotName = std::string("sounds/") + it->first;
00234 std::pair<c8*, long> emptySound = std::pair<c8*, long>(NULL, 0);
00235
00236 pParent->emitEvent(slotName, &emptySound);
00237
00238 if(!mIsReloading)
00239 pParent->removeEventSlot(slotName);
00240
00241
00242 c8 *fBuffer = (it->second).first;
00243 delete [] fBuffer;
00244 }
00245
00246
00247 mSounds.clear();
00248 }
00249
00250
00251 AssetProcessor* SoundProcessor::createProcessor(AssetGroup *parent)
00252 {
00253 return new SoundProcessor(parent);
00254 }
00255
00256 #endif // __COMPILE_WITH_SFML_AUDIO__
00257
00258