hanimmgr.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. /* $Header: /Commando/Code/ww3d2/hanimmgr.cpp 3 1/16/02 9:51a Jani_p $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando / G 3D Library *
  24. * *
  25. * $Archive:: /Commando/Code/ww3d2/hanimmgr.cpp $*
  26. * *
  27. * Author:: Greg_h *
  28. * *
  29. * $Modtime:: 1/16/02 9:49a $*
  30. * *
  31. * $Revision:: 3 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * HAnimManagerClass::HAnimManagerClass -- constructor *
  36. * HAnimManagerClass::~HAnimManagerClass -- destructor *
  37. * HAnimManagerClass::Load_Anim -- loads a set of motion data from a file *
  38. * HAnimManagerClass::Get_Anim_ID -- looks up the ID of a named Hierarchy Animation *
  39. * HAnimManagerClass::Get_Anim -- returns a pointer to the specified animation data *
  40. * HAnimManagerClass::Get_Anim -- returns a pointer to the specified Hierarchy Animation *
  41. * HAnimManagerClass::Free -- de-allocate all memory in use *
  42. * HAnimManagerClass::Free_All_Anims -- de-allocate all currently loaded animations *
  43. * HAnimManagerClass::Load_Raw_Anim -- Load a raw anim *
  44. * HAnimManagerClass::Load_Compressed_Anim -- load a compressed animation *
  45. * HAnimManagerClass::Add_Anim -- Adds an externally created animation to the manager *
  46. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  47. #include "hanimmgr.h"
  48. #include <string.h>
  49. #include "hanim.h"
  50. #include "hrawanim.h"
  51. #include "hcanim.h"
  52. #include "hmorphanim.h"
  53. #include "chunkio.h"
  54. #include "wwmemlog.h"
  55. #include "animatedsoundmgr.h"
  56. /***********************************************************************************************
  57. * HAnimManagerClass::HAnimManagerClass -- constructor *
  58. * *
  59. * INPUT: *
  60. * *
  61. * OUTPUT: *
  62. * *
  63. * WARNINGS: *
  64. * *
  65. * HISTORY: *
  66. * 08/11/1997 GH : Created. *
  67. *=============================================================================================*/
  68. HAnimManagerClass::HAnimManagerClass(void)
  69. {
  70. // Create the hash tables
  71. AnimPtrTable = new HashTableClass( 2048 );
  72. MissingAnimTable = new HashTableClass( 2048 );
  73. }
  74. /***********************************************************************************************
  75. * HAnimManagerClass::~HAnimManagerClass -- destructor *
  76. * *
  77. * INPUT: *
  78. * *
  79. * OUTPUT: *
  80. * *
  81. * WARNINGS: *
  82. * *
  83. * HISTORY: *
  84. * 08/11/1997 GH : Created. *
  85. *=============================================================================================*/
  86. HAnimManagerClass::~HAnimManagerClass(void)
  87. {
  88. Free_All_Anims();
  89. Reset_Missing(); // Jani: Deleting missing animations as well
  90. delete AnimPtrTable;
  91. AnimPtrTable = NULL;
  92. delete MissingAnimTable;
  93. MissingAnimTable = NULL;
  94. }
  95. /***********************************************************************************************
  96. * HAnimManagerClass::Load_Anim -- loads a set of motion data from a file *
  97. * *
  98. * INPUT: *
  99. * *
  100. * OUTPUT: *
  101. * *
  102. * WARNINGS: *
  103. * *
  104. * HISTORY: *
  105. * 08/11/1997 GH : Created. *
  106. *=============================================================================================*/
  107. int HAnimManagerClass::Load_Anim(ChunkLoadClass & cload)
  108. {
  109. WWMEMLOG(MEM_ANIMATION);
  110. switch (cload.Cur_Chunk_ID())
  111. {
  112. case W3D_CHUNK_ANIMATION:
  113. return Load_Raw_Anim(cload);
  114. break;
  115. case W3D_CHUNK_COMPRESSED_ANIMATION:
  116. return Load_Compressed_Anim(cload);
  117. break;
  118. case W3D_CHUNK_MORPH_ANIMATION:
  119. return Load_Morph_Anim(cload);
  120. break;
  121. }
  122. return 0;
  123. }
  124. /***********************************************************************************************
  125. * HAnimManagerClass::Load_Morph_Anim -- Load a HMorphAnimClass *
  126. * *
  127. * INPUT: *
  128. * *
  129. * OUTPUT: *
  130. * *
  131. * WARNINGS: *
  132. * *
  133. * HISTORY: *
  134. * 5/23/2000 pds : Created. *
  135. *=============================================================================================*/
  136. int HAnimManagerClass::Load_Morph_Anim(ChunkLoadClass & cload)
  137. {
  138. HMorphAnimClass * newanim = new HMorphAnimClass;
  139. if (newanim == NULL) {
  140. goto Error;
  141. }
  142. SET_REF_OWNER( newanim );
  143. if (newanim->Load_W3D(cload) != HMorphAnimClass::OK) {
  144. // load failed!
  145. newanim->Release_Ref();
  146. goto Error;
  147. } else if (Peek_Anim(newanim->Get_Name()) != NULL) {
  148. // duplicate exists!
  149. newanim->Release_Ref(); // Release the one we just loaded
  150. goto Error;
  151. } else {
  152. Add_Anim( newanim );
  153. newanim->Release_Ref();
  154. }
  155. return 0;
  156. Error:
  157. return 1;
  158. }
  159. /***********************************************************************************************
  160. * HAnimManagerClass::Load_Raw_Anim -- Load a raw anim *
  161. * *
  162. * INPUT: *
  163. * *
  164. * OUTPUT: *
  165. * *
  166. * WARNINGS: *
  167. * *
  168. * HISTORY: *
  169. * 5/23/2000 gth : Created. *
  170. *=============================================================================================*/
  171. int HAnimManagerClass::Load_Raw_Anim(ChunkLoadClass & cload)
  172. {
  173. HRawAnimClass * newanim = new HRawAnimClass;
  174. if (newanim == NULL) {
  175. goto Error;
  176. }
  177. SET_REF_OWNER( newanim );
  178. if (newanim->Load_W3D(cload) != HRawAnimClass::OK) {
  179. // load failed!
  180. newanim->Release_Ref();
  181. goto Error;
  182. } else if (Peek_Anim(newanim->Get_Name()) != NULL) {
  183. // duplicate exists!
  184. newanim->Release_Ref(); // Release the one we just loaded
  185. goto Error;
  186. } else {
  187. Add_Anim( newanim );
  188. newanim->Release_Ref();
  189. }
  190. return 0;
  191. Error:
  192. return 1;
  193. }
  194. /***********************************************************************************************
  195. * HAnimManagerClass::Load_Compressed_Anim -- load a compressed animation *
  196. * *
  197. * INPUT: *
  198. * *
  199. * OUTPUT: *
  200. * *
  201. * WARNINGS: *
  202. * *
  203. * HISTORY: *
  204. * 5/23/2000 gth : Created. *
  205. *=============================================================================================*/
  206. int HAnimManagerClass::Load_Compressed_Anim(ChunkLoadClass & cload)
  207. {
  208. HCompressedAnimClass * newanim = new HCompressedAnimClass;
  209. if (newanim == NULL) {
  210. goto Error;
  211. }
  212. SET_REF_OWNER( newanim );
  213. if (newanim->Load_W3D(cload) != HCompressedAnimClass::OK) {
  214. // load failed!
  215. newanim->Release_Ref();
  216. goto Error;
  217. } else if (Peek_Anim(newanim->Get_Name()) != NULL) {
  218. // duplicate exists!
  219. newanim->Release_Ref(); // Release the one we just loaded
  220. goto Error;
  221. } else {
  222. Add_Anim( newanim );
  223. newanim->Release_Ref();
  224. }
  225. return 0;
  226. Error:
  227. return 1;
  228. }
  229. /***********************************************************************************************
  230. * HAnimManagerClass::Peek_Anim -- returns a pointer to the specified animation data *
  231. * *
  232. * INPUT: *
  233. * *
  234. * OUTPUT: *
  235. * *
  236. * WARNINGS: *
  237. * *
  238. * HISTORY: *
  239. * 08/11/1997 GH : Created. *
  240. *=============================================================================================*/
  241. HAnimClass * HAnimManagerClass::Peek_Anim(const char * name)
  242. {
  243. return (HAnimClass*)AnimPtrTable->Find( name );
  244. }
  245. /***********************************************************************************************
  246. * HAnimManagerClass::Get_Anim -- returns a pointer to the specified animation data *
  247. * *
  248. * INPUT: *
  249. * *
  250. * OUTPUT: *
  251. * *
  252. * WARNINGS: *
  253. * *
  254. * HISTORY: *
  255. * 08/11/1997 GH : Created. *
  256. *=============================================================================================*/
  257. HAnimClass * HAnimManagerClass::Get_Anim(const char * name)
  258. {
  259. HAnimClass * anim = Peek_Anim( name );
  260. if ( anim != NULL ) {
  261. anim->Add_Ref();
  262. }
  263. return anim;
  264. }
  265. /***********************************************************************************************
  266. * HAnimManagerClass::Free_All_Anims -- de-allocate all currently loaded animations *
  267. * *
  268. * INPUT: *
  269. * *
  270. * OUTPUT: *
  271. * *
  272. * WARNINGS: *
  273. * *
  274. * HISTORY: *
  275. * 08/11/1997 GH : Created. *
  276. *=============================================================================================*/
  277. void HAnimManagerClass::Free_All_Anims(void)
  278. {
  279. // Make an iterator, and release all ptrs
  280. HAnimManagerIterator it( *this );
  281. for( it.First(); !it.Is_Done(); it.Next() ) {
  282. HAnimClass *anim = it.Get_Current_Anim();
  283. anim->Release_Ref();
  284. }
  285. // Then clear the table
  286. AnimPtrTable->Reset();
  287. }
  288. /***********************************************************************************************
  289. * HAnimManagerClass::Add_Anim -- Adds an externally created animation to the manager *
  290. * *
  291. * INPUT: *
  292. * *
  293. * OUTPUT: *
  294. * *
  295. * WARNINGS: *
  296. * *
  297. * HISTORY: *
  298. * 05/31/2000 PDS : Created. *
  299. *=============================================================================================*/
  300. bool HAnimManagerClass::Add_Anim(HAnimClass *new_anim)
  301. {
  302. WWASSERT (new_anim != NULL);
  303. // Increment the refcount on the new animation and add it to our table.
  304. new_anim->Add_Ref ();
  305. AnimPtrTable->Add( new_anim );
  306. //
  307. // Check to see if this animation has any embedded sounds that may
  308. // need to play while its animating.
  309. //
  310. bool has_sound_trigger = AnimatedSoundMgrClass::Does_Animation_Have_Embedded_Sounds( new_anim );
  311. new_anim->Set_Has_Embedded_Sounds( has_sound_trigger );
  312. return true;
  313. }
  314. /*
  315. ** Missing Anims
  316. **
  317. ** The idea here, allow the system to register which anims are determined to be missing
  318. ** so that if they are asked for again, we can quickly return NULL, without searching the
  319. ** disk again.
  320. */
  321. void HAnimManagerClass::Register_Missing( const char * name )
  322. {
  323. MissingAnimTable->Add( new MissingAnimClass( name ) );
  324. }
  325. bool HAnimManagerClass::Is_Missing( const char * name )
  326. {
  327. return ( MissingAnimTable->Find( name ) != NULL );
  328. }
  329. void HAnimManagerClass::Reset_Missing( void )
  330. {
  331. // Make an iterator, and release all ptrs
  332. HashTableIteratorClass it( *MissingAnimTable );
  333. for( it.First(); !it.Is_Done(); it.Next() ) {
  334. MissingAnimClass *missing = (MissingAnimClass *)it.Get_Current();
  335. delete missing;
  336. }
  337. // Then clear the table
  338. MissingAnimTable->Reset();
  339. }
  340. /*
  341. ** Iterator converter from HashableClass to HAnimClass
  342. */
  343. HAnimClass * HAnimManagerIterator::Get_Current_Anim( void )
  344. {
  345. return (HAnimClass *)Get_Current();
  346. }