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