home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / GAMES_C / DEU50.ZIP / SOURCE.ZIP / TEXTURES.C < prev    next >
C/C++ Source or Header  |  1994-03-27  |  10KB  |  360 lines

  1. /*
  2.    Texture display by Raphaël Quinet <quinet@montefiore.ulg.ac.be>
  3.           and Trevor Phillips <rphillip@cc.curtin.edu.au>
  4.  
  5.    You are allowed to use any parts of this code in another program, as
  6.    long as you give credits to the authors in the documentation and in
  7.    the program itself.  Read the file README.1ST for more information.
  8.  
  9.    This program comes with absolutely no warranty.
  10.  
  11.    TEXTURES.C - Textures in 256 colors.
  12. */
  13.  
  14. /* the includes */
  15. #include "deu.h"
  16.  
  17.  
  18. /*
  19.    display a floor or ceiling texture at coords x0, y0
  20. */
  21.  
  22. void DisplayFloorTexture( int x0, int y0, char *texname)
  23. {
  24.    MDirPtr             dir;
  25.    int                 n;
  26.    unsigned char huge *pixels;
  27.  
  28.    dir = FindMasterDir( MasterDir, texname);
  29.    if (dir == NULL)
  30.    {
  31.       SetColor( DARKGRAY);
  32.       DrawScreenLine( x0, y0, x0 + 63, y0 + 63);
  33.       DrawScreenLine( x0, y0 + 63, x0 + 63, y0);
  34.       return;
  35.    }
  36.    BasicWadSeek( dir->wadfile, dir->dir.start);
  37.    pixels = GetFarMemory( 4100 * sizeof( char));
  38.    BasicWadRead( dir->wadfile, &(pixels[ 4]), 4096L);
  39.    if (GfxMode < -1)
  40.    {
  41.       /* Probably a bug in the VESA driver...    */
  42.       /* It requires "size-1" instead of "size"! */
  43.       ((unsigned int huge *)pixels)[ 0] = 63;
  44.       ((unsigned int huge *)pixels)[ 1] = 63;
  45.    }
  46.    else
  47.    {
  48.       ((unsigned int huge *)pixels)[ 0] = 64;
  49.       ((unsigned int huge *)pixels)[ 1] = 64;
  50.    }
  51.    putimage( x0, y0, pixels, COPY_PUT);
  52.    farfree( pixels);
  53. }
  54.  
  55.  
  56.  
  57. /*
  58.    display a picture "picname" at coords x0, y0
  59. */
  60.  
  61. void DisplayPic( int x0, int y0, char *picname)
  62. {
  63.    MDirPtr             dir;
  64.    int                 xsize, ysize, xofs, yofs;
  65.    int                 x, y;
  66.    long                offset;
  67.    unsigned char       srow, rowlen, pixel;
  68. #ifndef OLD_PIX
  69.    unsigned char huge *pixels;
  70.    long                l;
  71.    long huge          *offsets;
  72. #endif
  73.  
  74.    dir = FindMasterDir( MasterDir, picname);
  75.    if (dir == NULL)
  76.    {
  77.       SetColor( DARKGRAY);
  78.       DrawScreenLine( x0, y0, x0 + 63, y0 + 63);
  79.       DrawScreenLine( x0, y0 + 63, x0 + 63, y0);
  80.       return;
  81.    }
  82.    BasicWadSeek( dir->wadfile, dir->dir.start);
  83.    BasicWadRead( dir->wadfile, &xsize, 2L);
  84.    BasicWadRead( dir->wadfile, &ysize, 2L);
  85.    BasicWadRead( dir->wadfile, &xofs, 2L);
  86.    BasicWadRead( dir->wadfile, &yofs, 2L);
  87.    /* ignore the picture offsets */
  88.    xofs = 0;
  89.    yofs = 0;
  90. #ifdef OLD_PIX
  91.    for (x = 0; x < xsize && !bioskey( 1); x++)
  92.    {
  93.       /* Seek to point of next pointer. */
  94.       BasicWadSeek( dir->wadfile, dir->dir.start + 8L + x * 4L);
  95.       BasicWadRead( dir->wadfile, &offset, 4L);
  96.       /* Seek to start of column data. */
  97.       BasicWadSeek( dir->wadfile, dir->dir.start + offset);
  98.       /* Read starting row. */
  99.       BasicWadRead( dir->wadfile, &srow, 1L);
  100.       while (srow != 255)
  101.       {
  102.      /* Read length of row. */
  103.      BasicWadRead( dir->wadfile, &rowlen, 1L);
  104.      /* Read excess 0. */
  105.      BasicWadRead( dir->wadfile, &pixel, 1L);
  106.      for (y = 0; y < rowlen; y++)
  107.      {
  108.         /* Read next pixel colour. */
  109.         BasicWadRead( dir->wadfile, &pixel, 1L);
  110.         putpixel( x0 + xofs + x, y0 + yofs + srow + y, pixel);
  111.      }
  112.      /* Read excess 0. */
  113.      BasicWadRead( dir->wadfile, &pixel, 1L);
  114.      /* Read next starting row. */
  115.      BasicWadRead( dir->wadfile, &srow, 1L);
  116.       }
  117.    }
  118. #else
  119.    pixels = GetFarMemory( (xsize * ysize + 4) * sizeof( char));
  120.    for (l = 0; l < xsize * ysize; l++)
  121.       pixels[ 4 + l] = 0;
  122.    offsets = GetFarMemory( xsize * sizeof( long));
  123.    BasicWadRead( dir->wadfile, offsets, (long) xsize * 4L);
  124.    for (x = 0; x < xsize && !bioskey( 1); x++)
  125.    {
  126.       /* Seek to start of column data. */
  127.       BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ x]);
  128.       /* Read starting row. */
  129.       BasicWadRead( dir->wadfile, &srow, 1L);
  130.       while (srow != 255)
  131.       {
  132.      /* Read length of row. */
  133.      BasicWadRead( dir->wadfile, &rowlen, 1L);
  134.      /* Read excess 0. */
  135.      BasicWadRead( dir->wadfile, &pixel, 1L);
  136.      for (y = 0; y < rowlen; y++)
  137.      {
  138.         /* Read next pixel colour. */
  139.         BasicWadRead( dir->wadfile, &pixel, 1L);
  140.         l = (long) xofs + (long) x + (long) (yofs + srow + y) * (long) xsize;
  141.         if (l < (long) xsize * (long) ysize)
  142.            pixels[ 4L + l] = pixel;
  143.      }
  144.      /* Read excess 0. */
  145.      BasicWadRead( dir->wadfile, &pixel, 1L);
  146.      /* Read next starting row. */
  147.      BasicWadRead( dir->wadfile, &srow, 1L);
  148.       }
  149.    }
  150.    farfree( offsets);
  151.    if (GfxMode < -1)
  152.    {
  153.       /* Probably a bug in the VESA driver...    */
  154.       /* It requires "size-1" instead of "size"! */
  155.       ((unsigned int huge *)pixels)[ 0] = xsize - 1;
  156.       ((unsigned int huge *)pixels)[ 1] = ysize - 1;
  157.    }
  158.    else
  159.    {
  160.       ((unsigned int huge *)pixels)[ 0] = xsize;
  161.       ((unsigned int huge *)pixels)[ 1] = ysize;
  162.    }
  163.    putimage( x0, y0, pixels, COPY_PUT);
  164.    farfree( pixels);
  165. #endif
  166. }
  167.  
  168.  
  169.  
  170. /*
  171.    display a wall texture ("texture1" or "texture2" object) at coords x0, y0
  172. */
  173.  
  174. void DisplayWallTexture( int x0, int y0, char *texname)
  175. {
  176.    MDirPtr  dir, pdir;
  177.    long    *offsets;
  178.    int      n, xsize, ysize, xofs, yofs, fields, pnameind, junk;
  179.    long     numtex, texofs;
  180.    char     tname[9], picname[9];
  181.  
  182.    /* offset for texture we want. */
  183.    texofs = 0;
  184.    /* search for texname in texture1 names */
  185.    dir = FindMasterDir( MasterDir, "TEXTURE1");
  186.    BasicWadSeek( dir->wadfile, dir->dir.start);
  187.    BasicWadRead( dir->wadfile, &numtex, 4);
  188.    /* read in the offsets for texture1 names and info. */
  189.    offsets = GetMemory( numtex * sizeof( long));
  190.    for (n = 0; n < numtex; n++)
  191.       BasicWadRead( dir->wadfile, &(offsets[ n]), 4L);
  192.    for (n = 0; n < numtex && !texofs; n++)
  193.    {
  194.       BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
  195.       BasicWadRead( dir->wadfile, &tname, 8);
  196.       if (!strncmp(tname, texname, 8))
  197.      texofs = dir->dir.start + offsets[ n];
  198.    }
  199.    free( offsets);
  200.    if (Registered && !texofs)
  201.    {
  202.       /* search for texname in texture2 names */
  203.       dir = FindMasterDir( MasterDir, "TEXTURE2");
  204.       BasicWadSeek( dir->wadfile, dir->dir.start);
  205.       BasicWadRead( dir->wadfile, &numtex, 4);
  206.       /* read in the offsets for texture2 names */
  207.       offsets = GetMemory( numtex * sizeof( long));
  208.       for (n = 0; n < numtex; n++)
  209.      BasicWadRead( dir->wadfile, &(offsets[ n]), 4L);
  210.       for (n = 0; n < numtex && !texofs; n++)
  211.       {
  212.      BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
  213.      BasicWadRead( dir->wadfile, &tname, 8);
  214.      if (!strncmp( tname, texname, 8))
  215.         texofs = dir->dir.start + offsets[ n];
  216.       }
  217.       free( offsets);
  218.    }
  219.  
  220.    /* texture name not found */
  221.    if (!texofs)
  222.       return;
  223.  
  224.    /* read the info for this texture */
  225.    BasicWadSeek(dir->wadfile, texofs + 12L);
  226.    BasicWadRead(dir->wadfile, &xsize, 2L);
  227.    BasicWadRead(dir->wadfile, &ysize, 2L);
  228.    BasicWadSeek(dir->wadfile, texofs + 20L);
  229.    BasicWadRead(dir->wadfile, &fields, 2L);
  230.  
  231.    for (n = 0; n < fields; n++)
  232.    {
  233.       BasicWadSeek(dir->wadfile, texofs + 22L + n * 10L);
  234.       BasicWadRead(dir->wadfile, &xofs, 2L);
  235.       BasicWadRead(dir->wadfile, &yofs, 2L);
  236.       BasicWadRead(dir->wadfile, &pnameind, 2L);
  237.       BasicWadRead(dir->wadfile, &junk, 2L);  /* Junk should = 1. */
  238.       BasicWadRead(dir->wadfile, &junk, 2L);  /* Junk should = 0. */
  239.       /* OK, now look up the pic's name in the PNAMES entry. */
  240.       pdir = FindMasterDir( MasterDir, "PNAMES");
  241.       BasicWadSeek(pdir->wadfile, pdir->dir.start + 4L + pnameind * 8L);
  242.       BasicWadRead(pdir->wadfile, &picname, 8L);
  243.       picname[ 8] = '\0';
  244.       DisplayPic(x0 + xofs, y0 + yofs, picname);
  245.    }
  246. }
  247.  
  248.  
  249.  
  250. /*
  251.    choose a floor or ceiling texture
  252. */
  253.  
  254. void ChooseFloorTexture( int x0, int y0, char *prompt, int listsize, char **list, char *name)
  255. {
  256.    if (UseMouse)
  257.       HideMousePointer();
  258.    SwitchToVGA256();
  259.    /* if we only have a 320x200x256 VGA driver, we must change x0 and y0.  Yuck! */
  260.    if (GfxMode > -2)
  261.    {
  262.       x0 = -1;
  263.       y0 = -1;
  264.    }
  265.    InputNameFromListWithFunc( x0, y0, prompt, listsize, list, 5, name, 64, 64, DisplayFloorTexture);
  266.    SwitchToVGA16();
  267.    if (UseMouse)
  268.       ShowMousePointer();
  269. }
  270.  
  271.  
  272.  
  273. /*
  274.    choose a wall texture
  275. */
  276.  
  277. void ChooseWallTexture( int x0, int y0, char *prompt, int listsize, char **list, char *name)
  278. {
  279.    if (UseMouse)
  280.       HideMousePointer();
  281.    SwitchToVGA256();
  282.    /* if we only have a 320x200x256 VGA driver, we must change x0 and y0.  Yuck! */
  283.    if (GfxMode > -2)
  284.    {
  285.       x0 = 0;
  286.       y0 = -1;
  287.    }
  288.    InputNameFromListWithFunc( x0, y0, prompt, listsize, list, 11, name, 256, 128, DisplayWallTexture);
  289.    SwitchToVGA16();
  290.    if (UseMouse)
  291.       ShowMousePointer();
  292. }
  293.  
  294.  
  295.  
  296. /*
  297.    function used by qsort to sort the sprite names
  298. */
  299. int SortSprites( const void *a, const void *b)
  300. {
  301.    return strcmp( *((char **)a), *((char **)b));
  302. }
  303.  
  304.  
  305.  
  306. /*
  307.    choose a "sprite"
  308. */
  309.  
  310. void ChooseSprite( int x0, int y0, char *prompt, char *sname)
  311. {
  312.    MDirPtr dir;
  313.    int n, listsize;
  314.    char **list;
  315.    char *name;
  316.  
  317.    /* count the names */
  318.    dir = FindMasterDir( MasterDir, "S_START");
  319.    dir = dir->next;
  320.    for (n = 0; dir && strcmp(dir->dir.name, "S_END"); n++)
  321.       dir = dir->next;
  322.    listsize = n;
  323.    /* get the actual names from master dir. */
  324.    dir = FindMasterDir( MasterDir, "S_START");
  325.    dir = dir->next;
  326.    list = GetMemory( listsize * sizeof( char *));
  327.    for (n = 0; n < listsize; n++)
  328.    {
  329.       list[ n] = GetMemory( 9 * sizeof( char));
  330.       strncpy( list[ n], dir->dir.name, 8);
  331.       list[ n][ 8] = '\0';
  332.       dir = dir->next;
  333.    }
  334.    qsort( list, listsize, sizeof( char *), SortSprites);
  335.    name = GetMemory( 9 * sizeof( char));
  336.    if (sname != NULL)
  337.       strncpy( name, sname, 8);
  338.    else
  339.       strcpy( name, list[ 0]);
  340.    if (UseMouse)
  341.       HideMousePointer();
  342.    SwitchToVGA256();
  343.    /* if we only have a 320x200x256 VGA driver, we must change x0 and y0.  Yuck! */
  344.    if (GfxMode > -2)
  345.    {
  346.       x0 = 0;
  347.       y0 = -1;
  348.    }
  349.    InputNameFromListWithFunc( x0, y0, prompt, listsize, list, 11, name, 256, 128, DisplayPic);
  350.    SwitchToVGA16();
  351.    if (UseMouse)
  352.       ShowMousePointer();
  353.    free( name);
  354.    for (n = 0; n < listsize; n++)
  355.       free( list[ n]);
  356.    free( list);
  357. }
  358.  
  359.  
  360.