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