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