/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

  1. #include <assert.h>
  2. #include "SoundMgr.h"
  3. #include "ScriptMgr.h"
  4. #include "jeResource.h"
  5. #include "GameMgr.h"
  6. #include "GameLog.h"
  7. CSoundMgr *CSoundMgr::Singleton = NULL;
  8. CSoundMgr *CSoundMgr::GetPtr()
  9. {
  10. if (CSoundMgr::Singleton == NULL)
  11. CSoundMgr::Singleton = new CSoundMgr();
  12. return CSoundMgr::Singleton;
  13. }
  14. CSoundMgr::CSoundMgr()
  15. {
  16. m_pSoundSys = NULL;
  17. m_pResMgr = NULL;
  18. m_Sounds.clear();
  19. m_3DSounds.clear();
  20. }
  21. CSoundMgr::~CSoundMgr()
  22. {
  23. Shutdown();
  24. }
  25. jeBoolean CSoundMgr::Release()
  26. {
  27. GLOG("CSoundMgr - Releasing memory...");
  28. delete this;
  29. return JE_TRUE;
  30. }
  31. jeBoolean CSoundMgr::Initialize(HWND hWnd)
  32. {
  33. GLOG("CSoundMgr - Initializing sound system...");
  34. m_pSoundSys = jeSound_CreateSoundSystem(hWnd);
  35. if (!m_pSoundSys)
  36. {
  37. GLOG("CSoundMgr - Could not create sound system!!");
  38. return JE_FALSE;
  39. }
  40. m_pResMgr = CGameMgr::GetPtr()->GetResourceManager();
  41. if (!m_pResMgr)
  42. {
  43. GLOG("CSoundMgr - Could not get resource manager!!");
  44. return JE_FALSE;
  45. }
  46. jeResource_MgrIncRefcount(m_pResMgr);
  47. if (!jeResource_OpenDirectory(m_pResMgr, "Sounds", "Sounds"))
  48. {
  49. GLOG("CSoundMgr - Could not register sounds directory with resource manager!!");
  50. return JE_FALSE;
  51. }
  52. if (!jeResource_OpenDirectory(m_pResMgr, "Music", "Music"))
  53. {
  54. GLOG("CSoundMgr - Could not register music directory with resource manager!!");
  55. return JE_FALSE;
  56. }
  57. // Register Singleton with AngelScript
  58. g_pScriptEngine->RegisterObjectType("SoundMgr", 0, 0);
  59. g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int AddSound(string &name, string &filename)", asMETHOD(CSoundMgr, AddSound), asCALL_THISCALL);
  60. g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int Add3DSound(string &name, string &filename)", asMETHOD(CSoundMgr, Add3DSound), asCALL_THISCALL);
  61. g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int RemoveSound(string &name)", asMETHOD(CSoundMgr, RemoveSound), asCALL_THISCALL);
  62. g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int HasSound(string &name)", asMETHOD(CSoundMgr, HasSound), asCALL_THISCALL);
  63. g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int PlaySound(string &name, float vol, float pan, float freq, int loop)", asMETHOD(CSoundMgr, PlaySound), asCALL_THISCALL);
  64. g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int StopSound(string &name)", asMETHOD(CSoundMgr, StopSound), asCALL_THISCALL);
  65. g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int ModifySound(string &name, float vol, float pan, float freq)", asMETHOD(CSoundMgr, ModifySound), asCALL_THISCALL);
  66. g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int IsSoundPlaying(string &name)", asMETHOD(CSoundMgr, IsSoundPlaying), asCALL_THISCALL);
  67. g_pScriptEngine->RegisterObjectMethod("SoundMgr", "int SetMasterVolume(float vol)", asMETHOD(CSoundMgr, SetMasterVolume), asCALL_THISCALL);
  68. g_pScriptEngine->RegisterGlobalProperty("SoundMgr SMgr", (void*)this);
  69. return JE_TRUE;
  70. }
  71. void CSoundMgr::Shutdown()
  72. {
  73. std::map<std::string, GameSound*>::iterator i;
  74. i = m_Sounds.begin();
  75. while (i != m_Sounds.end())
  76. {
  77. JE_SAFE_DELETE(i->second);
  78. i++;
  79. }
  80. m_Sounds.clear();
  81. i = m_3DSounds.begin();
  82. while (i != m_3DSounds.end())
  83. {
  84. JE_SAFE_DELETE(i->second);
  85. i++;
  86. }
  87. m_3DSounds.clear();
  88. jeResource_ReleaseResource(m_pResMgr, JE_RESOURCE_VFS, "Music");
  89. jeResource_ReleaseResource(m_pResMgr, JE_RESOURCE_VFS, "Sounds");
  90. jeResource_MgrDestroy(&m_pResMgr);
  91. jeSound_DestroySoundSystem(m_pSoundSys);
  92. m_pSoundSys = NULL;
  93. }
  94. jeBoolean CSoundMgr::AddSound(std::string &name, std::string &filename)
  95. {
  96. GameSound *gs = NULL;
  97. jeVFile *file = NULL, *dir = NULL;
  98. if (HasSound(name))
  99. return JE_TRUE;
  100. gs = new GameSound;
  101. if (!gs)
  102. {
  103. GLOG("CSoundMgr - Out of memory!!");
  104. return JE_FALSE;
  105. }
  106. gs->Sound = NULL;
  107. gs->SoundDef = NULL;
  108. dir = jeResource_GetVFile(m_pResMgr, "Sounds");
  109. if (!dir)
  110. {
  111. GLOG("CSoundMgr - Sounds directory not registered with resource manager!!");
  112. JE_SAFE_DELETE(gs);
  113. return JE_FALSE;
  114. }
  115. file = jeVFile_Open(dir, filename.c_str(), JE_VFILE_OPEN_READONLY);
  116. if (!file)
  117. {
  118. CGameLog::GetPtr()->Printf("CSoundMgr - Could not load %s!!", filename);
  119. JE_SAFE_DELETE(gs);
  120. return JE_FALSE;
  121. }
  122. gs->SoundDef = jeSound_LoadSoundDef(m_pSoundSys, file);
  123. if (!gs->SoundDef)
  124. {
  125. GLOG("CSoundMgr - Could not create sound def!!");
  126. jeVFile_Close(file);
  127. JE_SAFE_DELETE(gs);
  128. return JE_FALSE;
  129. }
  130. jeVFile_Close(file);
  131. file = NULL;
  132. m_Sounds[name] = gs;
  133. CGameLog::GetPtr()->Printf("CSoundMgr - Added %s as %s...", filename, name);
  134. return JE_TRUE;
  135. }
  136. jeBoolean CSoundMgr::Add3DSound(std::string &name, std::string &filename)
  137. {
  138. GameSound *gs = NULL;
  139. jeVFile *file = NULL, *dir = NULL;
  140. if (HasSound(name))
  141. return JE_TRUE;
  142. gs = new GameSound;
  143. if (!gs)
  144. {
  145. GLOG("CSoundMgr - Out of memory!!");
  146. return JE_FALSE;
  147. }
  148. gs->Sound = NULL;
  149. gs->SoundDef = NULL;
  150. dir = jeResource_GetVFile(m_pResMgr, "Sounds");
  151. if (!dir)
  152. {
  153. GLOG("CSoundMgr - Sounds directory not registered with resource manager!!");
  154. JE_SAFE_DELETE(gs);
  155. return JE_FALSE;
  156. }
  157. file = jeVFile_Open(dir, filename.c_str(), JE_VFILE_OPEN_READONLY);
  158. if (!file)
  159. {
  160. CGameLog::GetPtr()->Printf("CSoundMgr - Could not load %s!!", filename);
  161. JE_SAFE_DELETE(gs);
  162. return JE_FALSE;
  163. }
  164. gs->SoundDef = jeSound_LoadSoundDef(m_pSoundSys, file);
  165. if (!gs->SoundDef)
  166. {
  167. GLOG("CSoundMgr - Could not create sound def!!");
  168. jeVFile_Close(file);
  169. JE_SAFE_DELETE(gs);
  170. return JE_FALSE;
  171. }
  172. jeVFile_Close(file);
  173. file = NULL;
  174. m_3DSounds[name] = gs;
  175. CGameLog::GetPtr()->Printf("CSoundMgr - Added %s as %s...", filename, name);
  176. return JE_TRUE;
  177. }
  178. jeBoolean CSoundMgr::RemoveSound(std::string &name)
  179. {
  180. std::map<std::string, GameSound*>::iterator i;
  181. if (!HasSound(name))
  182. {
  183. CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
  184. return JE_FALSE;
  185. }
  186. i = m_Sounds.begin();
  187. while (i != m_Sounds.end())
  188. {
  189. if (i->first == name)
  190. {
  191. JE_SAFE_DELETE(i->second);
  192. m_Sounds.erase(i);
  193. CGameLog::GetPtr()->Printf("CSoundMgr - Removed sound %s...", name);
  194. return JE_TRUE;
  195. }
  196. i++;
  197. }
  198. i = m_3DSounds.begin();
  199. while (i != m_3DSounds.end())
  200. {
  201. if (i->first == name)
  202. {
  203. JE_SAFE_DELETE(i->second);
  204. m_3DSounds.erase(i);
  205. CGameLog::GetPtr()->Printf("CSoundMgr - Removed 3D sound %s...", name);
  206. return JE_TRUE;
  207. }
  208. i++;
  209. }
  210. CGameLog::GetPtr()->Printf("CSoundMgr - Could not find sound %s!!", name);
  211. return JE_FALSE;
  212. }
  213. jeBoolean CSoundMgr::HasSound(std::string &name)
  214. {
  215. std::map<std::string, GameSound*>::iterator i;
  216. i = m_Sounds.begin();
  217. while (i != m_Sounds.end())
  218. {
  219. if (i->first == name)
  220. return JE_TRUE;
  221. i++;
  222. }
  223. i = m_3DSounds.begin();
  224. while (i != m_3DSounds.end())
  225. {
  226. if (i->first == name)
  227. return JE_TRUE;
  228. i++;
  229. }
  230. return JE_FALSE;
  231. }
  232. jeBoolean CSoundMgr::PlaySound(std::string &name, float vol, float pan, float freq, jeBoolean loop)
  233. {
  234. std::map<std::string, GameSound*>::iterator i;
  235. if (!HasSound(name))
  236. {
  237. CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
  238. return JE_FALSE;
  239. }
  240. i = m_Sounds.begin();
  241. while (i != m_Sounds.end())
  242. {
  243. if (i->first == name)
  244. {
  245. i->second->Sound = jeSound_PlaySoundDef(m_pSoundSys, i->second->SoundDef, vol, pan, freq, loop);
  246. if (!i->second->Sound)
  247. {
  248. GLOG("CSoundMgr - Could not play sound!!");
  249. return JE_FALSE;
  250. }
  251. CGameLog::GetPtr()->Printf("CSoundMgr - Playing sound %s...", name);
  252. return JE_TRUE;
  253. }
  254. i++;
  255. }
  256. // 3D Sounds always loop
  257. i = m_3DSounds.begin();
  258. while (i != m_3DSounds.end())
  259. {
  260. if (i->first == name)
  261. {
  262. i->second->Sound = jeSound_PlaySoundDef(m_pSoundSys, i->second->SoundDef, vol, pan, freq, JE_TRUE);
  263. if (!i->second->Sound)
  264. {
  265. GLOG("CSoundMgr - Could not play sound!!");
  266. return JE_FALSE;
  267. }
  268. CGameLog::GetPtr()->Printf("CSoundMgr - Playing 3D sound %s...", name);
  269. return JE_TRUE;
  270. }
  271. i++;
  272. }
  273. CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
  274. return JE_FALSE;
  275. }
  276. jeBoolean CSoundMgr::StopSound(std::string &name)
  277. {
  278. std::map<std::string, GameSound*>::iterator i;
  279. if (!HasSound(name))
  280. {
  281. CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
  282. return JE_FALSE;
  283. }
  284. i = m_Sounds.begin();
  285. while (i != m_Sounds.end())
  286. {
  287. if (i->first == name)
  288. {
  289. if (!jeSound_StopSound(m_pSoundSys, i->second->Sound))
  290. {
  291. GLOG("CSoundMgr - Could not stop sound!!");
  292. return JE_FALSE;
  293. }
  294. i->second->Sound = NULL;
  295. CGameLog::GetPtr()->Printf("CSoundMgr - Stopped sound %s...", name);
  296. return JE_TRUE;
  297. }
  298. i++;
  299. }
  300. // 3D Sounds always loop
  301. i = m_3DSounds.begin();
  302. while (i != m_3DSounds.end())
  303. {
  304. if (i->first == name)
  305. {
  306. if (!jeSound_StopSound(m_pSoundSys, i->second->Sound))
  307. {
  308. GLOG("CSoundMgr - Could not stop sound!!");
  309. return JE_FALSE;
  310. }
  311. i->second->Sound = NULL;
  312. CGameLog::GetPtr()->Printf("CSoundMgr - Stopped 3D sound %s...", name);
  313. return JE_TRUE;
  314. }
  315. i++;
  316. }
  317. CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
  318. return JE_FALSE;
  319. }
  320. jeBoolean CSoundMgr::ModifySound(std::string &name, float vol, float pan, float freq)
  321. {
  322. std::map<std::string, GameSound*>::iterator i;
  323. if (!HasSound(name))
  324. {
  325. CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
  326. return JE_FALSE;
  327. }
  328. i = m_Sounds.begin();
  329. while (i != m_Sounds.end())
  330. {
  331. if (i->first == name)
  332. {
  333. if (!jeSound_SoundIsPlaying(m_pSoundSys, i->second->Sound))
  334. {
  335. GLOG("CSoundMgr - Sound is not playing!!");
  336. return JE_FALSE;
  337. }
  338. if (!jeSound_ModifySound(m_pSoundSys, i->second->Sound, vol, pan, freq))
  339. {
  340. GLOG("CSoundMgr - Could not modify sound!!");
  341. return JE_FALSE;
  342. }
  343. CGameLog::GetPtr()->Printf("CSoundMgr - Modified sound %s...", name);
  344. return JE_TRUE;
  345. }
  346. i++;
  347. }
  348. // 3D Sounds always loop
  349. i = m_3DSounds.begin();
  350. while (i != m_3DSounds.end())
  351. {
  352. if (i->first == name)
  353. {
  354. if (!jeSound_SoundIsPlaying(m_pSoundSys, i->second->Sound))
  355. {
  356. GLOG("CSoundMgr - Sound is not playing!!");
  357. return JE_FALSE;
  358. }
  359. if (!jeSound_ModifySound(m_pSoundSys, i->second->Sound, vol, pan, freq))
  360. {
  361. GLOG("CSoundMgr - Could not modify sound!!");
  362. return JE_FALSE;
  363. }
  364. CGameLog::GetPtr()->Printf("CSoundMgr - Modified 3D sound %s...", name);
  365. return JE_TRUE;
  366. }
  367. i++;
  368. }
  369. CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
  370. return JE_FALSE;
  371. }
  372. jeBoolean CSoundMgr::IsSoundPlaying(std::string &name)
  373. {
  374. std::map<std::string, GameSound*>::iterator i;
  375. if (!HasSound(name))
  376. {
  377. CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
  378. return JE_FALSE;
  379. }
  380. i = m_Sounds.begin();
  381. while (i != m_Sounds.end())
  382. {
  383. if (i->first == name)
  384. return jeSound_SoundIsPlaying(m_pSoundSys, i->second->Sound);
  385. i++;
  386. }
  387. // 3D Sounds always loop
  388. i = m_3DSounds.begin();
  389. while (i != m_3DSounds.end())
  390. {
  391. if (i->first == name)
  392. return jeSound_SoundIsPlaying(m_pSoundSys, i->second->Sound);
  393. i++;
  394. }
  395. CGameLog::GetPtr()->Printf("CSoundMgr - Sound %s is not registered!!", name);
  396. return JE_FALSE;
  397. }
  398. jeBoolean CSoundMgr::SetMasterVolume(float vol)
  399. {
  400. assert(m_pSoundSys != NULL);
  401. assert(vol > 0.0f);
  402. return jeSound_SetMasterVolume(m_pSoundSys, vol);
  403. }