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

  1. /*
  2. *
  3. *  rgh.c by Aaron Contorer for NCSA
  4. *
  5. *  graphics routines for drawing on Hercules monochrome card
  6. *  Input coordinate space = 0..4095 by 0..4095
  7. *  MUST BE COMPILED WITH LARGE MEMORY MODEL!
  8. *
  9. *  RGH = routines callable from outside
  10. *  HGC = routines for internal use only
  11. *
  12. */
  13. #include <stdio.h>    /* used for debugging only */
  14. #include <stdlib.h>
  15. #include <dos.h>    /* used for HGC init call */
  16. #ifdef MSC
  17. #include <conio.h>
  18. #endif
  19. #ifdef MEMORY_DEBUG
  20. #include "memdebug.h"
  21. #endif
  22. #include "externs.h"
  23.  
  24. #ifdef __TURBOC__
  25. #define outpw outport
  26. #endif
  27.  
  28. #define TRUE 1
  29. #define FALSE 0
  30. #define INXMAX 4096
  31. #define INXSHIFT 12
  32. #define INYMAX 4096
  33. #define INYSHIFT 12
  34. #define MAGNIFY 1000
  35. #define SCRNXHI 719
  36. #define SCRNYHI 347
  37. #define MAXHERC 32        /* max. number of Hercules windows */
  38. #define HGCxmax 720        /* max. number of pixels in the x direction */
  39. #define HGCymax 348        /* max. number of pixels in the y direction */
  40.  
  41.             /* Hercules control */
  42. #define INDXPORT 0x3b4
  43. #define DATAPORT 0x3b5
  44. #define CTRLPORT 0x3b8
  45. #define CONFPORT 0x3bf
  46. #define SCRN_ON 8
  47.  
  48. #ifdef QAK
  49. #define GRPH 0x02
  50. #define TEXT 0x20
  51. #endif
  52. #define GRPH 0x06
  53. #define TEXT 0x25
  54.             /* graphics */
  55. #ifdef MSC
  56. static unsigned int HGCgtable[12]={ 
  57.     0x35, 0x2d, 0x2e, 0x07,
  58.     0x5b, 0x02, 0x57, 0x57,
  59.     0x02, 0x03, 0x00, 0x00
  60. };
  61.             /* text */
  62. static unsigned int HGCttable[12]={
  63.     0x61, 0x50, 0x52, 0x0f,
  64.     0x19, 0x06, 0x19, 0x19,
  65.     0x02, 0x0d, 0x0b, 0x0c
  66. };
  67.  
  68. #else
  69. static unsigned char HGCgtable[12]={ 
  70.     0x35, 0x2d, 0x2e, 0x07,
  71.     0x5b, 0x02, 0x57, 0x57,
  72.     0x02, 0x03, 0x00, 0x00
  73. };
  74.             /* text */
  75. static unsigned char HGCttable[12]={
  76.     0x61, 0x50, 0x52, 0x0f,
  77.     0x19, 0x06, 0x19, 0x19,
  78.     0x02, 0x0d, 0x0b, 0x0c
  79. };
  80. #endif
  81.  
  82. static int HGCactive;        /* number of currently HGCactive window */
  83. static char *HGCram;
  84. static char HGCpbit[SCRNXHI+1];
  85. static unsigned char power2[8] = {1,2,4,8,16,32,64,128};
  86. static char *HGCname = "Hercules High-Resolution Graphics";
  87. static void HGCrasline(int w,int x0,int y0,int x1,int y1);
  88.                     /* Current status of an HGC window */
  89. struct HGCWIN {
  90.     char inuse;                 /* true if window in use */
  91.     int pencolor, rotation, size;
  92.     int winbot,winleft,wintall,winwide;    /* position of the window in virtual space */
  93. };
  94.  
  95. static struct HGCWIN HGCwin[MAXHERC];
  96. void HGCsetup()                /* prepare variables for use in other functions */
  97. {
  98.     int x;
  99.  
  100.     HGCpbit[0]=128; 
  101.     HGCpbit[1]=64; 
  102.     HGCpbit[2]=32; 
  103.     HGCpbit[3]=16;
  104.     HGCpbit[4]=8;   
  105.     HGCpbit[5]=4;  
  106.     HGCpbit[6]=2;  
  107.     HGCpbit[7]=1;
  108.     for(x=8; x<=SCRNXHI; x++) 
  109.         HGCpbit[x]=HGCpbit[x&7];
  110. #if defined(MSC) && !defined(__TURBOC__)
  111.     FP_SEG(HGCram) = 0xB000;  FP_OFF(HGCram) = 0;
  112. #else
  113.     HGCram=(char *)0xB0000;
  114. #endif
  115. }
  116.  
  117. void RGHgmode()                /* go into HGC graphics mode */
  118. {
  119. #ifdef MSC
  120.      unsigned int *hdata=HGCgtable;
  121.     unsigned int port;
  122. #else
  123.      char *hdata=HGCgtable;
  124.     char port;
  125. #endif
  126.     long *video;         /* long does 4 chars at a time */
  127.     int memloc;
  128.                         /* set video chips */
  129. #ifdef MSC
  130.     outpw(CTRLPORT,GRPH|SCRN_ON);
  131.     outpw(CONFPORT,0x01);
  132.     for(port=0; port<12; port++) {
  133.         outpw(INDXPORT,port);
  134.         outpw(DATAPORT,*(hdata++));
  135.       }    /* end for */
  136. #else
  137.     outp(CTRLPORT,GRPH|SCRN_ON);
  138.     outp(CONFPORT,0x01);
  139.     for(port=0; port<12; port++) {
  140.         outp(INDXPORT,port);
  141.         outp(DATAPORT,*(hdata++));
  142.       }
  143. #endif
  144.                         /* clear video buffer */
  145.     video=(long *)HGCram;
  146.     for(memloc=0; memloc<8191; memloc++)
  147.         *(video++)=0;
  148. }
  149.  
  150. void RGHtmode()        /* go into HGC text mode, compatible with IBM Monochrome */
  151. {
  152. #ifdef MSC
  153.     unsigned int *hdata=HGCttable;
  154.     unsigned port;
  155. #else
  156.     char *hdata=HGCttable;
  157.     char port;
  158. #endif
  159.     long *video; /* long does 4 chars at a time */
  160.     int memloc;
  161.                 /* set video chips */
  162. #ifdef MSC
  163.     outpw(CTRLPORT,TEXT|SCRN_ON);
  164.     outpw(CONFPORT,0x01);
  165.     for(port=0; port<12; port++) {
  166.         outpw(INDXPORT,port);
  167.         outpw(DATAPORT,*(hdata++));
  168.       }
  169. #else
  170.     outp(CTRLPORT,TEXT|SCRN_ON);
  171.     outp(CONFPORT,0x01);
  172.     for(port=0; port<12; port++) {
  173.         outp(INDXPORT,port);
  174.         outp(DATAPORT,*(hdata++));
  175.       }
  176. #endif
  177.  
  178.                 /* clear video buffer */
  179.     video=(long *)HGCram;
  180.     for(memloc=0; memloc<1000; memloc++)
  181.         *(video++)=0;
  182.     HGCactive=-1;
  183. }
  184.  
  185. static void HGCrasline(w,x0,y0,x1,y1)
  186. int w,x0,y0,x1,y1;
  187.                 /* draw a line from (x0,y0) to (x1,y1) */
  188.                 /* uses Bresenham's Line Algorithm */
  189. {
  190.     int x,y,dx,dy,d,temp,
  191.     dx2,dy2,        /* 2dx and 2dy */
  192.     direction;        /* +1 or -1, used for slope */
  193.     char transpose;    /* true if switching x and y for vertical-ish line */
  194.  
  195.     x0=(int)(((long)x0*HGCxmax)>>INXSHIFT);
  196.     y0=HGCymax-1-(int)(((long)y0*HGCymax)>>INYSHIFT);
  197.     x1=(int)(((long)x1*HGCxmax)>>INXSHIFT);
  198.     y1=HGCymax-1-(int)(((long)y1*HGCymax)>>INYSHIFT);
  199.     if(abs(y1-y0)>abs(x1-x0)) {        /* transpose vertical-ish to horizontal-ish */
  200.         temp=x1; 
  201.         x1=y1; 
  202.         y1=temp;
  203.         temp=x0; 
  204.         x0=y0; 
  205.         y0=temp;
  206.         transpose=TRUE;
  207.       } 
  208.     else 
  209.         transpose=FALSE;
  210.                                     /* make sure line is left to right */
  211.     if(x1<x0) {
  212.         temp=x1; 
  213.         x1=x0; 
  214.         x0=temp; 
  215.         temp=y1; 
  216.         y1=y0; 
  217.         y0=temp;
  218.       }
  219.                                 /* SPECIAL CASE: 1 POINT */
  220.     if(x1==x0&&y1==y0) { 
  221.         HGCram[0x2000*(y1%4)+90*(y1/4)+(x1/8)]|=HGCpbit[x1];
  222.         return;
  223.       }        
  224.                                 /* ANY LINE > 1 POINT */
  225.     x=x0;
  226.     y=y0;
  227.     dx=x1-x0;
  228.     if(y1>=y0) {
  229.         dy=y1-y0;
  230.         direction=1;
  231.       } 
  232.     else {
  233.         dy=y0-y1;
  234.         direction=-1;
  235.       }
  236.     dx2=dx<<1;
  237.     dy2=dy<<1;
  238.     d=(dy<<1)-dx;
  239.     if (transpose) {    
  240.                             /* CASE OF VERTICALISH (TRANSPOSED) LINES */
  241.         while(x<=x1) {
  242.             if(y>=0&&y<HGCxmax&&x>=0&&x<HGCymax)
  243.                 HGCram[0x2000*(x%4)+90*(x/4)+(y/8)]|=HGCpbit[y];
  244.             while(d>=0) {
  245.                 y+=direction;
  246.                 d-=dx2;
  247.               }
  248.             d+=dy2;
  249.             x++;
  250.           } 
  251.       } 
  252.     else {
  253.                             /* CASE OF HORIZONTALISH LINES */
  254.         while(x<=x1) {
  255.             if(x>=0&&x<HGCxmax&&y>=0&&y<HGCymax)
  256.                 HGCram[0x2000*(y%4)+90*(y/4)+(x/8)]|=HGCpbit[x];
  257.             while(d>=0) {
  258.                 y+=direction;
  259.                 d-=dx2;
  260.               }
  261.             d+=dy2;
  262.             x++;
  263.           }
  264.       }                    /* end horizontalish */
  265.     w=w;
  266. }                         /* end HGCrasline() */
  267.  
  268. void RGHclrscr(w)
  269. int w;
  270. /* 
  271.     Clear the screen.
  272. */
  273. {
  274.     if(w==HGCactive) {
  275.         HGCsetup();
  276.         RGHgmode();
  277.       }
  278. }
  279.  
  280. int RGHnewwin()
  281. /* 
  282.     Set up a new window; return its number.
  283.     Returns -1 if cannot create window.
  284. */
  285. {
  286.     int w=0;
  287.  
  288.     while(w<MAXHERC&&HGCwin[w].inuse) 
  289.         w++;
  290.     if(w==MAXHERC)
  291.         return(-1);                 /* no windows available */
  292.     HGCwin[w].pencolor=7;
  293.     HGCwin[w].winbot=0;
  294.     HGCwin[w].wintall=3120;
  295.     HGCwin[w].winleft=0;
  296.     HGCwin[w].winwide=4096;
  297.     HGCwin[w].inuse=TRUE;
  298.     return(w);
  299. }
  300.  
  301. void RGHclose(w)
  302. int w;
  303. {
  304.     if(HGCactive==w) {
  305.         RGHclrscr(w);
  306.         HGCactive=-1;
  307.       }
  308.     HGCwin[w].inuse=FALSE;
  309. }
  310.  
  311. void RGHpoint(w,x,y)
  312. int w,x,y;            /* set pixel at location (x,y) -- no range checking performed */
  313. {
  314.     int x2,y2;         /* on-screen coordinates */
  315.     if(w==HGCactive) {
  316.         x2=(int)(((long)x*HGCxmax)>>INXSHIFT);
  317.         y2=SCRNYHI-(int)(((long)y*HGCymax)>>INYSHIFT);
  318.         HGCram[0x2000*(y2%4)+90*(y2/4)+(x2/8)]|=HGCpbit[x2];
  319.       }
  320. }  
  321.  
  322. void RGHdrawline(w,x0,y0,x1,y1)
  323. int w,x0,y0,x1,y1;
  324. {
  325.     if(w==HGCactive) {
  326.         HGCrasline(w,x0,y0,x1,y1);
  327.       }
  328. }
  329.  
  330. void RGHpagedone(w)
  331. int w;
  332. /*
  333.     Do whatever has to be done when the drawing is all done.
  334.     (For printers, that means eject page.)
  335. */
  336. {
  337.     /* do nothing for HGC */
  338.     w=w;
  339. }
  340.  
  341. void RGHdataline(w,data,count)
  342. int w,count;
  343. char *data;
  344. /*
  345.     Copy 'count' bytes of data to screen starting at HGCactive
  346.     cursor location.
  347. */
  348. {
  349.     /* Function not supported yet. */
  350.     w=w;
  351.     count=count;
  352.     data=data;
  353. }
  354.  
  355. void RGHpencolor(w,color)
  356. int w,color;
  357. /*
  358.     Change pen color to the specified color.
  359. */
  360. {
  361.     /* Function not supported yet. */
  362.     w=w;
  363.     color=color;
  364. }
  365.  
  366. void RGHcharmode(int w,int rotation,int size)
  367. /*
  368.     Set description of future device-supported graphtext.
  369.     Rotation=quadrant.
  370. */
  371. {
  372.     /* No rotatable device-supported graphtext is available on HGC. */
  373.     w=w;
  374.     rotation=rotation;
  375.     size=size;
  376. }
  377.  
  378. /* Not yet supported: */
  379. void RGHshowcur() {}
  380. void RGHlockcur() {}
  381. void RGHhidecur() {}
  382.  
  383. void RGHbell(w)
  384. int w;                /* Ring bell in window w */
  385. {
  386.     if(w==HGCactive) putchar(7);
  387. }
  388.  
  389. char *RGHdevname()
  390. /* return name of device that this RG supports */
  391. {
  392.     return(HGCname);
  393. }
  394.  
  395. void RGHinit()
  396. /* initialize all RGH variables */
  397. {
  398.     int i;
  399.  
  400.     HGCsetup();
  401.     for(i=0; i<MAXHERC; i++)
  402.         HGCwin[i].inuse=FALSE;
  403.     HGCactive=-1;
  404. }
  405.  
  406. void RGHuncover(w)
  407. int w;
  408. /*
  409.     Make this window visible, hiding all others.
  410.     Caller should follow this with clrscr and redraw to show the HGCactive
  411.     contents of the window.
  412. */
  413. {
  414.     HGCactive=w;
  415. }
  416.  
  417. void RGHinfo(int w,int a,int b,int c,int d,int v)
  418. {
  419. /* Needed for possible future functionality */
  420.  
  421.     w=w;
  422.     a=a;
  423.     b=b;
  424.     c=c;
  425.     d=d;
  426.     v=v;
  427. }
  428.