home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / ega / egapaint.arc / PAINT.C < prev    next >
Text File  |  1988-05-26  |  47KB  |  1,890 lines

  1. /**************************************************************************
  2.  *                                                                        *
  3.  *                               P A I N T                                *
  4.  *                                 V?.??                                  *
  5.  *                                                                        *
  6.  *                        By: Jim Hudson                                  *
  7.  *                            4600 Hillsboro Ave No                       *
  8.  *                            New Hope, MN    55428                       *
  9.  *                            612-533-4450                                *
  10.  *                                                                        *
  11.  *   All contributions, comments, improvements, praise, or whatever       *
  12.  *    will not be reported to the IRS and will, be greatly appreciated    *
  13.  *                                                                        *
  14.  **************************************************************************/
  15.  
  16.  
  17. #ifndef TRUE
  18. #define TRUE 1
  19. #endif
  20. #ifndef FALSE
  21. #define FALSE 0
  22. #endif
  23.  
  24. #define ON           1
  25. #define OFF          0
  26.  
  27. #define NO_MOUSE  0x80
  28. #define NOT_MOVED 0
  29. #define RIGHT     1
  30. #define LEFT      2
  31. #define UP          3
  32. #define DOWN      4
  33.  
  34. #define LEFTB      1
  35. #define RIGHTB      2
  36. #define CENTERB   4
  37.  
  38. #define XHOME      320
  39. #define YHOME      170
  40.  
  41. #define TIMED_OUT  300
  42. #define DELAY_TIME 1500        /* may need to be adjusted for processor speed */
  43. #define STR_LENGTH 80
  44. #define sqr(X) ((X) * (X))
  45. #define round(x) (int)((x)<0?-floor(-(x)+.5):floor((x)+.5))
  46.  
  47. #define len(A,B) ((A > B) ? (A - B) : (B - A))
  48.  
  49. #define F11        0x2300        /* machine dependent  */
  50. #define F12        0x2400
  51.  
  52. #define ENABLE 0x0f
  53. #define INDEXREG 0x3ce
  54. #define VALREG 0x3cf
  55. #define OUTINDEX(index, val) {outport(INDEXREG, index); outport(VALREG, val);}
  56. #define EGABASE 0xa0000000L
  57.  
  58. #include <dos.h>
  59. #include <stdio.h>
  60. #include <math.h>
  61. #include <bios.h>
  62. #include <conio.h>
  63. #include <ctype.h>
  64. #include <graphics.h>
  65. #include <alloc.h>
  66. #include <string.h>
  67. #include <process.h>
  68. #include <stdlib.h>
  69. #include <dir.h>
  70.  
  71. extern int paint_help(void);
  72. extern void lstyle(void);
  73. void get_keyboard(void), text_mode(void), print_text(void), v_print_text(void);
  74. void swap_page(void), save_pik(void), read_pik(void), get_scale(void);
  75. void vector(void), set_fill(void), delete_area(void);
  76. void xhair(int state), grid(int state);
  77. void help(void), capture(void), uncapture(void), check_flags(void);
  78. void p_message(char *str), xcircle(int state);
  79. void fill_box(void), fill_circle(void);
  80. void copy(void), move(void), save_pic(void), load_pic(void);
  81. void new_color(void), xrectangle(void), xellipse(void);
  82. void scr_restore(char *text), line_style(void);
  83. int scr_save(void), get_string(char *text, char name[]);
  84. void gprintf(int, int, char *fmt, ... );
  85. void wcomp(unsigned size, char *ptr, FILE *fd);
  86. void rcomp(unsigned size, char *ptr, FILE *fs);
  87. void pointf(int x, int y);
  88. void xline(int startx, int starty, int endx, int endy, int state);
  89. void xline1(int startx, int starty, int endx, int endy, int state);
  90. void xarc(int state);
  91. extern void printscr(int reverse);
  92. void center(double x[], double y[], double *xc, double *yc, double *r);
  93.  
  94. /* mouse stuff */
  95.  
  96. extern void cmouses(int *function, int *arg2, int *arg3, int *arg4);
  97.  
  98. void swap_mouse(void), set_limits(void);
  99. void set_mouse_position(void), get_mouse_position(void);
  100. void mouse_motion(char *dx, char *dy), wait_on(int button);
  101. int r_button_pressed(void), l_button_pressed(void), mouse_reset(void);
  102. int c_button_pressed(void);
  103.  
  104. char *color_num[] = {
  105.         "Black",
  106.         "Blue",
  107.         "Green",
  108.         "Cyan",
  109.         "Red",
  110.         "Magenta",
  111.         "Brown",
  112.         "Light Gray",
  113.         "Dark Gray",
  114.         "Light Blue",
  115.         "Light Green",
  116.         "Light Cyan",
  117.         "Light Red",
  118.         "Light Magenta",
  119.         "Yellow",
  120.         "White"
  121. };
  122. char *fill_num[] = {
  123.         "Empty ",
  124.         "Solid ",
  125.         "Line ",
  126.         "Light Slash ",
  127.         "Heavy Slash ",
  128.         "Back Slash ",
  129.         "Lt Back Slash ",
  130.         "Hatch ",
  131.         "Heavy Hatch ",
  132.         "Interleave ",
  133.         "Wide Dot ",
  134.         "Close Dot "
  135. };
  136. char *line_width[] = {
  137.         "Narrow",
  138.         "      ",
  139.         "      ",
  140.         "wide"
  141. };
  142.  
  143. int bkcolor = LIGHTGRAY, cc = BLUE, fill_style = SOLID_FILL, msg_color;
  144. int on_flag = FALSE, cap_flag = FALSE, first_point = TRUE, time_out = TIMED_OUT;
  145. int first_flag = FALSE, second_flag = FALSE, kb_flag = TRUE, xhair_flag = FALSE;
  146. int maxx = 635, maxy = 349, minx = 5, miny = 20, inc = 10, x, y;
  147. int header = TRUE, page = 0, height, text_ok = 0, text_size = 5;
  148. int startx = 0, starty = 0, endx = 0, endy = 0, gf = FALSE;
  149. void *xhair_buf, *first, *second, *arc1;
  150. unsigned size, c_size;
  151. unsigned char *c_buffer;
  152. struct textsettingstype oldtext;
  153. char text_line[80], oldc;
  154. int cb_flag = FALSE, width = 0;
  155. double scale = 1.000;
  156. int grid_flag = OFF, arc_mode = 0, arc_count = 0;
  157.  
  158. /* more mouse stuff */
  159.  
  160. int mouse_flag = FALSE;        /* assume on */
  161. int function = 0, arg2 = 0, arg3 = 0, arg4 = 0;
  162. char deltax, deltay;        /* make sure this is a signed value  */
  163.  
  164. /* This aspect ratio is preset because that's the way my monitor works   */
  165. /* properly, 'getaspectratio(&xasp, &yasp)' is called to return 7750 and */
  166. /* 10000 respectivly.  Either use the system call or adjust the xasp for */
  167. /* your needs.                                                             */
  168.  
  169. int xasp = 7180, yasp = 10000;
  170.  
  171. union k {
  172.     char c[2];
  173.     int  i;
  174. } key;
  175.  
  176. struct linesettingstype oldlinestyle;
  177.  
  178. void main()
  179. {
  180.     char info_line[120], temp[10], *info_ptr;
  181.     register int i, oldx, oldy;
  182.     int Gdriver = DETECT, Gmode;
  183.  
  184.     startx = starty = endx = endy = 0;
  185.  
  186.     if(registerfarbgidriver(EGAVGA_driver_far) < 0) exit(1);
  187.     if(registerfarbgifont(small_font_far) < 0) exit(1);
  188.     if(registerfarbgifont(triplex_font_far) < 0) exit(1);
  189.  
  190.     initgraph(&Gdriver, &Gmode, "");
  191.     if(getgraphmode() != EGAHI) {
  192.         restorecrtmode();
  193.         printf("\n\nI'm sorry, but this program ONLY works with an EGA system\n\n");
  194.         exit(0);
  195.     }
  196.     gettextsettings(&oldtext);
  197. /*    getaspectratio(&xasp, &yasp);    */
  198.     height = textheight("H");
  199.     oldx = x = XHOME;
  200.     oldy = y = YHOME;
  201.     setbkcolor(bkcolor);
  202.  
  203.     if(mouse_reset()) mouse_flag = NO_MOUSE;
  204.     else {
  205.         set_mouse_position();
  206.         set_limits();
  207.     }
  208.     setcolor(LIGHTGREEN);    /* first point marker  */
  209.     line(x-2, y, x, y-2);
  210.     line(x, y-2, x+2, y);
  211.     line(x+2, y, x, y+2);
  212.     line(x, y+2, x-2, y);
  213.     line(x-7, y, x-4, y);
  214.     line(x+4, y, x+7, y);
  215.     line(x, y-7, x, y-4);
  216.     line(x, y+4, x, y+7);
  217.     size = imagesize(x-7, y-7, x+7, y+7);
  218.     first = malloc(size);
  219.     getimage(x-7, y-7, x+7, y+7, first);
  220.     putimage(x-7, y-7, first, XOR_PUT);
  221.  
  222.     line(x-3, y-3, x+3, y-3);     /* second point marker */
  223.     line(x+3, y-3, x+3, y+3);
  224.     line(x+3, y+3, x-3, y+3);
  225.     line(x-3, y+3, x-3, y-3);
  226.     size = imagesize(x-3, y-3, x+3, y+3);
  227.     second = malloc(size);
  228.     getimage(x-3, y-3, x+3, y+3, second);
  229.     putimage(x-3, y-3, second, XOR_PUT);
  230.  
  231.     line(x-4, y-3, x+4, y+3);
  232.     line(x+4, y-3, x-4, y+3);
  233.     size = imagesize(x-4, y-3, x+4, y+3);
  234.     arc1 = malloc(size);
  235.     getimage(x-4, y-3, x+4, y+3, arc1);
  236.     putimage(x-4, y-3, arc1, XOR_PUT);
  237.  
  238.     setcolor(DARKGRAY);
  239.     line(x - 5, y, x + 4, y);    /* cross hair marker  */
  240.     line(x, y + 5, x, y - 4);
  241.     size = imagesize(x-5, y-4, x+4, y+5);
  242.     xhair_buf = malloc(size);
  243.     getimage(x-5, y-4, x+4, y+5, xhair_buf);
  244.     putimage(x-5, y-4, xhair_buf, XOR_PUT);
  245.  
  246.     while(TRUE){          /* we exit this loop when we quit         */
  247.         if(kb_flag) {     /* only paint the header if a key was hit */
  248.             if(bkcolor == cc) cc = 0;
  249.             if(header) {
  250.                 setcolor(bkcolor);
  251.                 setviewport(0, 0, maxx, 9, 1);
  252.                 clearviewport();
  253.                 if (bkcolor == BLACK        || bkcolor == BLUE     ||
  254.                     bkcolor == RED          || bkcolor == MAGENTA  ||
  255.                     bkcolor == BROWN        || bkcolor == DARKGRAY ||
  256.                     bkcolor == LIGHTBLUE    || bkcolor == LIGHTRED ||
  257.                     bkcolor == LIGHTMAGENTA || bkcolor == GREEN)
  258.                     msg_color = WHITE;
  259.                 else msg_color = BROWN;
  260.                 setcolor(msg_color);
  261.                 setviewport(0, 0, maxx, maxy, 1);
  262.  
  263.                 /* build the information line, this is slower than just
  264.                    addessing unique X coordinates but with all the variables
  265.                    I think it comes out alittle better.  Notice the use
  266.                    of 'stpcpy' - this updates the pointer where 'strcpy'
  267.                    does not.
  268.                 */
  269.  
  270.                 info_ptr = info_line;
  271.                 info_ptr = stpcpy(info_ptr, "X: ");
  272.                 itoa(x, temp, 10);
  273.                 info_ptr = stpcpy(info_ptr, temp);
  274.                 info_ptr = stpcpy(info_ptr, " Y: ");
  275.                 itoa(y, temp, 10);
  276.                 info_ptr = stpcpy(info_ptr, temp);
  277.                 info_ptr = stpcpy(info_ptr, on_flag ? " Pen Down " : " Pen Up ");
  278.                 if(second_flag) info_ptr = stpcpy(info_ptr, "Second Point ");
  279.                 else if(first_flag) info_ptr = stpcpy(info_ptr, "First Point ");
  280.                 info_ptr = stpcpy(info_ptr, "Inc = ");
  281.                 itoa(inc, temp, 10);
  282.                 info_ptr = stpcpy(info_ptr, temp);
  283.                 if(mouse_flag != NO_MOUSE) {
  284.                     info_ptr = stpcpy(info_ptr, mouse_flag ? " Mouse Off" : " Mouse On");
  285.                 }
  286.                 strcpy(info_ptr, " Color: ");
  287.                 gprintf(5, 1, "%s", info_line);
  288.                 gprintf(580, 1, "Page %d", page+1);
  289.                 setcolor(cc);
  290.                 gprintf(textwidth(info_line), 1, "%s", color_num[cc]);
  291.                 if(!(arc_mode || text_ok)) p_message("  ");
  292.                 miny = 20;
  293.                 setcolor(msg_color);
  294.                 if(!(first_point || text_ok)) {
  295.                     gprintf(275, height+2,
  296.                         "Delta X %+6.3f Delta Y %+6.3f Delta XY %+6.3f",
  297.                         ((x-startx)/66.000) * scale, ((y-starty)/47.000) * scale,
  298.                         hypot(((x-startx)/66.000) * scale, ((y-starty)/47.000) * scale));
  299.                 }
  300.                 setcolor(cc);
  301.             } else miny = 10;
  302.             setviewport(0, 0, maxx, maxy, 1);
  303.             setcolor(cc);
  304.             xhair(ON);
  305.             kb_flag = FALSE;
  306.             set_mouse_position();
  307.         }
  308.         if(bioskey(1)) get_keyboard();
  309.         if(!mouse_flag) {                /* process the mouse movements */
  310.             /* if center button -- do last function  */
  311.             if(c_button_pressed()) {
  312.                 wait_on(CENTERB);
  313.                 if(key.c[0] >= 'a' && key.c[0] <= 'z') {
  314.                     cb_flag = TRUE;
  315.                     get_keyboard();
  316.                 }
  317.             }
  318.             /* right button defines endpoints  */
  319.             if(r_button_pressed()) {
  320.                 if(first_point) {
  321.                     if(first_flag) check_flags();
  322.                     startx = x;
  323.                     starty = y;
  324.                     putimage(x-7, y-7, first, XOR_PUT);
  325.                     first_flag = TRUE;
  326.                 }
  327.                 else {
  328.                     if(second_flag) check_flags();
  329.                     endx = x;
  330.                     endy = y;
  331.                     putimage(x-3, y-3, second, XOR_PUT);
  332.                     second_flag = TRUE;
  333.                 }
  334.                 first_point = !first_point;
  335.                 wait_on(RIGHTB);
  336.             }
  337.             oldx = x; oldy = y;
  338.             mouse_motion(&deltax, &deltay);
  339.             if(deltax || deltay) {
  340.                 xhair(OFF);
  341.                 get_mouse_position();
  342.                 if(l_button_pressed()) xline(oldx, oldy, x, y, 0);
  343.                 time_out = 0;
  344.             } else time_out++;
  345.             if(time_out == TIMED_OUT) {
  346.                 kb_flag = TRUE;
  347.             }
  348.             xhair(ON);
  349.         }
  350.     }
  351. }
  352.  
  353. void get_keyboard(void)
  354. {
  355.         kb_flag = TRUE;                     /* make sure the header gets painted     */
  356.         if(!cb_flag) key.i = bioskey(0); /* cb_flag says the center button on the */
  357.         cb_flag = FALSE;                 /* mouse was hit - repeat last command   */
  358.         xhair(OFF);
  359.         if (!key.c[0]) switch(key.c[1]) {
  360.             case 75:                        /* left */
  361.                 if((x - inc) < minx) break;
  362.                 if (on_flag) xline(x, y, x-inc, y, 0);
  363.                 x -= inc;
  364.                 break;
  365.             case 77:                        /* right */
  366.                 if((x + inc) > maxx) break;
  367.                 if (on_flag) xline(x, y, x+inc, y, 0);
  368.                 x += inc;
  369.                 break;
  370.             case 72:                        /* up */
  371.                 if((y - inc) < miny) break;
  372.                 if (on_flag) xline(x, y, x, y-inc, 0);
  373.                 y -= inc;
  374.                 break;
  375.             case 80:                        /* down */
  376.                 if((y + inc) > maxy) break;
  377.                 if (on_flag) xline(x, y, x, y+inc, 0);
  378.                 y += inc;
  379.                 break;
  380.             case 71:                        /* up left */
  381.                 if((x - inc) < minx || (y - inc) < miny) break;
  382.                 if (on_flag) xline(x, y, x-inc, y-inc, 0);
  383.                 x -= inc; y -= inc;
  384.                 break;
  385.             case 73:                        /* up right */
  386.                 if((x + inc) > maxx || (y - inc) < miny) break;
  387.                 if (on_flag) xline(x, y, x+inc, y-inc, 0);
  388.                 x += inc; y -= inc;
  389.                 break;
  390.             case 79:                        /* down left */
  391.                 if((x - inc) < minx || (y + inc) > maxy) break;
  392.                 if (on_flag) xline(x, y, x-inc, y+inc, 0);
  393.                 x -= inc; y += inc;
  394.                 break;
  395.             case 81:                        /* down right */
  396.                 if((x + inc) > maxx || (y + inc) > maxy) break;
  397.                 if (on_flag) xline(x, y, x+inc, y+inc, 0);
  398.                 x += inc; y += inc;
  399.                 break;
  400.             case 59:                /* F1 */
  401.                 if(text_ok) {
  402.                     print_text();
  403.                     break;
  404.                 }
  405.                 inc = 1;
  406.                 break;
  407.             case 60:                /* F2 */
  408.                 if(text_ok) {
  409.                     v_print_text();
  410.                     break;
  411.                 }
  412.                 inc = 5;
  413.                 break;
  414.             case 61: inc = 10;      /* F3 */
  415.                 break;
  416.             case 62: inc = 20;      /* F4 */
  417.                 break;
  418.             case 63:                /* F5 - Change background color */
  419.                 bkcolor++;
  420.                 if(bkcolor >= MAXCOLORS) bkcolor = 0;
  421.                 setbkcolor(bkcolor);
  422.                 break;
  423.             case 64:                /* F6 - Save an icon to a file */
  424.                 save_pik();
  425.                 break;
  426.             case 65:                  /* F7 - Restore the icon from a file */
  427.                 read_pik();
  428.                 break;
  429.             case 66:                  /* F8 - Clear the first and second flags */
  430.                 check_flags();
  431.                 break;
  432.             case 67:                /* F9 - Turn info lines on/off */
  433.                 if(header) {
  434.                     p_message("WARNING --- ALL messages will be turned off");
  435.                     delay(DELAY_TIME);
  436.                     setcolor(bkcolor);
  437.                     setviewport(0, 0, maxx, 19, 1);
  438.                     clearviewport();
  439.                     setviewport(0, 0, maxx, maxy, 1);
  440.                     setcolor(cc);
  441.                 }
  442.                 header ^= 1;
  443.                 set_limits();
  444.                 break;
  445.             case 68:                /* F10 - Turn mouse ON/OFF    */
  446.                 swap_mouse(); break;
  447.             case (F11 >> 8):        /* F11 - Change FILL pattern  */
  448.                 set_fill(); break;
  449.             case (F12 >> 8):        /* F12 - Change line style    */
  450.                 line_style(); break;
  451.             case 83:                  /* DELETE key homes cursor    */
  452.                 x = XHOME; y = YHOME; break;
  453.             case 119:
  454.                 x = 10; y = miny + 10; break;
  455.             case -124:
  456.                 x = 630; y = miny + 10; break;
  457.             case 115:
  458.                 x = 10; break;
  459.             case 116:
  460.                 x = 630; break;
  461.             case 117:
  462.                 x = 10; y = 340; break;
  463.             case 118:
  464.                 x = 630; y = 340; break;
  465.             }
  466.         else if(!text_ok) switch(tolower(key.c[0])) {
  467.             case '1': cc = 1; break;
  468.             case '2': cc = 2; break;
  469.             case '3': cc = 3; break;
  470.             case '4': cc = 4; break;
  471.             case '5': cc = 5; break;
  472.             case '6': cc = 6; break;
  473.             case '7': cc = 7; break;
  474.             case '8': cc = 8; break;
  475.             case '9': cc = 9; break;
  476.             case '0': cc = 10; break;
  477.             case '!': cc = 11; break;
  478.             case '@': cc = 12; break;
  479.             case '#': cc = 13; break;
  480.             case '$': cc = 14; break;
  481.             case '%': cc = 15; break;
  482.             case '?': help(); break;
  483.             case 'a': xarc(FALSE); break;
  484.             case 'b': xrectangle(); break;
  485.             case 'c': xcircle(TRUE); break;
  486.             case 'd': delete_area(); break;
  487.             case 'e': xellipse(); break;
  488.             case 'f': break;
  489.             case 'g': grid_flag ? grid(OFF) : grid(ON); break;
  490.             case 'h': break;
  491.             case 'j': capture(); break;
  492.             case 'k': uncapture(); break;
  493.             case 'l': xline(startx, starty, endx, endy, 1); break;
  494.             case 'm': move(); break;
  495.             case 'n': new_color(); break;
  496.             case 'o': on_flag = !on_flag; break;
  497.             case 'p': swap_page(); break;
  498.             case 'p' - 0x60: printscr(0); break;
  499.             case 'q':
  500.                 p_message("Do you really want to quit [Y/N]");
  501.                 key.i = bioskey(0);
  502.                 if(tolower(key.c[0]) == 'y') {
  503.                     closegraph();
  504.                     restorecrtmode();
  505.                     /* free any allocated buffers */
  506.                     free(first); free(second); free(xhair_buf);
  507.                     free(c_buffer);
  508.                     exit(0);
  509.                 }
  510.                 break;
  511.             case 'r': load_pic(); break;
  512.             case 's': check_flags(); save_pic(); break;
  513.             case 's' - 0x60: get_scale(); break;
  514.             case 't': text_mode(); break;
  515.             case 'w':
  516.                 setfillstyle(fill_style, cc);
  517.                 floodfill(x, y, cc);
  518.                 break;
  519.             case 'v': vector(); break;
  520.             case 'x': copy(); break;
  521.             case 'z':
  522.                 p_message("Attempt to clear display, Hit \"z\" again");
  523.                 key.i = bioskey(0);
  524.                 if(tolower(key.c[0]) == 'z') {
  525.                     if(grid_flag) {
  526.                         grid(OFF);
  527.                         gf = TRUE;
  528.                     } else gf = FALSE;
  529.                     check_flags();
  530.                     clearviewport();
  531.                     x = XHOME;
  532.                     y = YHOME;
  533.                     if(gf) grid(ON);
  534.                 }
  535.                 break;
  536.             case '\r':
  537.                 if(arc_mode) {
  538.                     xarc(TRUE);
  539.                     break;
  540.                 }
  541.                 if(first_point) {
  542.                     if(first_flag) check_flags();
  543.                     startx = x;
  544.                     starty = y;
  545.                     putimage(x-7, y-7, first, XOR_PUT);
  546.                     first_flag = TRUE;
  547.                 }
  548.                 else {
  549.                     if(second_flag) check_flags();
  550.                     endx = x;
  551.                     endy = y;
  552.                     putimage(x-3, y-3, second, XOR_PUT);
  553.                     second_flag = TRUE;
  554.                 }
  555.                 first_point = !first_point;
  556.                 break;
  557.             }
  558. }
  559.  
  560. /* change the screen scale for the Delta X, Delta Y  display */
  561. void get_scale(void)
  562. {
  563.     char temp[10], sc_string[80], *st_ptr;
  564.     int gf = FALSE;
  565.  
  566.     p_message("                                                ");
  567.     st_ptr = sc_string;
  568.     st_ptr = stpcpy(st_ptr, "Enter new screen scale <CR> to abort [");
  569.     sprintf(temp, "%2.4lf", scale);
  570.     st_ptr = stpcpy(st_ptr, temp);
  571.     strcpy(st_ptr, "] ");
  572.  
  573.     if(!get_string(sc_string, temp)) return;
  574.  
  575.     if(grid_flag) {
  576.         grid(OFF);
  577.         gf = TRUE;
  578.     }
  579.     sscanf(temp, "%lf", &scale);
  580.     if(gf) grid(ON);
  581. }
  582.  
  583. /* draw an ellipse after checking two endpoints were marked */
  584. void xellipse(void)
  585. {
  586.     if(!(first_flag & second_flag)) {
  587.         p_message("Mark first AND second points");
  588.         delay(DELAY_TIME);
  589.         return;
  590.     }
  591.     check_flags();
  592.     ellipse(x, y, 0, 360, len(startx,endx) , len(starty,endy));
  593.  
  594. }
  595. /* draw a retangle after checking two endpoints were marked */
  596. void xrectangle(void)
  597. {
  598.     if(!(first_flag & second_flag)) {
  599.         p_message("Mark first AND second points");
  600.         delay(DELAY_TIME);
  601.         return;
  602.     }
  603.     check_flags();
  604.  
  605.     xline(startx, starty, endx,   starty, 0);
  606.     xline(endx,   starty, endx,   endy,   0);
  607.     xline(endx,   endy,   startx, endy,   0);
  608.     xline(startx, endy,   startx, starty, 0);
  609. }
  610.  
  611. /* draw the normalized circle */
  612.  
  613. void xcircle(int state)
  614. {
  615.     long dx, dy;
  616.  
  617.     if(state) {
  618.         if(!(first_flag & second_flag)) {
  619.             p_message("Mark first AND second points");
  620.             delay(DELAY_TIME);
  621.             return;
  622.         }
  623.         check_flags();
  624.     }
  625.     dx = len(startx,endx);
  626.     dy = len(starty,endy);
  627.  
  628.     if((!dx && !dy) || (dx && dy)) {
  629.         p_message("Indicate only one axis");
  630.         delay(DELAY_TIME);
  631.         return;
  632.     }
  633.     if(dx) dy = (dx * (long)xasp) / (long)yasp;
  634.     if(dy) dx = (dy * (long)yasp) / (long)xasp;
  635.     ellipse(startx, starty, 0, 360, (int)dx, (int)dy);
  636. }
  637.  
  638. /* save the display  */
  639. void save_pic()
  640. {
  641.     char fname[STR_LENGTH];
  642.     char ftemp[STR_LENGTH];
  643.     char fback[STR_LENGTH];
  644.     char dr[MAXDRIVE];
  645.     char dir[MAXDIR];
  646.     char fil[MAXFILE];
  647.     char ext[MAXEXT];
  648.     FILE *fp;
  649.     void *buffer_ul, *buffer_ll;
  650.     char *file_ptr;
  651.     int ok_flag = TRUE, gf = FALSE;
  652.     unsigned size_ul, size_ll;
  653.  
  654.     while(ok_flag) {
  655.         if(!get_string("Enter filename: ",fname)) return; /* return if no keys were entered */
  656.  
  657.         fnsplit(fname, dr, dir, fil, ext);
  658.         strcpy(ext, ".BAK");
  659.         fnmerge(fback, dr, dir, fil, ext);
  660.         strcpy(ext, ".$$$");
  661.         fnmerge(ftemp, dr, dir, fil, ext);
  662.         strcpy(ext, ".PIC");
  663.         fnmerge(fname, dr, dir, fil, ext);
  664.  
  665.         if(!(fp = fopen(ftemp, "wb"))) {
  666.             if(header) gprintf(320, height+2, "cannot open file %s", fname);
  667.             delay(DELAY_TIME);
  668.         }
  669.         else ok_flag = FALSE;
  670.     }
  671.     p_message("Saving the screen to the file");
  672.     if(grid_flag) {
  673.         grid(OFF);
  674.         gf = TRUE;
  675.     }
  676.     size_ul = imagesize(0, 0, maxx, ((maxy+1)/2)-1);
  677.     size_ll = imagesize(0, (maxy+1)/2, maxx, maxy);
  678.  
  679.     buffer_ul = malloc(size_ul);
  680.     buffer_ll = malloc(size_ll);
  681.  
  682.     if(buffer_ul == NULL || buffer_ll == NULL) {
  683.         p_message("Out of memory");
  684.         delay(DELAY_TIME);
  685.         free(buffer_ul);
  686.         free(buffer_ll);
  687.         if(gf) grid(ON);
  688.         return;
  689.     }
  690.  
  691.     p_message("   ");
  692.     getimage(0, 0, maxx, ((maxy+1)/2)-1, buffer_ul);
  693.     p_message("Saving the screen to the file");
  694.     getimage(0, (maxy+1)/2, maxx, maxy, buffer_ll);
  695.  
  696.     file_ptr = buffer_ul;
  697.     wcomp(size_ul, file_ptr, fp);
  698.  
  699.     free(buffer_ul);
  700.  
  701.     file_ptr = buffer_ll;
  702.     wcomp(size_ll, file_ptr, fp);
  703.  
  704.     free(buffer_ll);
  705.  
  706.     fputc(EOF, fp);
  707.     fcloseall();
  708.     unlink(fback);
  709.     rename(fname, fback);
  710.     rename(ftemp, fname);
  711.     p_message("  ");
  712.     if(gf) grid(ON);
  713. }
  714. /* read a picture file  */
  715. void load_pic()
  716. {
  717.     char fname[STR_LENGTH], *fname_ptr;
  718.     int ok_flag = TRUE;
  719.     FILE *fp;
  720.     char *buffer, *buf_ptr;
  721.     unsigned size_buf, size_buf1;
  722.  
  723.     size_buf = imagesize(0, 0, maxx, ((maxy+1)/2)-1);
  724.     buffer = malloc(size_buf);
  725.  
  726.     if(buffer == NULL) {
  727.         p_message("Out of memory");
  728.         delay(DELAY_TIME);
  729.         free(buffer);
  730.         return;
  731.     }
  732.  
  733.     while(ok_flag) {
  734.         if(!get_string("Enter filename: ",fname)) {
  735.             free(buffer);
  736.             return; /* return if no keys were entered */
  737.         }
  738.         fname_ptr = fname;
  739.  
  740.         while(*fname_ptr != '\0' && *fname_ptr != '.') fname_ptr++;
  741.         strcpy(fname_ptr, ".PIC");
  742.  
  743.         if(!(fp = fopen(fname, "rb"))) {
  744.             if(header) gprintf(320, height+2, "cannot open file %s", fname);
  745.             delay(DELAY_TIME);
  746.         }
  747.         else ok_flag = FALSE;
  748.     }
  749.     setcolor(bkcolor);
  750.     setviewport(0,0,639,20,1);
  751.     clearviewport();
  752.     setviewport(0,0,639,349,1);
  753.     setcolor(cc);
  754.  
  755.     p_message("Reading the file");
  756.  
  757.     size_buf1 = size_buf;
  758.     rcomp(size_buf1, buffer, fp);
  759.     putimage(0, 0, buffer, XOR_PUT);
  760.     size_buf1 = size_buf;
  761.     rcomp(size_buf1, buffer, fp);
  762.     putimage(0, (maxy+1)/2, buffer, XOR_PUT);
  763.  
  764.     free(buffer);
  765.     fclose(fp);
  766. }
  767.  
  768. /* move one region to another   */
  769.  
  770.  
  771. /* be careful of start end points with this -- you can get a fold */
  772.  
  773.  
  774. void move(void)
  775. {
  776.     register int i, j;
  777.     int oldx, oldy, xx, yy, cx, cy, xdir, ydir, gf = FALSE;
  778.     unsigned char c;
  779.  
  780.     if(!(first_flag & second_flag)) {
  781.         p_message("Mark first AND second points");
  782.         delay(DELAY_TIME);
  783.         return;
  784.     }
  785.     check_flags();
  786.     if(grid_flag) {
  787.         grid(OFF);
  788.         gf = TRUE;
  789.     }
  790.  
  791.     oldx = x; oldy = y;
  792.  
  793.     xdir = x > max(startx, endx) ? 0 : 1;
  794.     ydir = y > max(starty, endy) ? 0 : 1;
  795.  
  796.     cx = len(startx, endx) + 1;
  797.     cy = len(starty, endy) + 1;
  798.  
  799.  
  800.     for(i = 0, (xx = xdir ? min(startx,endx) : max(startx, endx));
  801.         i < cx;
  802.         i++, xdir ? xx++ : xx--, xdir ? x++ : x--) {
  803.  
  804.         for(j = 0, y = oldy, (yy = ydir ? min(starty,endy) : max(starty, endy));
  805.             j < cy;
  806.             j++, ydir ? yy++ : yy--, ydir ? y++ : y--) {
  807.  
  808.             c = getpixel(xx, yy);
  809.             putpixel(xx, yy, 0);
  810.             if(c) putpixel(x, y, c);
  811.         }
  812.     }
  813.     x = oldx; y = oldy;
  814.     if(gf) grid(ON);
  815. }
  816.  
  817. /* copy one region to another - it's the same as move except it does not
  818.     clear the original pixel */
  819. void copy(void)
  820. {
  821.     register int i, j;
  822.     int oldx, oldy, xx, yy, cx, cy, xdir, ydir, gf = FALSE;
  823.     unsigned char c;
  824.  
  825.     if(!(first_flag & second_flag)) {
  826.         p_message("Mark first AND second points");
  827.         delay(DELAY_TIME);
  828.         return;
  829.     }
  830.     check_flags();
  831.     if(grid_flag) {
  832.         grid(OFF);
  833.         gf = TRUE;
  834.     }
  835.  
  836.     oldx = x; oldy = y;
  837.  
  838.     xdir = x > max(startx, endx) ? 0 : 1;
  839.     ydir = y > max(starty, endy) ? 0 : 1;
  840.  
  841.     cx = len(startx, endx) + 1;
  842.     cy = len(starty, endy) + 1;
  843.  
  844.     for(i = 0, (xx = xdir ? min(startx,endx) : max(startx, endx));
  845.         i < cx;
  846.         i++, xdir ? xx++ : xx--, xdir ? x++ : x--) {
  847.  
  848.         for(j = 0, y = oldy, (yy = ydir ? min(starty,endy) : max(starty, endy));
  849.             j < cy;
  850.             j++, ydir ? yy++ : yy--, ydir ? y++ : y--) {
  851.  
  852.             c = getpixel(xx, yy);
  853.             if(c) putpixel(x, y, c);
  854.         }
  855.     }
  856.     x = oldx; y = oldy;
  857.     if(gf) grid(ON);
  858. }
  859.  
  860. void delete_area(void)
  861. {
  862.     register int i, j, cx, cy, xx, yy, gf = FALSE;
  863.  
  864.     if(!(first_flag & second_flag)) {
  865.         p_message("Mark first AND second points");
  866.         delay(DELAY_TIME);
  867.         return;
  868.     }
  869.     check_flags();
  870.     if(grid_flag) {
  871.         grid(OFF);
  872.         gf = TRUE;
  873.     }
  874.  
  875.     for(i = 0, xx = min(startx, endx); i <= len(startx, endx); i++, xx++)
  876.         for(j = 0, yy = min(starty, endy); j <= len(starty, endy) ; j++, yy++)
  877.             putpixel(xx, yy, 0);
  878.     if(gf) grid(ON);
  879. }
  880.  
  881. /*
  882.     GPRINTF: Used like PRINTF except the output is sent to the
  883.     screen in graphics mode at the specified co-ordinate.
  884.     NOTE: a string HAS to be defined as gprintf(x, y, "%s", "text")
  885. */
  886.  
  887. void gprintf( int xx, int yy, char *fmt, ... )
  888. {
  889.   va_list  argptr;                    /* Argument list pointer        */
  890.   char str[140];                    /* Buffer to build sting into    */
  891.  
  892.   va_start( argptr, format );        /* Initialize va_ functions        */
  893.  
  894.   vsprintf( str, fmt, argptr );    /* prints string to buffer    */
  895.   outtextxy( xx, yy, str );            /* Send string in graphics mode */
  896.   va_end( argptr );                    /* Close va_ functions            */
  897. }
  898.  
  899. /* print an informational message on the second line
  900.    but only if HEADER is turned on
  901. */
  902. void p_message(char *str)
  903. {
  904.     if(!header) return;
  905.  
  906.     setcolor(bkcolor);
  907.     setviewport(0, height + 2, maxx, 19, 1);
  908.     clearviewport();
  909.  
  910.     setcolor(msg_color);
  911.     setviewport(0, 0, maxx, maxy, 1);
  912.     gprintf(5, height + 2, "%s", str);
  913.     setcolor(cc);
  914. }
  915.  
  916. /* turn the first and second markers off.  Since the icon is XOR
  917.    we have to first test if they are displayed
  918. */
  919. void check_flags(void)
  920. {
  921.     if(first_flag) {
  922.         first_flag = FALSE;
  923.         putimage(startx-7, starty-7, first, XOR_PUT);
  924.     }
  925.     if(second_flag) {
  926.         second_flag = FALSE;
  927.         putimage(endx-3, endy-3, second, XOR_PUT);
  928.     }
  929.     first_point = TRUE;        /* reset to first point only */
  930. }
  931.  
  932. /* save an area (icon) to memory */
  933. void capture(void)
  934. {
  935.     register int i, j, c;
  936.     int xx, yy, gf = FALSE;
  937.     unsigned char *buf_ptr;
  938.     unsigned long temp_size;
  939.     union {
  940.         int i;
  941.         unsigned char c[2];
  942.     } cx, cy;
  943.  
  944.     if(!(first_flag & second_flag)) {
  945.         p_message("Mark first AND second points");
  946.         delay(DELAY_TIME);
  947.         return;
  948.     }
  949.  
  950.     if(cap_flag) {
  951.         p_message("There is an icon in memory.  Do you wish to overwrite it? [Y/N] ");
  952.         key.i = bioskey(0);
  953.         c = tolower(key.c[0]);
  954.         if(c != 'y') return;
  955.     }
  956.     check_flags();
  957.     if(grid_flag) {
  958.         grid(OFF);
  959.         gf = TRUE;
  960.     }
  961.  
  962.     p_message("  ");
  963.  
  964.     cx.i = len(startx, endx) + 1;
  965.     cy.i = len(starty, endy) + 1;
  966.  
  967.     temp_size = (long)cx.i * (long)cy.i;
  968.     if(temp_size > 64000L) {
  969.         p_message("Icon is too big");
  970.         delay(DELAY_TIME);
  971.         return;
  972.     }
  973.     c_size = cx.i * cy.i;
  974.     if(cap_flag) free(c_buffer);
  975.     c_buffer = malloc(c_size + 4);
  976.  
  977.     if(c_buffer == NULL) {
  978.         p_message("Out of memory");
  979.         delay(DELAY_TIME);
  980.         free(c_buffer);
  981.         return;
  982.     }
  983.  
  984.     buf_ptr = c_buffer;
  985.     *buf_ptr++ = cx.c[0];
  986.     *buf_ptr++ = cx.c[1];
  987.     *buf_ptr++ = cy.c[0];
  988.     *buf_ptr++ = cy.c[1];
  989.  
  990.     for(i = 0, xx = min(startx, endx); i < cx.i; i++, xx++) {
  991.         for(j = 0, yy = min(starty, endy); j < cy.i; j++, yy++) {
  992.             c = getpixel(xx, yy);
  993.             *buf_ptr++ = c;
  994.         }
  995.     }
  996.     cap_flag = TRUE;
  997.     if(gf) grid(ON);
  998. }
  999.  
  1000. /* display the saved icon */
  1001. void uncapture(void)
  1002. {
  1003.     register int i, j, c;
  1004.     int xx, yy;
  1005.     unsigned char *buf_ptr;
  1006.     union {
  1007.         int i;
  1008.         unsigned char c[2];
  1009.     } cx, cy;
  1010.  
  1011.     if(!cap_flag) {
  1012.         p_message("There is NO captured picture - use the \"J\" command first");
  1013.         delay(DELAY_TIME);
  1014.         return;
  1015.     }
  1016.     buf_ptr = c_buffer;
  1017.     cx.c[0] = *buf_ptr++;
  1018.     cx.c[1] = *buf_ptr++;
  1019.     cy.c[0] = *buf_ptr++;
  1020.     cy.c[1] = *buf_ptr++;
  1021.  
  1022.     for(i = 0, xx = x; i < cx.i; i++, xx++) {
  1023.         for(j = 0, yy=y; j < cy.i; j++, yy++) {
  1024.             c = *buf_ptr++;
  1025.             if(c != 0 && c != bkcolor) putpixel(xx, yy, c);
  1026.         }
  1027.     }
  1028. }
  1029.  
  1030. /* why are you reading this comment */
  1031. void help(void)
  1032. {
  1033.     if(paint_help()) {
  1034.         settextjustify(oldtext.horiz, oldtext.vert);
  1035.         settextstyle(oldtext.font, oldtext.direction, oldtext.charsize);
  1036.         p_message("Out of memory");
  1037.         delay(DELAY_TIME);
  1038.         return;
  1039.     }
  1040. }
  1041.  
  1042. /* change all occurences in a specified area to a new color  */
  1043. void new_color(void)
  1044. {
  1045.     unsigned char from, to, cx, cy;
  1046.     register int i, j, c, xx, yy;
  1047.  
  1048.     if(!(first_flag & second_flag)) {
  1049.         p_message("Mark first AND second points");
  1050.         delay(DELAY_TIME);
  1051.         return;
  1052.     }
  1053.     check_flags();
  1054.  
  1055.     p_message("Enter the FROM color number");
  1056.     key.i = bioskey(0);
  1057.     from = key.c[0];
  1058.     switch(from) {
  1059.         case '!': from = 11;
  1060.                   break;
  1061.         case '@': from = 12;
  1062.                   break;
  1063.         case '#': from = 13;
  1064.                   break;
  1065.         case '$': from = 14;
  1066.                   break;
  1067.         case '%': from = 15;
  1068.                   break;
  1069.         case '0': from = 10;
  1070.                   break;
  1071.         default:  from -= '0';
  1072.                   break;
  1073.     }
  1074.     p_message("Enter the TO color number");
  1075.     key.i = bioskey(0);
  1076.     to = key.c[0];
  1077.     switch(to) {
  1078.         case '!': to = 11;
  1079.                   break;
  1080.         case '@': to = 12;
  1081.                   break;
  1082.         case '#': to = 13;
  1083.                   break;
  1084.         case '$': to = 14;
  1085.                   break;
  1086.         case '%': to = 15;
  1087.                   break;
  1088.         case '0': to = 10;
  1089.                   break;
  1090.         default:  to -= '0';
  1091.                   break;
  1092.     }
  1093.     if( to == bkcolor) to = 0;
  1094.     p_message("Changing colors");
  1095.  
  1096.     for(i = 0, xx = min(startx, endx); i <= len(startx, endx); i++, xx++) {
  1097.         for(j = 0, yy = min(starty, endy); j <= len(starty, endy) ; j++, yy++) {
  1098.             c = getpixel(xx, yy);
  1099.             if( c == from) putpixel(xx, yy, to);
  1100.         }
  1101.     }
  1102. }
  1103.  
  1104. /* jump to the nearest pixel of thesame color as the drawing color  */
  1105. void vector(void)
  1106. {
  1107.     register int i, xx, yy, z;
  1108.     int f0 = 0;
  1109.  
  1110.     xx = x;
  1111.     yy = y;
  1112.  
  1113.     p_message("Searching - please wait (Hit any key to abort)");
  1114.     if(x > 0 && x < maxx && y > miny && y < maxy)
  1115.         if(getpixel(x, y) == cc) return;
  1116.  
  1117.     i = inc;
  1118.  
  1119.     while(f0 != 15) {
  1120.         f0 = 0;
  1121.         z = 2*i;
  1122.         while(z--) {
  1123.             x++;
  1124.             if(x == maxx) f0 &= 1;
  1125.             if(x < maxx) if(getpixel(x, y) == cc) return;
  1126.         }
  1127.         z = 2*i;
  1128.         while(z--) {
  1129.             y++;
  1130.             if(y == maxy) f0 &= 2;
  1131.             if(y < maxy) if(getpixel(x, y) == cc) return;
  1132.         }
  1133.         z = 2*i;
  1134.         while(z--) {
  1135.             x--;
  1136.             if(x == 0) f0 &= 4;
  1137.             if(x > minx) if(getpixel(x, y) == cc) return;
  1138.         }
  1139.         z = 2*i;
  1140.         while(z--) {
  1141.             y--;
  1142.             if(y == miny) f0 &= 8;
  1143.             if(y > miny) if(getpixel(x, y) == cc) return;
  1144.         }
  1145.         x -= inc; y -= inc; i += inc;
  1146.         if(bioskey(1)) {
  1147.             bioskey(0);                /* get the character  */
  1148.             x = xx; y = yy;
  1149.             return;
  1150.         }
  1151.     }
  1152.     x = xx; y = yy;
  1153. }
  1154.  
  1155. /* change visual and active pages  */
  1156. void swap_page(void)
  1157. {
  1158.     check_flags();
  1159.     page ^= 1;
  1160.     setvisualpage(page);
  1161.     setactivepage(page);
  1162.  
  1163. }
  1164.  
  1165. /* turn cross hairs on, but only if they were off */
  1166. /* turn cross hairs off, but only if they were on */
  1167. void xhair(int state)
  1168. {
  1169.     if((state && !xhair_flag) || (!state && xhair_flag)) {
  1170.         putimage(x-5, y-4, xhair_buf, XOR_PUT);
  1171.         xhair_flag ^= 1;
  1172.     }
  1173. }
  1174.  
  1175. /* step through the 12 fill patterns   */
  1176. void set_fill(void)
  1177. {
  1178.     int length;
  1179.     static char *fill_text = "Current fill pattern: ";
  1180.  
  1181.     if(!header) return;                        /* only if we are looking at it */
  1182.     setlinestyle(0, 0, 0);
  1183.     key.i = 0;
  1184.     setviewport(0, 0, maxx, 2 * height, 1);
  1185.     do {
  1186.         clearviewport();
  1187.         gprintf(0, (height/2)-1, "%s%s", fill_text, fill_num[fill_style]);
  1188.         length = textwidth(fill_text) + textwidth(fill_num[fill_style]);
  1189.         setfillstyle(fill_style, cc);
  1190.         rectangle(length, 0, length+50, height*2);
  1191.         floodfill(length+1, 1, cc);
  1192.         gprintf(length + 58, (height/2)-1,"%s", "Hit <CR> for next -- F11 to return");
  1193.         key.i = bioskey(0);
  1194.         if(key.c[0] == 13) {
  1195.             fill_style++;
  1196.             if(fill_style == 12) fill_style = 0;
  1197.         }
  1198.     } while(key.i != F11); /* 68 = F10 */
  1199.     setlinestyle(0, 0, width);
  1200.     setviewport(0, 0, maxx, maxy, 1);
  1201.     setcolor(cc);
  1202.     setbkcolor(bkcolor);
  1203. }
  1204.  
  1205. void line_style(void)
  1206. {
  1207.     int length;
  1208.     static char *line_type = "Current line type: ";
  1209.  
  1210.     if(!header) return;
  1211.     setviewport(0, 0, maxx, 2 * height, 1);
  1212.     getlinesettings(&oldlinestyle);
  1213.     do {
  1214.         clearviewport();
  1215.         gprintf(0, (height/2)-1, "%s%s", line_type, line_width[width]);
  1216.         setlinestyle(oldlinestyle.linestyle, oldlinestyle.upattern, width);
  1217.         length = textwidth(line_type) + textwidth(line_width[width]);
  1218.         line(length + 8, height, length+58, height);
  1219.         gprintf(length + 66, (height/2)-1,"%s%s%s", "Hit <CR> for ",(width==0) ? "wide" : "narrow"," - F12 for style, or <ESC>");
  1220.         key.i = bioskey(0);
  1221.         if(key.c[0] == 0x1b) break;        /* abort */
  1222.         if(key.c[0] == 13) width ^= 3;
  1223.     } while (key.i != F12);
  1224.     setviewport(0, 0, maxx, maxy, 1);
  1225.     setcolor(cc);
  1226.     setbkcolor(bkcolor);
  1227.  
  1228.     if(key.c[0] != 0x1b) lstyle    ();
  1229.  
  1230.  
  1231. }
  1232.  
  1233. /* save the previously defined icon to a file  */
  1234. void save_pik(void)
  1235. {
  1236.     char fname[STR_LENGTH], *fname_ptr;
  1237.     FILE *fp;
  1238.     unsigned char *file_ptr;
  1239.     unsigned pik_size;
  1240.     int ok_flag = TRUE;
  1241.  
  1242.     if(!cap_flag) {
  1243.         p_message("There is NO captured picture - use the \"J\" command first");
  1244.         delay(DELAY_TIME);
  1245.         return;
  1246.     }
  1247.  
  1248.     while(ok_flag) {
  1249.         if(!get_string("Enter filename: ",fname)) return; /* return if no keys were entered */
  1250.         fname_ptr = fname;
  1251.  
  1252.         while(*fname_ptr != '\0' && *fname_ptr != '.') fname_ptr++;
  1253.         strcpy(fname_ptr, ".PIK");
  1254.  
  1255.         if(!(fp = fopen(fname, "wb"))) printf("cannot open file %s\n", fname);
  1256.         else ok_flag = FALSE;
  1257.     }
  1258.  
  1259.     p_message("Saving the icon");
  1260.  
  1261.     file_ptr = c_buffer;
  1262.     pik_size = c_size + 4;    /* size of the saved image plus the x,y size */
  1263.     putw(pik_size, fp);        /* write the size of the picture first          */
  1264.  
  1265.     while(pik_size--) fputc(*file_ptr++, fp);
  1266.  
  1267.     putc(EOF, fp);            /* just in case                         */
  1268.     fclose(fp);
  1269. }
  1270.  
  1271. /* read the icon file that has been saved, use 'K' to display it  */
  1272. void read_pik(void)
  1273. {
  1274.     char fname[STR_LENGTH], *fname_ptr,c;
  1275.     FILE *fp;
  1276.     unsigned char *file_ptr;
  1277.     unsigned pik_size;
  1278.     int ok_flag = TRUE;
  1279.  
  1280.     if(cap_flag) {
  1281.         p_message("There is an icon in memory.  Do you wish to overwrite it? [Y/N] ");
  1282.         key.i = bioskey(0);
  1283.         c = tolower(key.c[0]);
  1284.         if(c != 'y') return;
  1285.     }
  1286.     check_flags();
  1287.     p_message("  ");
  1288.     while(ok_flag) {
  1289.         fname_ptr = fname;
  1290.         if(!get_string("Enter filename: ",fname)) return; /* return if no keys were entered */
  1291.         while(*fname_ptr != '\0' && *fname_ptr != '.') fname_ptr++;
  1292.         strcpy(fname_ptr, ".PIK");
  1293.  
  1294.         if(!(fp = fopen(fname, "rb"))) printf("cannot open file %s\n", fname);
  1295.         else ok_flag = FALSE;
  1296.     }
  1297.     if(cap_flag) free(c_buffer);
  1298.     p_message("Reading icon");
  1299.  
  1300.     pik_size = getw(fp);    /* get the size of the picture first */
  1301.  
  1302.     c_size = pik_size-4;    /* global for 'what's there'            */
  1303.     c_buffer = malloc(pik_size);
  1304.  
  1305.     if(c_buffer == NULL) {
  1306.         p_message("Out of memory");
  1307.         delay(DELAY_TIME);
  1308.         free(c_buffer);
  1309.         return;
  1310.     }
  1311.     file_ptr = c_buffer;    /* preserve the pointer                */
  1312.     while(pik_size--) *file_ptr++ = fgetc(fp);
  1313.     cap_flag = TRUE;
  1314.     fclose(fp);
  1315. }
  1316.  
  1317.  
  1318. /* get a text string, displayed on line 2, return the character count
  1319.    or zero if <ESC> was hit
  1320. */
  1321. int get_string(char *text, char name[])
  1322. {
  1323.     char *name_ptr;
  1324.     int j, count = 0;
  1325.     if(header) {
  1326.         setcolor(msg_color);
  1327.         gprintf(5, height+2, "%s", text);
  1328.     }
  1329.     name_ptr = name;
  1330.     j = textwidth(text);
  1331.  
  1332.     while(TRUE) {
  1333.         if(header) gprintf(j, height+2, "%c", '_');
  1334.         key.i = bioskey(0);
  1335.         if(key.c[0] == 13) break;
  1336.         if(key.c[0] == 27) {        /* provision for an abort */
  1337.             count = 0;
  1338.             break;
  1339.         }
  1340.         if(key.c[0] == 0x08) {       /* backspace */
  1341.             setcolor(bkcolor);
  1342.             if(header) gprintf(j, height+2, "%c", 0xdb);
  1343.             j -=textwidth("H");
  1344.             if(header) {
  1345.                 gprintf(j, height+2, "%c", 0xdb);
  1346.                 setcolor(msg_color);
  1347.             }
  1348.             name_ptr--;
  1349.             count--;
  1350.             continue;
  1351.         }
  1352.         *name_ptr++ = key.c[0];
  1353.         if(header) {
  1354.             setcolor(bkcolor);
  1355.             gprintf(j, height+2, "%c", 0xdb);
  1356.             setcolor(msg_color);
  1357.             gprintf(j, height+2, "%c", key.c[0]);
  1358.         }
  1359.         j += textwidth("H");
  1360.         count++;
  1361.     }
  1362.     *name_ptr = '\0';
  1363.     setcolor(cc);
  1364.     return count;
  1365. }
  1366.  
  1367. void text_mode(void)
  1368. {
  1369.  
  1370.     char *text_ptr;
  1371.     int j, count = 0, ok = 0;
  1372.     char *msg1 = "Enter text size(1-9) [";
  1373.     char *msg2 = "Enter text: ";
  1374.     char *msg3 = "Locate cursor and hit <F1> for horizontal or <F2> for vertical";
  1375.  
  1376.     if(header) {
  1377.         setcolor(msg_color);
  1378.         gprintf(5, height+2, "%s%d] ", msg1, text_size);
  1379.     }
  1380.     while(!ok) {
  1381.         if(header) {
  1382.             setcolor(bkcolor);
  1383.             gprintf(textwidth(msg1)+textwidth("5] "), height+2, "%c", 0xdb);
  1384.             setcolor(msg_color);
  1385.             gprintf(textwidth(msg1)+textwidth("5] "), height+2, "%c", '_');
  1386.         }
  1387.         key.i = bioskey(0);
  1388.         if(key.c[0] == 13) break;
  1389.         if(key.c[0] == 27) {
  1390.             setcolor(cc);
  1391.             return;
  1392.         }
  1393.         text_size = key.c[0] - '0';
  1394.         if(text_size > 0 && text_size < 10) ok = 1;
  1395.     }
  1396.     if(header) {
  1397.         setcolor(bkcolor);
  1398.         gprintf(textwidth(msg1)+textwidth("5] "), height+2, "%c", 0xdb);
  1399.         setcolor(msg_color);
  1400.         gprintf(textwidth(msg1)+textwidth("5] "), height+2, "%d", text_size);
  1401.         delay(200);
  1402.         setcolor(bkcolor);
  1403.         setviewport(0, height+2, maxx, 20, 1);
  1404.         clearviewport();
  1405.         setviewport(0, 0, maxx, maxy, 1);
  1406.         setcolor(msg_color);
  1407.         outtextxy(5, height+2, msg2);
  1408.     }
  1409.     text_ptr = text_line;
  1410.     j = textwidth(msg2);
  1411.  
  1412.     while(TRUE) {
  1413.         if(header) gprintf(j, height+2, "%c", '_');
  1414.         key.i = bioskey(0);
  1415.         if(key.c[0] == 13) break;
  1416.         if(key.c[0] == 27) {        /* provision for an abort */
  1417.             count = 0;
  1418.             break;
  1419.         }
  1420.         if(key.c[0] == 0x08) {       /* backspace */
  1421.             setcolor(bkcolor);
  1422.             if(header) gprintf(j, height+2, "%c", 0xdb);
  1423.             j -=textwidth("H");
  1424.             if(header) {
  1425.                 gprintf(j, height+2, "%c", 0xdb);
  1426.                 setcolor(msg_color);
  1427.             }
  1428.             text_ptr--;
  1429.             count--;
  1430.             continue;
  1431.         }
  1432.         *text_ptr++ = key.c[0];
  1433.         if(header) {
  1434.             setcolor(bkcolor);
  1435.             gprintf(j, height+2, "%c", 0xdb);
  1436.             setcolor(msg_color);
  1437.             gprintf(j, height+2, "%c", key.c[0]);
  1438.         }
  1439.         j += textwidth("H");
  1440.         count++;
  1441.     }
  1442.     *text_ptr = '\0';
  1443.     setcolor(cc);
  1444.  
  1445.     if(!count) return;
  1446.     text_ok = TRUE;
  1447.     p_message(msg3);
  1448. /*    delay(DELAY_TIME); */
  1449.     on_flag = FALSE;                /* make sure pen is up */
  1450. }
  1451.  
  1452. void print_text(void)
  1453. {
  1454.     text_ok = 0;
  1455.     settextstyle(SMALL_FONT, HORIZ_DIR, text_size);
  1456.     gprintf(x, y - textheight("H"), text_line);
  1457.     settextstyle(oldtext.font, oldtext.direction, oldtext.charsize);
  1458. }
  1459.  
  1460. void v_print_text(void)
  1461. {
  1462.     text_ok = 0;
  1463.     settextstyle(SMALL_FONT, VERT_DIR, text_size);
  1464.     gprintf(x, y - textwidth(text_line), text_line);
  1465.     settextstyle(oldtext.font, oldtext.direction, oldtext.charsize);
  1466. }
  1467.  
  1468.  
  1469. /********************************/
  1470. /*                                */
  1471. /*         MOUSE STUFF          */
  1472. /*                                */
  1473. /********************************/
  1474.  
  1475. /* turn mouse on/off, but only if it is present. Mouse reset
  1476.    determined if installed. NO_MOUSE indicates not present
  1477. */
  1478. void swap_mouse(void)
  1479. {
  1480.     if(mouse_flag != NO_MOUSE) mouse_flag ^= 1;
  1481. }
  1482.  
  1483. int mouse_reset(void)
  1484. {
  1485.     function = 0;
  1486.     cmouses(&function, &arg2, &arg3, &arg4);
  1487.  
  1488.     if(function == 0) return TRUE;
  1489.     return FALSE;
  1490. }
  1491.  
  1492. /*return true if button is pressed, else zero  */
  1493. int r_button_pressed(void)
  1494. {
  1495.     function = 3;
  1496.     cmouses(&function, &arg2, &arg3, &arg4);
  1497.     return (arg2 & 2);
  1498. }
  1499. /*return true if button is pressed, else zero  */
  1500. int l_button_pressed(void)
  1501. {
  1502.     function = 3;
  1503.     cmouses(&function, &arg2, &arg3, &arg4);
  1504.     return (arg2 & 1);
  1505. }
  1506.  
  1507.  
  1508. /*return true if button is pressed, else zero  */
  1509. int c_button_pressed(void)
  1510. {
  1511.     function = 3;
  1512.     cmouses(&function, &arg2, &arg3, &arg4);
  1513.     return (arg2 & 4);
  1514. }
  1515.  
  1516. /* set mouse cursor coordinates, from global x,y (current pos)  */
  1517. void set_mouse_position(void)
  1518. {
  1519.     function = 4;
  1520.     cmouses(&function, &arg2, &x, &y);
  1521. }
  1522.  
  1523. /* return mouse cursor coordinates, from global x,y (current pos)  */
  1524. void get_mouse_position(void)
  1525. {
  1526.     function = 3;
  1527.     cmouses(&function, &arg2, &x, &y);
  1528. }
  1529.  
  1530. /* return direction of travel  */
  1531. void mouse_motion(char *dx, char *dy)
  1532. {
  1533.     int a3, a4;
  1534.     function = 11;
  1535.     cmouses(&function, &arg2, &a3, &a4);
  1536.  
  1537.     if(a3 > 0) *dy = DOWN;
  1538.     else if(a3 < 0) *dy = UP;
  1539.     else *dy = NOT_MOVED;
  1540.  
  1541.     if(a4 > 0) *dx = RIGHT;
  1542.     else if(a4 < 0) *dx = LEFT;
  1543.     else *dx = NOT_MOVED;
  1544. }
  1545.  
  1546. /* return TRUE when specified button is released  */
  1547. void wait_on(int button)
  1548. {
  1549.     switch(button) {
  1550.         case LEFTB: while(l_button_pressed());
  1551.                     break;
  1552.         case RIGHTB: while(r_button_pressed());
  1553.                     break;
  1554.         default: while(c_button_pressed());
  1555.     }
  1556. }
  1557.  
  1558. void set_limits(void)
  1559. {
  1560.     /* set X limit */
  1561.  
  1562.     function = 7;
  1563.     arg3 = 5;
  1564.     arg4 = 635;
  1565.     cmouses(&function, &arg2, &arg3, &arg4);
  1566.  
  1567.     /* set Y limit */
  1568.  
  1569.     function = 8;
  1570.     arg3 = header ? 20 : 5;
  1571.     arg4 = 345;
  1572.     cmouses(&function, &arg2, &arg3, &arg4);
  1573. }
  1574.  
  1575. #define INDICATE 0xd9
  1576.  
  1577. void wcomp(unsigned size, char *ptr, FILE *fd)
  1578. {
  1579.     unsigned char c, last, chr_cnt = 0;
  1580.     int flag = 0;
  1581.  
  1582.     c = *ptr++;
  1583.     fputc(c, fd);
  1584.     size--;
  1585.  
  1586.     while(size--) {
  1587.         last = c;
  1588.         c = *ptr++;
  1589.  
  1590.         if(c == INDICATE) {
  1591.             if(flag) fputc(chr_cnt, fd);
  1592.             chr_cnt = 0;
  1593.             flag = 0;
  1594.             fputc(c, fd);
  1595.             fputc(0, fd);
  1596.             continue;
  1597.         }
  1598.  
  1599.         if(c == last) {
  1600.             if(!flag) fputc(INDICATE, fd);
  1601.             flag = 1;
  1602.             chr_cnt++;
  1603.             if(chr_cnt == 0xff) {
  1604.                 fputc(chr_cnt, fd);
  1605.                 fputc(c, fd);
  1606.                 c = !c;
  1607.                 chr_cnt = 0;
  1608.                 flag = 0;
  1609.             }
  1610.             continue;
  1611.         }
  1612.         if(flag) {
  1613.             fputc(chr_cnt, fd);
  1614.             chr_cnt = 0;
  1615.             flag = 0;
  1616.         }
  1617.         fputc(c, fd);
  1618.     }
  1619.     if(chr_cnt) fputc(chr_cnt, fd);
  1620.  
  1621.     return;
  1622.  
  1623. }
  1624.  
  1625. void rcomp(unsigned size, char *ptr, FILE *fs)
  1626. {
  1627.     unsigned char c, count, last = 0, count1 = 0;
  1628.  
  1629.     c = fgetc(fs);
  1630.     *ptr++ = c;
  1631.     size--;
  1632.  
  1633.     while(size > 0) {
  1634.  
  1635.         last = c;
  1636.  
  1637.         c = fgetc(fs);
  1638.  
  1639.         if(c == INDICATE) {
  1640.             count = fgetc(fs);
  1641.             count1 = count;
  1642.             if(count == 0) {
  1643.                 *ptr++ = last;
  1644.                 size--;
  1645.             }
  1646.             else while(count--) {
  1647.                 *ptr++ = last;
  1648.                 size--;
  1649.             }
  1650.             if(count1 == 0xff) {
  1651.                 c =fgetc(fs);
  1652.                 if(c != last) {
  1653.                     *ptr++ = c;
  1654.                     size--;
  1655.                 }
  1656.             }
  1657.         } else {
  1658.             *ptr++ = c;
  1659.             size--;
  1660.         }
  1661.     }
  1662.     return;
  1663. }
  1664.  
  1665. void grid(int state)
  1666. {
  1667.     int oldcolor;
  1668.     double lpos, hpos, inc, sc_factor;
  1669.     register int i;
  1670.  
  1671.     if(!(state ^ grid_flag)) return;
  1672.     
  1673.     oldcolor = getcolor();
  1674.     sc_factor = scale;
  1675.     while((sc_factor < 1.000) || (sc_factor >= 10.000)) {
  1676.         if(sc_factor >= 10.000L) sc_factor /= 10.000L;
  1677.         else if(sc_factor < 1.000L)  sc_factor *= 10.000L;
  1678.     }
  1679.     if(sc_factor > 5.000L) sc_factor /= 2.000L;
  1680.  
  1681.     inc = 66.000L/sc_factor;
  1682.  
  1683.     OUTINDEX(0, WHITE);
  1684.     OUTINDEX(1, ENABLE);
  1685.     OUTINDEX(3, 0x18);
  1686.  
  1687.     for(hpos = 5.0;  hpos <= 635.0; hpos += inc)
  1688.         for(i = 20; i <= 345; i++) pointf((int)hpos, i);
  1689.  
  1690.     for(lpos = 20.0; lpos <= 345.0; lpos += (inc * xasp / yasp))
  1691.         for(i = 5; i <= 635; i++) pointf(i, (int)lpos);
  1692.  
  1693.     OUTINDEX(0, 0);
  1694.     OUTINDEX(1, 0);
  1695.     OUTINDEX(3, 0);
  1696.     OUTINDEX(8, 0xff);
  1697.  
  1698.     setcolor(oldcolor);
  1699.     grid_flag ^= 1;
  1700. }
  1701.  
  1702. void pointf(int x, int y)
  1703. {
  1704.     unsigned char mask = 0x80, exist_color;
  1705.     char far *base;
  1706.  
  1707.     base = (char *)(EGABASE + ((long)y * 80L + ((long)x/8L)));
  1708.     if(page) base += 0x8000L;
  1709.     mask >>= x % 8;
  1710.     exist_color = *base;
  1711.     OUTINDEX(8, mask);
  1712.     *base &= 0xff;
  1713. }
  1714. void xline(int startx, int starty, int endx, int endy, int state)
  1715. {
  1716.     xline1(startx, starty, endx, endy, state);
  1717.     if(width == 3) {
  1718.         if(!(endy - starty)) {
  1719.             xline1(startx, starty-1, endx, endy-1, 0);
  1720.             xline1(startx, starty+1, endx, endy+1, 0);
  1721.         } else if(!(endx - startx)) {
  1722.             xline1(startx-1, starty, endx-1, endy, 0);
  1723.             xline1(startx+1, starty, endx+1, endy, 0);
  1724.         } else if((endy - starty) <= (endx-startx)) {
  1725.             xline1(startx, starty-1, endx, endy-1, 0);
  1726.             xline1(startx, starty+1, endx, endy+1, 0);
  1727.         } else {
  1728.             xline1(startx, starty-1, endx, endy-1, 0);
  1729.             xline1(startx, starty+1, endx, endy+1, 0);
  1730.         }
  1731.     }
  1732. }
  1733. /* draw a line after checking two endpoints were marked -- if state is 1 */
  1734. void xline1(int startx, int starty, int endx, int endy, int state)
  1735. {
  1736.     register int t, distance;
  1737.     int xerr = 0, yerr = 0, delta_x, delta_y;
  1738.     int incx, incy, ccc;
  1739.  
  1740.     if(state) {
  1741.         if(!(first_flag & second_flag)) {
  1742.             p_message("Mark first AND second points");
  1743.             delay(DELAY_TIME);
  1744.             return;
  1745.         }
  1746.         check_flags();
  1747.     }
  1748.  
  1749.     delta_x = endx - startx;
  1750.     delta_y = endy - starty;
  1751.  
  1752.     if(delta_x > 0) incx = 1;
  1753.     else if(delta_x == 0) incx = 0;
  1754.     else incx = -1;
  1755.  
  1756.     if(delta_y > 0) incy = 1;
  1757.     else if(delta_y == 0) incy = 0;
  1758.     else incy = -1;
  1759.  
  1760.     delta_x = abs(delta_x);
  1761.     delta_y = abs(delta_y);
  1762.     distance = delta_x > delta_y ? delta_x : delta_y;
  1763.  
  1764.     for(t = 0; t <= distance+1; t++) {
  1765.  
  1766. /*        ccc = getpixel(startx, starty);
  1767.         if(ccc != cc) putpixel(startx, starty,(ccc ^ cc)); 
  1768. */
  1769.         putpixel(startx, starty,cc);
  1770.  
  1771.         xerr += delta_x;
  1772.         yerr += delta_y;
  1773.         if(xerr > distance) {
  1774.             xerr -= distance;
  1775.             startx += incx;
  1776.         }
  1777.         if(yerr > distance) {
  1778.             yerr -= distance;
  1779.             starty += incy;
  1780.         }
  1781.     }
  1782. }
  1783.  
  1784. double xx[3], yy[3];
  1785.  
  1786.  
  1787.  
  1788. /* undocumented "A" function -- I am still experimenting to get curve fitting
  1789.    to work.   Screen aspect ratio is a b**ch.  This routine will paint a circle
  1790.    given three points on the circumference.  I can't get the sizing very good.
  1791.    From here one would arc from the angle of the first point to the angle of
  1792.    the third point.  The method used to get three points when we are used to
  1793.    inputting start,end stuff could be very useful for the ellipse and maybe
  1794.    some other things.
  1795. */
  1796.  
  1797.  
  1798. void xarc(int state)
  1799. {
  1800.     double xc, yc, rad;
  1801.     int tempx1, tempy1;
  1802.  
  1803.     if(!state) {
  1804.         if(header) p_message("Use <CR> to mark FIRST POINT");
  1805.         arc_count = 1;
  1806.         arc_mode = TRUE;
  1807.     } else {
  1808.         switch(arc_count) {
  1809.         case 1:
  1810.             xx[0] = x;
  1811.             yy[0] = y;
  1812.  
  1813.             putimage(x-4, y-3, arc1, XOR_PUT);
  1814.             if(header) p_message("Use <CR> to mark SECOND POINT");
  1815.             break;
  1816.         case 2:
  1817.             xx[1] = x;
  1818.             yy[1] = y;
  1819.             putimage(x-3, y-3, second, XOR_PUT);
  1820.             if(header) p_message("Use <CR> to mark END point");
  1821.             break;
  1822.         case 3:
  1823.             xx[2] = x;
  1824.             yy[2] = y;
  1825.  
  1826.             arc_mode = FALSE;
  1827.             putimage(xx[1]-3, yy[1]-3, second, XOR_PUT);
  1828.             putimage(xx[0]-4, yy[0]-3, arc1, XOR_PUT);
  1829. /*
  1830. gprintf(10, 60, "xx = %d, %d, %d", xx[0], xx[1], xx[2]);
  1831. gprintf(10, 80, "yy = %d, %d, %d", yy[0], yy[1], yy[2]);
  1832. */
  1833.             center(xx, yy, &xc, &yc, &rad);
  1834.         
  1835.             startx = xc;
  1836.             starty = yc;
  1837.             endx = xc + rad;
  1838.             endy = yc;
  1839.             xcircle(FALSE);
  1840.  
  1841. /*            circle(round(xc), round(yc), round(rad));
  1842.  
  1843.  
  1844. gprintf(10, 120, "xx = %d, %d, %d", xx[0], xx[1], xx[2]);
  1845. gprintf(10, 140, "yy = %d, %d, %d", yy[0], yy[1], yy[2]);
  1846. gprintf(10, 160, "xc = %d, yc = %d, rad = %d", round(xc), round(yc), round(rad));
  1847.  
  1848. */
  1849.         }
  1850.         key.i = 0;
  1851.         arc_count += 1;
  1852.     }
  1853. }
  1854.  
  1855. void center(double x[], double y[], double *xc, double *yc, double *r)
  1856. {
  1857.     double x2[2], xh[2], y2[2], yk[2];
  1858.     double x2y2[2], xnyn, ykn;
  1859.  
  1860.     x2[0] = sqr(x[1]) - sqr(x[0]);
  1861.     xh[0] = 2.0  * x[1] - 2.0  * x[0];
  1862.     y2[0] = sqr(y[1]) - sqr(y[0]);
  1863.     yk[0] = 2.0  * y[1] - 2.0  * y[0];
  1864.  
  1865.     x2[1] = sqr(x[2]) - sqr(x[1]);
  1866.     xh[1] = 2.0  * x[2] - 2.0  * x[1];
  1867.     y2[1] = sqr(y[2]) - sqr(y[1]);
  1868.     yk[1] = 2.0  * y[2] - 2.0  * y[1];
  1869.  
  1870.     x2y2[0] = x2[0] + y2[0];
  1871.     x2y2[1] = x2[1] + y2[1];
  1872.  
  1873.     xnyn = x2y2[0];
  1874.     ykn = yk[0];
  1875.  
  1876.     x2y2[0] *= xh[1];
  1877.     yk[0] *= xh[1];
  1878.  
  1879.     x2y2[1] *= xh[0];
  1880.     yk[1] *= xh[0];
  1881.  
  1882.     *yc = (x2y2[0] - x2y2[1]) / (yk[0] - yk[1]);
  1883.     *xc = (xnyn - (*yc * ykn)) / xh[0];
  1884.  
  1885.     x[0] -= *xc;
  1886.     y[0] -= *yc;
  1887.  
  1888.     *r = hypot(x[0], y[0]);
  1889. }
  1890.