home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dlib06.zip / DLIB / DDRAW.C next >
Text File  |  1994-07-04  |  4KB  |  185 lines

  1. #define INCL_DEV
  2. #define INCL_DOS
  3. #define INCL_WIN
  4. #define INCL_32
  5. #include <os2.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include "dlib.h"
  10. #include "portio.h"
  11.  
  12. extern PCHAR VScreen;
  13. extern ULONG VScreenX, VScreenY, VScreenSize;
  14.  
  15.  
  16.  
  17. void __syscall dPixel(ULONG x, ULONG y, unsigned char color)
  18. /* Plane a dot at (x,y) with color...., with clipping */
  19. {
  20.     color = dNearestColor(color);
  21.     if (x > VScreenX || y > VScreenY) {
  22.        return;
  23.     } 
  24.     VScreen[(y*VScreenX) + x] = color;
  25. }
  26.  
  27.  
  28.  
  29.  
  30.  
  31. #define dInlinePixel(x,y,c)     VScreen[((y)*VScreenX) + (x)] = color
  32. /* ...compiler can optomise this better, but it has no clipping. */
  33.  
  34.  
  35.  
  36.  
  37.  
  38. void __syscall dLine(int x1, int y1, int x2, int y2, unsigned char color)
  39. /* Draw a line from (x1,y1) to (x2,y2) with color. 
  40.    Bresnham's line algorithm. Fairly fast. */
  41. {
  42.    unsigned int x,y;
  43.    int d,dx,dy,incrE,incrNE;
  44.  
  45.    color = dNearestColor(color);
  46.    if (x1>x2) {
  47.      x = x2;  x2 = x1;  x1 =x;
  48.      y = y2;  y2= y1;  y1 = y;
  49.    } else {
  50.      x = x1;
  51.      y = y1;
  52.    }
  53.  
  54.    dx = x2 - x1;
  55.    dy = y2 - y1;
  56.    if (abs(dx) >= abs(dy)) {
  57.       if (dy >= 0) {
  58.         dPixel(x, y, color);
  59.         d = (dy<<1) - dx;
  60.         incrE = dy<<1;
  61.         incrNE = (dy-dx)<<1;
  62.         while (x < x2){
  63.            if (d <= 0){
  64.               d += incrE;
  65.               x++;
  66.            } else {
  67.               d += incrNE;
  68.               x++;
  69.               y++;
  70.            }
  71.            dPixel(x, y, color);
  72.         }
  73.       } else {
  74.         dPixel(x, y, color);
  75.         d = (dy<<1)+dx;
  76.         incrE = dy<<1;
  77.         incrNE = (dy+dx)<<1;
  78.         while (x < x2) {
  79.            if (d>=0) {
  80.                d+=incrE;
  81.                x++;
  82.            } else {
  83.                d+=incrNE;
  84.                x++;
  85.                y--;
  86.            }
  87.            dPixel(x,y,color);
  88.         }
  89.       }
  90.   } else {
  91.       if (dy>=0) {
  92.         dPixel(x,y,color);
  93.         d=(dx<<1) - dy;
  94.         incrE=dx<<1;
  95.         incrNE=(dx-dy)<<1;
  96.         while (y<y2) {
  97.            if (d<=0) {
  98.               d+=incrE;
  99.               y++;
  100.            } else {
  101.               d+=incrNE;
  102.               y++;
  103.               x++;
  104.            }
  105.            dPixel(x,y,color);
  106.         }
  107.       } else {
  108.         dPixel(x,y,color);
  109.         d=(dx<<1)+dy;
  110.         incrE=dx<<1;
  111.         incrNE=(dy+dx)<<1;
  112.         while (y>y2) {
  113.            if (d<=0) {
  114.               d+=incrE;
  115.               y--;
  116.            } else {
  117.               d+=incrNE;
  118.               y--;
  119.               x++;
  120.            }
  121.            dPixel(x,y,color);
  122.         }
  123.       }
  124.    }
  125. }
  126.  
  127.  
  128.  
  129.  
  130. void _points(int x, int y, USHORT xc, USHORT yc, CHAR color)
  131. /* This is used in drawing circles, in draws in 8 quadrents at a time.
  132.     This makes the circle drawing calculations quite quick */
  133. {
  134.    dPixel(x+xc, y+yc, color);
  135.    dPixel(y+xc, x+yc, color);
  136.    dPixel(y+xc, -x+yc, color);
  137.    dPixel(x+xc, -y+yc, color);
  138.    dPixel(-x+xc, -y+yc, color);
  139.    dPixel(-y+xc, -x+yc, color);
  140.    dPixel(-y+xc, x+yc, color);
  141.    dPixel(-x+xc, y+yc, color);
  142. }
  143.  
  144.  
  145.  
  146. void __syscall dCircle(int x1, int y1, int radius, unsigned char color)
  147. /* dCircle -- draws circles using integer math. This speeds the whole
  148.     process greatly. The only multiplies are * 2's, which the compiler
  149.     should optomize's to left shifts. */
  150. {
  151.   int x=0,y=radius,d=1-radius;
  152.  
  153.   color = dNearestColor(color);
  154.   _points(x,y,x1,y1,color);             /* draw 8 quadrants */
  155.   while (y>x) {
  156.     if (d<0) {
  157.        d+=2*x+3;
  158.        x++;
  159.     } else {
  160.        d+=2*(x-y)+5;
  161.        x++;
  162.        y--;
  163.     }
  164.     _points(x,y,x1,y1,color);            /* draw 8 quadrants */
  165.   }
  166. }
  167.  
  168.  
  169.  
  170.  
  171. void __syscall dBox(int x1, int y1, int x2, int y2, unsigned char color)
  172. /* Draws a solid box using color, from (x1,y1) to (x2,y2). 
  173.    This assumes (x1,y1) is top right, (x2,y2) is bottom left */
  174. {
  175.     PCHAR loc = VScreen + (y1 * VScreenX) + x1;
  176.     ULONG size = x2 - x1;
  177.  
  178.     color = dNearestColor(color);
  179.     while (y1 < y2) {
  180.        memset(loc, color, size);
  181.        loc+=VScreenX;
  182.        y1++;
  183.     }
  184. }
  185.