terrainmaterial.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/wwphys/terrainmaterial.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 3/08/02 2:52p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "terrainmaterial.h"
  36. #include "texture.h"
  37. #include "assetmgr.h"
  38. #include "chunkio.h"
  39. #include "w3d_file.h"
  40. ////////////////////////////////////////////////////////////////
  41. // Local constants
  42. ////////////////////////////////////////////////////////////////
  43. enum
  44. {
  45. CHUNKID_VARIABLES = 0x02261040,
  46. };
  47. enum
  48. {
  49. VARID_XXXXXXX_XXXX = 0x01,
  50. VARID_METERS_PER_TILE,
  51. VARID_TEXTURE_NAME,
  52. VARID_MIRRORED_UVS,
  53. VARID_SURFACE_TYPE,
  54. };
  55. //////////////////////////////////////////////////////////////////////
  56. //
  57. // TerrainMaterialClass
  58. //
  59. //////////////////////////////////////////////////////////////////////
  60. TerrainMaterialClass::TerrainMaterialClass (void) :
  61. Texture (NULL),
  62. MetersPerTile (10.0F),
  63. SurfaceType (0),
  64. AreUVsMirrored (false)
  65. {
  66. return ;
  67. }
  68. //////////////////////////////////////////////////////////////////////
  69. //
  70. // ~TerrainMaterialClass
  71. //
  72. //////////////////////////////////////////////////////////////////////
  73. TerrainMaterialClass::~TerrainMaterialClass (void)
  74. {
  75. //
  76. // Release the texture
  77. //
  78. REF_PTR_RELEASE (Texture);
  79. return ;
  80. }
  81. //////////////////////////////////////////////////////////////////////
  82. //
  83. // Set_Texture
  84. //
  85. //////////////////////////////////////////////////////////////////////
  86. void
  87. TerrainMaterialClass::Set_Texture (const char *texture_name)
  88. {
  89. //
  90. // Release the old texture
  91. //
  92. REF_PTR_RELEASE (Texture);
  93. //
  94. // Load the new texture
  95. //
  96. TextureName = texture_name;
  97. #ifndef PARAM_EDITING_ON
  98. const char *dir_delim = ::strrchr (texture_name, '\\');
  99. if (dir_delim != NULL) {
  100. TextureName = dir_delim + 1;
  101. }
  102. #endif
  103. Texture = WW3DAssetManager::Get_Instance ()->Get_Texture (TextureName);
  104. return ;
  105. }
  106. //////////////////////////////////////////////////////////////////////
  107. //
  108. // Save
  109. //
  110. //////////////////////////////////////////////////////////////////////
  111. void
  112. TerrainMaterialClass::Set_Surface_Type (int type)
  113. {
  114. if ((type >= 0) && (type < SURFACE_TYPE_MAX)) {
  115. SurfaceType = type;
  116. }
  117. }
  118. //////////////////////////////////////////////////////////////////////
  119. //
  120. // Save
  121. //
  122. //////////////////////////////////////////////////////////////////////
  123. bool
  124. TerrainMaterialClass::Save (ChunkSaveClass &csave)
  125. {
  126. //
  127. // Write the variables
  128. //
  129. csave.Begin_Chunk (CHUNKID_VARIABLES);
  130. WRITE_MICRO_CHUNK_WWSTRING (csave, VARID_TEXTURE_NAME, TextureName);
  131. WRITE_MICRO_CHUNK (csave, VARID_METERS_PER_TILE, MetersPerTile);
  132. WRITE_MICRO_CHUNK (csave, VARID_MIRRORED_UVS, AreUVsMirrored);
  133. WRITE_MICRO_CHUNK (csave, VARID_SURFACE_TYPE, SurfaceType);
  134. csave.End_Chunk ();
  135. return true;
  136. }
  137. //////////////////////////////////////////////////////////////////////
  138. //
  139. // Load
  140. //
  141. //////////////////////////////////////////////////////////////////////
  142. bool
  143. TerrainMaterialClass::Load (ChunkLoadClass &cload)
  144. {
  145. while (cload.Open_Chunk ()) {
  146. switch (cload.Cur_Chunk_ID ()) {
  147. //
  148. // Load all the variables from this chunk
  149. //
  150. case CHUNKID_VARIABLES:
  151. Load_Variables (cload);
  152. break;
  153. }
  154. cload.Close_Chunk ();
  155. }
  156. //
  157. // Load the texture into memory
  158. //
  159. Set_Texture (TextureName);
  160. return true;
  161. }
  162. ////////////////////////////////////////////////////////////////
  163. //
  164. // Load_Variables
  165. //
  166. ////////////////////////////////////////////////////////////////
  167. void
  168. TerrainMaterialClass::Load_Variables (ChunkLoadClass &cload)
  169. {
  170. while (cload.Open_Micro_Chunk ()) {
  171. switch (cload.Cur_Micro_Chunk_ID ()) {
  172. //
  173. // Read each of the microchunks
  174. //
  175. READ_MICRO_CHUNK_WWSTRING (cload, VARID_TEXTURE_NAME, TextureName);
  176. READ_MICRO_CHUNK (cload, VARID_METERS_PER_TILE, MetersPerTile);
  177. READ_MICRO_CHUNK (cload, VARID_MIRRORED_UVS, AreUVsMirrored);
  178. READ_MICRO_CHUNK (cload, VARID_SURFACE_TYPE, SurfaceType);
  179. }
  180. cload.Close_Micro_Chunk ();
  181. }
  182. //
  183. // Enforcing that the surface type is valid; the game will assert this later otherwise...
  184. // Also fixed it on the editor side, surface types are initialized to zero.
  185. //
  186. if ((SurfaceType < 0) || (SurfaceType >= SURFACE_TYPE_MAX)) {
  187. SurfaceType = 0;
  188. }
  189. return ;
  190. }