home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gle / gle / d_pc.c < prev    next >
C/C++ Source or Header  |  1992-11-29  |  24KB  |  966 lines

  1. /*--------------------------------------------------------------*/
  2. /*    IBM PC Driver 1.0 (TURBO  C) , for GLE V3.0        */
  3. /*--------------------------------------------------------------*/
  4.  
  5. /*---------------------------------------------------------------------------*/
  6. #include "all.h"
  7. #include <graphics.h>
  8. #include <math.h>
  9. #include "core.h"
  10. #include "mygraph.h"
  11. #include "mydev.h"
  12. int ingraphmode;
  13. extern int gunit;
  14. extern struct gmodel g;
  15. /*---------------------------------------------------------------------------*/
  16. #define pi 3.141592653
  17. #define true (!false)
  18. #define BLACKANDWHITE 1
  19. #define false 0
  20. #define dbg if ((gle_debug & 64)>0)
  21. #define stop if ((gle_debug & 128)>0) return
  22. extern int gle_debug;
  23. int incap=true;
  24. int getch(void);
  25. int kbhit(void);
  26. /*---------------------------------------------------------------------------*/
  27. /* The global variables for the PC screen driver */
  28. /*-----------------------------------------------*/
  29.  
  30. int i,l,j,ix,iy;
  31. double f;
  32.  
  33.  
  34. #define gerr() i = graphresult(); if (i!=0) printf("Graph error: %s \n",grapherrormsg(i));
  35. #define xsizecm 21.0
  36. #define ysizecm 18.0
  37. #define sx(v) ( (int) ((v) * d_xscale)+1)
  38. #define sy(v) ( d_maxy - ((int) ((v) * d_yscale)))
  39. #define rx(v) ( (int) ((v) * d_xscale)+1)
  40. #define ry(v) ( d_maxy - ((int) ((v) * d_yscale)))
  41. #define textmode() {getch(); restorecrtmode();}
  42. #define graphmode() {getch(); setgraphmode(d_graphmode);}
  43. #define gon  if ((gle_debug & 256) > 0) graphmode()
  44. #define goff if ((gle_debug & 256) > 0) textmode()
  45.  
  46. double d_scale, d_xscale, d_yscale;
  47. int d_graphmode;
  48. int d_fillstyle=1,d_fillcolor;
  49. int d_lstyle,d_lwidth,d_pat;
  50. int d_maxy;
  51.  
  52. int d_zzline(double cx,double cy,double zx,double zy);
  53. int addline(char *s);
  54.  
  55. int getwhite();
  56. int getwhite()
  57. {
  58.     int i;
  59.     i = getmaxcolor();
  60.     if (i>15) i = 15;
  61.     return i;
  62. }
  63. d_devcmd(char *s)
  64. {}
  65.  
  66. int undxy(int dx, int dy, double *x, double *y);
  67. undxy(int dx, int dy, double *x, double *y)
  68. {
  69.     double ddx,ddy;
  70.     char buff[80];
  71.     ddx = (dx)/d_xscale;
  72.     ddy = (dy - d_maxy) / -d_yscale;
  73.     g_undev(ddx,ddy,x,y);
  74. }
  75. dxy(double x, double y, int *dx, int *dy)
  76. {
  77.     static double fx,fy;
  78.     g_dev(x,y,&fx,&fy);
  79.     *dx = sx(fx);
  80.     *dy = sy(fy);
  81. }
  82. rxy(double x, double y, int *dx, int *dy)
  83. {
  84.     static double fx,fy,zx,zy;
  85.     g_dev(x,y,&fx,&fy);
  86.     g_dev(0.0,0.0,&zx,&zy);
  87.     *dx = (int) ( (fx-zx) * d_xscale +1);
  88.     *dy = (int) ( (fy-zy) * d_yscale);
  89. }
  90. /*---------------------------------------------------------------------------*/
  91. d_dfont(char *c)
  92. {
  93.     /* only used for the DFONT driver which builds fonts */
  94. }
  95. /*---------------------------------------------------------------------------*/
  96. static char lastline[80];
  97. d_message(char *s)
  98. {
  99.     static int single_step;
  100.     int oldcolor,oldx,oldy;
  101.     if (ingraphmode) {
  102.         oldx = getx(); oldy = gety();
  103.         oldcolor = getcolor();
  104.         setfillstyle(1,BLACK);
  105.         setcolor(WHITE);
  106.         bar(1,getmaxy()-19,getmaxx(),getmaxy());
  107.         settextstyle(0,0,0);
  108.         settextjustify(LEFT_TEXT,BOTTOM_TEXT);
  109.         moveto(4,getmaxy()-10);
  110.         if (strlen(lastline)>0) outtext(lastline);
  111.         moveto(4,getmaxy()-2);
  112.         outtext(s);
  113.         ncpy(lastline,s,78);
  114.         setcolor(oldcolor);
  115.         setfillstyle(d_fillstyle,d_fillcolor);
  116.         moveto(oldx,oldy);
  117.         if (single_step) {
  118.             if (getch()=='g') single_step = false;
  119.         }
  120.         if (kbhit()) {
  121.             if ((bioskey(1) & 0xff) != 27) {
  122.              if (getch()=='s') single_step = true;
  123.              while (kbhit()) getch();
  124.              getch();
  125.             }
  126.         }
  127.     } else {
  128.         printf("%s\n",s);
  129.     }
  130. }
  131. /*---------------------------------------------------------------------------*/
  132. d_source(char *s)
  133. {
  134.     s=s;
  135. }
  136. /*---------------------------------------------------------------------------*/
  137. d_get_type(char *t)
  138. {
  139.     strcpy(t,"INTERACTIVE, PC");
  140. }
  141. /*---------------------------------------------------------------------------*/
  142. d_set_path(int onoff)
  143. {}
  144. /*---------------------------------------------------------------------------*/
  145. d_newpath()
  146. {
  147. }
  148. int svga_mode=grError;
  149. int huge detect_svga()
  150. {
  151.   gprint("Loading USER BGI driver \n");
  152.   return svga_mode;
  153. }
  154. add_svga()
  155. {
  156.     int g_error;
  157.     char *s,buff[80],sname[40];
  158.     s = getenv("GLE_ADDBGI");
  159.     if (s==NULL) return;
  160.     strcpy(buff,s);
  161.     s = strtok(buff," .");
  162.     if (s==NULL) {gprint("Expecting mode.name, e.g. 3.svga256,  found {%s} \n",buff); return;}
  163.     svga_mode = atoi(s);
  164.     s = strtok(NULL," .");
  165.     if (s==NULL) {gprint("Expecting 'mode.name' , e.g. 3.svga256,  found {%s} \n",buff); return;}
  166.     strcpy(sname,s);
  167.     installuserdriver(sname,detect_svga);
  168.     g_error = graphresult();
  169.     if (g_error<0) {
  170.         gprint("InstallUserDriver error %s\n",grapherrormsg(g_error));
  171.         text_inkey();
  172.     }
  173. }
  174.  
  175. /*---------------------------------------------------------------------------*/
  176. char *bgidir(void);
  177. d_open(double width, double height)
  178. {
  179.     static int g_driver,g_error;
  180.     double f;
  181.     static int doneopen;
  182.  
  183.     if (!doneopen) {
  184.         add_svga();
  185.  
  186.         doneopen = true;
  187.         /* detectgraph(&g_driver, &d_graphmode);
  188.         if (g_driver<0) {
  189.             printf("No graphics hardware detected !!!!! \n");
  190.             gle_abort("Could not load BGI graphics\n");
  191.         }  */
  192.         g_driver = DETECT;
  193.         initgraph(&g_driver,&d_graphmode,bgidir());
  194.     } else  {
  195.         setgraphmode(d_graphmode);
  196.     }
  197.     g_error = graphresult();
  198.     if (g_error<0) {
  199.         printf("Init graph error %s\n",grapherrormsg(g_error));
  200.         ingraphmode = false; text_inkey(); gle_abort("Init graph error\n");
  201.     }
  202.     if (g_driver==HERCMONO) pokeb(0x40,0x49,6); /* this makes mouse work on herc card */
  203.     ingraphmode = true;
  204.     /* Get largest rectangle we can fit on the screen */
  205.     d_scale = xsizecm / width;
  206.     f = ysizecm / height;
  207.     if (f<d_scale) d_scale = f;
  208.  
  209.     d_xscale = d_scale * (getmaxx()-2) / xsizecm; /* Device Scale X, Device Scale y */
  210.     d_yscale = d_scale * (getmaxy()-22) / ysizecm;
  211.     d_maxy = getmaxy()-21;
  212.     g.userwidth = width;
  213.     g.userheight = height;
  214. }
  215. /*---------------------------------------------------------------------------*/
  216. d_tidyup()
  217. {
  218.     if (ingraphmode) {
  219.         gprint("AARRRrrrrgg,  Watch out I'm dying!!!\n");
  220.         d_close();
  221.     }
  222. }
  223. void mouseloc(int column,int  row);             /* set location of mouse cursor */
  224. int draw_boxline(int d_imde,double x1,double y1,double x2,double y2);
  225. int d_get_click(double *x, double *y, int *click);
  226. int d_get_point(double *x, double *y, int *click);
  227. void mousecursor( int toggle);
  228. int cross_hair(double x, double y);
  229. int mousecheck(void);
  230. double mouse_x,mouse_y,gridstep;
  231. int boxed_text,ismouse;
  232. int d_imde;
  233. double d_mousex,d_mousey;
  234.  
  235.  
  236. int gridit(double *x,double *y);
  237. gridit(double *x,double *y)
  238. {
  239.     *x = floor(*x/gridstep + .5) * gridstep;
  240.     *y = floor(*y/gridstep + .5) * gridstep;
  241. }
  242. d_prompts()
  243. {
  244.      if (d_imde==1) d_message("Click on position for TEXT");
  245.      if (d_imde==2) d_message("Click on position for LINE");
  246.      if (d_imde==3) d_message("Click on position for BOX");
  247.      d_message("t=text l=line b=box o=bOxed_text c=Click u=penUp f=finer g=greater ESC=Exit");
  248. }
  249. d_cursor_toggle(void)
  250. {
  251.     static int onoff=0;
  252.     onoff = !onoff;
  253.     setcolor(getwhite());
  254.     setwritemode(XOR_PUT);
  255.     if (ismouse) mousecursor(onoff); else cross_hair(d_mousex,d_mousey);
  256.     setcolor(0);
  257.     setwritemode(COPY_PUT);
  258. }
  259. d_xor(void)
  260. {
  261.     setcolor(getwhite());
  262.     setwritemode(XOR_PUT);
  263. }
  264. wait_for_return()
  265. {
  266.     int exitlp=false,click=0,clickex=0,oldmode;
  267.     double x1,y1,x2,y2,nx,ny;
  268.     char buff1[90],buff2[90];
  269. /* valid commands are */
  270. /* t = text, l = line, u=penup, b=box, o=bOxed text, f = finer, g = greater */
  271.     d_imde = 2;
  272.     if (d_mousex==0) {
  273.         d_mousex = g.userwidth/2;
  274.         d_mousey = g.userheight/2;
  275.     }
  276.     if (mousecheck()) ismouse = true;
  277.     if (!ismouse) gridstep = 1;
  278.     if (gridstep==0) gridstep = .1;
  279.     d_cursor_toggle();
  280.     for (;;) {
  281.      d_prompts();
  282.      oldmode = d_imde;
  283.      d_get_click(&x1,&y1,&click); if (click==3) goto aborting;
  284.      if (oldmode!=d_imde) goto next_one;
  285.      gridit(&x1,&y1);
  286.      if (d_imde==1) {
  287.         d_message("Enter the text at the top of the screen");
  288.         d_get_line(buff1,&click);
  289.         if (click==3) goto next_one;
  290.         sprintf(buff2,"amove %g %g ",x1,y1); addline(buff2);
  291.         if (boxed_text) addline("begin box add .2");
  292.         sprintf(buff2,"text %s",buff1);
  293.         addline(buff2);
  294.         if (boxed_text) addline("end box");
  295.         g_move(x1,y1);
  296.         g_text(buff1);
  297.         if (d_imde==1) d_imde = 2;
  298.         goto next_one;
  299.      }
  300.      sprintf(buff1,"amove %g %g",x1,y1); addline(buff1);
  301. poly:     d_xor();
  302.      draw_boxline(d_imde,x1,y1,x1,y1);
  303.      x2 = x1; y2 = y1;
  304.      if (d_imde==2 || d_imde==3) {
  305.       oldmode = d_imde;
  306.       for (click=0;click==0 && (d_imde==oldmode);) {
  307.         d_get_point(&nx,&ny,&click);
  308.         if (x2!=nx || y2!=ny) {
  309.           d_cursor_toggle();
  310.           d_xor();
  311.           draw_boxline(oldmode,x1,y1,x2,y2);
  312.           draw_boxline(oldmode,x1,y1,nx,ny);
  313.           d_cursor_toggle();
  314.         }
  315.         delay(10);
  316.         x2 = nx; y2 = ny;
  317.       }
  318.       d_cursor_toggle();
  319.       d_xor();
  320.       draw_boxline(oldmode,x1,y1,nx,ny);
  321.       if (click==1 && d_imde==oldmode) {
  322.         gridit(&x2,&y2);
  323.         setcolor(0);
  324.         setwritemode(COPY_PUT);
  325.         draw_boxline(d_imde,x1,y1,x2,y2);
  326.         if (d_imde==2) {
  327.             sprintf(buff1,"rline %g %g",x2-x1,y2-y1); addline(buff1);
  328.             x1 = x2; y1 = y2;
  329.             d_cursor_toggle();
  330.             if (click==1) goto poly;
  331.         }
  332.         if (d_imde==3) {
  333.             sprintf(buff1,"box %g %g ",x2-x1,y2-y1); addline(buff1);
  334.         }
  335.       }
  336.       d_cursor_toggle();
  337.      }
  338. next_one:;
  339.     setcolor(0);
  340.     setwritemode(COPY_PUT);
  341.     }
  342. aborting:;
  343.     d_cursor_toggle();
  344. }
  345. draw_boxline(int d_imde,double x1,double y1,double x2,double y2)
  346. {
  347.     if (d_imde==2) {
  348.         g_move(x1,y1);
  349.         g_line(x2,y2);
  350.     } else {
  351.         g_move(x1,y1);
  352.         g_box_stroke(x1,y1,x2,y2);
  353.     }
  354. }
  355. d_get_click(double *x, double *y, int *click)
  356. {
  357.     for (;;) {
  358.      d_get_point(x,y,click);
  359.      if (*click>0) return;
  360.      delay(10);
  361.     }
  362. }
  363. #include "edt.h"
  364. int ddgets(char *s);
  365. d_get_line(char *s,int *click)
  366. {
  367.     if (ddgets(s)) *click = 3;
  368.     else *click = 0;
  369. }
  370. ddgets(char *s)
  371. {
  372.     int c,cx=0;
  373.     char mbuff[80];
  374.     *s = 0;
  375.     bottom_line(s);
  376.     for (;;) {
  377.      c = text_inkey();
  378.      switch (c) {
  379.        case eescape: /* ESCAPE */
  380.        case equit: /* control c */
  381.         return true;
  382.       case ereturn: /* carriage return */
  383.         return false;
  384.       case edelete: /* delete */
  385.         if (strlen(s)==0) break;
  386.         if (cx<1) break;
  387.         ncpy(mbuff,s,cx-1);
  388.         strcat(mbuff,s + cx);
  389.         strcpy(s,mbuff);
  390.         cx--;
  391.         bottom_line(s);
  392.         break;
  393.       default: /* normal key */
  394.         if (c<26  && c!=9) {fner("Key has no affect"); break;}
  395.         if (c>200)  fner("Unimplemented command");
  396.         else {
  397.             ncpy(mbuff,s,cx);
  398.             mbuff[cx] = c; mbuff[cx+1] = 0;
  399.             strcat(mbuff,s + cx);
  400.             strcpy(s,mbuff);
  401.             bottom_line(s);
  402.             cx++;
  403.         }
  404.         break;
  405.        }
  406.      }
  407. }
  408. bottom_line(char *s)
  409. {
  410.     setfillstyle(1,BLACK);
  411.     setcolor(WHITE);
  412.     bar(1,getmaxy()-9,getmaxx(),getmaxy());
  413.     settextstyle(0,0,0);
  414.     settextjustify(LEFT_TEXT,BOTTOM_TEXT);
  415.     moveto(4,getmaxy()-2);
  416.     outtext("Enter text? ");
  417.     outtext(s);
  418.     setcolor(0);
  419. }
  420.  
  421. d_get_point(double *x, double *y, int *click)
  422. {
  423.     int i,c,ix,iy,oldmode;
  424.  
  425.     if (ismouse) {
  426.         undxy(mousecol(),mouserow(),x,y);
  427.         d_mousex = *x; d_mousey = *y;
  428.     } else {
  429.         *x = d_mousex;
  430.         *y = d_mousey;
  431.     }
  432.     *click = 0;
  433.     if (kbhit()) {
  434.         oldmode = d_imde;
  435.         c = text_inkey();
  436.         c = tolower(c);
  437.         switch (c) {
  438.           case 'l': /* line */
  439.             d_imde = 2;  break;
  440.           case 'b': /* box */
  441.             d_imde = 3;  break;
  442.           case 't': /* text */
  443.             d_imde = 1;  boxed_text = false; break;
  444.           case 'o': /* boxed text */
  445.             d_imde = 1;
  446.             boxed_text = true; break;
  447.           case 'f': /* finer grid */
  448.             gridstep = gridstep / 10; break;
  449.           case 'g': /* greater grid */
  450.             gridstep *= 10; break;
  451.           case ereturn: /* return, finished */
  452.           case eescape:
  453.             *click = 3; break;
  454.           case 'u':  /* pen up */
  455.             *click = 2; break;
  456.           case 'd': /* pen down */
  457.           case 'c': /* click */
  458.             *click = 1; break;
  459.           case eup: /* Move cursor up 1 grid step */
  460.             *y += gridstep; break;
  461.           case edown:
  462.             *y -= gridstep; break;
  463.           case eright:
  464.             *x += gridstep; break;
  465.           case eleft:
  466.             *x -= gridstep; break;
  467.         }
  468.         if (oldmode!=d_imde) *click = 2;
  469.     }
  470.     if (ismouse) {
  471.         i = mouseclick();
  472.         if (i>0) *click = i;
  473.         undxy(mousecol(),mouserow(),x,y);
  474.     } else {
  475.         gridit(x,y);
  476.     }
  477.     if (*x != d_mousex || *y != d_mousey) {
  478.         if (ismouse) {
  479.             dxy(d_mousex,d_mousey,&ix,&iy);
  480.             mouseloc(ix,iy);
  481.         } else {
  482.             d_cursor_toggle();
  483.             d_mousex = *x;
  484.             d_mousey = *y;
  485.             d_cursor_toggle();
  486.         }
  487.     }
  488. }
  489. cross_hair(double x,double y)
  490. {
  491.     int ix,iy;
  492.     dxy(x,y,&ix,&iy);
  493.     moveto(ix,iy-10);
  494.     lineto(ix,iy+10);
  495.     moveto(ix-10,iy);
  496.     lineto(ix+10,iy);
  497. }
  498. d_close()
  499. {
  500.     stop;gon;
  501.     if (ingraphmode == false) return;
  502.     g_flush();
  503.     d_message("Press <RETURN> to continue");
  504.     lastline[0] = 0;
  505.     if (kbhit()) getch();
  506.     wait_for_return();
  507.     ingraphmode = false;
  508.     restorecrtmode();
  509. }
  510. /*---------------------------------------------------------------------------*/
  511. d_set_line_cap(int i)
  512. {
  513.     i++;
  514. }
  515. /*---------------------------------------------------------------------------*/
  516. d_set_line_join(int i)
  517. {
  518.     i++;
  519. }
  520. /*---------------------------------------------------------------------------*/
  521. d_set_line_miterlimit(double d)
  522. {
  523.     i++;
  524. }
  525. /*---------------------------------------------------------------------------*/
  526. d_set_line_width(double w)
  527. {
  528.     stop;
  529.     d_lwidth = 1;
  530.     if (w>.099) d_lwidth = 3;
  531.     setlinestyle(d_lstyle,d_pat,d_lwidth);
  532.     gerr();goff;
  533. }
  534. /*---------------------------------------------------------------------------*/
  535. d_set_line_styled(double dd)
  536. {}
  537. d_set_line_style(char *s)
  538. {
  539.     static char *defline[] = {"","","12","41","14","92",
  540.     "1282","9229","4114","54"};
  541.     int i, j, nblack, nwhite, pat;
  542.     char *ss;
  543.     stop;gon;
  544.  
  545.     if (strlen(s)==1)  s = defline[*s-'0'];
  546.     if (strcmp(s,"")==0) {
  547.         d_lstyle = SOLID_LINE;
  548.         d_pat = 0;
  549.         setlinestyle(d_lstyle,0,d_lwidth);
  550.     } else {
  551.         ss = s;
  552.         pat = 0;                 /* make a 16 bit pattern from digits */
  553.         for (i=15; i>=0;) {
  554.         nblack = *s - '0';
  555.         s++;
  556.         for (j=0; j<nblack; j++) {
  557.             pat = pat | (1L << i);
  558.             i--;
  559.         }
  560.         if (*s == 0)  s = ss;   /* if odd number of digits, repeat */
  561.         nwhite = *s - '0';
  562.         s++;
  563.         i = i - nwhite;
  564.         if (*s == 0)  s = ss;
  565.         }
  566.         d_lstyle = USERBIT_LINE;
  567.         d_pat = pat;
  568.         setlinestyle(d_lstyle,pat,d_lwidth);
  569.     }
  570.     gerr();goff;
  571. }
  572. /*---------------------------------------------------------------------------*/
  573. d_fill()
  574. {
  575. }
  576. /*---------------------------------------------------------------------------*/
  577. d_fill_ary(int nwk,double (*wkx)[],double (*wky)[])
  578. {
  579.     int i;
  580. /*    fprintf(psfile,"%g %g moveto \n",(*wkx)[0],(*wky)[0]);
  581.     for (i=1;i<nwk;i++)
  582.         fprintf(psfile,"%g %g l \n",(*wkx)[i],(*wky)[i]);
  583. */
  584. }
  585. d_line_ary(int nwk,double (*wkx)[],double (*wky)[])
  586. {
  587.     int i;
  588.     stop;gon;
  589.     dxy( (*wkx)[0], (*wky)[0], &ix, &iy);
  590.     moveto(ix,iy);
  591.     for (i=1;i<nwk;i++) {
  592.         dxy( (*wkx)[i], (*wky)[i], &ix, &iy);
  593.         lineto(ix,iy);
  594.     }
  595.     gerr();goff;
  596. }
  597. /*---------------------------------------------------------------------------*/
  598. d_stroke()
  599. {
  600. }
  601. /*---------------------------------------------------------------------------*/
  602. d_clip()
  603. {
  604. }
  605. /*---------------------------------------------------------------------------*/
  606. d_set_matrix(double newmat[3][3])
  607. {
  608. }
  609. /*---------------------------------------------------------------------------*/
  610. d_move(double zx,double zy)
  611. {
  612. }
  613. /*---------------------------------------------------------------------------*/
  614. d_reverse()     /* reverse the order of stuff in the current path */
  615. {
  616. }
  617. /*---------------------------------------------------------------------------*/
  618. d_closepath()
  619. {
  620.     g_line(g.closex,g.closey);
  621. }
  622. /*---------------------------------------------------------------------------*/
  623. int dunx1,dunx2,duny1,duny2;
  624. d_unline()
  625. {
  626.      setcolor(getwhite());
  627.     moveto(dunx1,duny1);
  628.     lineto(dunx2,duny2);
  629. }
  630. d_zzline(double cx,double cy,double zx,double zy)
  631. {
  632.     static int ux,uy,ix,iy;
  633.     dxy(cx,cy,&ux,&uy);
  634.     dunx1 = ux; duny1 = uy;
  635.     dxy(zx,zy,&ix,&iy);
  636.      setcolor(0);
  637.     moveto(ux,uy);
  638.     lineto(ix,iy);
  639.     dunx2 = ix; duny2 = iy;
  640. }
  641.  
  642. d_line(double zx,double zy)
  643. {
  644.     static int ux,uy;
  645.     if (g.xinline==false) {
  646.         dxy(g.curx,g.cury,&ux,&uy);
  647.         moveto(ux,uy);
  648.         dunx1 = ux; duny1 = uy;
  649.     }
  650.     dxy(zx,zy,&ix,&iy);
  651.     lineto(ix,iy);
  652.     dunx2 = ix; duny2 = iy;
  653. }
  654. /*---------------------------------------------------------------------------*/
  655. d_clear()
  656. {
  657.     double width,height;
  658.     int x1,y1,x2,y2;
  659.     width = g.userwidth;
  660.     height = g.userheight;
  661.     stop;
  662.     d_lstyle = SOLID_LINE;
  663.     d_pat = 0;
  664.     d_lwidth = 1;
  665.     setgraphmode(d_graphmode);
  666.  
  667.     /* now draw bounding box of screen */
  668.     x1 = 0;
  669.     y1 = d_maxy-height*d_yscale -1;
  670.     x2 = width*d_xscale+2;
  671.     y2 = d_maxy+1;
  672.     setfillstyle(1,getwhite());
  673.     bar(x1,y1,x2,y2);
  674.     setcolor(BLACK);
  675.     goff;
  676. }
  677. /*---------------------------------------------------------------------------*/
  678. d_flush()
  679. {
  680. }
  681. /*---------------------------------------------------------------------------*/
  682. int polar_xy(double r, double angle, double *dx, double *dy);
  683. int xy_polar(double dx,double dy,double *radius,double *angle);
  684. d_arcto(dbl x1,dbl y1,dbl x2,dbl y2,dbl rrr)
  685. {
  686.     double x0,y0,r1,a1,r2,a2,r3,a3,a4,r5,sx,sy,ex,ey;
  687.     double bx1,by1,bx2,by2,dist,neg;
  688.     g_get_xy(&x0,&y0);
  689.     xy_polar(x1-x0,y1-y0,&r1,&a1);
  690.     xy_polar(x2-x1,y2-y1,&r2,&a2);
  691.     neg = 1;
  692.     a4 = (180-a2+a1);
  693.     a3 = a2 + (a4/2);
  694.     if ((a4/2)>90 && (a4/2)<180 ) neg = -1;
  695.     if ((a4/2)<0 && (a4/2)>-90 ) neg = -1;
  696.     r3 = neg*rrr/(tan((pi/180)*a4/2));
  697.     dbg gprint("rrr %g a4/2 %g t=%g a2=%g a1=%g r1=%g r2=%g r3=%g \n",rrr,a4/2,tan(a4/2),a2,a1,r1,r2,r3);
  698.     polar_xy(-r3,a1,&sx,&sy); sx += x1; sy += y1;
  699.     polar_xy(r3,a2,&ex,&ey); ex += x1; ey += y1;
  700.     g_line(sx,sy);
  701.     dist = sqrt(pow(ex-sx,2) + pow(ey-sy,2));
  702.     polar_xy(r1+ dist/2.5-r3,a1,&bx1,&by1); bx1 += x0; by1 += y0;
  703.     polar_xy(-r2+ -dist/2.5+r3,a2,&bx2,&by2); bx2 += x2; by2 += y2;
  704.     g_bezier(bx1,by1,bx2,by2,ex,ey);
  705.     g_line(x2,y2);
  706. }
  707. /*---------------------------------------------------------------------------*/
  708. d_arc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  709. {
  710.     static int ixr,iyr,icx,icy;
  711.     double ux,uy,z;
  712.     dxy(cx,cy,&icx,&icy);
  713.     rxy(r,r,&ixr,&iyr);
  714.     if (t1>t2) {z = t1; t1 = t2; t2 = z;}
  715.     /* arc(icx,icy,(int) t1, (int) t2, ixr); */
  716.     ellipse(icx,icy,(int) t1, (int) t2, ixr, iyr);
  717. }
  718. /*---------------------------------------------------------------------------*/
  719. d_narc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  720. {
  721.     static int ixr,iyr,icx,icy;
  722.     double ux,uy,z;
  723.     dxy(cx,cy,&icx,&icy);
  724.     rxy(r,r,&ixr,&iyr);
  725.     if (t1>t2) {z = t1; t1 = t2; t2 = z;}
  726.     /* arc(icx,icy,(int) t1, (int) t2, ixr);*/
  727.     ellipse(icx,icy,(int) t1, (int) t2, ixr, iyr);
  728. }
  729. /*---------------------------------------------------------------------------*/
  730. d_box_fill(dbl x1, dbl y1, dbl x2, dbl y2)
  731. {
  732.     static int ix1,iy1,ix2,iy2;
  733.     static int ii;
  734.     stop;gon;
  735.     dxy(x1,y1,&ix1,&iy1);
  736.     dxy(x2,y2,&ix2,&iy2);
  737.     if (ix1>ix2) { ii = ix1; ix1 = ix2; ix2 = ii; }
  738.     if (iy1>iy2) { ii = iy1; iy1 = iy2; iy2 = ii; }
  739.     bar(ix1,iy1,ix2,iy2);
  740.     goff;
  741. }
  742. d_box_stroke(dbl x1, dbl y1, dbl x2, dbl y2)
  743. {
  744.     static int ix1,iy1,ix2,iy2;
  745.     static int ii;
  746.     stop;gon;
  747.     dxy(x1,y1,&ix1,&iy1);
  748.     dxy(x2,y2,&ix2,&iy2);
  749.     if (ix1>ix2) { ii = ix1; ix1 = ix2; ix2 = ii; }
  750.     if (iy1>iy2) { ii = iy1; iy1 = iy2; iy2 = ii; }
  751. /*     rectangle(ix1,iy1,ix2,iy2); */
  752.     moveto(ix1,iy1);
  753.     lineto(ix2,iy1);
  754.     lineto(ix2,iy2);
  755.     lineto(ix1,iy2);
  756.     lineto(ix1,iy1);
  757.     goff;
  758. }
  759. /*---------------------------------------------------------------------------*/
  760. d_circle_stroke(double zr)
  761. {
  762.     static int ixr,iyr;
  763.     int ux,uy;
  764.     rxy(zr,zr,&ixr,&iyr);
  765.     dxy(g.curx,g.cury,&ux,&uy);
  766.     ellipse(ux,uy,0,360,ixr,iyr);
  767. }
  768. d_circle_fill(double zr)
  769. {
  770.     static int ixr,iyr;
  771.     int ux,uy;
  772.     dxy(g.curx,g.cury,&ux,&uy);
  773.     rxy(zr,zr,&ixr,&iyr);
  774.     fillellipse(ux,uy,ixr,iyr);
  775. }
  776. /*---------------------------------------------------------------------------*/
  777. d_bezier(dbl x1,dbl y1,dbl x2,dbl y2,dbl x3,dbl y3)
  778. {
  779.     double ax,bx,cx,ay,by,cy,dist;
  780.     double xxx,yyy,i,t,nstep,x0,y0;
  781.     g_get_xy(&x0,&y0);
  782.     dist = fabs(x3-x0) + fabs(y3-y0);
  783.     nstep = 12;
  784.     if (dist<1) nstep = 7;
  785.     if (dist<.5) nstep = 3;
  786.     if (dist<.1) {
  787.         g_line(x3,y3);
  788.         return;
  789.     }
  790.     cx = (x1-x0)*3;
  791.     bx = (x2-x1)*3-cx;
  792.     ax = x3-x0-cx-bx;
  793.     cy = (y1-y0)*3;
  794.     by = (y2-y1)*3-cy;
  795.     ay = y3-y0-cy-by;
  796.     for (i=0;i<=nstep;i++) {
  797.         t = i/nstep;
  798.         xxx = ax*pow(t,3.0) + bx*t*t + cx*t + x0;
  799.         yyy = ay*pow(t,3.0) + by*t*t + cy*t + y0;
  800.         g_line(xxx,yyy);
  801.     }
  802. }
  803. /*
  804. /* d_bezier(dbl zx1,dbl zy1,dbl zx2,dbl zy2,dbl zx3,dbl zy3)
  805. /* {
  806. /*     long  ax,bx,cx,ay,by,cy,dist;
  807. /*     long xxx,yyy,i,t,nstep;
  808. /*     long t2,t3;
  809. /*     int x0,y0,x1,y1,x2,y2,x3,y3;
  810. /*     double zx0,zy0;
  811. /*     g_get_xy(&zx0,&zy0);
  812. /*
  813. /*     dxy(zx1,zy1,&x1,&y1);
  814. /*     dxy(zx2,zy2,&x2,&y2);
  815. /*     dxy(zx3,zy3,&x3,&y3);
  816. /*     dxy(zx0,zy0,&x0,&y0);
  817. /*
  818. /*     x0 *= 1; y0 *= 1;
  819. /*     x1 *= 1; y1 *= 1;
  820. /*     x2 *= 1; y2 *= 1;
  821. /*     x3 *= 1; y3 *= 1;
  822. /*     nstep = 20;
  823. /*     cx = (x1-x0)*3;
  824. /*     bx = (x2-x1)*3-cx;
  825. /*     ax = x3-x0-cx-bx;
  826. /*     cy = (y1-y0)*3;
  827. /*     by = (y2-y1)*3-cy;
  828. /*     ay = y3-y0-cy-by;
  829. /*     t3 = 0;
  830. /*     for (i=0; i<=100; i += 10) {
  831. /*         t = i ;
  832. /*         t2 = (i*i)/100l;
  833. /*         t3 = (t2*i)/100l;
  834. /*         xxx = (ax*t3) + (bx*t2) + (cx*t) + ((long ) x0)*100l;
  835. /*         yyy = (ay*t3) + (by*t2) + (cy*t) + ((long ) y0)*100l;
  836. /*         lineto((int) (xxx/100l),(int) (yyy/100l));
  837. /*     }
  838. /* }
  839. */
  840. /*---------------------------------------------------------------------------*/
  841. d_set_color(long f)
  842. {
  843.     int i;
  844.     colortyp  cc;
  845.     stop;gon;
  846.     cc.l = f;
  847.     i = 0;
  848.     if (cc.b[B_R]>1 && cc.b[B_G]>1 && cc.b[B_B]>1) i = 8;
  849.     if (cc.b[B_R]>60 && cc.b[B_G]>60 && cc.b[B_B]>60) i = 7;
  850.     if (cc.b[B_R]>100) i = 12;
  851.     if (cc.b[B_B]>100) i = 9;
  852.     if (cc.b[B_G]>100) i = 10;
  853.     if (cc.b[B_R]>100 && cc.b[B_G]>100) i = 14;
  854.     if (cc.b[B_G]>100 && cc.b[B_B]>100) i = 5;
  855.     if (cc.b[B_R]>100 && cc.b[B_G]>100 && cc.b[B_B]>100) i = 7;
  856.     if (i>=getwhite()) i = 0;
  857.     if (cc.b[B_R]>250 && cc.b[B_G]>250 && cc.b[B_B]>250) i = getwhite();
  858.     setcolor(i); goff;
  859. }
  860. d_set_fill(long f)
  861. {
  862.     int i;
  863.     colortyp  cc;
  864.     stop;gon;
  865.     cc.l = f;
  866.     i = 0;
  867.     if (cc.b[B_R]>1 && cc.b[B_G]>1 && cc.b[B_B]>1) i = 8;
  868.     if (cc.b[B_R]>60 && cc.b[B_G]>60 && cc.b[B_B]>60) i = 7;
  869.     if (cc.b[B_R]>100) i = 12;
  870.     if (cc.b[B_B]>100) i = 9;
  871.     if (cc.b[B_G]>100) i = 10;
  872.     if (cc.b[B_R]>100 && cc.b[B_G]>100) i = 14;
  873.     if (cc.b[B_G]>100 && cc.b[B_B]>100) i = 5;
  874.     if (cc.b[B_R]>100 && cc.b[B_G]>100 && cc.b[B_B]>100) i = 7;
  875.     if (i>=getwhite()) i = 0;
  876.     if (cc.b[B_R]>250 && cc.b[B_G]>250 && cc.b[B_B]>250) i = getwhite();
  877.     d_fillcolor = i;
  878.     setfillstyle(d_fillstyle,d_fillcolor);
  879.     goff;
  880. }
  881. /*---------------------------------------------------------------------------*/
  882. d_beginclip()
  883. {
  884. }
  885. d_endclip()
  886. {
  887. }
  888. struct char_data {float wx,wy,x1,y1,x2,y2; };
  889. int font_get_chardata(struct char_data **cd, int ff, int cc);
  890. /*---------------------------------------------------------------------------*/
  891. int safnt;
  892. int simple_char(int cc);
  893. d_char(int font, int cc)
  894. {
  895.     static struct char_data cd;
  896.     static int ix1,ix2,iy1,iy2;
  897.     static int ux,uy;
  898.     char ss[2];
  899.  
  900.     ss[0] = cc;
  901.     ss[1] = 0;
  902.     if (safnt==0) safnt = pass_font("PLSR");
  903.     if (font!=82 && font>7) {
  904.         my_char(font,cc);
  905.         return;
  906.     }
  907.     if (gunit) simple_char(cc);
  908.     else  my_char(safnt,cc);
  909. }
  910.  
  911. int pass_font(char *s);
  912. int draw_char_fixed(char *s);
  913. int get_char_pcode(int ff, int cc, char **pp);
  914. simple_char(int cc)
  915. {
  916.     char *pp;
  917.     get_char_pcode(safnt,cc,&pp);
  918.     draw_char_fixed(pp);
  919. }
  920. int frxi(char **s);
  921. draw_char_fixed(char *s)
  922. {
  923.     int ux,uy,cx,cy,x1,y1,x2,y2,lx,ly;
  924.     long xsclm,ysclm,xscld,yscld;
  925.     double ox,oy;
  926.     dxy(g.curx,g.cury,&ux,&uy);
  927.     xsclm = g.fontsz*d_xscale; xscld = 1000;
  928.     ysclm = g.fontsz*d_yscale; yscld = 1000;
  929.     while (*s!=15) {
  930.       switch (*s++) {
  931.         case 1:
  932.         cx = frxi(&s); cy = frxi(&s);
  933.         moveto(ux + (cx*xsclm)/xscld, uy - (cy*ysclm)/yscld );
  934.         lx = cx; ly = cy;
  935.         break;
  936.         case 2:
  937.         cx = cx + frxi(&s); cy = cy + frxi(&s);
  938.         lineto(ux + (cx*xsclm)/xscld, uy - (cy*ysclm)/yscld );
  939.         break;
  940.         case 3:
  941.         cx = cx + frxi(&s); cy = cy + frxi(&s);
  942.         x1 = cx; y1 = cy;
  943.         cx = cx + frxi(&s); cy = cy + frxi(&s);
  944.         x2 = cx; y2 = cy;
  945.         cx = cx + frxi(&s); cy = cy + frxi(&s);
  946.         g_bezier(x1,y1,x2,y2,cx,cy);
  947.         break;
  948.         case 4:
  949.         lineto(ux + (lx*xsclm)/xscld, uy - (ly*ysclm)/yscld );
  950.         break;
  951.         case 5:
  952.         case 6:
  953.         case 7:
  954.         case 8:
  955.         break;
  956.         case 0: /* no char in this font */
  957.         goto abort;
  958.         default:
  959.         gprint("Error in mychar pcode %d \n",*s++);
  960.         goto abort;
  961.       }
  962.  
  963.     }
  964. abort:;
  965. }
  966.