home *** CD-ROM | disk | FTP | other *** search
/ ST-Computer Leser-CD 2000 January / LCD_01_2000.iso / games / doom / pmdoom / src / r_data.c < prev    next >
C/C++ Source or Header  |  1999-12-17  |  20KB  |  871 lines

  1. /*  Emacs style mode select   -*- C++ -*-  */
  2. /* ----------------------------------------------------------------------------- */
  3. /*  */
  4. /*  $Id:$ */
  5. /*  */
  6. /*  Copyright (C) 1993-1996 by id Software, Inc. */
  7. /*  */
  8. /*  This source is available for distribution and/or modification */
  9. /*  only under the terms of the DOOM Source Code License as */
  10. /*  published by id Software. All rights reserved. */
  11. /*  */
  12. /*  The source is distributed in the hope that it will be useful, */
  13. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of */
  14. /*  FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License */
  15. /*  for more details. */
  16. /*  */
  17. /*  $Log:$ */
  18. /*  */
  19. /*  Revision 1.3  1997/01/29 20:10 */
  20. /*  DESCRIPTION: */
  21. /*     Preparation of data for rendering, */
  22. /*     generation of lookups, caching, retrieval by name. */
  23. /*  */
  24. /* ----------------------------------------------------------------------------- */
  25.  
  26.  
  27. static const char
  28. rcsid[] = "$Id: r_data.c,v 1.4 1997/02/03 16:47:55 b1 Exp $";
  29.  
  30. #include "i_system.h"
  31. #include "z_zone.h"
  32.  
  33. #include "m_swap.h"
  34.  
  35. #include "w_wad.h"
  36.  
  37. #include "doomdef.h"
  38. #include "r_local.h"
  39. #include "p_local.h"
  40.  
  41. #include "doomstat.h"
  42. #include "r_sky.h"
  43.  
  44. #ifdef LINUX
  45. #include  <alloca.h>
  46. #endif
  47.  
  48.  
  49. #include "r_data.h"
  50.  
  51. /*  */
  52. /*  Graphics. */
  53. /*  DOOM graphics for walls and sprites */
  54. /*  is stored in vertical runs of opaque pixels (posts). */
  55. /*  A column is composed of zero or more posts, */
  56. /*  a patch or sprite is composed of zero or more columns. */
  57. /*   */
  58.  
  59.  
  60.  
  61. /*  */
  62. /*  Texture definition. */
  63. /*  Each texture is composed of one or more patches, */
  64. /*  with patches being lumps stored in the WAD. */
  65. /*  The lumps are referenced by number, and patched */
  66. /*  into the rectangular texture space using origin */
  67. /*  and possibly other attributes. */
  68. /*  */
  69. typedef struct
  70. {
  71.     short    originx;
  72.     short    originy;
  73.     short    patch;
  74.     short    stepdir;
  75.     short    colormap;
  76. } mappatch_t;
  77.  
  78.  
  79. /*  */
  80. /*  Texture definition. */
  81. /*  A DOOM wall texture is a list of patches */
  82. /*  which are to be combined in a predefined order. */
  83. /*  */
  84. typedef struct
  85. {
  86.     char        name[8];
  87.     boolean        masked;    
  88.     short        width;
  89.     short        height;
  90.     void        **columndirectory;    /*  OBSOLETE */
  91.     short        patchcount;
  92.     mappatch_t    patches[1];
  93. } maptexture_t;
  94.  
  95.  
  96. /*  A single patch from a texture definition, */
  97. /*   basically a rectangular area within */
  98. /*   the texture rectangle. */
  99. typedef struct
  100. {
  101.     /*  Block origin (allways UL), */
  102.     /*  which has allready accounted */
  103.     /*  for the internal origin of the patch. */
  104.     int        originx;    
  105.     int        originy;
  106.     int        patch;
  107. } texpatch_t;
  108.  
  109.  
  110. /*  A maptexturedef_t describes a rectangular texture, */
  111. /*   which is composed of one or more mappatch_t structures */
  112. /*   that arrange graphic patches. */
  113. typedef struct
  114. {
  115.     /*  Keep name for switch changing, etc. */
  116.     char    name[8];        
  117.     short    width;
  118.     short    height;
  119.     
  120.     /*  All the patches[patchcount] */
  121.     /*   are drawn back to front into the cached texture. */
  122.     short    patchcount;
  123.     texpatch_t    patches[1];        
  124.     
  125. } texture_t;
  126.  
  127.  
  128.  
  129. int        firstflat;
  130. int        lastflat;
  131. int        numflats;
  132.  
  133. int        firstpatch;
  134. int        lastpatch;
  135. int        numpatches;
  136.  
  137. int        firstspritelump;
  138. int        lastspritelump;
  139. int        numspritelumps;
  140.  
  141. int        numtextures;
  142. texture_t**    textures;
  143.  
  144.  
  145. int*            texturewidthmask;
  146. /*  needed for texture pegging */
  147. fixed_t*        textureheight;        
  148. int*            texturecompositesize;
  149. short**            texturecolumnlump;
  150. unsigned short**    texturecolumnofs;
  151. byte**            texturecomposite;
  152.  
  153. /*  for global animation */
  154. int*        flattranslation;
  155. int*        texturetranslation;
  156.  
  157. /*  needed for pre rendering */
  158. fixed_t*    spritewidth;    
  159. fixed_t*    spriteoffset;
  160. fixed_t*    spritetopoffset;
  161.  
  162. /*  Original colormap from WAD */
  163. byte        *orig_colormaps;
  164. lighttable_t    *colormaps;
  165.  
  166. /*  */
  167. /*  MAPTEXTURE_T CACHING */
  168. /*  When a texture is first needed, */
  169. /*   it counts the number of composite columns */
  170. /*   required in the texture and allocates space */
  171. /*   for a column directory and any new columns. */
  172. /*  The directory will simply point inside other patches */
  173. /*   if there is only one patch in a given column, */
  174. /*   but any columns with multiple patches */
  175. /*   will have new column_ts generated. */
  176. /*  */
  177.  
  178.  
  179.  
  180. /*  */
  181. /*  R_DrawColumnInCache */
  182. /*  Clip and draw a column */
  183. /*   from a patch into a cached post. */
  184. /*  */
  185. void
  186. R_DrawColumnInCache
  187. ( column_t*    patch,
  188.   byte*        cache,
  189.   int        originy,
  190.   int        cacheheight )
  191. {
  192.     int        count;
  193.     int        position;
  194.     byte*    source;
  195.     byte*    dest;
  196.     
  197.     dest = (byte *)cache + 3;
  198.     
  199.     while (patch->topdelta != 0xff)
  200.     {
  201.     source = (byte *)patch + 3;
  202.     count = patch->length;
  203.     position = originy + patch->topdelta;
  204.  
  205.     if (position < 0)
  206.     {
  207.         count += position;
  208.         position = 0;
  209.     }
  210.  
  211.     if (position + count > cacheheight)
  212.         count = cacheheight - position;
  213.  
  214.     if (count > 0)
  215.         memcpy (cache + position, source, count);
  216.         
  217.     patch = (column_t *)(  (byte *)patch + patch->length + 4); 
  218.     }
  219. }
  220.  
  221.  
  222.  
  223. /*  */
  224. /*  R_GenerateComposite */
  225. /*  Using the texture definition, */
  226. /*   the composite texture is created from the patches, */
  227. /*   and each column is cached. */
  228. /*  */
  229. void R_GenerateComposite (int texnum)
  230. {
  231.     byte*        block;
  232.     texture_t*        texture;
  233.     texpatch_t*        patch;    
  234.     patch_t*        realpatch;
  235.     int            x;
  236.     int            x1;
  237.     int            x2;
  238.     int            i;
  239.     column_t*        patchcol;
  240.     short*        collump;
  241.     unsigned short*    colofs;
  242.     
  243.     texture = textures[texnum];
  244.  
  245.     block = Z_Malloc (texturecompositesize[texnum],
  246.               PU_STATIC, 
  247.               &texturecomposite[texnum]);    
  248.  
  249.     collump = texturecolumnlump[texnum];
  250.     colofs = texturecolumnofs[texnum];
  251.     
  252.     /*  Composite the columns together. */
  253.     patch = texture->patches;
  254.         
  255.     for (i=0 , patch = texture->patches;
  256.      i<texture->patchcount;
  257.      i++, patch++)
  258.     {
  259.     realpatch = W_CacheLumpNum (patch->patch, PU_CACHE);
  260.     x1 = patch->originx;
  261.     x2 = x1 + SHORT(realpatch->width);
  262.  
  263.     if (x1<0)
  264.         x = 0;
  265.     else
  266.         x = x1;
  267.     
  268.     if (x2 > texture->width)
  269.         x2 = texture->width;
  270.  
  271.     for ( ; x<x2 ; x++)
  272.     {
  273.         /*  Column does not have multiple patches? */
  274.         if (collump[x] >= 0)
  275.         continue;
  276.         
  277.         patchcol = (column_t *)((byte *)realpatch
  278.                     + LONG(realpatch->columnofs[x-x1]));
  279.         R_DrawColumnInCache (patchcol,
  280.                  block + colofs[x],
  281.                  patch->originy,
  282.                  texture->height);
  283.     }
  284.                         
  285.     }
  286.  
  287.     /*  Now that the texture has been built in column cache, */
  288.     /*   it is purgable from zone memory. */
  289.     Z_ChangeTag (block, PU_CACHE);
  290. }
  291.  
  292.  
  293.  
  294. /*  */
  295. /*  R_GenerateLookup */
  296. /*  */
  297. void R_GenerateLookup (int texnum)
  298. {
  299.     texture_t*        texture;
  300.     byte*        patchcount;    /*  patchcount[texture->width] */
  301.     texpatch_t*        patch;    
  302.     patch_t*        realpatch;
  303.     int            x;
  304.     int            x1;
  305.     int            x2;
  306.     int            i;
  307.     short*        collump;
  308.     unsigned short*    colofs;
  309.     
  310.     texture = textures[texnum];
  311.  
  312.     /*  Composited texture not created yet. */
  313.     texturecomposite[texnum] = 0;
  314.     
  315.     texturecompositesize[texnum] = 0;
  316.     collump = texturecolumnlump[texnum];
  317.     colofs = texturecolumnofs[texnum];
  318.     
  319.     /*  Now count the number of columns */
  320.     /*   that are covered by more than one patch. */
  321.     /*  Fill in the lump / offset, so columns */
  322.     /*   with only a single patch are all done. */
  323.     patchcount = (byte *)alloca (texture->width);
  324.     memset (patchcount, 0, texture->width);
  325.     patch = texture->patches;
  326.         
  327.     for (i=0 , patch = texture->patches;
  328.      i<texture->patchcount;
  329.      i++, patch++)
  330.     {
  331.     realpatch = W_CacheLumpNum (patch->patch, PU_CACHE);
  332.     x1 = patch->originx;
  333.     x2 = x1 + SHORT(realpatch->width);
  334.     
  335.     if (x1 < 0)
  336.         x = 0;
  337.     else
  338.         x = x1;
  339.  
  340.     if (x2 > texture->width)
  341.         x2 = texture->width;
  342.     for ( ; x<x2 ; x++)
  343.     {
  344.         patchcount[x]++;
  345.         collump[x] = patch->patch;
  346.         colofs[x] = LONG(realpatch->columnofs[x-x1])+3;
  347.     }
  348.     }
  349.     
  350.     for (x=0 ; x<texture->width ; x++)
  351.     {
  352.     if (!patchcount[x])
  353.     {
  354.         printf ("R_GenerateLookup: column without a patch (%s)\n",
  355.             texture->name);
  356.         return;
  357.     }
  358.     /*  I_Error ("R_GenerateLookup: column without a patch"); */
  359.     
  360.     if (patchcount[x] > 1)
  361.     {
  362.         /*  Use the cached block. */
  363.         collump[x] = -1;    
  364.         colofs[x] = texturecompositesize[texnum];
  365.         
  366.         if (texturecompositesize[texnum] > 0x10000-texture->height)
  367.         {
  368.         I_Error ("R_GenerateLookup: texture %i is >64k",
  369.              texnum);
  370.         }
  371.         
  372.         texturecompositesize[texnum] += texture->height;
  373.     }
  374.     }    
  375. }
  376.  
  377.  
  378.  
  379.  
  380. /*  */
  381. /*  R_GetColumn */
  382. /*  */
  383. byte*
  384. R_GetColumn
  385. ( int        tex,
  386.   int        col )
  387. {
  388.     int        lump;
  389.     int        ofs;
  390.     
  391.     col &= texturewidthmask[tex];
  392.     lump = texturecolumnlump[tex][col];
  393.     ofs = texturecolumnofs[tex][col];
  394.     
  395.     if (lump > 0)
  396.     return (byte *)W_CacheLumpNum(lump,PU_CACHE)+ofs;
  397.  
  398.     if (!texturecomposite[tex])
  399.     R_GenerateComposite (tex);
  400.  
  401.     return texturecomposite[tex] + ofs;
  402. }
  403.  
  404.  
  405.  
  406.  
  407. /*  */
  408. /*  R_InitTextures */
  409. /*  Initializes the texture list */
  410. /*   with the textures from the world map. */
  411. /*  */
  412. void R_InitTextures (void)
  413. {
  414.     maptexture_t*    mtexture;
  415.     texture_t*        texture;
  416.     mappatch_t*        mpatch;
  417.     texpatch_t*        patch;
  418.  
  419.     int            i;
  420.     int            j;
  421.  
  422.     int*        maptex;
  423.     int*        maptex2;
  424.     int*        maptex1;
  425.     
  426.     char        name[9];
  427.     char*        names;
  428.     char*        name_p;
  429.     
  430.     int*        patchlookup;
  431.     
  432.     int            totalwidth;
  433.     int            nummappatches;
  434.     int            offset;
  435.     int            maxoff;
  436.     int            maxoff2;
  437.     int            numtextures1;
  438.     int            numtextures2;
  439.  
  440.     int*        directory;
  441.     
  442.     int            temp1;
  443.     int            temp2;
  444.     int            temp3;
  445.  
  446.     
  447.     /*  Load the patch names from pnames.lmp. */
  448.     name[8] = 0;    
  449.     names = W_CacheLumpName ("PNAMES", PU_STATIC);
  450.     nummappatches = LONG ( *((int *)names) );
  451.     name_p = names+4;
  452.     patchlookup = alloca (nummappatches*sizeof(*patchlookup));
  453.     
  454.     for (i=0 ; i<nummappatches ; i++)
  455.     {
  456.     strncpy (name,name_p+i*8, 8);
  457.     patchlookup[i] = W_CheckNumForName (name);
  458.     }
  459.     Z_Free (names);
  460.     
  461.     /*  Load the map texture definitions from textures.lmp. */
  462.     /*  The data is contained in one or two lumps, */
  463.     /*   TEXTURE1 for shareware, plus TEXTURE2 for commercial. */
  464.     maptex = maptex1 = W_CacheLumpName ("TEXTURE1", PU_STATIC);
  465.     numtextures1 = LONG(*maptex);
  466.     maxoff = W_LumpLength (W_GetNumForName ("TEXTURE1"));
  467.     directory = maptex+1;
  468.     
  469.     if (W_CheckNumForName ("TEXTURE2") != -1)
  470.     {
  471.     maptex2 = W_CacheLumpName ("TEXTURE2", PU_STATIC);
  472.     numtextures2 = LONG(*maptex2);
  473.     maxoff2 = W_LumpLength (W_GetNumForName ("TEXTURE2"));
  474.     }
  475.     else
  476.     {
  477.     maptex2 = NULL;
  478.     numtextures2 = 0;
  479.     maxoff2 = 0;
  480.     }
  481.     numtextures = numtextures1 + numtextures2;
  482.     
  483.     textures = Z_Malloc (numtextures*4, PU_STATIC, 0);
  484.     texturecolumnlump = Z_Malloc (numtextures*4, PU_STATIC, 0);
  485.     texturecolumnofs = Z_Malloc (numtextures*4, PU_STATIC, 0);
  486.     texturecomposite = Z_Malloc (numtextures*4, PU_STATIC, 0);
  487.     texturecompositesize = Z_Malloc (numtextures*4, PU_STATIC, 0);
  488.     texturewidthmask = Z_Malloc (numtextures*4, PU_STATIC, 0);
  489.     textureheight = Z_Malloc (numtextures*4, PU_STATIC, 0);
  490.  
  491.     totalwidth = 0;
  492.     
  493.     /*     Really complex printing shit... */
  494.     temp1 = W_GetNumForName ("S_START");  /*  P_??????? */
  495.     temp2 = W_GetNumForName ("S_END") - 1;
  496.     temp3 = ((temp2-temp1+63)/64) + ((numtextures+63)/64);
  497.     printf("[");
  498.     for (i = 0; i < temp3; i++)
  499.     printf(" ");
  500.     printf("         ]");
  501.     for (i = 0; i < temp3; i++)
  502.     printf("\x8");
  503.     printf("\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8");    
  504.     
  505.     for (i=0 ; i<numtextures ; i++, directory++)
  506.     {
  507.     if (!(i&63))
  508.         printf (".");
  509.  
  510.     if (i == numtextures1)
  511.     {
  512.         /*  Start looking in second texture file. */
  513.         maptex = maptex2;
  514.         maxoff = maxoff2;
  515.         directory = maptex+1;
  516.     }
  517.         
  518.     offset = LONG(*directory);
  519.  
  520.     if (offset > maxoff)
  521.         I_Error ("R_InitTextures: bad texture directory");
  522.     
  523.     mtexture = (maptexture_t *) ( (byte *)maptex + offset);
  524.  
  525.     texture = textures[i] =
  526.         Z_Malloc (sizeof(texture_t)
  527.               + sizeof(texpatch_t)*(SHORT(mtexture->patchcount)-1),
  528.               PU_STATIC, 0);
  529.     
  530.     texture->width = SHORT(mtexture->width);
  531.     texture->height = SHORT(mtexture->height);
  532.     texture->patchcount = SHORT(mtexture->patchcount);
  533.  
  534.     memcpy (texture->name, mtexture->name, sizeof(texture->name));
  535.     mpatch = &mtexture->patches[0];
  536.     patch = &texture->patches[0];
  537.  
  538.     for (j=0 ; j<texture->patchcount ; j++, mpatch++, patch++)
  539.     {
  540.         patch->originx = SHORT(mpatch->originx);
  541.         patch->originy = SHORT(mpatch->originy);
  542.         patch->patch = patchlookup[SHORT(mpatch->patch)];
  543.         if (patch->patch == -1)
  544.         {
  545.         I_Error ("R_InitTextures: Missing patch in texture %s",
  546.              texture->name);
  547.         }
  548.     }        
  549.     texturecolumnlump[i] = Z_Malloc (texture->width*2, PU_STATIC,0);
  550.     texturecolumnofs[i] = Z_Malloc (texture->width*2, PU_STATIC,0);
  551.  
  552.     j = 1;
  553.     while (j*2 <= texture->width)
  554.         j<<=1;
  555.  
  556.     texturewidthmask[i] = j-1;
  557.     textureheight[i] = texture->height<<FRACBITS;
  558.         
  559.     totalwidth += texture->width;
  560.     }
  561.  
  562.     Z_Free (maptex1);
  563.     if (maptex2)
  564.     Z_Free (maptex2);
  565.     
  566.     /*  Precalculate whatever possible.     */
  567.     for (i=0 ; i<numtextures ; i++)
  568.     R_GenerateLookup (i);
  569.     
  570.     /*  Create translation table for global animation. */
  571.     texturetranslation = Z_Malloc ((numtextures+1)*4, PU_STATIC, 0);
  572.     
  573.     for (i=0 ; i<numtextures ; i++)
  574.     texturetranslation[i] = i;
  575. }
  576.  
  577.  
  578.  
  579. /*  */
  580. /*  R_InitFlats */
  581. /*  */
  582. void R_InitFlats (void)
  583. {
  584.     int        i;
  585.     
  586.     firstflat = W_GetNumForName ("F_START") + 1;
  587.     lastflat = W_GetNumForName ("F_END") - 1;
  588.     numflats = lastflat - firstflat + 1;
  589.     
  590.     /*  Create translation table for global animation. */
  591.     flattranslation = Z_Malloc ((numflats+1)*4, PU_STATIC, 0);
  592.     
  593.     for (i=0 ; i<numflats ; i++)
  594.     flattranslation[i] = i;
  595. }
  596.  
  597.  
  598. /*  */
  599. /*  R_InitSpriteLumps */
  600. /*  Finds the width and hoffset of all sprites in the wad, */
  601. /*   so the sprite does not need to be cached completely */
  602. /*   just for having the header info ready during rendering. */
  603. /*  */
  604. void R_InitSpriteLumps (void)
  605. {
  606.     int        i;
  607.     patch_t    *patch;
  608.     
  609.     firstspritelump = W_GetNumForName ("S_START") + 1;
  610.     lastspritelump = W_GetNumForName ("S_END") - 1;
  611.     
  612.     numspritelumps = lastspritelump - firstspritelump + 1;
  613.     spritewidth = Z_Malloc (numspritelumps*4, PU_STATIC, 0);
  614.     spriteoffset = Z_Malloc (numspritelumps*4, PU_STATIC, 0);
  615.     spritetopoffset = Z_Malloc (numspritelumps*4, PU_STATIC, 0);
  616.     
  617.     for (i=0 ; i< numspritelumps ; i++)
  618.     {
  619.     if (!(i&63))
  620.         printf (".");
  621.  
  622.     patch = W_CacheLumpNum (firstspritelump+i, PU_CACHE);
  623.     spritewidth[i] = SHORT(patch->width)<<FRACBITS;
  624.     spriteoffset[i] = SHORT(patch->leftoffset)<<FRACBITS;
  625.     spritetopoffset[i] = SHORT(patch->topoffset)<<FRACBITS;
  626.     }
  627. }
  628.  
  629.  
  630.  
  631. /*  */
  632. /*  R_InitColormaps */
  633. /*  */
  634. extern int pixel_size;
  635.  
  636. void R_InitColormaps (void)
  637. {
  638.     int    lump, length;
  639.     int x,y;
  640.     
  641.     /*  Load in the light tables,  */
  642.     /*   256 byte align tables. */
  643.     lump = W_GetNumForName("COLORMAP"); 
  644. #if 0
  645.     length = W_LumpLength (lump) + 255; 
  646.     orig_colormaps = Z_Malloc (length, PU_STATIC, 0); 
  647.     orig_colormaps = (byte *)( ((int)orig_colormaps + 255)&~0xff); 
  648. #else
  649.     length = W_LumpLength (lump) ; 
  650.     orig_colormaps = Z_Malloc (length, PU_STATIC, 0); 
  651. #endif
  652.     W_ReadLump (lump,orig_colormaps); 
  653.  
  654.     colormaps=Z_Malloc((NUMCOLORMAPS+2)*256*sizeof(lighttable_t),PU_STATIC,0);
  655.  
  656.     /*  Copie orig_colormaps dans colormaps */
  657.     for (x=0;x<256;x++)
  658.     {
  659.         for (y=0;y<(NUMCOLORMAPS+2);y++)
  660.         {
  661.             unsigned long    couleur;
  662.  
  663.             /* Recopier la valeur sur les 4 octets (pour lowdetail en 8bits)*/
  664.             couleur=orig_colormaps[y*256+x] & 255;
  665.             couleur |= (couleur<<24)|(couleur<<16)|(couleur<<8);
  666.             colormaps[y*256+x]=couleur;
  667.         }
  668.     }
  669. }
  670.  
  671.  
  672.  
  673. /*  */
  674. /*  R_InitData */
  675. /*  Locates all the lumps */
  676. /*   that will be used by all views */
  677. /*  Must be called after W_Init. */
  678. /*  */
  679. void R_InitData (void)
  680. {
  681.     R_InitTextures ();
  682.     R_InitFlats ();
  683.     R_InitSpriteLumps ();
  684.     R_InitColormaps ();
  685. }
  686.  
  687.  
  688.  
  689. /*  */
  690. /*  R_FlatNumForName */
  691. /*  Retrieval, get a flat number for a flat name. */
  692. /*  */
  693. int R_FlatNumForName (char* name)
  694. {
  695.     int        i;
  696.     char    namet[9];
  697.  
  698.     i = W_CheckNumForName (name);
  699.  
  700.     if (i == -1)
  701.     {
  702.     namet[8] = 0;
  703.     memcpy (namet, name,8);
  704.     I_Error ("R_FlatNumForName: %s not found",namet);
  705.     }
  706.     return i - firstflat;
  707. }
  708.  
  709.  
  710.  
  711.  
  712. /*  */
  713. /*  R_CheckTextureNumForName */
  714. /*  Check whether texture is available. */
  715. /*  Filter out NoTexture indicator. */
  716. /*  */
  717. int    R_CheckTextureNumForName (char *name)
  718. {
  719.     int        i;
  720.  
  721.     /*  "NoTexture" marker. */
  722.     if (name[0] == '-')        
  723.     return 0;
  724.         
  725.     for (i=0 ; i<numtextures ; i++)
  726.     if (!strncasecmp (textures[i]->name, name, 8) )
  727.         return i;
  728.         
  729.     return -1;
  730. }
  731.  
  732.  
  733.  
  734. /*  */
  735. /*  R_TextureNumForName */
  736. /*  Calls R_CheckTextureNumForName, */
  737. /*   aborts with error message. */
  738. /*  */
  739. int    R_TextureNumForName (char* name)
  740. {
  741.     int        i;
  742.     
  743.     i = R_CheckTextureNumForName (name);
  744.  
  745.     if (i==-1)
  746.     {
  747.     I_Error ("R_TextureNumForName: %s not found",
  748.          name);
  749.     }
  750.     return i;
  751. }
  752.  
  753.  
  754.  
  755.  
  756. /*  */
  757. /*  R_PrecacheLevel */
  758. /*  Preloads all relevant graphics for the level. */
  759. /*  */
  760. int        flatmemory;
  761. int        texturememory;
  762. int        spritememory;
  763.  
  764. void R_PrecacheLevel (void)
  765. {
  766.     char*        flatpresent;
  767.     char*        texturepresent;
  768.     char*        spritepresent;
  769.  
  770.     int            i;
  771.     int            j;
  772.     int            k;
  773.     int            lump;
  774.     
  775.     texture_t*        texture;
  776.     thinker_t*        th;
  777.     spriteframe_t*    sf;
  778.  
  779.     if (demoplayback)
  780.     return;
  781.     
  782.     /*  Precache flats. */
  783.     flatpresent = alloca(numflats);
  784.     memset (flatpresent,0,numflats);    
  785.  
  786.     for (i=0 ; i<numsectors ; i++)
  787.     {
  788.     flatpresent[sectors[i].floorpic] = 1;
  789.     flatpresent[sectors[i].ceilingpic] = 1;
  790.     }
  791.     
  792.     flatmemory = 0;
  793.  
  794.     for (i=0 ; i<numflats ; i++)
  795.     {
  796.     if (flatpresent[i])
  797.     {
  798.         lump = firstflat + i;
  799.         flatmemory += lumpinfo[lump].size;
  800.         W_CacheLumpNum(lump, PU_CACHE);
  801.     }
  802.     }
  803.     
  804.     /*  Precache textures. */
  805.     texturepresent = alloca(numtextures);
  806.     memset (texturepresent,0, numtextures);
  807.     
  808.     for (i=0 ; i<numsides ; i++)
  809.     {
  810.     texturepresent[sides[i].toptexture] = 1;
  811.     texturepresent[sides[i].midtexture] = 1;
  812.     texturepresent[sides[i].bottomtexture] = 1;
  813.     }
  814.  
  815.     /*  Sky texture is always present. */
  816.     /*  Note that F_SKY1 is the name used to */
  817.     /*   indicate a sky floor/ceiling as a flat, */
  818.     /*   while the sky texture is stored like */
  819.     /*   a wall texture, with an episode dependend */
  820.     /*   name. */
  821.     texturepresent[skytexture] = 1;
  822.     
  823.     texturememory = 0;
  824.     for (i=0 ; i<numtextures ; i++)
  825.     {
  826.     if (!texturepresent[i])
  827.         continue;
  828.  
  829.     texture = textures[i];
  830.     
  831.     for (j=0 ; j<texture->patchcount ; j++)
  832.     {
  833.         lump = texture->patches[j].patch;
  834.         texturememory += lumpinfo[lump].size;
  835.         W_CacheLumpNum(lump , PU_CACHE);
  836.     }
  837.     }
  838.     
  839.     /*  Precache sprites. */
  840.     spritepresent = alloca(numsprites);
  841.     memset (spritepresent,0, numsprites);
  842.     
  843.     for (th = thinkercap.next ; th != &thinkercap ; th=th->next)
  844.     {
  845.     if (th->function.acp1 == (actionf_p1)P_MobjThinker)
  846.         spritepresent[((mobj_t *)th)->sprite] = 1;
  847.     }
  848.     
  849.     spritememory = 0;
  850.     for (i=0 ; i<numsprites ; i++)
  851.     {
  852.     if (!spritepresent[i])
  853.         continue;
  854.  
  855.     for (j=0 ; j<sprites[i].numframes ; j++)
  856.     {
  857.         sf = &sprites[i].spriteframes[j];
  858.         for (k=0 ; k<8 ; k++)
  859.         {
  860.         lump = firstspritelump + sf->lump[k];
  861.         spritememory += lumpinfo[lump].size;
  862.         W_CacheLumpNum(lump , PU_CACHE);
  863.         }
  864.     }
  865.     }
  866. }
  867.  
  868.  
  869.  
  870.  
  871.