home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / 2d / poly.c < prev    next >
Text File  |  1998-06-08  |  7KB  |  302 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: f:/miner/source/2d/rcs/poly.c $
  15.  * $Revision: 1.5 $
  16.  * $Author: john $
  17.  * $Date: 1994/11/13 13:03:43 $
  18.  *
  19.  * Graphical routines for drawing polygons.
  20.  *
  21.  * $Log: poly.c $
  22.  * Revision 1.5  1994/11/13  13:03:43  john
  23.  * Added paged out bit in bitmap structure.  Commented out the
  24.  * poly code that is never used.
  25.  * 
  26.  * Revision 1.4  1994/03/14  16:56:13  john
  27.  * Changed grs_bitmap structure to include bm_flags.
  28.  * 
  29.  * Revision 1.3  1993/10/15  16:23:14  john
  30.  * y
  31.  * 
  32.  * Revision 1.2  1993/10/08  14:30:39  john
  33.  * *** empty log message ***
  34.  * 
  35.  * Revision 1.1  1993/09/08  11:44:13  john
  36.  * Initial revision
  37.  * 
  38.  *
  39.  */
  40.  
  41. #include "mem.h"
  42. #include "gr.h"
  43. #include "grdef.h"
  44.  
  45. //#define USE_POLY_CODE 1
  46.  
  47. #define  MAX_SCAN_LINES 1200
  48.  
  49. #ifdef USE_POLY_CODE 
  50.  
  51. int y_edge_list[MAX_SCAN_LINES];
  52.  
  53. void gr_upoly(int nverts, int *vert )
  54. {           
  55.     int temp;
  56.     int startx, stopx;  // X coordinates of both ends of current edge.
  57.     int firstx, firsty; // Saved copy of the first vertex to connect later.
  58.     int dx_dy;          // Slope of current edge.
  59.     int miny, maxy;
  60.  
  61.     int starty, stopy;  // Y coordinates of both ends of current edge.
  62.  
  63.     int x1, x2, i;
  64.  
  65.     // Find the min and max rows to clear out the minimun y_edge_list.
  66.     // (Is it worth it?)
  67.     maxy = vert[1];
  68.     miny = vert[1];
  69.  
  70.     for (i=3; i<(nverts*2); i+=2 )
  71.     {
  72.         if (vert[i]>maxy) maxy=vert[i];
  73.         if (vert[i]<miny) miny=vert[i];
  74.     }
  75.  
  76.     miny >>= 16;
  77.     miny--;             // -1 to be safe
  78.     maxy >>= 16;
  79.     maxy++;             // +1 to be safe
  80.  
  81.     // Clear only the part of the y_edge_list that w will be using
  82.     if (miny < 0) miny = 0;
  83.     if (maxy > MAX_SCAN_LINES) maxy = MAX_SCAN_LINES;
  84.  
  85.     for (i=miny; i<maxy; i++ )
  86.         y_edge_list[i] = -1;
  87.  
  88.     // Save the first vertex so that we can connect to it at the end.
  89.     firstx = vert[0];
  90.     firsty = vert[1] >> 16;
  91.  
  92.     do
  93.     {
  94.         nverts--;
  95.  
  96.         // Get the beginning coordinates of the current edge.
  97.         startx = vert[0];
  98.         starty = vert[1] >> 16;
  99.  
  100.         // Get the ending coordinates of the current edge.
  101.         if (nverts > 0 ) {
  102.             stopx = vert[2];
  103.             stopy = vert[3] >> 16;
  104.             vert += 2;
  105.         } else  {
  106.             stopx = firstx;     // Last edge, uses first vertex as endpoint
  107.             stopy = firsty;
  108.         }
  109.  
  110.         if (stopy < starty )    {
  111.             temp = stopy;
  112.             stopy = starty;
  113.             starty = temp;
  114.             temp = stopx;
  115.             stopx = startx;
  116.             startx = temp;
  117.         }
  118.  
  119.         if (stopy == starty )
  120.         {
  121.             // Draw a edge going horizontally across screen
  122.             x1 = startx>>16;
  123.             x2 = stopx>>16;
  124.  
  125.             if (x2 > x1 )
  126.                 //gr_uscanline( x1, x2-1, stopy );
  127.                 gr_uscanline( x1, x2, stopy );
  128.             else
  129.                 //gr_uscanline( x2, x1-1, stopy );
  130.                 gr_uscanline( x2, x1, stopy );
  131.  
  132.         } else  {
  133.  
  134.             dx_dy = (stopx - startx) / (stopy - starty);
  135.  
  136.             for (; starty < stopy; starty++ )
  137.             {
  138.                 if (y_edge_list[starty]==-1)
  139.                     y_edge_list[starty] = startx;
  140.                 else    {
  141.                     x1 = y_edge_list[starty]>>16;
  142.                     x2 = startx>>16;
  143.  
  144.                     if (x2 > x1 )
  145.                         //gr_uscanline( x1, x2-1, starty );
  146.                         gr_uscanline( x1, x2, starty );
  147.                     else
  148.                         //gr_uscanline( x2, x1-1, starty );
  149.                         gr_uscanline( x2, x1, starty );
  150.                 }
  151.                 startx += dx_dy;
  152.             }
  153.         }
  154.  
  155.  
  156.     } while (nverts > 0);
  157. }
  158.  
  159.  
  160. void gr_poly(int nverts, int *vert )
  161. {
  162.     int temp;
  163.     int startx, stopx;  // X coordinates of both ends of current edge.
  164.     int firstx, firsty; // Saved copy of the first vertex to connect later.
  165.     int dx_dy;          // Slope of current edge.
  166.     int miny, maxy;
  167.  
  168.     int starty, stopy;  // Y coordinates of both ends of current edge.
  169.  
  170.     int x1, x2, i, j;
  171.  
  172.     // Find the min and max rows to clear out the minimun y_edge_list.
  173.     // (Is it worth it?)
  174.     maxy = vert[1];
  175.     miny = vert[1];
  176.  
  177.     j = 0;
  178.  
  179.     for (i=3; i<(nverts*2); i+=2 )
  180.     {
  181.         if (vert[i]>maxy) {
  182.             if ((maxy=vert[i]) > MAXY) j++;
  183.             //if (j>1) break;
  184.         }
  185.  
  186.         if (vert[i]<miny) {
  187.             if ((miny=vert[i]) < MINY) j++;
  188.             //if (j>1) break;
  189.         }
  190.     }
  191.  
  192.     miny >>= 16;
  193.     miny--;         // -1 to be safe
  194.     maxy >>= 16;
  195.     maxy++;          // +1 to be safe
  196.  
  197.     if (miny < MINY) miny = MINY;
  198.     if (maxy > MAXY) maxy = MAXY+1;
  199.  
  200.     // Clear only the part of the y_edge_list that w will be using
  201.     for (i=miny; i<maxy; i++ )
  202.        y_edge_list[i] = -1;
  203.  
  204.     // Save the first vertex so that we can connect to it at the end.
  205.     firstx = vert[0];
  206.     firsty = vert[1] >> 16;
  207.  
  208.     do
  209.     {
  210.         nverts--;
  211.  
  212.         // Get the beginning coordinates of the current edge.
  213.         startx = vert[0];
  214.         starty = vert[1] >> 16;
  215.  
  216.         // Get the ending coordinates of the current edge.
  217.         if (nverts > 0 ) {
  218.             stopx = vert[2];
  219.             stopy = vert[3] >> 16;
  220.             vert += 2;
  221.         } else  {
  222.             stopx = firstx;     // Last edge, uses first vertex as endpoint
  223.             stopy = firsty;
  224.         }
  225.  
  226.  
  227.         if (stopy < starty )    {
  228.             temp = stopy;
  229.             stopy = starty;
  230.             starty = temp;
  231.             temp = stopx;
  232.             stopx = startx;
  233.             startx = temp;
  234.         }
  235.  
  236.         if (stopy == starty )
  237.         {
  238.             // Draw a edge going horizontally across screen
  239.             if ((stopy >= MINY) && (stopy <=MAXY )) {
  240.                 x1 = startx>>16;
  241.                 x2 = stopx>>16;
  242.  
  243.                 if (x1 > x2 )   {
  244.                     temp = x2;
  245.                     x2 = x1;
  246.                     x1 = temp;
  247.                 }
  248.  
  249.                 if ((x1 <= MAXX ) && (x2 >= MINX))
  250.                 {
  251.                     if (x1 < MINX ) x1 = MINX;
  252.                     if (x2 > MAXX ) x2 = MAXX+1;
  253.                     //gr_uscanline( x1, x2-1, stopy );
  254.                     gr_scanline( x1, x2, stopy );
  255.                 }
  256.             }
  257.         } else  {
  258.  
  259.             dx_dy = (stopx - startx) / (stopy - starty);
  260.  
  261.             if (starty < MINY ) {
  262.                 startx = dx_dy*(MINY-starty)+startx;
  263.                 starty = MINY;
  264.             }
  265.  
  266.             if (stopy > MAXY ) {
  267.                 stopx = dx_dy*(MAXY-starty)+startx;
  268.                 stopy = MAXY+1;
  269.             }
  270.  
  271.             for (; starty < stopy; starty++ )
  272.             {   if (y_edge_list[starty]==-1)
  273.                     y_edge_list[starty] = startx;
  274.                 else    {
  275.                     x1 = y_edge_list[starty]>>16;
  276.                     x2 = startx>>16;
  277.  
  278.                     if (x1 > x2 )   {
  279.                         temp = x2;
  280.                         x2 = x1;
  281.                         x1 = temp;
  282.                     }
  283.  
  284.                     if ((x1 <= MAXX ) && (x2 >= MINX))
  285.                     {
  286.                         if (x1 < MINX ) x1 = MINX;
  287.                         if (x2 > MAXX ) x2 = MAXX+1;
  288.                         //gr_uscanline( x1, x2-1, starty );
  289.                         gr_scanline( x1, x2, starty );
  290.                     }
  291.                 }
  292.                 startx += dx_dy;
  293.             }
  294.         }
  295.  
  296.  
  297.     } while (nverts > 0);
  298. }
  299.  
  300. #endif
  301. 
  302.