home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tel2305s.zip / RG / RGV.C < prev    next >
C/C++ Source or Header  |  1992-02-25  |  6KB  |  282 lines

  1. /*
  2. *     rgv.c by Quincey Koziol for NCSA
  3. *
  4. *  graphics routines for drawing on EGA
  5. *  Input coordinate space = 0..4095 by 0..4095
  6. *  MUST BE COMPILED WITH LARGE MEMORY MODEL!
  7. *
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <dos.h>        /* used for VGA init call */
  13. #include "externs.h"
  14.  
  15. #define TRUE 1
  16. #define FALSE 0
  17. #define INXMAX 4096
  18. #define INXSHIFT 12
  19. #define INYMAX 4096
  20. #define INYSHIFT 12
  21. #define SCRNXHI 639
  22. #define SCRNYHI 479
  23. #define VGAxmax 640            /* max. number of pixels in the x direction */
  24. #define VGAymax 480            /* max. number of pixels in the y direction */
  25. #define VGAbytes 80         /* number of bytes per line */
  26. #define MAXRW 32            /* max. number of real windows */
  27.  
  28. static int VGAactive;        /* number of currently EGAactive window */
  29. static char *VGAname="Video Graphics Array, 640 x 480";
  30.  
  31. /* Current status of an VGA window */
  32. struct VGAWIN {
  33.     char inuse;         /* true if window in use */
  34.     int pencolor, rotation, size;
  35.     int winbot,winleft,wintall,winwide;
  36.                 /* position of the window in virtual space */
  37. };
  38.  
  39. static struct VGAWIN VGAwins[MAXRW];
  40.  
  41. /*
  42.     Set up a new window; return its number.
  43.     Returns -1 if cannot create window.
  44. */
  45. int RGVnewwin(void)
  46. {
  47.     int w=0;
  48.  
  49.     while(w<MAXRW && VGAwins[w].inuse) 
  50.         w++;
  51.     if(w==MAXRW) 
  52.         return(-1);             /* no windows available */
  53.     VGAwins[w].pencolor=7;
  54.     VGAwins[w].winbot=0;
  55.     VGAwins[w].wintall=4096;
  56.     VGAwins[w].winleft=0;
  57.     VGAwins[w].winwide=4096;
  58.     VGAwins[w].inuse=TRUE;
  59.     return(w);
  60. }
  61.  
  62. /*
  63.     Clear the screen.
  64. */
  65. void RGVclrscr(int w)
  66. {
  67.     if(w==VGAactive)
  68.         n_gmode(18);
  69. }
  70.  
  71. void RGVclose(int w)
  72. {
  73.     if(VGAactive==w) {
  74.         RGVclrscr(w);
  75.         VGAactive=-1;
  76.       }
  77.     VGAwins[w].inuse=FALSE;
  78. }
  79.  
  80. /* set pixel at location (x,y) -- no range checking performed */
  81. void RGVpoint(int w,int x,int y)
  82. {
  83.     int x2,y2;                 /* on-screen coordinates */
  84.     if(w==VGAactive) {
  85.         x2=(int)((VGAxmax*(long)x)>>INXSHIFT);
  86.         y2=SCRNYHI-(int)(((long)y*VGAymax)>>INYSHIFT);
  87.         VGAset(x2,y2,VGAwins[w].pencolor);
  88.       }
  89. }
  90.  
  91. /* draw a line from (x0,y0) to (x1,y1) */
  92. /* uses Bresenham's Line Algorithm */
  93. void RGVdrawline(int w,int x0,int y0,int x1,int y1)
  94. {
  95.     int x,y,dx,dy,d,temp,
  96.     dx2,dy2,                    /* 2dx and 2dy */
  97.     direction;                    /* +1 or -1, used for slope */
  98.     char transpose;                /* true if switching x and y for vertical-ish line */
  99.  
  100. #ifdef DEBUG
  101. printf("RGVdrawline(): x0=%d, y0%d\n",x0,y0);
  102. printf("x1=%d, y1=%d\n",x1,y1);
  103. #endif
  104.     if(w!=VGAactive) 
  105.         return;
  106.     x0=(int)(((long)x0*VGAxmax)>>INXSHIFT);
  107.     y0=VGAymax-1-(int)((VGAymax*(long)y0)>>INYSHIFT);
  108.     x1=(int)(((long)x1*VGAxmax)>>INXSHIFT);
  109.     y1=VGAymax-1-(int)((VGAymax*(long)y1)>>INYSHIFT);
  110.     if(abs(y1-y0)>abs(x1-x0)) {        /* transpose vertical-ish to horizontal-ish */
  111.         temp=x1; 
  112.         x1=y1; 
  113.         y1=temp;
  114.         temp=x0; 
  115.         x0=y0; 
  116.         y0=temp;
  117.         transpose=TRUE;
  118.       } 
  119.     else 
  120.         transpose=FALSE;
  121. /* make sure line is left to right */
  122.     if(x1<x0) {
  123.         temp=x1; 
  124.         x1=x0; 
  125.         x0=temp;
  126.  
  127.         temp=y1; 
  128.         y1=y0; 
  129.         y0=temp;
  130.       }
  131. /* SPECIAL CASE: 1 POINT */
  132.     if(x1==x0 && y1==y0) {
  133.         VGAset(x1,y1,VGAwins[w].pencolor);
  134.         return;
  135.       }
  136. /* ANY LINE > 1 POINT */
  137.     x=x0;
  138.     y=y0;
  139.     dx=x1-x0;
  140.     if(y1>=y0) {
  141.         dy=y1-y0;
  142.         direction=1;
  143.       } 
  144.     else {
  145.         dy=y0-y1;
  146.         direction=-1;
  147.       }
  148.     dx2=dx<<1;
  149.     dy2=dy<<1;
  150.     d=(dy<<1)-dx;
  151.     if(transpose) {        /* CASE OF VERTICALISH (TRANSPOSED) LINES */
  152.         while(x<=x1) {
  153.             if(y>=0 && y< VGAxmax && x>=0 && x<VGAymax)
  154.                 VGAset(y,x,VGAwins[w].pencolor);
  155.             while(d>=0) {
  156.                 y+=direction;
  157.                 d-=dx2;
  158.               }
  159.             d+=dy2;
  160.             x++;
  161.           }
  162.       } 
  163.     else {            /* CASE OF HORIZONTALISH LINES */
  164.         while(x<=x1) {
  165.             if(x>=0 && x<VGAxmax && y>=0 && y<VGAymax)
  166.                 VGAset(x,y,VGAwins[w].pencolor);
  167.             while(d>=0) {
  168.                 y+=direction;
  169.                 d-=dx2;
  170.               }
  171.             d+=dy2;
  172.             x++;
  173.           }
  174.       }             /* end horizontalish */
  175. }   /* end RGVdrawline() */
  176.  
  177. /*
  178.     Do whatever has to be done when the drawing is all done.
  179.     (For printers, that means eject page.)
  180. */
  181. void RGVpagedone(int w)
  182. {
  183.     /* do nothing for VGA */
  184.     w=w;
  185. }
  186.  
  187. /*
  188.     Copy 'count' bytes of data to screen starting at current
  189.     cursor location.
  190. */
  191. void RGVdataline(int w,char *data,int count)
  192. {
  193.     /* Function not supported yet. */
  194.     w=w;
  195.     data=data;
  196.     count=count;
  197. }
  198.  
  199. /*
  200.     Change pen color to the specified color.
  201. */
  202. void RGVpencolor(int w,int color)
  203. {
  204.     if(!color)
  205.         color=1;
  206.     color&=0x7;
  207.                                     /* flip color scale */
  208.     VGAwins[w].pencolor=8-color;
  209. }
  210.  
  211. /*
  212.     Set description of future device-supported graphtext.
  213.     Rotation=quadrant.
  214. */
  215. void RGVcharmode(int w,int rotation,int size)
  216. {
  217.     /* No rotatable device-supported graphtext is available on VGA. */
  218.     w=w;
  219.     rotation=rotation;
  220.     size=size;
  221. }
  222.  
  223. /* Not yet supported: */
  224. void RGVshowcur(void) {}
  225. void RGVlockcur(void) {}
  226. void RGVhidecur(void) {}
  227.  
  228. /* Ring bell in window w */
  229. void RGVbell(int w)
  230. {
  231.     if(w==VGAactive)
  232.         putchar(7);
  233. }
  234.  
  235. /* return name of device that this RG supports */
  236. char *RGVdevname(void )
  237. {
  238.     return(VGAname);
  239. }
  240.  
  241. /*
  242.     Make this window visible, hiding all others.
  243.     Caller should follow this with clrscr and redraw to show the current
  244.     contents of the window.
  245. */
  246. void RGVuncover(int w)
  247. {
  248.     VGAactive=w;
  249. }
  250.  
  251. /* initialize all RGV variables */
  252. void RGVinit(void)
  253. {
  254.     int i;
  255.     for(i=0; i<MAXRW; i++)
  256.         VGAwins[i].inuse = FALSE;
  257.     VGAactive=-1;
  258. }
  259.  
  260. void RGVinfo(int w,int a,int b,int c,int d,int v)
  261. {
  262.     w=w;
  263.     a=a;
  264.     b=b;
  265.     c=c;
  266.     d=d;
  267.     v=v;
  268. }
  269.  
  270. /* go into VGA graphics mode */
  271. void RGVgmode(void )
  272. {
  273.     n_gmode(18);
  274. }
  275.  
  276. /* go into VGA 80x25 color text mode */
  277. void RGVtmode(void)
  278. {
  279.     n_gmode(3);
  280.     VGAactive=-1;
  281. }
  282.