SoundNode.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : LevelEdit *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/LevelEdit/SoundNode.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 7/16/01 5:58p $*
  29. * *
  30. * $Revision:: 14 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "soundnode.h"
  37. #include "sceneeditor.h"
  38. #include "filemgr.h"
  39. #include "_assetmgr.h"
  40. #include "editorassetmgr.h"
  41. #include "w3d_file.h"
  42. #include "cameramgr.h"
  43. #include "collisiongroups.h"
  44. #include "persistfactory.h"
  45. #include "editorchunkids.h"
  46. #include "preset.h"
  47. #include "sound3d.h"
  48. #include "soundsettingspage.h"
  49. #include "nodeinfopage.h"
  50. #include "positionpage.h"
  51. #include "editorpropsheet.h"
  52. //////////////////////////////////////////////////////////////////////////////
  53. // Persist factories
  54. //////////////////////////////////////////////////////////////////////////////
  55. SimplePersistFactoryClass<SoundNodeClass, CHUNKID_NODE_NEW_SOUND> _NewSoundNodePersistFactory;
  56. class SoundNodePersistFactoryClass : public SimplePersistFactoryClass<SoundNodeClass, CHUNKID_NODE_OLD_SOUND>
  57. {
  58. public:
  59. PersistClass * Load (ChunkLoadClass &cload) const
  60. {
  61. SoundNodeClass * new_obj = new SoundNodeClass;
  62. SoundNodeClass * old_obj = NULL;
  63. //
  64. // Load the object's old pointer
  65. //
  66. cload.Open_Chunk();
  67. WWASSERT(cload.Cur_Chunk_ID() == SIMPLEFACTORY_CHUNKID_OBJPOINTER);
  68. cload.Read(&old_obj,sizeof(SoundNodeClass *));
  69. cload.Close_Chunk();
  70. //
  71. // Load the sound node object using a legacy load
  72. //
  73. cload.Open_Chunk();
  74. WWASSERT(cload.Cur_Chunk_ID() == SIMPLEFACTORY_CHUNKID_OBJDATA);
  75. new_obj->Use_Legacy_Load (true);
  76. new_obj->Load(cload);
  77. cload.Close_Chunk();
  78. SaveLoadSystemClass::Register_Pointer(old_obj,new_obj);
  79. return new_obj;
  80. }
  81. };
  82. SoundNodePersistFactoryClass _LegacySoundNodePersistFactory;
  83. //////////////////////////////////////////////////////////////////////////////
  84. // Save/load constants
  85. //////////////////////////////////////////////////////////////////////////////
  86. enum
  87. {
  88. CHUNKID_BASE_CLASS = 0x07160304,
  89. CHUNKID_INSTANCE_SETTINGS
  90. };
  91. //////////////////////////////////////////////////////////////////////////////
  92. //
  93. // SoundNodeClass
  94. //
  95. //////////////////////////////////////////////////////////////////////////////
  96. SoundNodeClass::SoundNodeClass (PresetClass *preset)
  97. : PhysObj (NULL),
  98. SoundObj (NULL),
  99. Sphere (NULL),
  100. OverridePreset (false),
  101. UseLegacyLoad (false),
  102. NodeClass (preset)
  103. {
  104. return ;
  105. }
  106. //////////////////////////////////////////////////////////////////////////////
  107. //
  108. // SoundNodeClass
  109. //
  110. //////////////////////////////////////////////////////////////////////////////
  111. SoundNodeClass::SoundNodeClass (const SoundNodeClass &src)
  112. : PhysObj (NULL),
  113. SoundObj (NULL),
  114. Sphere (NULL),
  115. OverridePreset (false),
  116. UseLegacyLoad (false),
  117. NodeClass (NULL)
  118. {
  119. *this = src;
  120. return ;
  121. }
  122. //////////////////////////////////////////////////////////////////////////////
  123. //
  124. // ~SoundNodeClass
  125. //
  126. //////////////////////////////////////////////////////////////////////////////
  127. SoundNodeClass::~SoundNodeClass (void)
  128. {
  129. Remove_From_Scene ();
  130. Release_Sound ();
  131. MEMBER_RELEASE (PhysObj);
  132. MEMBER_RELEASE (Sphere);
  133. return ;
  134. }
  135. //////////////////////////////////////////////////////////////////////////////
  136. //
  137. // Release_Sound
  138. //
  139. //////////////////////////////////////////////////////////////////////////////
  140. void
  141. SoundNodeClass::Release_Sound (void)
  142. {
  143. if (SoundObj != NULL) {
  144. SoundObj->Remove_From_Scene ();
  145. SoundObj->Stop ();
  146. SoundObj->Release_Ref ();
  147. SoundObj = NULL;
  148. }
  149. return ;
  150. }
  151. //////////////////////////////////////////////////////////////////////////////
  152. //
  153. // Initialize
  154. //
  155. // Note: This may be called more than once. It is used as an 'initialize'
  156. // and a 're-initialize'.
  157. //
  158. //////////////////////////////////////////////////////////////////////////////
  159. void
  160. SoundNodeClass::Initialize (void)
  161. {
  162. //
  163. // Start fresh
  164. //
  165. Show_Attenuation_Spheres (false);
  166. Release_Sound ();
  167. MEMBER_RELEASE (PhysObj);
  168. AudibleSoundDefinitionClass *definition = NULL;
  169. definition = static_cast<AudibleSoundDefinitionClass *> (m_Preset->Get_Definition ());
  170. if ( definition != NULL &&
  171. ::SuperClassID_From_ClassID (definition->Get_Class_ID ()) == CLASSID_SOUND)
  172. {
  173. //
  174. // Copy the preset's settings to our instance (if necessary)
  175. //
  176. if (OverridePreset == false) {
  177. InstanceSettings.Set_Volume (definition->Get_Volume ());
  178. InstanceSettings.Set_DropOff_Radius (definition->Get_DropOff_Radius ());
  179. InstanceSettings.Set_Start_Offset (definition->Get_Start_Offset ());
  180. InstanceSettings.Set_Pitch_Factor (definition->Get_Pitch_Factor ());
  181. InstanceSettings.Set_Max_Vol_Radius (definition->Get_Max_Vol_Radius ());
  182. }
  183. //
  184. // Create the render object this tile depends on
  185. //
  186. RenderObjClass *render_obj = ::Create_Render_Obj ("SPEAKER");
  187. WWASSERT (render_obj != NULL);
  188. if (render_obj != NULL) {
  189. // Create the new physics object
  190. PhysObj = new DecorationPhysClass;
  191. //
  192. // Configure the physics object with information about
  193. // its new render object and collision data.
  194. //
  195. PhysObj->Set_Model (render_obj);
  196. PhysObj->Set_Transform (Matrix3D(1));
  197. PhysObj->Set_Collision_Group (EDITOR_COLLISION_GROUP);
  198. PhysObj->Peek_Model ()->Set_User_Data ((PVOID)&m_HitTestInfo, FALSE);
  199. PhysObj->Peek_Model ()->Set_Collision_Type (COLLISION_TYPE_6);
  200. PhysObj->Set_Transform (m_Transform);
  201. //
  202. // Release our hold on the render object pointer
  203. //
  204. MEMBER_RELEASE (render_obj);
  205. //
  206. // Create the sound object
  207. //
  208. Update_Sound ();
  209. //
  210. // Update the position and orientation
  211. //
  212. Set_Transform (m_Transform);
  213. }
  214. }
  215. //
  216. // Make sure the attenuation spheres are correctly displayed
  217. //
  218. Show_Attenuation_Spheres (::Get_Scene_Editor ()->Are_Sound_Spheres_Displayed ());
  219. return ;
  220. }
  221. ////////////////////////////////////////////////////////////////
  222. //
  223. // Get_Factory
  224. //
  225. ////////////////////////////////////////////////////////////////
  226. const PersistFactoryClass &
  227. SoundNodeClass::Get_Factory (void) const
  228. {
  229. return _NewSoundNodePersistFactory;
  230. }
  231. /////////////////////////////////////////////////////////////////
  232. //
  233. // operator=
  234. //
  235. /////////////////////////////////////////////////////////////////
  236. const SoundNodeClass &
  237. SoundNodeClass::operator= (const SoundNodeClass &src)
  238. {
  239. NodeClass::operator= (src);
  240. return *this;
  241. }
  242. //////////////////////////////////////////////////////////////////////
  243. //
  244. // Add_To_Scene
  245. //
  246. //////////////////////////////////////////////////////////////////////
  247. void
  248. SoundNodeClass::Add_To_Scene (void)
  249. {
  250. if (SoundObj != NULL && SoundObj->Is_In_Scene () == false) {
  251. SoundObj->Add_To_Scene ();
  252. }
  253. if (Sphere != NULL) {
  254. Sphere->Display_Around_Node (*this);
  255. }
  256. NodeClass::Add_To_Scene ();
  257. return ;
  258. }
  259. //////////////////////////////////////////////////////////////////////
  260. //
  261. // Remove_From_Scene
  262. //
  263. //////////////////////////////////////////////////////////////////////
  264. void
  265. SoundNodeClass::Remove_From_Scene (void)
  266. {
  267. if (SoundObj != NULL && SoundObj->Is_In_Scene ()) {
  268. SoundObj->Stop ();
  269. SoundObj->Remove_From_Scene ();
  270. }
  271. if (Sphere != NULL) {
  272. Sphere->Remove_From_Scene ();
  273. }
  274. NodeClass::Remove_From_Scene ();
  275. return ;
  276. }
  277. //////////////////////////////////////////////////////////////////////
  278. //
  279. // Pre_Export
  280. //
  281. //////////////////////////////////////////////////////////////////////
  282. void
  283. SoundNodeClass::Pre_Export (void)
  284. {
  285. Add_Ref ();
  286. if (PhysObj != NULL && m_IsInScene) {
  287. ::Get_Scene_Editor ()->Remove_Object (PhysObj);
  288. }
  289. return ;
  290. }
  291. //////////////////////////////////////////////////////////////////////
  292. //
  293. // Post_Export
  294. //
  295. //////////////////////////////////////////////////////////////////////
  296. void
  297. SoundNodeClass::Post_Export (void)
  298. {
  299. if (PhysObj != NULL && m_IsInScene) {
  300. ::Get_Scene_Editor ()->Add_Dynamic_Object (PhysObj);
  301. }
  302. Release_Ref ();
  303. return ;
  304. }
  305. //////////////////////////////////////////////////////////////////////
  306. //
  307. // Show_Attenuation_Spheres
  308. //
  309. //////////////////////////////////////////////////////////////////////
  310. void
  311. SoundNodeClass::Show_Attenuation_Spheres (bool onoff)
  312. {
  313. if (onoff && Sphere == NULL) {
  314. Sphere = new AttenuationSphereClass;
  315. Sphere->Display_Around_Node (*this);
  316. Sphere->Set_Opacity (0.25F);
  317. Sphere->Set_Radius (Get_Attenuation_Radius ());
  318. //
  319. // Determine what color to make the sphere
  320. //
  321. if (m_Preset != NULL && m_Preset->Get_Definition () != NULL) {
  322. AudibleSoundDefinitionClass *definition = NULL;
  323. definition = static_cast<AudibleSoundDefinitionClass *> (m_Preset->Get_Definition ());
  324. if (::SuperClassID_From_ClassID (definition->Get_Class_ID ()) == CLASSID_SOUND) {
  325. Sphere->Set_Color (definition->Get_Sphere_Color ());
  326. }
  327. } else {
  328. Sphere->Set_Color (Vector3 (0, 0.75F, 0.75F));
  329. }
  330. } else if (onoff == false && Sphere != NULL) {
  331. Sphere->Remove_From_Scene ();
  332. MEMBER_RELEASE (Sphere);
  333. }
  334. return ;
  335. }
  336. /////////////////////////////////////////////////////////////////
  337. //
  338. // Set_ID
  339. //
  340. /////////////////////////////////////////////////////////////////
  341. void
  342. SoundNodeClass::Set_ID (uint32 id)
  343. {
  344. NodeClass::Set_ID (id);
  345. if (SoundObj != NULL) {
  346. SoundObj->Set_ID (m_ID);
  347. }
  348. return ;
  349. }
  350. /////////////////////////////////////////////////////////////////
  351. //
  352. // Update_Sound
  353. //
  354. /////////////////////////////////////////////////////////////////
  355. void
  356. SoundNodeClass::Update_Sound (void)
  357. {
  358. if (m_Preset == NULL) {
  359. return ;
  360. }
  361. //
  362. // Dig the definition out of the preset
  363. //
  364. AudibleSoundDefinitionClass *definition = NULL;
  365. definition = static_cast<AudibleSoundDefinitionClass *> (m_Preset->Get_Definition ());
  366. if (definition == NULL) {
  367. return ;
  368. }
  369. //
  370. // Start fresh
  371. //
  372. Release_Sound ();
  373. //
  374. // Make sure we 'get' all the assets locally that we need
  375. // for the preset.
  376. //
  377. m_Preset->Load_All_Assets ();
  378. //
  379. // Setup the current directory so the file system can find the sound
  380. // file we are about to load.
  381. //
  382. CString full_path = ::Get_File_Mgr ()->Make_Full_Path (definition->Get_Filename ());
  383. CString path_only = ::Strip_Filename_From_Path (full_path);
  384. ::Set_Current_Directory (path_only);
  385. //
  386. // Create and initialize the sound object
  387. //
  388. SoundObj = (AudibleSoundClass *)definition->Create ();
  389. if (SoundObj != NULL) {
  390. SoundObj->Set_Transform (m_Transform);
  391. SoundObj->Set_ID (m_ID);
  392. //
  393. // Make sure the sound object is 'static'
  394. //
  395. Sound3DClass *sound3d = SoundObj->As_Sound3DClass ();
  396. if (sound3d != NULL) {
  397. sound3d->Make_Static (true);
  398. }
  399. //
  400. // Override the preset settings with our instance settings
  401. //
  402. if (OverridePreset) {
  403. SoundObj->Set_Volume (InstanceSettings.Get_Volume ());
  404. SoundObj->Set_DropOff_Radius (InstanceSettings.Get_DropOff_Radius ());
  405. SoundObj->Set_Start_Offset (InstanceSettings.Get_Start_Offset ());
  406. SoundObj->Set_Pitch_Factor (InstanceSettings.Get_Pitch_Factor ());
  407. if (sound3d != NULL) {
  408. sound3d->Set_Max_Vol_Radius (InstanceSettings.Get_Max_Vol_Radius ());
  409. }
  410. }
  411. //
  412. // Add the sound to the scene (if necessary)
  413. //
  414. if (In_Scene ()) {
  415. SoundObj->Add_To_Scene ();
  416. }
  417. } else {
  418. WWDEBUG_SAY (("Unable to create sound object for preset %s\r\n", definition->Get_Name ()));
  419. }
  420. return ;
  421. }
  422. /////////////////////////////////////////////////////////////////
  423. //
  424. // Show_Settings_Dialog
  425. //
  426. /////////////////////////////////////////////////////////////////
  427. bool
  428. SoundNodeClass::Show_Settings_Dialog (void)
  429. {
  430. NodeInfoPageClass info_tab (this);
  431. PositionPageClass pos_tab (this);
  432. //
  433. // Display the settings tab for instance overrides
  434. //
  435. SoundSettingsPageClass settings_tab;
  436. settings_tab.Set_Definition (&InstanceSettings);
  437. //
  438. // Add each tab to the property sheet
  439. //
  440. EditorPropSheetClass prop_sheet;
  441. prop_sheet.Add_Page (&info_tab);
  442. prop_sheet.Add_Page (&pos_tab);
  443. prop_sheet.Add_Page (&settings_tab);
  444. //
  445. // Show the property sheet
  446. //
  447. UINT ret_code = prop_sheet.DoModal ();
  448. if (ret_code == IDOK) {
  449. OverridePreset = true;
  450. Update_Sound ();
  451. }
  452. // Return true if the user clicked OK
  453. return (ret_code == IDOK);
  454. }
  455. /////////////////////////////////////////////////////////////////
  456. //
  457. // Save
  458. //
  459. /////////////////////////////////////////////////////////////////
  460. bool
  461. SoundNodeClass::Save (ChunkSaveClass &csave)
  462. {
  463. //
  464. // Save the base class
  465. //
  466. csave.Begin_Chunk (CHUNKID_BASE_CLASS);
  467. NodeClass::Save (csave);
  468. csave.End_Chunk ();
  469. //
  470. // Save the parent class
  471. //
  472. if (OverridePreset) {
  473. csave.Begin_Chunk (CHUNKID_INSTANCE_SETTINGS);
  474. InstanceSettings.Save (csave);
  475. csave.End_Chunk ();
  476. }
  477. return true;
  478. }
  479. /////////////////////////////////////////////////////////////////
  480. //
  481. // Load
  482. //
  483. /////////////////////////////////////////////////////////////////
  484. bool
  485. SoundNodeClass::Load (ChunkLoadClass &cload)
  486. {
  487. //
  488. // Go straight to the base class if we need to suport
  489. // legacy loads
  490. //
  491. if (UseLegacyLoad) {
  492. return NodeClass::Load (cload);
  493. }
  494. while (cload.Open_Chunk ()) {
  495. switch (cload.Cur_Chunk_ID ()) {
  496. case CHUNKID_BASE_CLASS:
  497. NodeClass::Load (cload);
  498. break;
  499. case CHUNKID_INSTANCE_SETTINGS:
  500. InstanceSettings.Load (cload);
  501. OverridePreset = true;
  502. break;
  503. }
  504. cload.Close_Chunk ();
  505. }
  506. return true;
  507. }