00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "SoundSourceComponent.h"
00018 #include "../../core/GameManager.h"
00019
00020
00021
00022
00023 SoundSourceComponent::SoundSourceComponent(Entity *parent)
00024 : EntityComponent(parent), mSound(NULL), mMusic(NULL), mBuffer(NULL), mAttenuation(1.0f),
00025 mLoop(false), mMinDistance(1.0f), mPitch(1.0f), mPosition(vector3df(0, 0, 0)), mVolume(100)
00026 {
00027
00028 EventManager *pEventMgr = GameManager::Instance()->getEventManager();
00029
00030
00031 mName = "SoundSourceComponent";
00032
00033
00034 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onPositionChange",
00035 this, &SoundSourceComponent::onPositionChange);
00036 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onPause",
00037 this, &SoundSourceComponent::onPause);
00038 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onUnPause",
00039 this, &SoundSourceComponent::onUnPause);
00040 }
00041
00042
00043 SoundSourceComponent::~SoundSourceComponent()
00044 {
00045 if(mSound != NULL)
00046 delete mSound;
00047
00048 if(mMusic != NULL)
00049 delete mMusic;
00050
00051 if(mBuffer != NULL)
00052 delete[] mBuffer;
00053 }
00054
00055
00056 SoundSourceComponent* SoundSourceComponent::refFactory(Entity *parent)
00057 {
00058 return new SoundSourceComponent(parent);
00059 }
00060
00061
00062 sf::Sound* SoundSourceComponent::getSound()
00063 {
00064 return mSound;
00065 }
00066
00067
00068 sf::Music* SoundSourceComponent::getMusic()
00069 {
00070 return mMusic;
00071 }
00072
00073
00074 bool SoundSourceComponent::loadMusic(const std::string &fileName)
00075 {
00076
00077 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00078
00079
00080 unloadData();
00081
00082
00083 if(!fileSystem->existFile(fileName.c_str()))
00084 return false;
00085
00086
00087 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
00088
00089 mBufferSize = file->getSize();
00090 mBuffer = new c8[mBufferSize+1];
00091 file->read(mBuffer, mBufferSize);
00092 file->drop();
00093
00094
00095 mMusic = new sf::Music();
00096 mMusic->OpenFromMemory(mBuffer, mBufferSize);
00097
00098
00099 mMusic->SetAttenuation(mAttenuation);
00100 mMusic->SetLoop(mLoop);
00101 mMusic->SetMinDistance(mMinDistance);
00102 mMusic->SetPitch(mPitch);
00103 mMusic->SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00104 mMusic->SetVolume(mVolume);
00105
00106 return true;
00107 }
00108
00109
00110 bool SoundSourceComponent::loadSoundBuffer(const std::string &bufferName)
00111 {
00112
00113 unloadData();
00114
00115
00116 mSound = new sf::Sound();
00117
00118 sf::SoundBuffer *soundBuffer = GameManager::Instance()->getSoundManager()->getSoundBuffer(bufferName);
00119 if(soundBuffer == NULL)
00120 {
00121 delete mSound;
00122 mSound = NULL;
00123 return false;
00124 }
00125
00126 mSound->SetBuffer(*soundBuffer);
00127 std::cout << mSound->GetBuffer()->GetDuration() << "\n";
00128
00129 mSound->SetAttenuation(mAttenuation);
00130 mSound->SetLoop(mLoop);
00131 mSound->SetMinDistance(mMinDistance);
00132 mSound->SetPitch(mPitch);
00133 mSound->SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00134 mSound->SetVolume(mVolume);
00135
00136 return true;
00137 }
00138
00139
00140 void SoundSourceComponent::unloadData()
00141 {
00142 if(mSound != NULL)
00143 {
00144 delete mSound;
00145 mSound = NULL;
00146 }
00147
00148 if(mMusic != NULL)
00149 {
00150 delete mMusic;
00151 mMusic = NULL;
00152 }
00153
00154 if(mBuffer != NULL)
00155 {
00156 delete[] mBuffer;
00157 mBuffer = NULL;
00158 mBufferSize = 0;
00159 }
00160 }
00161
00162
00163 void SoundSourceComponent::play()
00164 {
00165 if(mSound != NULL)
00166 mSound->Play();
00167
00168 else if(mMusic != NULL)
00169 mMusic->Play();
00170 }
00171
00172
00173 void SoundSourceComponent::pause()
00174 {
00175 if(mSound != NULL)
00176 mSound->Pause();
00177
00178 else if(mMusic != NULL)
00179 mMusic->Pause();
00180 }
00181
00182
00183 void SoundSourceComponent::stop()
00184 {
00185 if(mSound != NULL)
00186 mSound->Stop();
00187
00188 else if(mMusic != NULL)
00189 mMusic->Stop();
00190 }
00191
00192
00193 f32 SoundSourceComponent::getAttenuation() const
00194 {
00195 return mAttenuation;
00196 }
00197
00198
00199 bool SoundSourceComponent::getIsPaused() const
00200 {
00201 if(mSound != NULL)
00202 {
00203 if(mSound->GetStatus() == sf::Sound::Paused)
00204 return true;
00205 }
00206
00207 else if(mMusic != NULL)
00208 {
00209 if(mMusic->GetStatus() == sf::Sound::Paused)
00210 return true;
00211 }
00212
00213 return false;
00214 }
00215
00216
00217 bool SoundSourceComponent::getIsPlaying() const
00218 {
00219 if(mSound != NULL)
00220 {
00221 if(mSound->GetStatus() == sf::Sound::Playing)
00222 return true;
00223 }
00224
00225 else if(mMusic != NULL)
00226 {
00227 if(mMusic->GetStatus() == sf::Sound::Playing)
00228 return true;
00229 }
00230
00231 return false;
00232 }
00233
00234
00235 bool SoundSourceComponent::getIsStopped() const
00236 {
00237 if(mSound != NULL)
00238 {
00239 if(mSound->GetStatus() == sf::Sound::Stopped)
00240 return true;
00241 }
00242
00243 else if(mMusic != NULL)
00244 {
00245 if(mMusic->GetStatus() == sf::Sound::Stopped)
00246 return true;
00247 }
00248
00249 return false;
00250 }
00251
00252
00253 bool SoundSourceComponent::getLoop() const
00254 {
00255 return mLoop;
00256 }
00257
00258
00259 f32 SoundSourceComponent::getMinDistance() const
00260 {
00261 return mMinDistance;
00262 }
00263
00264
00265
00266 f32 SoundSourceComponent::getOffset() const
00267 {
00268 if(mSound != NULL)
00269 return mSound->GetPlayingOffset();
00270
00271 return 0.0f;
00272 }
00273
00274
00275 f32 SoundSourceComponent::getPitch() const
00276 {
00277 return mPitch;
00278 }
00279
00280
00281 f32 SoundSourceComponent::getVolume() const
00282 {
00283 return mVolume;
00284 }
00285
00286
00287
00288 void SoundSourceComponent::setAttenuation(f32 attenuation)
00289 {
00290 if(mSound != NULL)
00291 mSound->SetAttenuation(attenuation);
00292
00293 else if(mMusic != NULL)
00294 mMusic->SetAttenuation(attenuation);
00295
00296 mAttenuation = attenuation;
00297 }
00298
00299
00300 void SoundSourceComponent::setLoop(bool value)
00301 {
00302 if(mSound != NULL)
00303 mSound->SetLoop(value);
00304
00305 else if(mMusic != NULL)
00306 mMusic->SetLoop(value);
00307
00308 mLoop = value;
00309 }
00310
00311
00312
00313 void SoundSourceComponent::setMinDistance(f32 minDistance)
00314 {
00315 if(mSound != NULL)
00316 mSound->SetMinDistance(minDistance);
00317
00318 else if(mMusic != NULL)
00319 mMusic->SetMinDistance(minDistance);
00320
00321 mMinDistance = minDistance;
00322 }
00323
00324
00325
00326 void SoundSourceComponent::setOffset(f32 offset)
00327 {
00328 if(mSound != NULL)
00329 mSound->SetPlayingOffset(offset);
00330 }
00331
00332
00333 void SoundSourceComponent::setPitch(f32 pitch)
00334 {
00335 if(mSound != NULL)
00336 mSound->SetPitch(pitch);
00337
00338 else if(mMusic != NULL)
00339 mMusic->SetPitch(pitch);
00340
00341 mPitch = pitch;
00342 }
00343
00344
00345 void SoundSourceComponent::setVolume(f32 volume)
00346 {
00347 if(mSound != NULL)
00348 mSound->SetVolume(volume);
00349
00350 else if(mMusic != NULL)
00351 mMusic->SetVolume(volume);
00352
00353 mVolume = volume;
00354 }
00355
00356
00357
00358 void SoundSourceComponent::onPositionChange(void *p)
00359 {
00360 mPosition += static_cast<vector3df*>(p)[0];
00361
00362 if(mSound != NULL)
00363 mSound->SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00364
00365 else if(mMusic != NULL)
00366 mMusic->SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00367 }
00368
00369
00370 void SoundSourceComponent::onPause(void *p)
00371 {
00372 mWasPaused = getIsPaused();
00373 pause();
00374 }
00375
00376
00377 void SoundSourceComponent::onUnPause(void *p)
00378 {
00379 if(!mWasPaused)
00380 play();
00381 }
00382
00383
00384
00385 void bindSoundSourceComponent(asIScriptEngine *engine)
00386 {
00387
00388 int r;
00389
00390
00391 r = engine->RegisterObjectType("SoundSourceComponent", sizeof(SoundSourceComponent), asOBJ_REF); assert(r >= 0);
00392
00393
00394 bindEntityComponentBase<SoundSourceComponent>(engine, "SoundSourceComponent");
00395
00396
00397 r = engine->RegisterGlobalBehaviour(asBEHAVE_REF_CAST, "SoundSourceComponent@ f(EntityComponent @)",
00398 asFUNCTION((asRefCast<EntityComponent,SoundSourceComponent>)), asCALL_CDECL); assert( r >= 0 );
00399 r = engine->RegisterGlobalBehaviour(asBEHAVE_IMPLICIT_REF_CAST, "EntityComponent@ f(SoundSourceComponent @)",
00400 asFUNCTION((asRefCast<SoundSourceComponent,EntityComponent>)), asCALL_CDECL); assert( r >= 0 );
00401
00402 r = engine->RegisterObjectBehaviour("SoundSourceComponent", asBEHAVE_FACTORY, "SoundSourceComponent@ f(Entity @)",
00403 asFUNCTION(SoundSourceComponent::refFactory), asCALL_CDECL); assert(r >= 0);
00404
00405
00406 r = engine->RegisterObjectMethod("SoundSourceComponent", "bool loadMusic(const string &)",
00407 asMETHOD(SoundSourceComponent, loadMusic), asCALL_THISCALL); assert(r >= 0);
00408 r = engine->RegisterObjectMethod("SoundSourceComponent", "bool loadSoundBuffer(const string &)",
00409 asMETHOD(SoundSourceComponent, loadSoundBuffer), asCALL_THISCALL); assert(r >= 0);
00410
00411 r = engine->RegisterObjectMethod("SoundSourceComponent", "void play()",
00412 asMETHOD(SoundSourceComponent, play), asCALL_THISCALL); assert(r >= 0);
00413 r = engine->RegisterObjectMethod("SoundSourceComponent", "void pause()",
00414 asMETHOD(SoundSourceComponent, play), asCALL_THISCALL); assert(r >= 0);
00415 r = engine->RegisterObjectMethod("SoundSourceComponent", "void stop()",
00416 asMETHOD(SoundSourceComponent, play), asCALL_THISCALL); assert(r >= 0);
00417
00418 r = engine->RegisterObjectMethod("SoundSourceComponent", "f32 getAttenuation()",
00419 asMETHOD(SoundSourceComponent, getAttenuation), asCALL_THISCALL); assert(r >= 0);
00420 r = engine->RegisterObjectMethod("SoundSourceComponent", "bool getIsPlaying()",
00421 asMETHOD(SoundSourceComponent, getIsPlaying), asCALL_THISCALL); assert(r >= 0);
00422 r = engine->RegisterObjectMethod("SoundSourceComponent", "bool getIsPaused()",
00423 asMETHOD(SoundSourceComponent, getIsPaused), asCALL_THISCALL); assert(r >= 0);
00424 r = engine->RegisterObjectMethod("SoundSourceComponent", "bool getIsStopped()",
00425 asMETHOD(SoundSourceComponent, getIsStopped), asCALL_THISCALL); assert(r >= 0);
00426 r = engine->RegisterObjectMethod("SoundSourceComponent", "f32 getMinDistance()",
00427 asMETHOD(SoundSourceComponent, getMinDistance), asCALL_THISCALL); assert(r >= 0);
00428 r = engine->RegisterObjectMethod("SoundSourceComponent", "f32 getOffset()",
00429 asMETHOD(SoundSourceComponent, getOffset), asCALL_THISCALL); assert(r >= 0);
00430 r = engine->RegisterObjectMethod("SoundSourceComponent", "f32 getPitch()",
00431 asMETHOD(SoundSourceComponent, getPitch), asCALL_THISCALL); assert(r >= 0);
00432 r = engine->RegisterObjectMethod("SoundSourceComponent", "f32 getVolume()",
00433 asMETHOD(SoundSourceComponent, getVolume), asCALL_THISCALL); assert(r >= 0);
00434
00435 r = engine->RegisterObjectMethod("SoundSourceComponent", "void setAttenuation(f32)",
00436 asMETHOD(SoundSourceComponent, setAttenuation), asCALL_THISCALL); assert(r >= 0);
00437 r = engine->RegisterObjectMethod("SoundSourceComponent", "void setLoop(bool)",
00438 asMETHOD(SoundSourceComponent, setLoop), asCALL_THISCALL); assert(r >= 0);
00439 r = engine->RegisterObjectMethod("SoundSourceComponent", "void setMinDistance(f32)",
00440 asMETHOD(SoundSourceComponent, setMinDistance), asCALL_THISCALL); assert(r >= 0);
00441 r = engine->RegisterObjectMethod("SoundSourceComponent", "void setOffset(f32)",
00442 asMETHOD(SoundSourceComponent, setOffset), asCALL_THISCALL); assert(r >= 0);
00443 r = engine->RegisterObjectMethod("SoundSourceComponent", "void setPitch(f32)",
00444 asMETHOD(SoundSourceComponent, setPitch), asCALL_THISCALL); assert(r >= 0);
00445 r = engine->RegisterObjectMethod("SoundSourceComponent", "void setVolume(f32)",
00446 asMETHOD(SoundSourceComponent, setVolume), asCALL_THISCALL); assert(r >= 0);
00447 }
00448
00449