LogicalSound.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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 : WWAudio *
  23. * *
  24. * $Archive:: /Commando/Code/WWAudio/LogicalSound.cpp $Modtime:: 7/02/99 10:18a $*
  25. * *
  26. * $Revision:: 6 $*
  27. * *
  28. *---------------------------------------------------------------------------------------------*
  29. * Functions: *
  30. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  31. #include "LogicalSound.H"
  32. #include "WWAudio.H"
  33. #include "SoundScene.H"
  34. #include "SoundChunkIDs.h"
  35. #include "persistfactory.h"
  36. //////////////////////////////////////////////////////////////////////////////////
  37. // Static factories
  38. //////////////////////////////////////////////////////////////////////////////////
  39. SimplePersistFactoryClass<LogicalSoundClass, CHUNKID_LOGICALSOUND> _LogicalSoundPersistFactory;
  40. //////////////////////////////////////////////////////////////////////////////////
  41. // Save/Load constants
  42. //////////////////////////////////////////////////////////////////////////////////
  43. enum
  44. {
  45. CHUNKID_VARIABLES = 0x03270459,
  46. CHUNKID_BASE_CLASS
  47. };
  48. enum
  49. {
  50. VARID_DROP_OFF_RADIUS = 0x01,
  51. VARID_IS_SINGLE_SHOT,
  52. VARID_TYPE_MASK,
  53. VARID_XXX1,
  54. VARID_POSITION,
  55. VARID_MAX_LISTENERS,
  56. VARID_NOTIFY_DELAY,
  57. VARID_LAST_NOTIFY,
  58. };
  59. ////////////////////////////////////////////////////////////////////////////////////////////////
  60. //
  61. // LogicalSoundClass
  62. //
  63. ////////////////////////////////////////////////////////////////////////////////////////////////
  64. LogicalSoundClass::LogicalSoundClass (void)
  65. : m_DropOffRadius (1),
  66. m_TypeMask (0),
  67. m_Position (0, 0, 0),
  68. m_IsSingleShot (false),
  69. m_OldestListenerTimestamp (0),
  70. m_MaxListeners (0),
  71. m_NotifyDelayInMS (2000),
  72. m_LastNotification (0)
  73. {
  74. return ;
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////////////////////
  77. //
  78. // ~LogicalSoundClass
  79. //
  80. ////////////////////////////////////////////////////////////////////////////////////////////////
  81. LogicalSoundClass::~LogicalSoundClass (void)
  82. {
  83. return ;
  84. }
  85. ////////////////////////////////////////////////////////////////////////////////////////////////
  86. //
  87. // Add_To_Scene
  88. //
  89. ////////////////////////////////////////////////////////////////////////////////////////////////
  90. void
  91. LogicalSoundClass::Add_To_Scene (bool /*start_playing*/)
  92. {
  93. SoundSceneClass *scene = WWAudioClass::Get_Instance ()->Get_Sound_Scene ();
  94. if ((scene != NULL) && (m_Scene == NULL)) {
  95. //
  96. // Add this sound to the culling system
  97. //
  98. m_Scene = scene;
  99. scene->Add_Logical_Sound (this, m_IsSingleShot);
  100. }
  101. return ;
  102. }
  103. ////////////////////////////////////////////////////////////////////////////////////////////////
  104. //
  105. // Remove_From_Scene
  106. //
  107. ////////////////////////////////////////////////////////////////////////////////////////////////
  108. void
  109. LogicalSoundClass::Remove_From_Scene (void)
  110. {
  111. if (m_Scene != NULL) {
  112. //
  113. // Remove this sound from the culling system
  114. //
  115. m_Scene->Remove_Logical_Sound (this, m_IsSingleShot);
  116. m_Scene = NULL;
  117. m_PhysWrapper = NULL;
  118. m_LastNotification = 0;
  119. }
  120. return ;
  121. }
  122. ////////////////////////////////////////////////////////////////////////////////////////////////
  123. //
  124. // Allow_Notify
  125. //
  126. ////////////////////////////////////////////////////////////////////////////////////////////////
  127. bool
  128. LogicalSoundClass::Allow_Notify (uint32 timestamp)
  129. {
  130. bool retval = false;
  131. if (m_IsSingleShot || timestamp > (m_LastNotification + m_NotifyDelayInMS)) {
  132. retval = true;
  133. m_LastNotification = timestamp;
  134. }
  135. return retval;
  136. }
  137. ////////////////////////////////////////////////////////////////////////////////////////////////
  138. //
  139. // On_Frame_Update
  140. //
  141. ////////////////////////////////////////////////////////////////////////////////////////////////
  142. bool
  143. LogicalSoundClass::On_Frame_Update (unsigned int milliseconds)
  144. {
  145. //
  146. // Update the sound's position if its linked to a render object
  147. //
  148. Apply_Auto_Position ();
  149. return SoundSceneObjClass::On_Frame_Update (milliseconds);;
  150. }
  151. /////////////////////////////////////////////////////////////////////////////////
  152. //
  153. // Get_Factory
  154. //
  155. /////////////////////////////////////////////////////////////////////////////////
  156. const PersistFactoryClass &
  157. LogicalSoundClass::Get_Factory (void) const
  158. {
  159. return _LogicalSoundPersistFactory;
  160. }
  161. //////////////////////////////////////////////////////////////////////////////////
  162. //
  163. // Save
  164. //
  165. //////////////////////////////////////////////////////////////////////////////////
  166. bool
  167. LogicalSoundClass::Save (ChunkSaveClass &csave)
  168. {
  169. csave.Begin_Chunk (CHUNKID_BASE_CLASS);
  170. SoundSceneObjClass::Save (csave);
  171. csave.End_Chunk ();
  172. csave.Begin_Chunk (CHUNKID_VARIABLES);
  173. WRITE_MICRO_CHUNK (csave, VARID_DROP_OFF_RADIUS, m_DropOffRadius);
  174. WRITE_MICRO_CHUNK (csave, VARID_IS_SINGLE_SHOT, m_IsSingleShot);
  175. WRITE_MICRO_CHUNK (csave, VARID_TYPE_MASK, m_TypeMask);
  176. WRITE_MICRO_CHUNK (csave, VARID_POSITION, m_Position);
  177. WRITE_MICRO_CHUNK (csave, VARID_MAX_LISTENERS, m_MaxListeners);
  178. WRITE_MICRO_CHUNK (csave, VARID_NOTIFY_DELAY, m_NotifyDelayInMS);
  179. WRITE_MICRO_CHUNK (csave, VARID_LAST_NOTIFY, m_LastNotification);
  180. csave.End_Chunk ();
  181. return true;
  182. }
  183. //////////////////////////////////////////////////////////////////////////////////
  184. //
  185. // Load
  186. //
  187. //////////////////////////////////////////////////////////////////////////////////
  188. bool
  189. LogicalSoundClass::Load (ChunkLoadClass &cload)
  190. {
  191. while (cload.Open_Chunk ()) {
  192. switch (cload.Cur_Chunk_ID ()) {
  193. case CHUNKID_BASE_CLASS:
  194. PersistClass::Load (cload);
  195. break;
  196. case CHUNKID_VARIABLES:
  197. {
  198. //
  199. // Read all the variables from their micro-chunks
  200. //
  201. while (cload.Open_Micro_Chunk ()) {
  202. switch (cload.Cur_Micro_Chunk_ID ()) {
  203. READ_MICRO_CHUNK (cload, VARID_DROP_OFF_RADIUS, m_DropOffRadius);
  204. READ_MICRO_CHUNK (cload, VARID_IS_SINGLE_SHOT, m_IsSingleShot);
  205. READ_MICRO_CHUNK (cload, VARID_TYPE_MASK, m_TypeMask);
  206. READ_MICRO_CHUNK (cload, VARID_POSITION, m_Position);
  207. READ_MICRO_CHUNK (cload, VARID_MAX_LISTENERS, m_MaxListeners);
  208. READ_MICRO_CHUNK (cload, VARID_NOTIFY_DELAY, m_NotifyDelayInMS);
  209. READ_MICRO_CHUNK (cload, VARID_LAST_NOTIFY, m_LastNotification);
  210. }
  211. cload.Close_Micro_Chunk ();
  212. }
  213. }
  214. break;
  215. }
  216. cload.Close_Chunk ();
  217. }
  218. return true;
  219. }