home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / TEL23SRC.ZIP / RG / RGE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-25  |  5.8 KB  |  306 lines

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