00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "SoundManager.h"
00019 #ifdef __COMPILE_WITH_SFML_AUDIO__
00020 #include "../core/GameManager.h"
00021
00022
00023
00024
00025 SoundManager::SoundManager()
00026 {
00027 init();
00028 }
00029
00030
00031 SoundManager::~SoundManager()
00032 {
00033 clear();
00034 }
00035
00036
00037 void SoundManager::init()
00038 {
00039 }
00040
00041
00042 void SoundManager::clear()
00043 {
00044 removeSoundBuffers();
00045 }
00046
00047
00048 void SoundManager::grab()
00049 {
00050
00051 }
00052
00053
00054 void SoundManager::drop()
00055 {
00056
00057 }
00058
00059
00060 f32 SoundManager::getGlobalVolume() const
00061 {
00062 return sf::Listener::GetGlobalVolume();
00063 }
00064
00065
00066 sf::SoundBuffer* SoundManager::getSoundBuffer(const std::string &fileName)
00067 {
00068 std::map<std::string, sf::SoundBuffer*>::iterator it = mSoundBuffers.find(fileName);
00069
00070 if(it != mSoundBuffers.end())
00071 return it->second;
00072
00073 return NULL;
00074 }
00075
00076
00077 bool SoundManager::loadSoundBuffer(const std::string &fileName)
00078 {
00079
00080 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00081
00082
00083 core::stringw wFilename = core::stringw(fileName.c_str());
00084 std::string fileBase = (fileSystem->getFileBasename(wFilename)).c_str();
00085
00086 if(getSoundBuffer(fileBase) != NULL)
00087 return false;
00088
00089
00090 if(!fileSystem->existFile(fileName.c_str()))
00091 return false;
00092
00093
00094 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
00095
00096 if(!file)
00097 return false;
00098
00099 long fSize = file->getSize();
00100 c8 *fBuffer = new c8[fSize+1];
00101 memset(fBuffer, 0, fSize+1);
00102
00103 file->read(fBuffer, fSize);
00104 file->drop();
00105
00106
00107 sf::SoundBuffer *soundBuffer = new sf::SoundBuffer();
00108 soundBuffer->LoadFromMemory(fBuffer, fSize);
00109
00110 mSoundBuffers[fileBase] = soundBuffer;
00111
00112
00113 delete[] fBuffer;
00114
00115 return true;
00116 }
00117
00118
00119 bool SoundManager::loadSoundBufferFromMemory(const std::string &name, c8 *buffer, long length)
00120 {
00121
00122 if(getSoundBuffer(name) != NULL)
00123 return false;
00124
00125
00126 sf::SoundBuffer *soundBuffer = new sf::SoundBuffer();
00127 soundBuffer->LoadFromMemory(buffer, length);
00128
00129 mSoundBuffers[name] = soundBuffer;
00130
00131
00132 return true;
00133 }
00134
00135
00136 void SoundManager::removeSoundBuffers()
00137 {
00138 std::map<std::string, sf::SoundBuffer*>::iterator it;
00139 for(it = mSoundBuffers.begin(); it != mSoundBuffers.end(); it++)
00140 delete it->second;
00141
00142 mSoundBuffers.clear();
00143 }
00144
00145
00146 bool SoundManager::removeSoundBuffer(const std::string &fileName)
00147 {
00148
00149 std::map<std::string, sf::SoundBuffer*>::iterator it = mSoundBuffers.find(fileName);
00150
00151 if(it != mSoundBuffers.end())
00152 {
00153 sf::SoundBuffer *soundBuffer = it->second;
00154
00155 delete soundBuffer;
00156 mSoundBuffers.erase(it);
00157
00158 return true;
00159 }
00160
00161
00162 return false;
00163 }
00164
00165
00166 void SoundManager::setGlobalVolume(f32 volume)
00167 {
00168 sf::Listener::SetGlobalVolume(volume);
00169 }
00170
00171 #endif // __COMPILE_WITH_SFML_AUDIO__
00172
00173