home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / WD_SRC.ZIP / SOURCE / DOOM_TEX.CPP < prev    next >
C/C++ Source or Header  |  1995-01-12  |  7KB  |  261 lines

  1. #include "..\Source\LastWolf.hpp"
  2.  
  3. MPointerArray pPNames;
  4.  
  5. BOOL InitDoomTex( MFile *pWadFile, DWORD directoryPos )
  6. {
  7.     DirEntry dirPNames;
  8.     WORD nPNames, i;
  9.     
  10.     // Load in the whole PNAMES lookup table.
  11.     GetDirEntry( pWadFile, "PNAMES", directoryPos, &dirPNames );
  12.     pWadFile->fio_Seek( dirPNames.resPos );
  13.     
  14.     pWadFile->fio_Read( &nPNames, 2 );
  15.     pWadFile->fio_Seek( pWadFile->fio_GetPosition() + 2 );
  16.     
  17.     pPNames.SetSize(nPNames);
  18.     for( i=0; i < nPNames; i++ )
  19.     {
  20.         pPNames.Set( i, new char[8] );
  21.         pWadFile->fio_Read( pPNames.Get(i), 8 );
  22.     }
  23.  
  24.     return TRUE;
  25. }
  26.  
  27. BOOL UnInitDoomTex()
  28. {
  29.     WORD i;
  30.     
  31.     for( i=0; i < pPNames.NumElements(); i++ )
  32.         delete pPNames.Get(i);
  33.  
  34.     return TRUE;
  35. }
  36.  
  37.  
  38. TextureID LoadSidedefTexture( DoomSidedef *pSidedef, MFile *pWadFile, DWORD directoryPos )
  39. {
  40.     TextureID idTexture;
  41.     
  42.     Texture *pNewTexture;
  43.     DWORD texturePos;
  44.     
  45.     char terminatedTextureName[50];
  46.     
  47.     
  48.     // Handle the case where there is no texture.
  49.     if( pSidedef->normalTex[0] == '-' )
  50.     {
  51.         idTexture = BAD_TEXTURE_ID;
  52.     }
  53.     else
  54.     {    
  55.         idTexture = tx_GetIDFromName( pSidedef->normalTex );
  56.  
  57.         // If the the texture didn't exist, load it from pWadFile.
  58.         if( idTexture == BAD_TEXTURE_ID )
  59.         {
  60.             strncpy( terminatedTextureName, pSidedef->normalTex, 8 );
  61.             terminatedTextureName[8] = 0;
  62.             
  63.             puts( terminatedTextureName  );
  64.             
  65.             // Create a new texture and fill in its name.
  66.             pNewTexture = tx_AddTexture( &idTexture );                        
  67.             strncpy( pNewTexture->textureName, pSidedef->normalTex, 8 );
  68.     
  69.             texturePos = FindTexture( pWadFile, pSidedef->normalTex, directoryPos );
  70.             assert( texturePos != -1 );
  71.             
  72.             ComposeTexture( pWadFile, directoryPos, texturePos, pNewTexture );        
  73.         
  74.             // Set the shifting and masking values here. 
  75.             switch( pNewTexture->width )
  76.             {
  77.                 case 32:
  78.                     pNewTexture->xTextureShift = (FIXED_SHIFT - 5);
  79.                     pNewTexture->xTextureMask = 31;
  80.                     break;                
  81.                 
  82.                 case 64:
  83.                     pNewTexture->xTextureShift = (FIXED_SHIFT - 6);
  84.                     pNewTexture->xTextureMask = 63;
  85.                     break;
  86.                 
  87.                 case 128:
  88.                     pNewTexture->xTextureShift = (FIXED_SHIFT - 7);
  89.                     pNewTexture->xTextureMask = 127;
  90.                     break;
  91.  
  92.                 case 256:
  93.                     pNewTexture->xTextureShift = (FIXED_SHIFT - 8);
  94.                     pNewTexture->xTextureMask = 255;
  95.                     break;
  96.             }            
  97.         }
  98.     }
  99.  
  100.     return idTexture;
  101. }
  102.  
  103.  
  104. DWORD FindTexture( MFile *pWadFile, BYTE *pTexName, DWORD directoryPos )
  105. {
  106.     DirEntry dirTexture1;
  107.     
  108.     DWORD nTextures;
  109.     WORD n;
  110.     MDWordArray textureOffsets;
  111.     TextureEntry testEntry;
  112.  
  113.  
  114.     // Get TEXTURE1 and TEXTURE2 directory entries.
  115.     GetDirEntry( pWadFile, "TEXTURE1", directoryPos, &dirTexture1 );
  116.  
  117.     // Find the texture name in TEXTURE1 .. TEXTURE2 doesn't seem to exist for Doom 2.
  118.     pWadFile->fio_Seek( dirTexture1.resPos );
  119.     pWadFile->fio_Read( &nTextures, 4 );
  120.         
  121.     textureOffsets.SetSize(nTextures);
  122.     pWadFile->fio_Read( textureOffsets.GetBuffer(), nTextures*4 );
  123.         
  124.     for( n=0; n < nTextures; n++ )
  125.     {
  126.         pWadFile->fio_Seek( textureOffsets.Get(n) + dirTexture1.resPos );
  127.         pWadFile->fio_Read( &testEntry, sizeof(testEntry) );
  128.             
  129.         if( strncmp( testEntry.textureName, pTexName, 8 ) == 0 )
  130.         {
  131.             return textureOffsets.Get(n) + dirTexture1.resPos;        
  132.         }        
  133.     }        
  134.  
  135.  
  136.     // If it gets here, it can't find it.
  137.     return -1;
  138. }                                                            
  139.  
  140.  
  141.  
  142. BOOL ComposeTexture( MFile *pWadFile, DWORD directoryPos, DWORD texturePos, Texture *pNewTexture )
  143. {
  144.     // Uses the maximum number of Patches automatically .. I was getting some
  145.     // stupid memory allocation error when I allocated them.
  146.     Patch Patches[64];
  147.  
  148.     TextureEntry texEntry;
  149.     WORD i;
  150.         
  151.     
  152.     // Read the texture descriptor.
  153.     pWadFile->fio_Seek( texturePos );
  154.     pWadFile->fio_Read( &texEntry, sizeof(texEntry) );
  155.     
  156.     // Read all the patches.
  157.     pWadFile->fio_Read( &Patches, sizeof(Patch) * texEntry.nPatchDescriptors );
  158.     
  159.     // Create the actual texture space.
  160.     pNewTexture->width = texEntry.totalWidth;
  161.     pNewTexture->height = texEntry.totalHeight;
  162.     
  163.     pNewTexture->pVertLines = new BYTE *[pNewTexture->width];
  164.     for( i=0; i < pNewTexture->width; i++ )
  165.         pNewTexture->pVertLines[i] = new BYTE[pNewTexture->height];
  166.     
  167.  
  168.     // Draw all the textures into the texture space.
  169.     for( i=0; i < texEntry.nPatchDescriptors; i++ )
  170.         PastePatch( pNewTexture, &Patches[i], pWadFile, directoryPos );
  171.  
  172.     return TRUE;
  173. }
  174.  
  175.  
  176. BOOL PastePatch( Texture *pPasteTo, Patch *pPasteFrom, MFile *pWadFile, DWORD directoryPos )
  177. {
  178.     DirEntry dirPicture;
  179.     WORD i, startExtra, endExtra;
  180.     char *patchName;
  181.         
  182.     // Temporary data for the Patch.
  183.     WORD picWidth, picHeight, xOffset, yOffset;
  184.     MDWordArray columnPointers;
  185.     
  186.     DWORD bufferSize, bufferPos;
  187.     BYTE *pPatchBuffer;
  188.     
  189.     WORD vertStart, vertLen;
  190.     
  191.     
  192.     // Read in the header.
  193.     patchName = (char *)pPNames.Get( pPasteFrom->iPNamesEntry );
  194.     GetDirEntry( pWadFile, patchName, directoryPos, &dirPicture );
  195.     pWadFile->fio_Seek( dirPicture.resPos );
  196.     
  197.     pWadFile->fio_Read( &picWidth, 2 );
  198.     pWadFile->fio_Read( &picHeight, 2 );
  199.     pWadFile->fio_Read( &xOffset, 2 );
  200.     pWadFile->fio_Read( &yOffset, 2 );
  201.                 
  202.     columnPointers.SetSize(picWidth);
  203.     pWadFile->fio_Read( columnPointers.GetBuffer(), picWidth*4 );
  204.     
  205.     // Allocate a giant buffer for all the picture data (the 1024 at the end is for the last column).
  206.     bufferSize = columnPointers.Get(columnPointers.NumElements()-1) + 1024;
  207.     pPatchBuffer = new BYTE[ bufferSize ];
  208.  
  209.     // Read the data into it.
  210.     pWadFile->fio_Seek( dirPicture.resPos );
  211.     pWadFile->fio_Read( pPatchBuffer, bufferSize );
  212.  
  213.     // Read in the picture data and paste it on pPasteTo.
  214.     for( i=0; i < picWidth; i++ )
  215.     {
  216.         if( i + pPasteFrom->xOffset >= pPasteTo->width || i + pPasteFrom->xOffset < 0 )
  217.             continue;
  218.             
  219.         bufferPos = columnPointers.Get(i);
  220.         
  221.         while(1)
  222.         {
  223.             vertStart = pPatchBuffer[bufferPos];
  224.             ++bufferPos;
  225.             if( vertStart == 255 )
  226.                 break;
  227.         
  228.             vertLen = pPatchBuffer[bufferPos];
  229.             ++bufferPos;
  230.             vertStart += pPasteFrom->yOffset;
  231.                         
  232.             startExtra = 1;
  233.             endExtra = 1;
  234.  
  235.             if( vertStart < 0 )
  236.             {
  237.                 startExtra += -vertStart;
  238.                 vertLen += vertStart;
  239.                 vertStart = 0;
  240.             }
  241.  
  242.             if( vertStart + vertLen > pPasteTo->height )
  243.             {
  244.                 endExtra += (vertStart+vertLen) - pPasteTo->height;
  245.                 vertLen = pPasteTo->height - vertStart;
  246.             }
  247.             
  248.             // The two fio_ReadByte()'s here are just for the extra 2 bytes the WAD has for each picture.
  249.             bufferPos += startExtra;
  250.             memcpy( &pPasteTo->pVertLines[ i+pPasteFrom->xOffset ][ vertStart ], &pPatchBuffer[bufferPos], vertLen );
  251.             bufferPos += vertLen;
  252.             bufferPos += endExtra;
  253.         }
  254.     }        
  255.     
  256.     delete pPatchBuffer;
  257.  
  258.     return TRUE;
  259. }
  260.  
  261.