home *** CD-ROM | disk | FTP | other *** search
/ Crazy Collection 12 / CC-12_1.iso / update / doompack / data.a00 / ADE2.ZIP / SOURCE / SOURCE2.ZIP / TEXTURES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-19  |  14.5 KB  |  457 lines

  1. /*
  2.    Texture display by RaphaĆ«l Quinet <quinet@montefiore.ulg.ac.be>,
  3.                       Trevor Phillips <rphillip@cc.curtin.edu.au>,
  4.                   and Christian Johannes Schladetsch <s924706@yallara.cs.rmit.OZ.AU>
  5.  
  6.    Doom II texture stuff by Adrian Cable.
  7.  
  8.    You are allowed to use any parts of this code in another program, as
  9.    long as you give credits to the authors in the documentation and in
  10.    the program itself.  Read the file README.1ST for more information.
  11.  
  12.    This program comes with absolutely no warranty.
  13.  
  14.    TEXTURES.C - Textures in 256 colors.
  15.  
  16.    Note from CJS:
  17.       DisplayPic() could be further speeded up by avoiding reading
  18.       the same column numerous times. However, this approach involves
  19.       exhorbitant memory usage for certain pictures.
  20. */
  21.  
  22. /* the includes */
  23. #include "deu.h"
  24.  
  25.  
  26. /*
  27.    display a floor or ceiling texture at coords x0, y0 and not beyond x1, y1
  28. */
  29.  
  30. void DisplayFloorTexture( int x0, int y0, int x1, int y1, char *texname)
  31. {
  32.    MDirPtr             dir;     /* main directory pointer to the texture entry */
  33.    unsigned char huge *pixels;  /* array of pixels that hold the image */
  34.  
  35.    dir = FindMasterDir( MasterDir, texname);
  36.    if (dir == NULL)
  37.    {
  38.       SetColor( DARKGRAY);
  39.       DrawScreenLine( x0, y0, x1, y1);
  40.       DrawScreenLine( x0, y1, x1, y0);
  41.       return;
  42.    }
  43.    BasicWadSeek( dir->wadfile, dir->dir.start);
  44.    pixels = GetFarMemory( 4100 * sizeof( char));
  45.    BasicWadRead( dir->wadfile, &(pixels[ 4]), 4096L);
  46.    if (GfxMode < -1)
  47.    {
  48.       /* Probably a bug in the VESA driver...    */
  49.       /* It requires "size-1" instead of "size"! */
  50.       ((unsigned int huge *)pixels)[ 0] = 63;
  51.       ((unsigned int huge *)pixels)[ 1] = 63;
  52.    }
  53.    else
  54.    {
  55.       ((unsigned int huge *)pixels)[ 0] = 64;
  56.       ((unsigned int huge *)pixels)[ 1] = 64;
  57.    }
  58.    putimage( x0, y0, pixels, COPY_PUT);
  59.    FreeFarMemory( pixels);
  60. }
  61.  
  62.  
  63.  
  64. /*
  65.    display a picture "picname" at coords x0, y0 and not beyond x1, y1
  66. */
  67.  
  68. void DisplayPic( int x0, int y0, int x1, int y1, char *picname)
  69. {
  70.    MDirPtr             dir;
  71.    int                 xsize, ysize, xofs, yofs;
  72.    int                 x, y;
  73.  
  74.    unsigned char huge *lpColumnData;
  75.    unsigned char huge *lpColumn;
  76.    long          huge *lpNeededOffsets;
  77.    int                 nColumns, nCurrentColumn;
  78.    long                lCurrentOffset;
  79.    int                 fColumnInMemory;
  80.    int                 i, n;
  81.    unsigned char       bRowStart, bColored;
  82.  
  83.  
  84.    if (bioskey( 1) != 0)
  85.       return; /* speedup */
  86.    dir = FindMasterDir( MasterDir, picname);
  87.    if (dir == NULL)
  88.    {
  89.       SetColor( DARKGRAY);
  90.       DrawScreenLine( x0, y0, x1, y1);
  91.       DrawScreenLine( x0, y1, x1, y0);
  92.       return;
  93.    }
  94.    BasicWadSeek( dir->wadfile, dir->dir.start);
  95.    BasicWadRead( dir->wadfile, &xsize, 2L);
  96.    BasicWadRead( dir->wadfile, &ysize, 2L);
  97.    BasicWadRead( dir->wadfile, &xofs, 2L);
  98.    BasicWadRead( dir->wadfile, &yofs, 2L);
  99.    /* ignore the picture offsets */
  100.    xofs = 0;
  101.    yofs = 0;
  102.  
  103. #define TEX_COLUMNBUFFERSIZE    (60L * 1024L)
  104. #define TEX_COLUMNSIZE          512L
  105.  
  106.    nColumns = xsize;
  107.  
  108.    /* Note from CJS:
  109.       I tried to use far memory originally, but kept getting out-of-mem errors
  110.       that is really strange - I assume that the wad dir et al uses all
  111.       the far mem, and there is only near memory available. NEVER seen
  112.       this situation before..... I'll keep them huge pointers anyway,
  113.       in case something changes later
  114.    */
  115.    lpColumnData    = GetMemory( TEX_COLUMNBUFFERSIZE);
  116.    lpNeededOffsets = GetMemory( nColumns * 4L);
  117.  
  118.    BasicWadRead( dir->wadfile, lpNeededOffsets, nColumns * 4L);
  119.  
  120.    /* read first column data, and subsequent column data */
  121.    BasicWadSeek( dir->wadfile, dir->dir.start + lpNeededOffsets[ 0]);
  122.    BasicWadRead( dir->wadfile, lpColumnData, TEX_COLUMNBUFFERSIZE);
  123.  
  124.    for (nCurrentColumn = 0; nCurrentColumn < nColumns; nCurrentColumn++)
  125.    {
  126.       lCurrentOffset  = lpNeededOffsets[ nCurrentColumn];
  127.       fColumnInMemory = lCurrentOffset >= lpNeededOffsets[ 0] && lCurrentOffset < (long)(lpNeededOffsets[ 0] + TEX_COLUMNBUFFERSIZE - TEX_COLUMNSIZE);
  128.       if (fColumnInMemory)
  129.       {
  130.          lpColumn = &lpColumnData[ lCurrentOffset - lpNeededOffsets[ 0]];
  131.       }
  132.       else
  133.       {
  134.          lpColumn = GetFarMemory( TEX_COLUMNSIZE);
  135.          BasicWadSeek( dir->wadfile, dir->dir.start + lCurrentOffset);
  136.          BasicWadRead( dir->wadfile, lpColumn, TEX_COLUMNSIZE);
  137.       }
  138.  
  139.       /* we now have the needed column data, one way or another, so write it */
  140.       n = 1;
  141.       bRowStart = lpColumn[ 0];
  142.       while (bRowStart != 255 && n < TEX_COLUMNSIZE)
  143.       {
  144.          bColored = lpColumn[ n];
  145.          n += 2;                                /* skip over 'null' pixel in data */
  146.          for (i = 0; i < bColored; i++)
  147.          {
  148.             x = x0 + xofs + nCurrentColumn;
  149.             y = y0 + yofs + bRowStart + i;
  150.             if (x >= x0 && y >= y0 && x <= x1 && y <= y1)
  151.                putpixel( x, y, lpColumn[ i + n]);
  152.          }
  153.          n += bColored + 1;     /* skip over written pixels, and the 'null' one */
  154.          bRowStart = lpColumn[ n++];
  155.       }
  156.       if (bRowStart != 255)
  157.          ProgError( "BUG: bRowStart != 255.");
  158.  
  159.       if (!fColumnInMemory)
  160.          FreeFarMemory( lpColumn);
  161.    }
  162.    FreeMemory( lpColumnData);
  163.    FreeMemory( lpNeededOffsets);
  164. }
  165.  
  166.  
  167.  
  168. /*
  169.    display a wall texture ("texture1" or "texture2" object) at coords x0, y0
  170. */
  171.  
  172. void DisplayWallTexture( int x0, int y0, int x1, int y1, char *texname)
  173. {
  174.    MDirPtr  dir;        /* main directory pointer to the TEXTURE* entries */
  175.    MDirPtr  pdir;       /* main directory pointer to the PNAMES entry */
  176.    long    *offsets;    /* array of offsets to texture names */
  177.    int      n;          /* general counter */
  178.    int      xsize, ysize; /* size of the texture */
  179.    int      xofs, yofs; /* offset in texture space for the wall patch */
  180.    int      fields;     /* number of wall patches used to build this texture */
  181.    int      pnameind;   /* patch name index in PNAMES table */
  182.    int      junk;       /* holds useless data */
  183.    long     numtex;     /* number of texture names in TEXTURE* list */
  184.    long     texofs;     /* offset in the wad file to the texture data */
  185.    char     tname[9];   /* texture name */
  186.    char     picname[9]; /* wall patch name */
  187.  
  188.    if (bioskey( 1) != 0)
  189.       return; /* speedup */
  190.  
  191.    /* offset for texture we want. */
  192.    texofs = 0;
  193.    /* search for texname in texture1 names */
  194.    dir = FindMasterDir( MasterDir, "TEXTURE1");
  195.    BasicWadSeek( dir->wadfile, dir->dir.start);
  196.    BasicWadRead( dir->wadfile, &numtex, 4);
  197.    /* read in the offsets for texture1 names and info. */
  198.    offsets = GetMemory( numtex * sizeof( long));
  199.    for (n = 0; n < numtex; n++)
  200.       BasicWadRead( dir->wadfile, &(offsets[ n]), 4L);
  201.    for (n = 0; n < numtex && !texofs; n++)
  202.    {
  203.       BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
  204.       BasicWadRead( dir->wadfile, &tname, 8);
  205.       if (!strnicmp(tname, texname, 8))
  206.          texofs = dir->dir.start + offsets[ n];
  207.    }
  208.    FreeMemory( offsets);
  209. // if (Registered && texofs == 0)
  210. // {
  211. //    /* search for texname in texture2 names */
  212. //    dir = FindMasterDir( MasterDir, "TEXTURE2");
  213. //    BasicWadSeek( dir->wadfile, dir->dir.start);
  214. //    BasicWadRead( dir->wadfile, &numtex, 4);
  215. //    /* read in the offsets for texture2 names */
  216. //    offsets = GetMemory( numtex * sizeof( long));
  217. //    for (n = 0; n < numtex; n++)
  218. //       BasicWadRead( dir->wadfile, &(offsets[ n]), 4L);
  219. //    for (n = 0; n < numtex && !texofs; n++)
  220. //    {
  221. //       BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
  222. //       BasicWadRead( dir->wadfile, &tname, 8);
  223. //       if (!strnicmp( tname, texname, 8))
  224. //          texofs = dir->dir.start + offsets[ n];
  225. //    }
  226. //    FreeMemory( offsets);
  227. // }
  228.  
  229.    /* clear the box where the texture size will be drawn - see below */
  230.    SetColor( LIGHTGRAY);
  231.    DrawScreenBox( x0 - 171, y0 + 40, x0 - 110, y0 + 50);
  232.  
  233.    /* texture name not found */
  234.    if (texofs == 0)
  235.       return;
  236.  
  237.    /* read the info for this texture */
  238.    BasicWadSeek( dir->wadfile, texofs + 12L);
  239.    BasicWadRead( dir->wadfile, &xsize, 2L);
  240.    BasicWadRead( dir->wadfile, &ysize, 2L);
  241.    BasicWadSeek( dir->wadfile, texofs + 20L);
  242.    BasicWadRead( dir->wadfile, &fields, 2L);
  243.  
  244.    /* display the texture size - yes, you can laugh at the way I did it... */
  245.    SetColor( BLACK);
  246.    DrawScreenText( x0 - 171, y0 + 40, "%dx%d", xsize, ysize);
  247.  
  248.    if (bioskey( 1) != 0)
  249.       return; /* speedup */
  250.  
  251.    if (x1 - x0 > xsize)
  252.       x1 = x0 + xsize;
  253.    if (y1 - y0 > ysize)
  254.       y1 = y0 + ysize;
  255.    /* not really necessary, except when xofs or yofs < 0 */
  256.    setviewport( x0, y0, x1, y1, TRUE);
  257.    /* display the texture */
  258.    for (n = 0; n < fields; n++)
  259.    {
  260.       BasicWadSeek( dir->wadfile, texofs + 22L + n * 10L);
  261.       BasicWadRead( dir->wadfile, &xofs, 2L);
  262.       BasicWadRead( dir->wadfile, &yofs, 2L);
  263.       BasicWadRead( dir->wadfile, &pnameind, 2L);
  264.       BasicWadRead( dir->wadfile, &junk, 2L);  /* Junk should = 1. */
  265.       BasicWadRead( dir->wadfile, &junk, 2L);  /* Junk should = 0. */
  266.       /* OK, now look up the pic's name in the PNAMES entry. */
  267.       pdir = FindMasterDir( MasterDir, "PNAMES");
  268.       BasicWadSeek( pdir->wadfile, pdir->dir.start + 4L + pnameind * 8L);
  269.       BasicWadRead( pdir->wadfile, &picname, 8L);
  270.       picname[ 8] = '\0';
  271.       /* coords changed because of the "setviewport" */
  272.       DisplayPic( xofs, yofs, x1 - x0, y1 - y0, strupr( picname));
  273.    }
  274.    /* restore the normal viewport */
  275.    setviewport( 0, 0, ScrMaxX, ScrMaxY, TRUE);
  276. }
  277.  
  278.  
  279.  
  280. /*
  281.    Function to get the size of a wall texture
  282. */
  283.  
  284. void GetWallTextureSize( int *xsize_r, int *ysize_r, char *texname)
  285. {
  286.    MDirPtr  dir;        /* pointer in main directory to texname */
  287.    long    *offsets;    /* array of offsets to texture names */
  288.    int      n;          /* general counter */
  289.    long     numtex;     /* number of texture names in TEXTURE* list */
  290.    long     texofs;     /* offset in doom.wad for the texture data */
  291.    char     tname[9];   /* texture name */
  292.  
  293.    /* offset for texture we want. */
  294.    texofs = 0;
  295.    /* search for texname in texture1 names */
  296.    dir = FindMasterDir( MasterDir, "TEXTURE1");
  297.    BasicWadSeek( dir->wadfile, dir->dir.start);
  298.    BasicWadRead( dir->wadfile, &numtex, 4);
  299.    /* read in the offsets for texture1 names and info. */
  300.    offsets = GetMemory( numtex * sizeof( long));
  301.    for (n = 0; n < numtex; n++)
  302.       BasicWadRead( dir->wadfile, &(offsets[ n]), 4L);
  303.    for (n = 0; n < numtex && !texofs; n++)
  304.    {
  305.       BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
  306.       BasicWadRead( dir->wadfile, &tname, 8);
  307.       if (!strnicmp(tname, texname, 8))
  308.          texofs = dir->dir.start + offsets[ n];
  309.    }
  310.    FreeMemory( offsets);
  311.    if (Registered && texofs == 0)
  312.    {
  313.       /* search for texname in texture2 names */
  314.       dir = FindMasterDir( MasterDir, "TEXTURE2");
  315.       BasicWadSeek( dir->wadfile, dir->dir.start);
  316.       BasicWadRead( dir->wadfile, &numtex, 4);
  317.       /* read in the offsets for texture2 names */
  318.       offsets = GetMemory( numtex * sizeof( long));
  319.       for (n = 0; n < numtex; n++)
  320.          BasicWadRead( dir->wadfile, &(offsets[ n]), 4L);
  321.       for (n = 0; n < numtex && !texofs; n++)
  322.       {
  323.          BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
  324.          BasicWadRead( dir->wadfile, &tname, 8);
  325.          if (!strnicmp( tname, texname, 8))
  326.             texofs = dir->dir.start + offsets[ n];
  327.       }
  328.       FreeMemory( offsets);
  329.    }
  330.  
  331.    if (texofs != 0)
  332.    {
  333.       /* read the info for this texture */
  334.       BasicWadSeek( dir->wadfile, texofs + 12L);
  335.       BasicWadRead( dir->wadfile, xsize_r, 2L);
  336.       BasicWadRead( dir->wadfile, ysize_r, 2L);
  337.    }
  338.    else
  339.    {
  340.       /* texture data not found */
  341.       *xsize_r = -1;
  342.       *ysize_r = -1;
  343.    }
  344. }
  345.  
  346.  
  347.  
  348.  
  349. /*
  350.    choose a floor or ceiling texture
  351. */
  352.  
  353. void ChooseFloorTexture( int x0, int y0, char *prompt, int listsize, char **list, char *name)
  354. {
  355.    if (UseMouse)
  356.       HideMousePointer();
  357.    SwitchToVGA256();
  358.    /* if we only have a 320x200x256 VGA driver, we must change x0 and y0.  Yuck! */
  359.    if (GfxMode > -2)
  360.    {
  361.       x0 = -1;
  362.       y0 = -1;
  363.    }
  364.    InputNameFromListWithFunc( x0, y0, prompt, listsize, list, 5, name, 64, 64, DisplayFloorTexture);
  365.    SwitchToVGA16();
  366.    if (UseMouse)
  367.       ShowMousePointer();
  368. }
  369.  
  370.  
  371.  
  372. /*
  373.    choose a wall texture
  374. */
  375.  
  376. void ChooseWallTexture( int x0, int y0, char *prompt, int listsize, char **list, char *name)
  377. {
  378.    if (UseMouse)
  379.       HideMousePointer();
  380.    SwitchToVGA256();
  381.    /* if we only have a 320x200x256 VGA driver, we must change x0 and y0.  Yuck! */
  382.    if (GfxMode > -2)
  383.    {
  384.       x0 = 0;
  385.       y0 = -1;
  386.    }
  387.    InputNameFromListWithFunc( x0, y0, prompt, listsize, list, 11, name, 256, 128, DisplayWallTexture);
  388.    SwitchToVGA16();
  389.    if (UseMouse)
  390.       ShowMousePointer();
  391. }
  392.  
  393.  
  394.  
  395. /*
  396.    function used by qsort to sort the sprite names
  397. */
  398. int SortSprites( const void *a, const void *b)
  399. {
  400.    return strcmp( *((char **)a), *((char **)b));
  401. }
  402.  
  403.  
  404.  
  405. /*
  406.    choose a "sprite"
  407. */
  408.  
  409. void ChooseSprite( int x0, int y0, char *prompt, char *sname)
  410. {
  411.    MDirPtr dir;
  412.    int n, listsize;
  413.    char **list;
  414.    char name[ 9];
  415.  
  416.    /* count the names */
  417.    dir = FindMasterDir( MasterDir, "S_START");
  418.    dir = dir->next;
  419.    for (n = 0; dir && strcmp(dir->dir.name, "S_END"); n++)
  420.       dir = dir->next;
  421.    listsize = n;
  422.    /* get the actual names from master dir. */
  423.    dir = FindMasterDir( MasterDir, "S_START");
  424.    dir = dir->next;
  425.    list = GetMemory( listsize * sizeof( char *));
  426.    for (n = 0; n < listsize; n++)
  427.    {
  428.       list[ n] = GetMemory( 9 * sizeof( char));
  429.       strncpy( list[ n], dir->dir.name, 8);
  430.       list[ n][ 8] = '\0';
  431.       dir = dir->next;
  432.    }
  433.    qsort( list, listsize, sizeof( char *), SortSprites);
  434.    if (sname != NULL)
  435.       strncpy( name, sname, 8);
  436.    else
  437.       strcpy( name, list[ 0]);
  438.    if (UseMouse)
  439.       HideMousePointer();
  440.    SwitchToVGA256();
  441.    /* if we only have a 320x200x256 VGA driver, we must change x0 and y0.  Yuck! */
  442.    if (GfxMode > -2)
  443.    {
  444.       x0 = 0;
  445.       y0 = -1;
  446.    }
  447.    InputNameFromListWithFunc( x0, y0, prompt, listsize, list, 11, name, 256, 128, DisplayPic);
  448.    SwitchToVGA16();
  449.    if (UseMouse)
  450.       ShowMousePointer();
  451.    for (n = 0; n < listsize; n++)
  452.       FreeMemory( list[ n]);
  453.    FreeMemory( list);
  454. }
  455.  
  456.  
  457.