home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / dots151.zip / GRAPHSRC.ZIP / GTC.C < prev    next >
C/C++ Source or Header  |  1990-07-08  |  9KB  |  314 lines

  1. /*
  2.     gtc - graphics driver using Turbo C graphics functions
  3. */
  4. #define INCLUDING
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <graphics.h>            /* Turbo C header file */
  10. #include "g.h"                    /* portable CORE device driver header file */
  11.  
  12.  
  13. /* LOCAL DEFINITIONS */
  14.  
  15.  
  16. /* Constants */
  17.  
  18. /*    pointers to optional functions (NULL if not implemented)    */
  19.  
  20. int    (*new_linewidth)(int)=0;        /* (*new_linewidth)(width) int width; */
  21. int    (*new_linestyle)(int)=0;        /* (*new_linestyle)(style) int style; */
  22. int    (*new_charsize)(int, int)=0;    /* (*new_charsize)(w,h) int w,h; */
  23. int    (*draw_marker)(int)=0;            /* (*draw_marker)(n) int n; */
  24.  
  25. /*    exported graphics variables    */
  26.  
  27. char *machine        =    "BGI";
  28. char *interface_version    =    ".91";
  29. char *config_file    =    NULL;
  30. int plotting_device    =    0;
  31. int erasing            =    1;
  32. int flipping        =    1;
  33. int max_color        =    1;
  34. int current_color    =    0;
  35. int current_background_color = 0;
  36. int pen_diameter    =    1;
  37.                     /* height/width parameters of screen */
  38. int pixels_wide        =    720;
  39. int pixels_high        =    348;
  40. double best_width    =    1.;
  41. double best_height    =    .652;
  42.                     /* text parameters */
  43. int char_height        =    12;    /* includes blank scans */
  44. static font_height    =    9;    /* no blank scans */
  45. int char_width        =    8;
  46. int char_rows        =    38;
  47. int char_columns    =    90;
  48. int x_offset        =    0;
  49. int y_offset        =    8;
  50. int    char_v_adjusted    =    1;
  51. int    char_h_adjusted    =    0;
  52. struct textsettingstype ts;        /* Borland's text info structure */
  53.  
  54.                     /* cursor key codes */
  55. int    up_arrow    =    0x48;
  56. int    down_arrow    =    0x50;
  57. int    left_arrow    =    0x4b;
  58. int    right_arrow    =    0x4d;
  59. int    escaped_arrows    =    1;    /* cursor keys are preceded by 0 */
  60. int    escape_char    =    0;
  61.  
  62. struct PRIM_ATTR prim_attr;
  63.  
  64. static int cursor_x=0, cursor_y=0;    /* pixel loc. of cursor
  65.                  (lower left corner of char) */
  66. static char active_page=0;        /* 0 for page at b000,
  67.                                     1 for page at b800  */
  68. static char graph_status;        /* value saved from 40:0049 */
  69.  
  70. /*    set_color - set color to be used for subsequent primitives
  71.     0 -> white     prim_attr.color_count-1 -> black  */
  72. set_color(color) int color;
  73. {
  74.     color = max_color - color&max_color;
  75.     setcolor(color);
  76.     current_color=color; 
  77. }    
  78. set_background_color(color) int color; 
  79.     {current_background_color = max_color - color&max_color;
  80.     }
  81. set_intensity(intensity) double intensity;
  82. {    int i;            /* 0->black  1->white */
  83.     if(intensity > 1.) intensity = 1.;
  84.     i=(int)(intensity*prim_attr.color_count);
  85.     if(i<0) i=0;
  86.     if(i > max_color) i = max_color;
  87.     set_color(max_color - i);
  88. }
  89. set_background_intensity(intensity) double intensity;
  90. {    int i;
  91.     if(intensity > 1.) intensity = 1.;
  92.     i=(int)(intensity*prim_attr.color_count);
  93.     if(i<0) i=0;
  94.     if(i > max_color) i = max_color;
  95.     set_background_color(max_color - i);
  96. }
  97.  
  98. inquire_color() {return current_color;}
  99. double inquire_intensity() {return current_color/(double)max_color;}
  100.  
  101.  
  102. /*    clear_graphics - clear screen  */
  103. clear_graphics()
  104. {    clearviewport();
  105. }
  106.  
  107. /*    gotoxy - direct cursor positioning
  108.         (set x and y pixel position of lower left corner of char,
  109.         can be off the screen)     */
  110. gotoxy(x,y) int x,y;
  111. {    cursor_x=x; cursor_y=y;
  112.     moveto(x,y);
  113. }
  114.  
  115.  
  116. /*    Draw a straight line  */
  117.  
  118. static draw2(x1,y1, x2,y2)
  119. int    x1,y1;        /* Coordinates of first point */
  120. int    x2,y2;        /* Coordinates of last point */
  121. {
  122.     line(x1,y1, x2,y2);
  123. }
  124.  
  125. /*  flip bits along a line */
  126.  
  127. static flip2(x1,y1, x2,y2)
  128. int    x1,y1;        /* Coordinates of first point */
  129. int    x2,y2;        /* Coordinates of last point */
  130. {
  131.     setwritemode(1);    /* XOR mode */
  132.     line(x1,y1, x2,y2);
  133.     setwritemode(0);    /* MOV mode */
  134. }
  135.  
  136.  
  137. /*     Erase line    */
  138.  
  139. static erase2(x1,y1, x2,y2)
  140. int    x1,y1;        /* Coordinates of first point */
  141. int    x2,y2;        /* Coordinates of last point */
  142. {
  143.     setcolor(0);
  144.     line(x1,y1, x2,y2);
  145.     setcolor(current_color);
  146. }
  147.  
  148. static puts2(s) char *s;
  149. {    char far *sp;
  150.     if(strchr(s, '\n') == NULL)
  151.         {sp = s;
  152.         cursor_x = getx();
  153.         cursor_y = gety();
  154. #ifdef INCLUDING
  155.         if(cursor_y<font_height) cursor_y=font_height;
  156.         else if(cursor_y>=pixels_high) cursor_y=pixels_high-1;
  157.         if(cursor_x<0) cursor_x=0;
  158.         else if(cursor_x+char_width>=pixels_wide)
  159.             cursor_x=pixels_wide-char_width;
  160. #endif
  161.         setfillstyle(SOLID_FILL, current_background_color);
  162.         bar(cursor_x, cursor_y, cursor_x + textwidth(sp), 
  163.                                                     cursor_y - char_height);
  164. /*
  165. */
  166.         outtext(sp);
  167.         }
  168.     else
  169.         while(*s) 
  170.             putchar2(*s++);
  171. }
  172.     
  173. static putchar2(c) int c;
  174. {
  175.     static char s[2] = "x";
  176.     static char far *sp = s;
  177.  
  178.     if(c=='\n') 
  179.         gotoxy(0, gety() + char_height);
  180.     else
  181.         {s[0] = c;
  182.         cursor_x = getx();
  183.         cursor_y = gety();
  184. #ifdef INCLUDING
  185.         if(cursor_y<font_height) cursor_y=font_height;
  186.         else if(cursor_y>=pixels_high) cursor_y=pixels_high-1;
  187.         if(cursor_x<0) cursor_x=0;
  188.         else if(cursor_x+char_width>=pixels_wide)
  189.             cursor_x=pixels_wide-char_width;
  190.         moveto(cursor_x, cursor_y);
  191. #endif
  192.         setfillstyle(SOLID_FILL, current_background_color);
  193.         bar(cursor_x, cursor_y, cursor_x + textwidth(sp), 
  194.                                                     cursor_y - char_height);
  195. /*
  196. */
  197.         outtext(sp);
  198.         }
  199. }
  200.  
  201. static int huge Always_One( void )
  202. {
  203.   return( 1 );
  204. }
  205.  
  206. /*    init_graphics - initialize hardware for graphics */
  207. init_graphics()
  208. {    int usermode, graphmode, graphdriver, xasp, yasp, lomode, himode;
  209.     static int vga_scan_lines[] = {200, 350, 480};
  210.     char *bgi_path, *s, *t, *u, buf[40];
  211.     extern char **_argv;        /* global variable supplied by Turbo C */
  212.  
  213.     s = getenv("BGIDRIVER");
  214.     if(s == NULL) graphdriver = DETECT;
  215.     else graphdriver = installuserdriver( s, Always_One );
  216.  
  217.     usermode = -7;
  218.     s = getenv("GRAPHMODE");
  219.     if(s != NULL) usermode = atoi(s);
  220.  
  221.     s = getenv("BGIDIR");
  222.     if(s==NULL || *s == 0) s = getenv("PATH");
  223.     bgi_path = strncpy(buf, _argv[0], 39);
  224.     if(bgi_path != NULL)
  225.         {        /* delete program name to yield path to program (with '\') */
  226.         for (u = t = bgi_path; *u; u++)
  227.             if(*u == '\\')
  228.                 t = u;
  229.         if(t > bgi_path) t[1] = 0;
  230.         }
  231.  
  232.     while(1)
  233.         {
  234. /*        printf("bgi_path=%s\n", (bgi_path==NULL)?"NULL":bgi_path); getchar(); */
  235.         initgraph(&graphdriver, &graphmode, bgi_path);
  236.         if(graphdriver > 0) break;
  237. /*        printf("%s\n", grapherrormsg(graphresult())); */
  238.         if((s != NULL) && *s) bgi_path = s;
  239.         else 
  240.             {fprintf(stderr, grapherrormsg(graphresult())); 
  241.             exit(1);
  242.             }
  243.         if(s = strchr(bgi_path, ';')) {*s++ = 0;}
  244.         graphdriver = DETECT;
  245.         }
  246.  
  247.     getmoderange(graphdriver, &lomode, &himode);
  248.     if(usermode >= lomode && usermode <= himode && usermode != graphmode)
  249.         setgraphmode(usermode);
  250.     graphmode = getgraphmode();
  251.  
  252.     pixels_wide = getmaxx() + 1;
  253.     pixels_high = getmaxy() + 1;
  254.     if(graphdriver == 9)
  255.         pixels_high = vga_scan_lines[graphmode];
  256.  
  257.     max_color = getmaxcolor();
  258.     set_color(0);                            /* white */
  259.     set_background_color(max_color);        /* black */
  260.  
  261.     settextjustify(LEFT_TEXT, BOTTOM_TEXT);
  262.     gettextsettings(&ts);
  263. /*    if(ts.font) */
  264.         {char_width = textwidth("M");
  265.         font_height = textheight("Mj");
  266.         }
  267. /*    else */
  268.         {char_width = font_height = 8;
  269.         }
  270.     char_height = (font_height*3)/2;        /* allow for space between lines */
  271.  
  272.     char_rows = pixels_high/char_height;
  273.     char_columns = pixels_wide/char_width;
  274.  
  275.     y_offset = font_height;
  276.  
  277.     prim_attr.color_count = getmaxcolor() + 1;
  278.     prim_attr.intensity_count = 1;
  279.     prim_attr.intensities_in_hardware = 0;
  280.     prim_attr.hardware_linestyles = 0;       /* # linestyles in hardware          */
  281.     prim_attr.software_linestyles = 0;       /* # linestyles in software          */
  282.     prim_attr.linewidth_count = 0;           /* # linewidths                      */
  283.     prim_attr.linewidths_in_hardware = 0;    /* nonzero if supported in hardware  */
  284.     prim_attr.linewidth_minimum = 1;         /*                                   */
  285.     prim_attr.linewidth_maximum = 1;         /*                                   */
  286.     prim_attr.hardware_pens = 1;             /*                                   */
  287.     prim_attr.software_pens = 1;             /*                                   */
  288.     prim_attr.charfont_count = 1;            /* # fonts                           */
  289.     prim_attr.charsize_count = 1;            /* # character sizes                 */
  290.     prim_attr.charsize_in_hardware = 0;      /* nonzero if supported in hardware  */
  291.     prim_attr.charsize_minimum = font_height;          /*                                   */
  292.     prim_attr.charsize_maximum = font_height;          /*                                   */
  293.     prim_attr.hardware_markers = 0;          /* # markers in hardware             */
  294.     prim_attr.software_markers = 0;          /* # markers in software             */
  295.     prim_attr.pick_id_count = 0;             /* # pick IDs in hardware            */
  296.  
  297. }
  298.  
  299.  
  300. /*    finish_graphics - clean up after graphics */
  301. finish_graphics()
  302. {    closegraph();
  303. }
  304.  
  305.  
  306. /*    exported function pointers (declared & initialized here) */
  307.  
  308. int (*draw_line)()=draw2;
  309. int (*erase_line)()=erase2;
  310. int (*draw_char)()=putchar2;
  311. int (*draw_text)()=puts2;
  312. int (*flip_line)()=flip2;
  313.  
  314.