Shade Table Util.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #ifdef PRECOMPILEDHEADERS
  2. #include "TileEngine All.h"
  3. #else
  4. #include <stdio.h>
  5. #include "types.h"
  6. #include "lighting.h"
  7. #include "Shade Table Util.h"
  8. #include "video.h"
  9. #include "WorldDat.h"
  10. #include "Fileman.h"
  11. #endif
  12. #define SHADE_TABLE_DIR "ShadeTables"
  13. CHAR8 TileSurfaceFilenames[NUMBEROFTILETYPES][32];
  14. BOOLEAN gfForceBuildShadeTables = FALSE;
  15. #ifdef JA2TESTVERSION
  16. extern UINT32 uiNumTablesSaved;
  17. extern UINT32 uiNumTablesLoaded;
  18. #endif
  19. void DetermineRGBDistributionSettings()
  20. {
  21. STRING512 DataDir;
  22. STRING512 ExecDir;
  23. STRING512 ShadeTableDir;
  24. UINT32 uiRBitMask, uiGBitMask, uiBBitMask;
  25. UINT32 uiPrevRBitMask, uiPrevGBitMask, uiPrevBBitMask;
  26. UINT32 uiNumBytesRead;
  27. HWFILE hfile;
  28. BOOLEAN fSaveRGBDist = FALSE;
  29. BOOLEAN fCleanShadeTable = FALSE;
  30. BOOLEAN fLoadedPrevRGBDist = FALSE;
  31. //First, determine if we have a file saved. If not, then this is the first time, and
  32. //all shade tables will have to be built and saved to disk. This can be time consuming, adding up to
  33. //3-4 seconds to the time of a map load.
  34. GetExecutableDirectory( ExecDir );
  35. sprintf( ShadeTableDir, "%s\\Data\\%s", ExecDir, SHADE_TABLE_DIR );
  36. //Check to make sure we have a ShadeTable directory. If we don't create one!
  37. if( !SetFileManCurrentDirectory( ShadeTableDir ) )
  38. {
  39. if( !MakeFileManDirectory( ShadeTableDir ) )
  40. {
  41. AssertMsg( 0, "ShadeTable directory doesn't exist, and couldn't create one!" );
  42. }
  43. if( !SetFileManCurrentDirectory( ShadeTableDir ) )
  44. {
  45. AssertMsg( 0, "Couldn't access the newly created ShadeTable directory." );
  46. }
  47. fSaveRGBDist = TRUE;
  48. }
  49. if( !fSaveRGBDist )
  50. { //Load the previous RGBDist and determine if it is the same one
  51. if( !FileExists( "RGBDist.dat" ) || FileExists( "ResetShadeTables.txt" ) )
  52. { //Can't find the RGBDist.dat file. The directory exists, but the file doesn't, which
  53. //means the user deleted the file manually. Now, set it up to create a new one.
  54. fSaveRGBDist = TRUE;
  55. fCleanShadeTable = TRUE;
  56. }
  57. else
  58. {
  59. hfile = FileOpen( "RGBDist.dat", FILE_ACCESS_READ, FALSE );
  60. if( !hfile )
  61. {
  62. AssertMsg( 0, "Couldn't open RGBDist.dat, even though it exists!" );
  63. }
  64. FileRead( hfile, &uiPrevRBitMask, sizeof( UINT32 ), &uiNumBytesRead );
  65. FileRead( hfile, &uiPrevGBitMask, sizeof( UINT32 ), &uiNumBytesRead );
  66. FileRead( hfile, &uiPrevBBitMask, sizeof( UINT32 ), &uiNumBytesRead );
  67. fLoadedPrevRGBDist = TRUE;
  68. FileClose( hfile );
  69. }
  70. }
  71. if( !GetPrimaryRGBDistributionMasks( &uiRBitMask, &uiGBitMask, &uiBBitMask ) )
  72. {
  73. AssertMsg( 0, "Failed to extract the current RGB distribution masks." );
  74. }
  75. if( fLoadedPrevRGBDist )
  76. {
  77. if( uiRBitMask != uiPrevRBitMask || uiGBitMask != uiPrevGBitMask || uiBBitMask != uiPrevBBitMask )
  78. { //The user has changed modes since the last time he has played JA2. This essentially can only happen if:
  79. //1) The video card has been changed since the last run of JA2.
  80. //2) Certain video cards have different RGB distributions in different operating systems such as
  81. // the Millenium card using Windows NT or Windows 95
  82. //3) The user has physically modified the RGBDist.dat file.
  83. fSaveRGBDist = TRUE;
  84. fCleanShadeTable = TRUE;
  85. }
  86. }
  87. if( fCleanShadeTable )
  88. { //This means that we are going to remove all of the current shade tables, if any exist, and
  89. //start fresh.
  90. EraseDirectory( ShadeTableDir );
  91. }
  92. if( fSaveRGBDist )
  93. { //The RGB distribution is going to be saved in a tiny file for future reference. As long as the
  94. //RGB distribution never changes, the shade table will grow until eventually, all tilesets are loaded,
  95. //shadetables generated and saved in this directory.
  96. hfile = FileOpen( "RGBDist.dat", FILE_ACCESS_WRITE | FILE_CREATE_ALWAYS, FALSE );
  97. if( !hfile )
  98. {
  99. AssertMsg( 0, "Couldn't create RGBDist.dat for writing!" );
  100. }
  101. FileWrite( hfile, &uiRBitMask, sizeof( UINT32 ), &uiNumBytesRead );
  102. FileWrite( hfile, &uiGBitMask, sizeof( UINT32 ), &uiNumBytesRead );
  103. FileWrite( hfile, &uiBBitMask, sizeof( UINT32 ), &uiNumBytesRead );
  104. FileClose( hfile );
  105. }
  106. //We're done, so restore the executable directory to JA2\Data.
  107. sprintf( DataDir, "%s\\Data", ExecDir );
  108. SetFileManCurrentDirectory( DataDir );
  109. }
  110. BOOLEAN LoadShadeTable( HVOBJECT pObj, UINT32 uiTileTypeIndex )
  111. {
  112. HWFILE hfile;
  113. INT32 i;
  114. UINT32 uiNumBytesRead;
  115. UINT8 ShadeFileName[ 100 ];
  116. UINT8 *ptr;
  117. //ASSUMPTIONS:
  118. //We are assuming that the uiTileTypeIndex is referring to the correct file
  119. //stored in the TileSurfaceFilenames[]. If it isn't, then that is a huge problem
  120. //and should be fixed. Also assumes that the directory is set to Data\ShadeTables.
  121. strcpy( ShadeFileName, TileSurfaceFilenames[ uiTileTypeIndex ] );
  122. ptr = strstr( ShadeFileName, "." );
  123. if( !ptr )
  124. {
  125. return FALSE;
  126. }
  127. ptr++;
  128. sprintf( ptr, "sha" );
  129. hfile = FileOpen( ShadeFileName, FILE_ACCESS_READ, FALSE );
  130. if( !hfile )
  131. { //File doesn't exist, so generate it
  132. FileClose( hfile );
  133. return FALSE;
  134. }
  135. //MISSING: Compare time stamps.
  136. for( i = 0; i < 16; i++ )
  137. {
  138. pObj->pShades[ i ] = MemAlloc( 512 );
  139. Assert( pObj->pShades[ i ] );
  140. FileRead( hfile, pObj->pShades[ i ], 512, &uiNumBytesRead );
  141. }
  142. //The file exists, now make sure the
  143. FileClose( hfile );
  144. #ifdef JA2TESTVERSION
  145. uiNumTablesLoaded++;
  146. #endif
  147. return TRUE;
  148. }
  149. BOOLEAN SaveShadeTable( HVOBJECT pObj, UINT32 uiTileTypeIndex )
  150. {
  151. HWFILE hfile;
  152. INT32 i;
  153. UINT32 uiNumBytesWritten;
  154. UINT8 ShadeFileName[ 100 ];
  155. UINT8 *ptr;
  156. #ifdef JA2TESTVERSION
  157. uiNumTablesSaved++;
  158. #endif
  159. //ASSUMPTIONS:
  160. //We are assuming that the uiTileTypeIndex is referring to the correct file
  161. //stored in the TileSurfaceFilenames[]. If it isn't, then that is a huge problem
  162. //and should be fixed. Also assumes that the directory is set to Data\ShadeTables.
  163. strcpy( ShadeFileName, TileSurfaceFilenames[ uiTileTypeIndex ] );
  164. ptr = strstr( ShadeFileName, "." );
  165. if( !ptr )
  166. {
  167. return FALSE;
  168. }
  169. ptr++;
  170. sprintf( ptr, "sha" );
  171. hfile = FileOpen( ShadeFileName, FILE_ACCESS_WRITE | FILE_CREATE_ALWAYS, FALSE );
  172. if( !hfile )
  173. {
  174. FileClose( hfile );
  175. AssertMsg( 0, String( "Can't create %s", ShadeFileName ) );
  176. return FALSE;
  177. }
  178. for( i = 0; i < 16; i++ )
  179. {
  180. FileWrite( hfile, pObj->pShades[ i ], 512, &uiNumBytesWritten );
  181. }
  182. FileClose( hfile );
  183. return TRUE;
  184. }
  185. BOOLEAN DeleteShadeTableDir( )
  186. {
  187. return( RemoveFileManDirectory( SHADE_TABLE_DIR, TRUE ) );
  188. }