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