home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP12 / TEXTTYPE.CPP < prev    next >
C/C++ Source or Header  |  1996-05-30  |  2KB  |  123 lines

  1. //
  2. // File name: TextType.CPP
  3. //
  4. // Description:
  5. //
  6. // Author: John De Goes
  7. //
  8. // Project: Cutting Edge 3D Game Programming
  9. //
  10.  
  11. #include <StdIO.H>
  12.  
  13. #define DOS
  14.  
  15. #ifdef DOS
  16.   #include <IOStream.H>
  17. #endif
  18.  
  19. #include "TextType.HPP"
  20.  
  21. int ImageData::LoadINI ( char *FileName )
  22.   {
  23.   int ErrCode, FileCount = 0, N;
  24.   char String [ 256 ];
  25.   #ifdef DOS
  26.   double IPos;
  27.   #endif
  28.   FILE *InFile;
  29.   if ( ( InFile = fopen ( FileName, "rt" ) ) == 0 )
  30.      return 0;
  31.  
  32.   for ( ;; )
  33.       {
  34.       ErrCode = fscanf ( InFile, "%s", String );
  35.       if ( ( ErrCode != 0 ) && ( ErrCode != EOF ) )
  36.          ++FileCount;
  37.       else break;
  38.       }
  39.  
  40.   rewind ( InFile );
  41.  
  42.   TCount = FileCount;
  43.   if ( ( TMap = new BMPImage [ TCount ] ) == 0 )
  44.      {
  45.      fclose ( InFile );
  46.      return 0;
  47.      }
  48.   #ifdef DOS
  49.   cout << "\nLoading texture data:\n";
  50.   #endif
  51.   for ( N = 0; N < TCount; N++ )
  52.       {
  53.       #ifdef DOS
  54.       IPos = ( ( double ) N / ( double ) TCount ) * 101.0F;
  55.       cout << ( ( long ) IPos ) << "%\r";
  56.       #endif
  57.       fscanf ( InFile, "%s", String );
  58.       if ( TMap [ N ].Load ( String ) != BMPImage::Success )
  59.          {
  60.          fclose ( InFile );
  61.          return 0;
  62.          }
  63.       }
  64.   fclose ( InFile );
  65.   return 1;
  66.   }
  67.  
  68. int ImageData::LoadBT ( FILE *InFile )
  69.   {
  70.   int N;
  71.   #ifdef DOS
  72.   double IPos;
  73.   #endif
  74.  
  75.   // Load the number of textures:
  76.   fread ( &TCount, sizeof TCount, 1, InFile );
  77.  
  78.   // Allocate memory for bitmap images:
  79.   if ( ( TMap = new BMPImage [ TCount ] ) == 0 )
  80.      return 0;
  81.  
  82.   // Load palette:
  83.   TMap [ 0 ].LoadPal ( InFile );
  84.  
  85.   // Assign palette to all images:
  86.   for ( N = 1; N < TCount; N++ )
  87.       TMap [ N ].Palette = TMap [ 0 ].Palette;
  88.  
  89.   #ifdef DOS
  90.   cout << "\nLoading texture data:\n";
  91.   #endif
  92.  
  93.   // Load the textures:
  94.   for ( N = 0; N < TCount; N++ )
  95.       {
  96.       #ifdef DOS
  97.       IPos = ( ( double ) N / ( double ) TCount ) * 101.0F;
  98.       cout << ( ( long ) IPos ) << "%\r";
  99.       #endif
  100.       if ( TMap [ N ].LoadBT ( InFile ) == 0 )
  101.          return 0;
  102.       }
  103.   return 1;
  104.   }
  105.  
  106. int ImageData::SaveBT ( FILE *OutFile )
  107.   {
  108.   int N;
  109.   // Save the number of texture maps:
  110.   fwrite ( &TCount, sizeof TCount, 1, OutFile );
  111.  
  112.   // Save the palette:
  113.   TMap [ 0 ].SavePal ( OutFile );
  114.  
  115.   // Save the texture data:
  116.   for ( N = 0; N < TCount; N++ )
  117.       {
  118.       if ( TMap [ N ].SaveBT ( OutFile ) == 0 )
  119.          return 0;
  120.       }
  121.   return 1;
  122.   }
  123.