shddefmanager.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 : WWSHADE *
  23. * *
  24. * $Archive:: wwshade/shddefmanager.cpp $*
  25. * *
  26. * $Org Author:: Jani_p
  27. *
  28. * $Author:: Kenny_m
  29. *
  30. * $Modtime:: 5/20/02 3:12p $*
  31. * *
  32. * $Revision:: 2 $*
  33. * *
  34. * 5/20/02 KM Added save load behavior
  35. *---------------------------------------------------------------------------------------------*
  36. * Functions: *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "w3d_file.h"
  39. #include "chunkio.h"
  40. #include "shddef.h"
  41. #include "shddefmanager.h"
  42. #include "shddeffactory.h"
  43. #include "wwdebug.h"
  44. #include "string.h"
  45. /*
  46. ** Static head of the factory list
  47. */
  48. ShdDefFactoryClass *ShdDefManagerClass::_FactoryListHead = NULL;
  49. //**********************************************************************************************
  50. //! Used to look up the ShdDefFactory for a given class id
  51. /*!
  52. @param class_id - class id to look up the factory for
  53. @returns the factory if it is found or NULL
  54. */
  55. ShdDefFactoryClass *
  56. ShdDefManagerClass::Find_Factory (uint32 class_id)
  57. {
  58. ShdDefFactoryClass *factory = 0;
  59. //
  60. // Loop through all the factories and see if we can
  61. // find the one who owns the corresponding class-id.
  62. //
  63. for ( ShdDefFactoryClass *curr_factory = _FactoryListHead;
  64. (factory == 0) && (curr_factory != 0);
  65. curr_factory = curr_factory->NextFactory)
  66. {
  67. //
  68. // Is this the factory we were looking for?
  69. //
  70. if (curr_factory->Get_Class_ID () == class_id) {
  71. factory = curr_factory;
  72. }
  73. }
  74. return factory;
  75. }
  76. //**********************************************************************************************
  77. //! Used to look up the ShdDefFactory for a given named shader def
  78. /*!
  79. @param name - name of the desired shader def
  80. @returns the factory if it is found or NULL if not
  81. */
  82. ShdDefFactoryClass * ShdDefManagerClass::Find_Factory (const char *name)
  83. {
  84. ShdDefFactoryClass * factory = 0;
  85. //
  86. // Loop through all the factories and see if we can
  87. // find the one who owns the corresponding class-id.
  88. //
  89. for ( ShdDefFactoryClass *curr_factory = _FactoryListHead;
  90. (factory == 0) && (curr_factory != 0);
  91. curr_factory = curr_factory->NextFactory) {
  92. //
  93. // Is this the factory we were looking for?
  94. //
  95. if (::stricmp (curr_factory->Get_Name (), name) == 0) {
  96. factory = curr_factory;
  97. }
  98. }
  99. return factory;
  100. }
  101. //**********************************************************************************************
  102. //! Registers a factory with the system
  103. /*!
  104. @param factory - the factory to register
  105. */
  106. void ShdDefManagerClass::Register_Factory (ShdDefFactoryClass *factory)
  107. {
  108. WWASSERT (factory->NextFactory == 0);
  109. WWASSERT (factory->PrevFactory == 0);
  110. Link_Factory (factory);
  111. }
  112. //**********************************************************************************************
  113. //! removes a factory from the system
  114. /*!
  115. @param factory - the factory to unregister
  116. */
  117. void ShdDefManagerClass::Unregister_Factory (ShdDefFactoryClass *factory)
  118. {
  119. WWASSERT (factory != 0);
  120. Unlink_Factory (factory);
  121. return ;
  122. }
  123. ShdDefFactoryClass * ShdDefManagerClass::Get_First (void)
  124. {
  125. return _FactoryListHead;
  126. }
  127. ShdDefFactoryClass * ShdDefManagerClass::Get_Next (ShdDefFactoryClass *current)
  128. {
  129. //
  130. // Simply return the next factory in the chain
  131. //
  132. if (current != NULL) {
  133. return current->NextFactory;
  134. }
  135. return _FactoryListHead;
  136. }
  137. void ShdDefManagerClass::Link_Factory (ShdDefFactoryClass *factory)
  138. {
  139. WWASSERT (factory->NextFactory == 0);
  140. WWASSERT (factory->PrevFactory == 0);
  141. // Adding this factory in front of the current head of the list
  142. factory->NextFactory = _FactoryListHead;
  143. // If the list wasn't empty, link the next factory back to this factory
  144. if (factory->NextFactory != 0) {
  145. factory->NextFactory->PrevFactory = factory;
  146. }
  147. // Point the head of the list at this factory now
  148. _FactoryListHead = factory;
  149. return ;
  150. }
  151. void ShdDefManagerClass::Unlink_Factory (ShdDefFactoryClass *factory)
  152. {
  153. WWASSERT(factory != 0);
  154. // Handle the factory's prev pointer:
  155. if (factory->PrevFactory == 0) {
  156. // this factory is the head
  157. WWASSERT (_FactoryListHead == factory);
  158. _FactoryListHead = factory->NextFactory;
  159. } else {
  160. // link it's prev with it's next
  161. factory->PrevFactory->NextFactory = factory->NextFactory;
  162. }
  163. // Handle the factory's next pointer if its not at the end of the list:
  164. if (factory->NextFactory != 0) {
  165. factory->NextFactory->PrevFactory = factory->PrevFactory;
  166. }
  167. // factory is now un-linked
  168. factory->NextFactory = 0;
  169. factory->PrevFactory = 0;
  170. return ;
  171. }
  172. ShdDefClass* ShdDefManagerClass::Create_ShdDefClass_Instance(uint32 class_id)
  173. {
  174. // first look up the factory for this classid
  175. ShdDefFactoryClass * factory = Find_Factory (class_id);
  176. if (factory != NULL)
  177. {
  178. return factory->Create();
  179. }
  180. else
  181. {
  182. return NULL;
  183. }
  184. }
  185. void ShdDefManagerClass::Save_Shader(ChunkSaveClass& csave, ShdDefClass* shddef)
  186. {
  187. csave.Begin_Chunk(W3D_CHUNK_SHDSUBMESH_SHADER_CLASSID);
  188. uint32 id=shddef->Get_Class_ID();
  189. csave.Write(&id,sizeof(uint32));
  190. csave.End_Chunk();
  191. csave.Begin_Chunk(W3D_CHUNK_SHDSUBMESH_SHADER_DEF);
  192. shddef->Save(csave);
  193. csave.End_Chunk();
  194. }
  195. void ShdDefManagerClass::Load_Shader(ChunkLoadClass& cload, ShdDefClass** shddef)
  196. {
  197. uint32 id;
  198. cload.Open_Chunk();
  199. cload.Read(&id,sizeof(uint32));
  200. cload.Close_Chunk();
  201. *shddef=Create_ShdDefClass_Instance(id);
  202. cload.Open_Chunk();
  203. (*shddef)->Load(cload);
  204. cload.Close_Chunk();
  205. }