home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / RAYCAST.ZIP / RAY2D.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-16  |  6.1 KB  |  283 lines

  1. #include "fixed.h"
  2. #include "ray.h"
  3. #include "asm.h"
  4. #include "rayrend.h"
  5. #include "globals.h"
  6. #include "scrconf.h"
  7.  
  8. MYFIXED Scale_Coeff_2d=ONE;
  9.  
  10. #define MIN_SCALE_2D (ONE / 100)
  11. #define MAX_SCALE_2D (ONE * 10)
  12. #define SCALE_INC_2D (ONE + ONE/20)
  13.  
  14. void Map_Scale_Decrease()
  15. {
  16.    if (Scale_Coeff_2d > MIN_SCALE_2D) {
  17.       Scale_Coeff_2d=fixeddiv(Scale_Coeff_2d, SCALE_INC_2D);
  18.    } /* endif */
  19. }
  20.  
  21. void Map_Scale_Increase()
  22. {
  23.    if (Scale_Coeff_2d < MAX_SCALE_2D) {
  24.       Scale_Coeff_2d=fixedmult(Scale_Coeff_2d, SCALE_INC_2D);
  25.    } /* endif */
  26. }
  27.  
  28. #define POINTS_IN_ARROW 4
  29. #define ARROW_COLOR (255)
  30. #define RED_COLOR (64+15)
  31.  
  32. const long arrow_struct[POINTS_IN_ARROW][2]= {
  33.    { 12, 0 } ,
  34.    { 3, 9 } ,
  35.    { 3, -9} ,
  36.    { -12, 0} };
  37.  
  38. long x1_2d, x2_2d, y1_2d, y2_2d;
  39.  
  40. BOOL Clip_Line_2d();
  41. void Draw_Arrow_2d(long view_angle);
  42. void Line_Draw_2d(Byte color);
  43.  
  44. void Render_2d(long x, long y, long view_angle)
  45. {
  46.  
  47. #ifdef NO_2D_YET
  48. return;
  49. #endif
  50.    clearBuff();
  51.    short index1;
  52.    pvector2 v1, v2;
  53.  
  54.    // Draw dem lines
  55.  
  56.    for (index1=0; index1<Number_Of_Linedefs; index1++) {
  57.      v1=Vector_List+Ld_List[index1].v[0];
  58.      v2=Vector_List+Ld_List[index1].v[1];
  59.  
  60.  
  61.      // Get translated coordinates
  62.  
  63.      x1_2d= (fixedmult(((v1->x<<SHIFT) - x), Scale_Coeff_2d) >> SHIFT) + WINDOW_MIDDLEW;
  64.      x2_2d= (fixedmult(((v2->x<<SHIFT) - x), Scale_Coeff_2d) >> SHIFT) + WINDOW_MIDDLEW;
  65.      y1_2d= (fixedmult((y - (v1->y<<SHIFT)), Scale_Coeff_2d) >> SHIFT) + WINDOW_MIDDLE;
  66.      y2_2d= (fixedmult((y - (v2->y<<SHIFT)), Scale_Coeff_2d) >> SHIFT) + WINDOW_MIDDLE ;
  67.      if (!Clip_Line_2d())
  68.         Line_Draw_2d(RED_COLOR);
  69.    } /* endfor */
  70.  
  71.    Draw_Arrow_2d(view_angle); // Draw the arrow of player's direction
  72.  
  73. }
  74.  
  75. BOOL Clip_Line_2d()
  76.  
  77. // Clips line and returns whether it is at all visible
  78.  
  79. {
  80.  
  81.    long tempx, tempy;
  82.    long temp_div;
  83.  
  84.    // Clipping x
  85.  
  86.    // Make first vertex the left one
  87.  
  88.    if (x1_2d>x2_2d) {
  89.      tempx=x1_2d;
  90.      x1_2d=x2_2d;
  91.      x2_2d=tempx;
  92.      tempy=y1_2d;
  93.      y1_2d=y2_2d;
  94.      y2_2d=tempy;
  95.    }
  96.  
  97.    // If the right vertex is to the left of the window, or the left vertex is to the right of the window,
  98.    // the line is not visible
  99.  
  100.    if ((x2_2d<0) || (x1_2d>=WINDOW_WIDTH)) {
  101.       return TRUE;
  102.    }
  103.  
  104.    // Ok, line is visible on x
  105.  
  106.    // Clip against left side of window
  107.  
  108.    if (x1_2d<0) {
  109.       tempx=x1_2d;
  110.       x1_2d=0;
  111.       temp_div=(y1_2d - y2_2d) * (-tempx) / (tempx - x2_2d);
  112.       y1_2d+=temp_div;
  113.    }
  114.  
  115.    // Clip against right side of window
  116.  
  117.    if (x2_2d>=WINDOW_WIDTH) {
  118.       tempx=x2_2d;
  119.       x2_2d=WINDOW_WIDTH-1;
  120.       temp_div=y1_2d+((y1_2d - y2_2d) * (WINDOW_WIDTH -1 - x1_2d)/(x1_2d - tempx));
  121.       y2_2d=temp_div;
  122.    }
  123.  
  124.    // Clipping y
  125.  
  126.    // Make first vertex the top vertex
  127.  
  128.    if (y1_2d>y2_2d) {
  129.      tempx=x1_2d;
  130.      x1_2d=x2_2d;
  131.      x2_2d=tempx;
  132.      tempy=y1_2d;
  133.      y1_2d=y2_2d;
  134.      y2_2d=tempy;
  135.    }
  136.  
  137.    // If bottom vertex is above window, or top vertex below window, the line is not visible
  138.  
  139.    if ((y2_2d<0) || (y1_2d>=WINDOW_HEIGHT)) {
  140.       return TRUE;
  141.    }
  142.  
  143.    // Ok, line is visible on y
  144.  
  145.    // Clip against top of window
  146.  
  147.    if (y1_2d<0) {
  148.       tempy=y1_2d;
  149.       y1_2d=0;
  150.       temp_div=(x1_2d - x2_2d) * (-tempy) / (tempy - y2_2d);
  151.       x1_2d+=temp_div;
  152.    }
  153.  
  154.    // Clip against bottom of window
  155.  
  156.    if (y2_2d>=WINDOW_HEIGHT) {
  157.       tempy=y2_2d;
  158.       y2_2d=WINDOW_HEIGHT;
  159.       temp_div=x1_2d+((x1_2d - x2_2d)  * (WINDOW_HEIGHT -1 - y1_2d) / (y1_2d - tempy));
  160.       x2_2d=temp_div;
  161.    }
  162.  
  163.    // The line is visible and clipped
  164.  
  165.    return FALSE;
  166.  
  167. }
  168.  
  169. void Line_Draw_2d(Byte color) {
  170.  
  171. // Code taken from Flights o' Fantasy that uses Bresenham's algorithym
  172.  
  173. long x_unit, y_unit;
  174.  
  175. long offset=y1_2d * Get_Phys_Screen_Width() + x1_2d;
  176.  
  177. long ydiff=y2_2d-y1_2d;
  178.  
  179. if (ydiff<0) {
  180.  
  181.    ydiff=-ydiff;
  182.    y_unit=-Get_Phys_Screen_Width();
  183.  
  184. }
  185. else y_unit=Get_Phys_Screen_Width();
  186.  
  187. long xdiff=x2_2d-x1_2d;
  188.  
  189. if (xdiff<0) {
  190.  
  191.    xdiff=-xdiff;
  192.    x_unit=-1;
  193.  
  194. }
  195. else x_unit=1;
  196.  
  197. long error_term=0;
  198. if (xdiff>ydiff) {
  199.  
  200.    long length=xdiff+1;
  201.  
  202.    for (long position=0; position < length; position++) {
  203.  
  204.       buff[offset]=color;
  205.  
  206.       offset+=x_unit;
  207.  
  208.       error_term+=ydiff;
  209.  
  210.       if (error_term>xdiff) {
  211.          error_term-=xdiff;
  212.          offset+=y_unit;
  213.       }
  214.  
  215.    } /* endfor */
  216.  
  217. } else {
  218.  
  219.    long length=ydiff+1;
  220.  
  221.    for (long position=0; position < length; position++) {
  222.  
  223.       buff[offset]=color;
  224.  
  225.       offset+=y_unit;
  226.  
  227.       error_term+=xdiff;
  228.  
  229.       if (error_term>0) {
  230.          error_term-=ydiff;
  231.          offset+=x_unit;
  232.       }
  233.  
  234.    } /* endfor */
  235.  
  236. } /* endif */
  237.  
  238. }
  239.  
  240. void Draw_Arrow_2d(long view_angle)
  241. {
  242.    short counter;
  243.    long temp_arrow[POINTS_IN_ARROW][2];
  244.  
  245.    // Translate points in arrow
  246.  
  247.    for (counter=0; counter< POINTS_IN_ARROW; counter++) {
  248.       temp_arrow[counter][0]=(arrow_struct[counter][0] * rcos_table[view_angle])+
  249.                 (arrow_struct[counter][1] * rsin_table[view_angle]);
  250.       temp_arrow[counter][0]=fixedmult(temp_arrow[counter][0],Scale_Coeff_2d);
  251.       temp_arrow[counter][0]>>=SHIFT;
  252.       temp_arrow[counter][0]+=WINDOW_MIDDLEW;
  253.       temp_arrow[counter][1]=(arrow_struct[counter][1] * rcos_table[view_angle])-
  254.                 (arrow_struct[counter][0] * rsin_table[view_angle]);
  255.       temp_arrow[counter][1]=fixedmult(temp_arrow[counter][1],Scale_Coeff_2d);
  256.       temp_arrow[counter][1]>>=SHIFT;
  257.       temp_arrow[counter][1]+=WINDOW_MIDDLE;
  258.    } /* endfor */
  259.  
  260.    // Draw the arrow lines
  261.  
  262.    x1_2d=WINDOW_MIDDLEW; y1_2d=WINDOW_MIDDLE; 
  263.    x2_2d=temp_arrow[0][0]; y2_2d=temp_arrow[0][1];
  264.  
  265.    Line_Draw_2d(ARROW_COLOR);
  266.  
  267.    x1_2d=WINDOW_MIDDLEW; y1_2d=WINDOW_MIDDLE; 
  268.    x2_2d=temp_arrow[3][0]; y2_2d=temp_arrow[3][1];
  269.  
  270.    Line_Draw_2d(ARROW_COLOR);
  271.  
  272.    x1_2d=temp_arrow[0][0]; y1_2d=temp_arrow[0][1];
  273.    x2_2d=temp_arrow[1][0]; y2_2d=temp_arrow[1][1];
  274.  
  275.    Line_Draw_2d(ARROW_COLOR);
  276.  
  277.    x1_2d=temp_arrow[0][0]; y1_2d=temp_arrow[0][1];
  278.    x2_2d=temp_arrow[2][0]; y2_2d=temp_arrow[2][1];
  279.  
  280.    Line_Draw_2d(ARROW_COLOR);
  281.  
  282. }
  283.