/tags/jet3d_dev_msvc2003/source/Tools/GameShell/SoundMgr.cpp
C++ | 603 lines | 480 code | 119 blank | 4 comment | 82 complexity | f1ee5c04454708818621bd03c0e2d96c 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();
- this->register_func("AddSound", cpp_method(this, CSoundMgr, EOSAddSound));
- this->register_func("Add3DSound", cpp_method(this, CSoundMgr, EOSAdd3DSound));
- this->register_func("RemoveSound", cpp_method(this, CSoundMgr, EOSRemoveSound));
- this->register_func("HasSound", cpp_method(this, CSoundMgr, EOSHasSound));
- this->register_func("PlaySound", cpp_method(this, CSoundMgr, EOSPlaySound));
- this->register_func("StopSound", cpp_method(this, CSoundMgr, EOSStopSound));
- this->register_func("ModifySound", cpp_method(this, CSoundMgr, EOSModifySound));
- this->register_func("IsSoundPlaying", cpp_method(this, CSoundMgr, EOSIsSoundPlaying));
- this->register_func("SetMasterVolume", cpp_method(this, CSoundMgr, EOSSetMasterVolume));
- }
- 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;
- }
- CScriptMgr::GetPtr()->BindGlobal("$SndMgr", 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(const char *name, const char *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, 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(const char *name, const char *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, 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(const char *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(const char *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(const char *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(const char *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(const char *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(const char *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);
- }
- void CSoundMgr::EOSAddSound()
- {
- std::string name, filename;
- jeBoolean res;
- name = exe->pop()->gets(0);
- filename = exe->pop()->gets(0);
- res = this->AddSound(name.c_str(), filename.c_str());
- exe->push((int)res);
- }
- void CSoundMgr::EOSAdd3DSound()
- {
- std::string name, filename;
- jeBoolean res;
- name = exe->pop()->gets(0);
- filename = exe->pop()->gets(0);
- res = this->Add3DSound(name.c_str(), filename.c_str());
- exe->push((int)res);
- }
- void CSoundMgr::EOSHasSound()
- {
- std::string name;
- jeBoolean res;
- name = exe->pop()->gets(0);
- res = this->HasSound(name.c_str());
- exe->push((int)res);
- }
- void CSoundMgr::EOSRemoveSound()
- {
- std::string name;
- jeBoolean res;
- name = exe->pop()->gets(0);
- res = this->RemoveSound(name.c_str());
- exe->push((int)res);
- }
- void CSoundMgr::EOSPlaySound()
- {
- std::string name;
- float vol, pan, freq;
- jeBoolean loop, res;
- name = exe->pop()->gets(0);
- vol = exe->pop()->getd(0);
- pan = exe->pop()->getd(0);
- freq = exe->pop()->getd(0);
- loop = (jeBoolean)exe->pop()->geti(0);
- res = this->PlaySound(name.c_str(), vol, pan, freq, loop);
- exe->push((int)res);
- }
- void CSoundMgr::EOSStopSound()
- {
- std::string name;
- jeBoolean res;
- name = exe->pop()->gets(0);
- res = this->StopSound(name.c_str());
- exe->push((int)res);
- }
- void CSoundMgr::EOSModifySound()
- {
- std::string name;
- float vol, pan, freq;
- jeBoolean res;
- name = exe->pop()->gets(0);
- vol = exe->pop()->getd(0);
- pan = exe->pop()->getd(0);
- freq = exe->pop()->getd(0);
- res = this->ModifySound(name.c_str(), vol, pan, freq);
- exe->push((int)res);
- }
- void CSoundMgr::EOSIsSoundPlaying()
- {
- std::string name;
- jeBoolean res;
- name = exe->pop()->gets(0);
- res = this->IsSoundPlaying(name.c_str());
- exe->push((int)res);
- }
- void CSoundMgr::EOSSetMasterVolume()
- {
- float vol;
- jeBoolean res;
- vol = exe->pop()->getd(0);
- res = this->SetMasterVolume(vol);
- exe->push((int)res);
- }