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