/branches/JSTUDIO_VERSION_2_X/source/Tools/GameShell/SoundMgr.cpp
# · C++ · 501 lines · 398 code · 98 blank · 5 comment · 82 complexity · 5a0cfa8477973f2ccbe53641d01fa34e MD5 · raw file
- #include <assert.h>
- #include "SoundMgr.h"
- #include "ScriptMgr.h"
- #include "jeResource.h"
- #include "GameMgr.h"
- #include "GameLog.h"
- CSoundMgr *CSoundMgr::Singleton = NULL;
- CSoundMgr *CSoundMgr::GetPtr()
- {
- if (CSoundMgr::Singleton == NULL)
- CSoundMgr::Singleton = new CSoundMgr();
- return CSoundMgr::Singleton;
- }
- CSoundMgr::CSoundMgr()
- {
- m_pSoundSys = NULL;
- m_pResMgr = NULL;
- m_Sounds.clear();
- m_3DSounds.clear();
- }
- CSoundMgr::~CSoundMgr()
- {
- Shutdown();
- }
- jeBoolean CSoundMgr::Release()
- {
- GLOG("CSoundMgr - Releasing memory...");
- delete this;
- return JE_TRUE;
- }
- jeBoolean CSoundMgr::Initialize(HWND hWnd)
- {
- GLOG("CSoundMgr - Initializing sound system...");
- m_pSoundSys = jeSound_CreateSoundSystem(hWnd);
- if (!m_pSoundSys)
- {
- GLOG("CSoundMgr - Could not create sound system!!");
- return JE_FALSE;
- }
- m_pResMgr = CGameMgr::GetPtr()->GetResourceManager();
- if (!m_pResMgr)
- {
- GLOG("CSoundMgr - Could not get resource manager!!");
- return JE_FALSE;
- }
- jeResource_MgrIncRefcount(m_pResMgr);
- if (!jeResource_OpenDirectory(m_pResMgr, "Sounds", "Sounds"))
- {
- GLOG("CSoundMgr - Could not register sounds directory with resource manager!!");
- return JE_FALSE;
- }
- if (!jeResource_OpenDirectory(m_pResMgr, "Music", "Music"))
- {
- GLOG("CSoundMgr - Could not register music directory with resource manager!!");
- return JE_FALSE;
- }
- // Register Singleton with AngelScript
- g_pScriptEngine->RegisterObjectType("SoundMgr", 0, 0);
- g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int AddSound(string &name, string &filename)", asMETHOD(CSoundMgr, AddSound), asCALL_THISCALL);
- g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int Add3DSound(string &name, string &filename)", asMETHOD(CSoundMgr, Add3DSound), asCALL_THISCALL);
- g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int RemoveSound(string &name)", asMETHOD(CSoundMgr, RemoveSound), asCALL_THISCALL);
- g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int HasSound(string &name)", asMETHOD(CSoundMgr, HasSound), asCALL_THISCALL);
- g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int PlaySound(string &name, float vol, float pan, float freq, int loop)", asMETHOD(CSoundMgr, PlaySound), asCALL_THISCALL);
- g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int StopSound(string &name)", asMETHOD(CSoundMgr, StopSound), asCALL_THISCALL);
- g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int ModifySound(string &name, float vol, float pan, float freq)", asMETHOD(CSoundMgr, ModifySound), asCALL_THISCALL);
- g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int IsSoundPlaying(string &name)", asMETHOD(CSoundMgr, IsSoundPlaying), asCALL_THISCALL);
- g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int SetMasterVolume(float vol)", asMETHOD(CSoundMgr, SetMasterVolume), asCALL_THISCALL);
- g_pScriptEngine->RegisterGlobalProperty("SoundMgr SMgr", (void*)this);
- return JE_TRUE;
- }
- void CSoundMgr::Shutdown()
- {
- std::map<std::string, GameSound*>::iterator i;
- i = m_Sounds.begin();
- while (i != m_Sounds.end())
- {
- JE_SAFE_DELETE(i->second);
- i++;
- }
- m_Sounds.clear();
- i = m_3DSounds.begin();
- while (i != m_3DSounds.end())
- {
- JE_SAFE_DELETE(i->second);
- i++;
- }
- m_3DSounds.clear();
- jeResource_ReleaseResource(m_pResMgr, JE_RESOURCE_VFS, "Music");
- jeResource_ReleaseResource(m_pResMgr, JE_RESOURCE_VFS, "Sounds");
- jeResource_MgrDestroy(&m_pResMgr);
- jeSound_DestroySoundSystem(m_pSoundSys);
- m_pSoundSys = NULL;
- }
- jeBoolean CSoundMgr::AddSound(std::string &name, std::string &filename)
- {
- GameSound *gs = NULL;
- jeVFile *file = NULL, *dir = NULL;
- if (HasSound(name))
- return JE_TRUE;
- gs = new GameSound;
- if (!gs)
- {
- GLOG("CSoundMgr - Out of memory!!");
- return JE_FALSE;
- }
- gs->Sound = NULL;
- gs->SoundDef = NULL;
- dir = jeResource_GetVFile(m_pResMgr, "Sounds");
- if (!dir)
- {
- GLOG("CSoundMgr - Sounds directory not registered with resource manager!!");
- JE_SAFE_DELETE(gs);
- return JE_FALSE;
- }
- file = jeVFile_Open(dir, filename.c_str(), JE_VFILE_OPEN_READONLY);
- if (!file)
- {
- CGameLog::GetPtr()->Printf("CSoundMgr - Could not load %s!!", filename);
- JE_SAFE_DELETE(gs);
- return JE_FALSE;
- }
- gs->SoundDef = jeSound_LoadSoundDef(m_pSoundSys, file);
- if (!gs->SoundDef)
- {
- GLOG("CSoundMgr - Could not create sound def!!");
- jeVFile_Close(file);
- JE_SAFE_DELETE(gs);
- return JE_FALSE;
- }
-
- jeVFile_Close(file);
- file = NULL;
- m_Sounds[name] = gs;
- CGameLog::GetPtr()->Printf("CSoundMgr - Added %s as %s...", filename, name);
- return JE_TRUE;
- }
- jeBoolean CSoundMgr::Add3DSound(std::string &name, std::string &filename)
- {
- GameSound *gs = NULL;
- jeVFile *file = NULL, *dir = NULL;
- if (HasSound(name))
- return JE_TRUE;
- gs = new GameSound;
- if (!gs)
- {
- GLOG("CSoundMgr - Out of memory!!");
- return JE_FALSE;
- }
- gs->Sound = NULL;
- gs->SoundDef = NULL;
- dir = jeResource_GetVFile(m_pResMgr, "Sounds");
- if (!dir)
- {
- GLOG("CSoundMgr - Sounds directory not registered with resource manager!!");
- JE_SAFE_DELETE(gs);
- return JE_FALSE;
- }
- file = jeVFile_Open(dir, filename.c_str(), JE_VFILE_OPEN_READONLY);
- if (!file)
- {
- CGameLog::GetPtr()->Printf("CSoundMgr - Could not load %s!!", filename);
- JE_SAFE_DELETE(gs);
- return JE_FALSE;
- }
- gs->SoundDef = jeSound_LoadSoundDef(m_pSoundSys, file);
- if (!gs->SoundDef)
- {
- GLOG("CSoundMgr - Could not create sound def!!");
- jeVFile_Close(file);
- JE_SAFE_DELETE(gs);
- return JE_FALSE;
- }
-
- jeVFile_Close(file);
- file = NULL;
- m_3DSounds[name] = gs;
- CGameLog::GetPtr()->Printf("CSoundMgr - Added %s as %s...", filename, name);
- return JE_TRUE;
- }
- jeBoolean CSoundMgr::RemoveSound(std::string &name)
- {
- std::map<std::string, GameSound*>::iterator i;
- if (!HasSound(name))
- {
- CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
- return JE_FALSE;
- }
- i = m_Sounds.begin();
- while (i != m_Sounds.end())
- {
- if (i->first == name)
- {
- JE_SAFE_DELETE(i->second);
- m_Sounds.erase(i);
- CGameLog::GetPtr()->Printf("CSoundMgr - Removed sound %s...", name);
- return JE_TRUE;
- }
- i++;
- }
- i = m_3DSounds.begin();
- while (i != m_3DSounds.end())
- {
- if (i->first == name)
- {
- JE_SAFE_DELETE(i->second);
- m_3DSounds.erase(i);
- CGameLog::GetPtr()->Printf("CSoundMgr - Removed 3D sound %s...", name);
- return JE_TRUE;
- }
- i++;
- }
- CGameLog::GetPtr()->Printf("CSoundMgr - Could not find sound %s!!", name);
- return JE_FALSE;
- }
- jeBoolean CSoundMgr::HasSound(std::string &name)
- {
- std::map<std::string, GameSound*>::iterator i;
- i = m_Sounds.begin();
- while (i != m_Sounds.end())
- {
- if (i->first == name)
- return JE_TRUE;
- i++;
- }
- i = m_3DSounds.begin();
- while (i != m_3DSounds.end())
- {
- if (i->first == name)
- return JE_TRUE;
- i++;
- }
- return JE_FALSE;
- }
- jeBoolean CSoundMgr::PlaySound(std::string &name, float vol, float pan, float freq, jeBoolean loop)
- {
- std::map<std::string, GameSound*>::iterator i;
- if (!HasSound(name))
- {
- CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
- return JE_FALSE;
- }
- i = m_Sounds.begin();
- while (i != m_Sounds.end())
- {
- if (i->first == name)
- {
- i->second->Sound = jeSound_PlaySoundDef(m_pSoundSys, i->second->SoundDef, vol, pan, freq, loop);
- if (!i->second->Sound)
- {
- GLOG("CSoundMgr - Could not play sound!!");
- return JE_FALSE;
- }
- CGameLog::GetPtr()->Printf("CSoundMgr - Playing sound %s...", name);
- return JE_TRUE;
- }
- i++;
- }
- // 3D Sounds always loop
- i = m_3DSounds.begin();
- while (i != m_3DSounds.end())
- {
- if (i->first == name)
- {
- i->second->Sound = jeSound_PlaySoundDef(m_pSoundSys, i->second->SoundDef, vol, pan, freq, JE_TRUE);
- if (!i->second->Sound)
- {
- GLOG("CSoundMgr - Could not play sound!!");
- return JE_FALSE;
- }
- CGameLog::GetPtr()->Printf("CSoundMgr - Playing 3D sound %s...", name);
- return JE_TRUE;
- }
- i++;
- }
- CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
- return JE_FALSE;
- }
- jeBoolean CSoundMgr::StopSound(std::string &name)
- {
- std::map<std::string, GameSound*>::iterator i;
- if (!HasSound(name))
- {
- CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
- return JE_FALSE;
- }
- i = m_Sounds.begin();
- while (i != m_Sounds.end())
- {
- if (i->first == name)
- {
- if (!jeSound_StopSound(m_pSoundSys, i->second->Sound))
- {
- GLOG("CSoundMgr - Could not stop sound!!");
- return JE_FALSE;
- }
- i->second->Sound = NULL;
-
- CGameLog::GetPtr()->Printf("CSoundMgr - Stopped sound %s...", name);
- return JE_TRUE;
- }
- i++;
- }
- // 3D Sounds always loop
- i = m_3DSounds.begin();
- while (i != m_3DSounds.end())
- {
- if (i->first == name)
- {
- if (!jeSound_StopSound(m_pSoundSys, i->second->Sound))
- {
- GLOG("CSoundMgr - Could not stop sound!!");
- return JE_FALSE;
- }
- i->second->Sound = NULL;
-
- CGameLog::GetPtr()->Printf("CSoundMgr - Stopped 3D sound %s...", name);
- return JE_TRUE;
- }
- i++;
- }
- CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
- return JE_FALSE;
- }
- jeBoolean CSoundMgr::ModifySound(std::string &name, float vol, float pan, float freq)
- {
- std::map<std::string, GameSound*>::iterator i;
- if (!HasSound(name))
- {
- CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
- return JE_FALSE;
- }
- i = m_Sounds.begin();
- while (i != m_Sounds.end())
- {
- if (i->first == name)
- {
- if (!jeSound_SoundIsPlaying(m_pSoundSys, i->second->Sound))
- {
- GLOG("CSoundMgr - Sound is not playing!!");
- return JE_FALSE;
- }
- if (!jeSound_ModifySound(m_pSoundSys, i->second->Sound, vol, pan, freq))
- {
- GLOG("CSoundMgr - Could not modify sound!!");
- return JE_FALSE;
- }
- CGameLog::GetPtr()->Printf("CSoundMgr - Modified sound %s...", name);
- return JE_TRUE;
- }
- i++;
- }
- // 3D Sounds always loop
- i = m_3DSounds.begin();
- while (i != m_3DSounds.end())
- {
- if (i->first == name)
- {
- if (!jeSound_SoundIsPlaying(m_pSoundSys, i->second->Sound))
- {
- GLOG("CSoundMgr - Sound is not playing!!");
- return JE_FALSE;
- }
- if (!jeSound_ModifySound(m_pSoundSys, i->second->Sound, vol, pan, freq))
- {
- GLOG("CSoundMgr - Could not modify sound!!");
- return JE_FALSE;
- }
- CGameLog::GetPtr()->Printf("CSoundMgr - Modified 3D sound %s...", name);
- return JE_TRUE;
- }
- i++;
- }
- CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
- return JE_FALSE;
- }
- jeBoolean CSoundMgr::IsSoundPlaying(std::string &name)
- {
- std::map<std::string, GameSound*>::iterator i;
- if (!HasSound(name))
- {
- CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
- return JE_FALSE;
- }
- i = m_Sounds.begin();
- while (i != m_Sounds.end())
- {
- if (i->first == name)
- return jeSound_SoundIsPlaying(m_pSoundSys, i->second->Sound);
- i++;
- }
- // 3D Sounds always loop
- i = m_3DSounds.begin();
- while (i != m_3DSounds.end())
- {
- if (i->first == name)
- return jeSound_SoundIsPlaying(m_pSoundSys, i->second->Sound);
- i++;
- }
- CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
- return JE_FALSE;
- }
- jeBoolean CSoundMgr::SetMasterVolume(float vol)
- {
- assert(m_pSoundSys != NULL);
- assert(vol > 0.0f);
- return jeSound_SetMasterVolume(m_pSoundSys, vol);
- }