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