MeshDeformData.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando / G 3D engine *
  23. * *
  24. * File Name : MeshDeformData.cpp *
  25. * *
  26. * Programmer : Patrick Smith *
  27. * *
  28. * Start Date : 04/26/99 *
  29. * *
  30. * Last Update :
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "MeshDeformData.H"
  36. #include "Util.H"
  37. #include "MeshDeformSaveDefs.H"
  38. ///////////////////////////////////////////////////////////////////////////
  39. //
  40. // ~MeshDeformModData
  41. //
  42. ///////////////////////////////////////////////////////////////////////////
  43. MeshDeformModData::~MeshDeformModData (void)
  44. {
  45. Free_Sets_List ();
  46. return ;
  47. }
  48. ///////////////////////////////////////////////////////////////////////////
  49. //
  50. // Record_Mesh_State
  51. //
  52. ///////////////////////////////////////////////////////////////////////////
  53. void
  54. MeshDeformModData::Record_Mesh_State (TriObject &tri_obj, float state, bool update_all)
  55. {
  56. //
  57. // Ask each set to update its state
  58. //
  59. for (int index = 0; index < m_SetsList.Count (); index ++) {
  60. if (index != m_CurrentSet) {
  61. if (update_all) {
  62. m_SetsList[index]->Set_State (state);
  63. }
  64. m_SetsList[index]->Update_Mesh (tri_obj);
  65. }
  66. }
  67. if (m_CurrentSet < m_SetsList.Count ()) {
  68. m_SetsList[m_CurrentSet]->Set_State (state);
  69. m_SetsList[m_CurrentSet]->Update_Mesh (tri_obj);
  70. }
  71. return ;
  72. }
  73. ///////////////////////////////////////////////////////////////////////////
  74. //
  75. // Free_Sets_List
  76. //
  77. ///////////////////////////////////////////////////////////////////////////
  78. void
  79. MeshDeformModData::Free_Sets_List (void)
  80. {
  81. //
  82. // Delete all the object pointers in the set list
  83. //
  84. for (int index = 0; index < m_SetsList.Count (); index ++) {
  85. MeshDeformSetClass *set = m_SetsList[index];
  86. SAFE_DELETE (set);
  87. }
  88. // Remove all the entries from the list
  89. m_SetsList.Delete_All ();
  90. return ;
  91. }
  92. ///////////////////////////////////////////////////////////////////////////
  93. //
  94. // Set_Max_Deform_Sets
  95. //
  96. ///////////////////////////////////////////////////////////////////////////
  97. void
  98. MeshDeformModData::Set_Max_Deform_Sets (int max)
  99. {
  100. int current_max = m_SetsList.Count ();
  101. if (max > current_max) {
  102. //
  103. // Add the new sets to the list
  104. //
  105. int sets_to_add = max - current_max;
  106. for (int index = 0; index < sets_to_add; index ++) {
  107. MeshDeformSetClass *set = new MeshDeformSetClass;
  108. m_SetsList.Add (set);
  109. }
  110. } else if (max < current_max) {
  111. //
  112. // Remove the obsolete sets from the list
  113. //
  114. int sets_to_remove = current_max - max;
  115. for (int index = 0; index < sets_to_remove; index ++) {
  116. // Restore the set before we delete it
  117. Restore_Set (max);
  118. // Delete the set
  119. MeshDeformSetClass *set = m_SetsList[max];
  120. SAFE_DELETE (set);
  121. m_SetsList.Delete (max);
  122. }
  123. }
  124. return ;
  125. }
  126. ///////////////////////////////////////////////////////////////////////////
  127. //
  128. // Restore_Set
  129. //
  130. ///////////////////////////////////////////////////////////////////////////
  131. void
  132. MeshDeformModData::Restore_Set (int set_index)
  133. {
  134. if (set_index == -1) {
  135. // Restore ALL the set
  136. for (int index = 0; index < m_SetsList.Count (); index ++) {
  137. m_SetsList[index]->Restore_Members ();
  138. }
  139. } else {
  140. m_SetsList[set_index]->Restore_Members ();
  141. }
  142. return ;
  143. }
  144. ///////////////////////////////////////////////////////////////////////////
  145. //
  146. // Save
  147. //
  148. ///////////////////////////////////////////////////////////////////////////
  149. IOResult
  150. MeshDeformModData::Save (ISave *save_obj)
  151. {
  152. DWORD bytes = 0L;
  153. save_obj->BeginChunk (DEFORM_CHUNK_INFO);
  154. //
  155. // Write the set count info to the chunk
  156. //
  157. DeformChunk info = { 0 };
  158. info.SetCount = m_SetsList.Count ();
  159. IOResult result = save_obj->Write (&info, sizeof (info), &bytes);
  160. save_obj->EndChunk ();
  161. //
  162. // Now write a chunk for each set
  163. //
  164. for (int index = 0; (index < m_SetsList.Count ()) && (result == IO_OK); index ++) {
  165. result = m_SetsList[index]->Save (save_obj);
  166. }
  167. // Return IO_OK on success IO_ERROR on failure
  168. return result;
  169. }
  170. ///////////////////////////////////////////////////////////////////////////
  171. //
  172. // Load
  173. //
  174. ///////////////////////////////////////////////////////////////////////////
  175. IOResult
  176. MeshDeformModData::Load (ILoad *load_obj)
  177. {
  178. Free_Sets_List ();
  179. DWORD bytes = 0L;
  180. //
  181. // Is this the chunk we were expecting?
  182. //
  183. IOResult result = load_obj->OpenChunk ();
  184. if ( (result == IO_OK) &&
  185. (load_obj->CurChunkID () == DEFORM_CHUNK_INFO)) {
  186. DeformChunk info = { 0 };
  187. result = load_obj->Read (&info, sizeof (info), &bytes);
  188. load_obj->CloseChunk ();
  189. //
  190. // Read the set information from the chunk
  191. //
  192. for (unsigned int index = 0; (index < info.SetCount) && (result == IO_OK); index ++) {
  193. MeshDeformSetClass *set = new MeshDeformSetClass;
  194. m_SetsList.Add (set);
  195. result = set->Load (load_obj);
  196. }
  197. }
  198. // Return IO_OK on success IO_ERROR on failure
  199. return result;
  200. }