home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1988 / 05 / prolog / hercgraf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-26  |  5.4 KB  |  251 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                             HERCGRAF.C                                   */
  3. /*                Grafik-Routinen für Hercules-Karte                        */
  4. /*                 (C) 2.88 M.Schlöter & PASCAL INT.                        */
  5. /*--------------------------------------------------------------------------*/
  6.  
  7. /* Siehe PASCAL 8 & 9/87 (begin, end, ...) */
  8. #include <stdio.h>
  9. #include <my-h.h>
  10. #include <dos.h>
  11. #include <stdlib.h>
  12. /* Bildschirmspeicher */
  13. #define SCRBASE 0xB800
  14. /* Register */
  15. #define MODEREG 0x3B8
  16. #define INDREG 0x3B4
  17. #define DATAREG 0x3B5
  18. #define KONFREG 0x3BF
  19.  
  20. /* Bildschirm-Grenzen */
  21. #define XMIN 0
  22. #define YMIN 0
  23. #define XMAX 719
  24. #define YMAX 347
  25.  
  26. /* Berechne Adresse im Speicher */
  27. #define ADR(x,y) ((((y) & 3) << 13) + (((y) >> 2)*90) + ((x) >> 3))
  28. /* Bitmaske */
  29. #define MASK(x) (128 >> ((x)%8))
  30.  
  31. /* Punkt auf dem Bildschirm ? */
  32. #define checkwin(x,y) ((x>=XMIN)&&(x<=XMAX)&&(y>=YMIN)&&(y<=YMAX))
  33.  
  34. char GrafInit[] = {0x35, 0x2D, 0x2E, 0x07, 0x5B, 0x02,
  35.                    0x57, 0x57, 0x02, 0x03, 0x00, 0x00 };
  36. char TextInit[] = {0x61, 0x50, 0x52, 0x0F, 0x19, 0x06,
  37.                    0x19, 0x19, 0x02, 0x0D, 0x0B, 0x0C };
  38. int XOrFlag = 0;
  39.  
  40. void cls_0(void)
  41. begin
  42.   int i;
  43.   char *p;
  44.  
  45.   for(i = 0; i < 0x7FFF; i++) begin
  46.     p = MK_FP(SCRBASE,i);
  47.     *p = 0;
  48.   end;
  49. end;
  50.  
  51.  
  52. void  hires_0(void)
  53. begin
  54.   int i;
  55.  
  56.   outportb(MODEREG,0x82);
  57.   for(i = 0; i < 12; i++)  begin
  58.     outportb(INDREG,i);
  59.     outportb(DATAREG,GrafInit[i]);
  60.   end;
  61.   outportb(KONFREG,3); /* Full-Mode */
  62.   outportb(MODEREG,0x82+0x8);
  63. end;
  64.  
  65.  
  66. void  textmode_0(void)
  67. begin
  68.   int i;
  69.  
  70.   outportb(MODEREG,0x20);
  71.   for(i = 0; i < 12; i++)  begin
  72.     outportb(INDREG,i);
  73.     outportb(DATAREG,TextInit[i]);
  74.   end;
  75.   outportb(KONFREG,1); /* Diag-Mode */
  76.   outportb(MODEREG,0x20+0x8);
  77. end;
  78.  
  79.  
  80.  
  81. /* Plot mit Clipping */
  82. void Plot_0(x,y,col)
  83.   int x,y,col;
  84.  
  85. begin
  86.   char *p;
  87.  
  88.   if (checkwin(x,y)) begin
  89.     p = MK_FP(SCRBASE,ADR(x,y));
  90.     if (!(XOrFlag)) begin
  91.       if (col)
  92.         *p |= MASK(x);
  93.       else
  94.         *p &= ~MASK(x);
  95.     end
  96.     else
  97.       *p ^= MASK(x);
  98.   end;
  99. end;
  100.  
  101.  
  102. void XOrMode_0(flag)
  103.   int flag;
  104.  
  105. begin
  106.   XOrFlag = flag;
  107. end;
  108.  
  109.  
  110. void TestPixel_0(x, y, res)
  111.   int x, y, *res;
  112.  
  113. begin
  114.   char *p;
  115.  
  116.   if (not(checkwin(x,y)))
  117.     *res = -1;
  118.   else begin
  119.     p = MK_FP(SCRBASE,ADR(x,y));
  120.     if (*p & MASK(x))
  121.       *res = 1;
  122.     else
  123.       *res = 0;
  124.   end;
  125. end;
  126.  
  127.  
  128. /* Integer-DDA mit Clipping siehe PASCAL 4/88 ff */
  129. void Draw_0(x1, y1, x2, y2, col)
  130.   int x1,y1,x2,y2,col;
  131.  
  132. begin
  133.   register delta_x, delta_y;  /* That is stored in SI and DI ! */
  134.   int      line_delta, temp, loop;
  135.  
  136. begin
  137.   line_delta = 0;
  138.   delta_x = x2 - x1;
  139.   delta_y = y2 - y1;
  140.   if (delta_y < 0)
  141.   begin       /* change points */
  142.     temp = x1;  x1 = x2;  x2 = temp;
  143.     temp = y1;  y1 = y2;  y2 = temp;
  144.     delta_y = -delta_y;
  145.     delta_x = -delta_x;
  146.   end;
  147.   Plot_0(x1, y1, col);
  148.   if (delta_x >= 0)
  149.     if (delta_x < delta_y)
  150.       for (loop = 1; loop < delta_y; loop++)
  151.         if (line_delta < 0)
  152.           begin
  153.             x1++;
  154.             y1++;
  155.             Plot_0(x1, y1, col);
  156.             line_delta += (delta_y - delta_x);
  157.           end
  158.         else
  159.           begin
  160.             y1++;
  161.             Plot_0(x1, y1, col);
  162.             line_delta -= delta_x;
  163.           end
  164.     else
  165.       for (loop = 1; loop < delta_x; loop++ )
  166.         if (line_delta <= 0)
  167.           begin
  168.             x1++;
  169.             Plot_0(x1, y1, col);
  170.             line_delta += delta_y;
  171.           end
  172.         else
  173.           begin
  174.             x1++;
  175.             y1++;
  176.             Plot_0(x1, y1, col);
  177.             line_delta += (delta_y - delta_x);
  178.           end
  179.   else
  180.     if (abs(delta_x) >= delta_y)
  181.       for (loop = 1; loop < abs(delta_x); loop++)
  182.         if (line_delta <= 0)
  183.           begin
  184.             x1--;
  185.             Plot_0(x1, y1, col);
  186.             line_delta += delta_y;
  187.           end
  188.         else
  189.           begin
  190.             x1--;
  191.             y1++;
  192.             Plot_0(x1, y1, col);
  193.             line_delta += (delta_x + delta_y);
  194.           end
  195.   else
  196.     for (loop = 1; loop < delta_y; loop++)
  197.       if (line_delta < 0)
  198.         begin
  199.           x1--;
  200.           y1++;
  201.           Plot_0(x1, y1, col);
  202.           line_delta += (delta_x + delta_y);
  203.         end
  204.       else
  205.         begin
  206.           y1--;
  207.           Plot_0(x1, y1, col);
  208.           line_delta += delta_x;
  209.         end;
  210.   Plot_0(x2, y2, col)
  211. end;
  212. }
  213.  
  214. /* Bresenham-Algorithmus, siehe PASCAL 4/87 */
  215. void  Circle_0(x_center,y_center,rad, col)
  216.   int x_center,y_center,rad, col;
  217.  
  218. begin
  219.   int  delta,
  220.        AspRX,
  221.        AspRY;
  222. #define aspect_ratio 2/3
  223.   register x, y;
  224.  
  225.   x = 0;
  226.   y =  rad;
  227.   delta = 1 - rad;
  228.  
  229.   repeat
  230.     AspRX =  x*aspect_ratio;
  231.     AspRY =  y*aspect_ratio;
  232.     Plot_0(x_center + x, y_center + AspRY, col);
  233.     Plot_0(x_center - x, y_center + AspRY, col);
  234.     Plot_0(x_center + x, y_center - AspRY, col);
  235.     Plot_0(x_center - x, y_center - AspRY, col);
  236.     Plot_0(x_center + y, y_center + AspRX, col);
  237.     Plot_0(x_center - y, y_center + AspRX, col);
  238.     Plot_0(x_center + y, y_center - AspRX, col);
  239.     Plot_0(x_center - y, y_center - AspRX, col);
  240.     if  (delta > 0) begin
  241.       x++;
  242.       y--;
  243.       delta += 2*x - 2*y + 3;
  244.     end
  245.     else begin
  246.       x++;
  247.       delta += 2*x + 1;
  248.     end;
  249.   until (x > y);
  250. end;
  251.