home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 201_01 / ansisys.c < prev    next >
Text File  |  1979-12-31  |  14KB  |  282 lines

  1. /** ansisys.c
  2. *
  3. *   ANSISYS.C
  4. *   (C) Copyright 1985 Don F. Ridgway
  5. *   All rights reserved.
  6. *   This program may be copied for
  7. *   personal, non-profit use only.
  8. *
  9. *   This is an original and unique C programming
  10. *   language header/function file to #include with
  11. *   your C programs to give them "smart" cursor
  12. *   control and eye-catching "turtlegraphics"-
  13. *   type screen and graphics display qualities.
  14. *
  15. *   Programmed by:
  16. *   Don F. Ridgway
  17. *   Owner & Chief Programmer/Analyst
  18. *   A-1 IBM Programming & Training Service
  19. *   CUSTOM BUSINESS PROGRAMS
  20. *   119 Plantation Ct., Suite D
  21. *   Temple Terrace, FL  33617-3731
  22. *   Ph: (813) 985-3342  (10:00 am - 2:00 pm EST)
  23. *
  24. *   Written, compiled & tested in Microsoft C,
  25. *   ver. 2.03, and Lattice C, ver. 2.15, under
  26. *   PC-DOS 2.1 on a Compaq w/640Kb RAM & 8087, using
  27. *   the TURBO Pascal 3.0 screen editor.  (260+ lines.)
  28. *
  29. *   NOTE:  To utilize these macros you must have: (1) The
  30. *   ANSI.SYS file that came with your PC-DOS or MS-DOS 2.xx
  31. *   operating system on your boot disk and, (2) you must boot
  32. *   up with a CONFIG.SYS file on that boot disk which
  33. *   contains the statement: DEVICE = ANSI.SYS.  This loads
  34. *   the ANSI.SYS device driver into DOS at bootup time.  The
  35. *   operating system searches (for) CONFIG.SYS before it looks
  36. *   for an AUTOEXEC.BAT file.  Please refer to your DOS
  37. *   Reference Guide under "ANSI.SYS" and "COPY" for details.
  38. *
  39. *   (Simply, at A> prompt on a boot diskette, type:
  40. *                 COPY CON:CONFIG.SYS<cr>
  41. *                 DEVICE=ANSI.SYS<cr>
  42. *                 <F6><cr>
  43. *   and then reboot and you're ready to go!  Small bother
  44. *   for the brilliant performance gained in your C programs--
  45. *   just have these two files, ANSI.SYS and CONFIG.SYS, on
  46. *   your boot disk whenever you boot up.)
  47. *
  48. *   (The diskette these programs are sent to you on is a
  49. *   PC-DOS 2.1 boot disk so you may boot up with it and run
  50. *   ANSIDEMO.EXE.  Note the ANSI.SYS and CONFIG.SYS files.)
  51. *
  52. *   This custom C module/header file connects the C programming
  53. *   language to the MS-DOS/PC-DOS "ANSI.SYS" device driver
  54. *   used to implement extended screen and keyboard functions.
  55. *   Like any C program, each of the following macros can itself
  56. *   become a building block for a still larger one.  Note the
  57. *   evolution of WINDOW(row1,col1,row2,col2,fill,border) from
  58. *   DRAW(row1,col1,row2,col2,icon) and FILL(row1,col1,row2,col2,fill).
  59. *
  60. *   Please refer to the MS-DOS/PC-DOS Reference Manual and the
  61. *   ANSI.SYS Device Driver commands for the "original" commands
  62. *   and control sequences that are here made into C macros.
  63. *
  64. *   Refer to the IBM Technical Reference Manual or to the
  65. *   appendix of the BASIC Version 2 Reference for the ASCII
  66. *   Character Codes and the Extended Keyboard Function codes.
  67. *
  68. *   Run the ANSIDEMO.EXE for a superb demonstration of all these
  69. *   powerful macros and C programming tools in action.  The
  70. *   actual source code is included in ANSIDEMO.C, an excellent
  71. *   demonstration/introduction to the C programming language.
  72. *
  73. *   Simply #include "ansisys.c" this file in your programs
  74. *   to enable the following "smart" screen and cursor commands
  75. *   to really supercharge your C programs with professional
  76. *   features that are easier, safer and more portable than
  77. *   tacked-on assembly languge routines.
  78. *
  79. *   Remember that C is "case sensitive" so be sure and
  80. *   reference the following macros with CAPITAL LETTERS.
  81. *
  82. **/
  83.  
  84. #define BEEP                           printf("\007")
  85.         /* 800 Mz tone for 1/4 second -- same as PRINT CHR$(7) */
  86. #define CLEARSCREEN                    printf("\033[2J")
  87. #define CLS                            CLEARSCREEN
  88.         /* clears the screen and positions cursor at top left corner */
  89.         /* "\033" is Octal for "Escape" or ASCII Decimal 27  (CHR$(27)) */
  90.         /* "Escape-[" is the lead-in for the ANSI.SYS code routines */
  91. #define CURSPOS(x,y)                   printf("\033[%u;%uH",(x),(y))
  92. #define XY(x,y)                        CURSPOS(x,y)
  93.         /* positions cursor at x = row, y = column */
  94. #define EOL                            printf("\033[K")
  95.         /* erases to end of line, including cursor position */
  96.         /* NOTE:  error in DOS documentation has 'K' lower case */
  97. #define XYEOL(x,y)                     printf("\033[%u;%uH\033[K",(x),(y))
  98.         /* positions cursor at x,y then erases to end of line */
  99. #define XYWHERE        printf("\033[6n");scanf("%*1c%2d%*1c%2d%*2c",&row,&col)
  100.         /* requests cursor position, device driver answers row,col--declare int */
  101. #define CURSUP(x)                      printf("\033[%uA",(x))
  102. #define CURSDWN(x)                     printf("\033[%uB",(x))
  103.         /* cursor up or down x-many lines */
  104. #define CURSFWD(y)                     printf("\033[%uC",(y))
  105. #define CURSBCK(y)                     printf("\033[%uD",(y))
  106.         /* cursor forward (right) or backward (left) y-many spaces */
  107. #define SAVCURS                        printf("\033[s")
  108. #define RECALLCURS                     printf("\033[u")
  109.         /* cursor position is saved for later recall via RECALLCURS */
  110. #define CPR(x,y,z)                     printf("\033[%u;%uH%c",(x),(y),(z))
  111. #define XYCHAR(x,y,z)                  CPR(x,y,z)
  112.         /* position cursor at x,y and print char z (using ASCII code) */
  113. #define XCTRPRINTF(x,str)   printf("\033[%u;%uH%s",(x),((80-(strlen(str)-1))/2),str)
  114.         /* on row x, center (and printf) the string str (in double quotes) */
  115. #define CURSPOSPRTF(x,y,str)           printf("\033[%u;%uH%s",(x),(y),str)
  116. #define XYPRINTF(x,y,str)              CURSPOSPRTF(x,y,str)
  117.         /* at position x,y printf the string str (in double quotes) */
  118. #define XKREAD(x)                      x=0;x=bdos(1);if (bdos(11)) x=bdos(8)+128
  119.         /* extended code keyboard read, reads function keys, arrow keys, etc. */
  120. #define XKREADE(x)                     x=0;x=bdos(1);if (bdos(11)) x=bdos(1)+128
  121.         /* same as XKREAD(), except this one echoes the input on the screen */
  122. #define CHKBRK                          if (key==196) break
  123.         /* if F10 key was pressed, break out of loop */
  124. #define SETSCREEN(a)                   printf("\033[=%uh",a)
  125.      /* set screen graphics mode */
  126.      /* 0=40x25 monochrome,1=40x25 color,2=80x25 mono,3=80x25 color,        */
  127.      /* 4=320x200 color,5=320x200 mono,6=640x200 mono,7=enable word-wrap.   */
  128. #define RESETSCREEN(a)                 printf("\033[=%ul",a)
  129.      /* reset screen graphics mode */
  130.      /* the attributes are same as SETSCREEN(a) except 7=disables word-wrap */
  131. #define SETDISPLAY(a,b,c)              printf("\033[%u;%u;%um",a,b,c)
  132.      /* set screen display attributes and colors = (a,b,c) any order:       */
  133.      /* 0 = default, 1 = high intensity, 4 = underline,                     */
  134. /* 5=blinking,7=inverse,8=invisible (black-on-black),30=foreground black,   */
  135. /* 31=fore red,32=fore green,33=fore yellow,34=fore blue,35=fore magenta,   */
  136. /* 36=fore cyan,37=fore white,40=background black,41=back red,42=back green,*/
  137. /* 43=back yellow,44=back blue,45=back magenta,46=back cyan,47=back white.  */
  138. #define HLON                           SETDISPLAY(0,0,1)
  139.         /* set high light (high intensity) on */
  140. #define BLON                           SETDISPLAY(0,0,5)
  141.         /* set blinking on */
  142. #define HLOFF                          SETDISPLAY(0,0,0)
  143. #define BLOFF                          HLOFF
  144.         /* set high intensity, blink (and all other display attributes) to off */
  145. #define PROMPT(x,y,cc)            SETDISPLAY(0,0,7);printf("\033[%u;%uH",(x),(y));\
  146.                                        cc=getchar();SETDISPLAY(0,0,0)
  147.         /* at position x,y read inverse prompt for input cc */
  148. #define XKPROMPT(x,y,z)             HLON;XY((x),(y));printf(" \b");XKREAD(z);HLOFF
  149.         /* at position x,y read highlighted prompt for input z */
  150. #define WINDOW(a,b,c,d,e,f)            DRAW(a,b,c,d,f);FILL(a+1,b+2,c-1,d-2,e)
  151.         /* a rectangle determined by upper left-hand corner coordinates, */
  152.         /* row1 = a, col1 = b, and lower right-hand corner coordinates,  */
  153.         /* row2 = c, col2 = d, is filled with extended graphics character */
  154.         /* ASCII decimal code e, and the border is ASCII decimal code f  */
  155. #define WINDOW2(a,b,c,d,e,f)        DRAW(a,b,c,d,f);DRAW(a+1,b+1,c-1,d-1,255);\
  156.                                        FILL(a+1,b+2,c-1,d-2,e)
  157.         /* same as WINDOW(a,b,c,d,e,f) except use this one to overwrite other */
  158.         /* drawings because this one fills empty spaces with blanks */
  159.  
  160. /* ------------------------------------------------------------- */
  161. /** DRAW(row1,col1,row2,col2,icon)                               */
  162. /*                                                               */
  163. /*  can be rectangle, vertical line, horizontal line or point!   */
  164. /*                                                               */
  165. /* row1,col1=Upper Left-hand corner of border                    */
  166. /* row2,col2=Lower Right-hand corner                             */
  167. /* icon=ASCII Decimal number of Character want border made of    */
  168. /*                                                               */
  169. /*   (Note:  Error-trapping is up to you in calling program,     */
  170. /*           e.g., [0<=row<=24], [0<=col<=80], graphics mode,    */
  171. /*           etc.)                                               */
  172. /*                                                               */
  173. /* Dbl Lines=205;Sngl Line=196;Dark=176;Medium=177;Light=178     */
  174. /* White=219;Blank=255;Sunshine=15;Music notes=14;Asterisks=42   */
  175. /* Happy Face=1,2;Hearts=3;Diamonds=4;Clubs=5;Spades=6;Beeps=7   */
  176. /* ------------------------------------------------------------- */
  177. /**/
  178.  
  179. DRAW(row1,col1,row2,col2,icon)
  180. int row1,col1,row2,col2,icon;
  181. {
  182.    int hlen,vlen,r,c,hzl,vtl,ulc,llc,urc,lrc;
  183.  
  184.    hlen=col2-col1;
  185.    vlen=row2-row1;
  186.    if (hlen<0 || vlen<0) BEEP;         /* audibly alert possible input error */
  187.  
  188.    if (hlen<=0 && vlen<=0)             /* then it's a point or a corner */
  189.       {
  190.       CPR(row1,col1,icon);
  191.       return(0);
  192.       }
  193.  
  194.    if (vlen<=0)                        /* then it's a horizontal line */
  195.       {
  196.       CURSPOS(row1,col1);
  197.       for (c=0;c<=hlen;c++)
  198.          printf("%c",icon);
  199.          return(0);
  200.       }
  201.  
  202.    switch (icon)
  203.       {
  204.       case 196:                        /* for Single line border */
  205.       case 218:
  206.                 hzl=196;vtl=179;ulc=218;llc=192;urc=191;lrc=217;
  207.                 break;
  208.       case 201:                        /* for Double line border */
  209.       case 205:
  210.                 hzl=205;vtl=186;ulc=201;llc=200;urc=187;lrc=188;
  211.                 break;
  212.       case 213:                        /* for Double top, single side */
  213.                 hzl=205;vtl=179;ulc=213;llc=212;urc=184;lrc=190;
  214.                 break;
  215.       default:
  216.                 hzl=vtl=ulc=llc=urc=lrc=icon;  /* for same char all around */
  217.       }
  218.  
  219.    if (hlen<=0)              /* it's a vertical line -- use vtl from above */
  220.       {
  221.       CURSPOS(row1,col1);
  222.       for (r=row1;r<=row2;r++)
  223.          CPR(r,col1,vtl);
  224.          return(0);
  225.       }
  226.                              /* if it's fallen through this far it's a rectangle */
  227.    CURSPOS(row1,col1);
  228.    for (c=1;c<=hlen;c++)     /* print horizintal icon top row, left to right */
  229.       printf("%c",hzl);
  230.    CPR(row1,col2,urc);       /* print upper right-hand corner */
  231.    for (r=row1+1;r<row2;r++) /* print vertical right-hand column, top to bottom */
  232.      CPR(r,col2,vtl);
  233.    CPR(row2,col2,lrc);       /* print lower right-hand corner */
  234.    CURSPOS(row2,col2-1);
  235.    for (c=1;c<=hlen;c++)     /* print horizontal bottom row, right to left */
  236.       printf("%c\b\b",hzl);  /* one forward, two back (NOTE: this is slow) */
  237.    CPR(row2,col1,llc);       /* print lower left-hand corner */
  238.    for (r=row2-1;r>row1;r--) /* print vertical left-hand column, bottom to top */
  239.      CPR(r,col1,vtl);
  240.    CPR(row1,col1,ulc);       /* print upper left-hand corner to complete object */
  241.    return(0);
  242. }                            /* end DRAW() function */
  243.  
  244. /* ------------------------------------------------------------- */
  245. /** FILL(row1,col1,row2,col2,icon)                               */
  246. /*                                                               */
  247. /*  can be "window," vertical line, horizontal line or point!    */
  248. /*                                                               */
  249. /* row1,col1=Upper Left-hand corner of area-to-be-filled         */
  250. /* row2,col2=Lower Right-hand corner                             */
  251. /* icon=ASCII Decimal number of Character want area filled with  */
  252. /*                                                               */
  253. /*   (Note:  Error-trapping is up to you in calling program,     */
  254. /*           e.g., [0<=row<=24], [0<=col<=80], graphics mode,    */
  255. /*           etc.                                                */
  256. /*                                                               */
  257. /* Dbl Lines=205;Sngl Line=196;Dark=176;Medium=177;Light=178     */
  258. /* White=219;Blank=255;Sunshine=15;Music notes=14;Asterisks=42   */
  259. /* Happy Face=1,2;Hearts=3;Diamonds=4;Clubs=5;Spades=6;Beeps=7   */
  260. /* ------------------------------------------------------------- */
  261. /**/
  262.  
  263. FILL(row1,col1,row2,col2,icon)
  264. int row1,col1,row2,col2,icon;
  265. {
  266.    int hlen,vlen,r,c;
  267.  
  268.    hlen=col2-col1;
  269.    vlen=row2-row1;
  270.    if (hlen<0 || vlen<0) BEEP;  /* audibly alert possible input error */
  271.  
  272.    for (r=row1;r<=row2;r++)
  273.      {
  274.      CURSPOS(r,col1);
  275.        {
  276.        for (c=0;c<=hlen;c++)
  277.          printf("%c",icon);
  278.        }
  279.      }
  280.    return(0);
  281. }                            /* end FILL() function */
  282.