home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / lambda / soundpot / a / backgmmn.lbr / MYLIB2.CZ / MYLIB2.C
Encoding:
C/C++ Source or Header  |  1993-10-26  |  13.3 KB  |  541 lines

  1. /*
  2. MYLIB2.C
  3.  
  4. These routines are CONDITIONALLY compiled; i.e., only as needed.
  5.  
  6. ----------------------------------------------------------------------
  7. Incorporates special mods used by my Backgammon game, BACKGMMN.C etc.
  8. ----------------------------------------------------------------------
  9.  
  10. A set of common I/O functions that seem to turn up a lot in my programs, 
  11. including terminal functions for Kaypro 10, 4'84, 2X etc.
  12.  
  13. Uses Software Toolworks' C/80 3.1 compiler.  Place #include "mylib2.c" at the
  14. end of your source file for correct CONDITIONAL COMPILATION. 
  15.  
  16. David C. Oshel
  17. 1219 Harding Ave.
  18. Ames, Iowa 50010
  19.  
  20. Last modified:  March 25, 1986
  21.  
  22. -----------------------------------------------------------------------------
  23. ** WARNING ** These routines use direct console IO, bdos function 6!
  24.  
  25.          YOU MUST CALL INIT_LIB() BEFORE USING THESE ROUTINES!
  26.  
  27.                       ======= UTILITIES =======
  28.  
  29.  * init_lib()        - CALL THIS FIRST, OR THE RESULT WILL BE VERY STRANGE!
  30.  *
  31.  * puts(p)        - unformatted print, e.g., puts("Hello, sailor!\n");
  32.  * gets(p,max)        - printable input only, no prompt character
  33.  *
  34.  * ask(p)        - demand Yes or No response to question p
  35.  * random()        - effective random 16-bit integer IFF gets() is used
  36.  * sleep(n)        - sleep n/10ths of a second, roughly (from C80.LIB)
  37.  * rollup()        - roll up 23 lines of screen
  38.  * ONscript()        - printer echo ON  for output via puts, chrout
  39.  * OFFscript()        - printer echo OFF for output via puts, chrout
  40.  * ONinterrupt()    - Ctl-C, Ctl-B cause program exit
  41.  * OFFinterrupt()    - Ctl-C, Ctl-B cause comedy
  42.  * hide_input(p,max)    - like gets, but used when entering passwords
  43.  * chrout(c)        - if scripting, echo output also to LST:
  44.  * putscreen(p)        - like puts, but always and only to screen
  45.  
  46.  
  47.            ======= KAYPRO 10 TERMINAL/VIDEO FUNCTIONS =======
  48.  
  49.  * gotoxy(x,y)        - 0,0 is top left, horz <= 79 precedes vert <= 24,
  50.  *                        where 0,24 is on the 25th, status, line.
  51.  * beep()        - terminal bell
  52.  * home()        - home cursor, do not clear screen
  53.  * clr_screen()        - home and clear
  54.  *
  55.  * shadow_box(h,v,x1,y1,x2,y2) - like box, but with shadow, calls box
  56.  * box(tlx,tly,brx,bry) - draw a line box, coords: topleft XY, bottomright XY
  57.  *              note that box calls ldraw(x1,y1,x2,y2), below
  58.  *
  59.  * clr_lend()        - clear from cursor to end of line
  60.  * clr_send()        - clear from cursor to end of screen
  61.  * rev_vid(),
  62.  * nor_vid()        - reverse field 
  63.  * dim_vid(),
  64.  * bri_vid()        - low/high intensity
  65.  * on_blink(),
  66.  * off_blink()        - blinking chars
  67.  * ul_start(),
  68.  * ul_stop()        - start/stop underline
  69.  * save_cursor(),
  70.  * retn_cursor()    - remember/restore current cursor location
  71.  * ins_line(),
  72.  * del_line()        - insert/delete screen text line
  73.  * on_cursor(),
  74.  * off_cursor()        - hide/show cursor
  75.  * vm_on(),
  76.  * vm_off()        - "Video Mode" commands
  77.  * pixel(x,y)        - draw pixel at x,y (video coords, x <= 159, y <= 99)
  78.  * no_pixel(x,y)    - erase pixel at x,y
  79.  * ldraw(x1,y1,x2,y2)      - draw/ erase graphics line, see discussion for box
  80.  * lwipe(x1,y1,x2,y2)    - range for video coordinates as for pixel
  81. */
  82.  
  83.  
  84. #ifndef TRUE
  85. #define TRUE 1
  86. #endif
  87. #ifndef FALSE
  88. #define FALSE 0
  89. #endif
  90.  
  91.  
  92. /* hide this here so's not to worry about it elsewhere */
  93. /* "printf.c" collides with one of these, can't remember which */
  94. /* puts() takes longer to write, but executes faster */
  95. extern char Cmode, IOpread[4], IOpwrit[4], IOpeof[4];
  96.  
  97. /* make these known only to what follows */
  98. static int MYbstout, MYscrtp, MYretnirp; /* odd names mark semi-private   */
  99. static unsigned RNDloc; /* effective random location, bumped by gets()    */
  100.             /* and scrambled when the LCG random() is called  */
  101.             /* makes a decent algorithm for interactive games */
  102.  
  103.  
  104. #ifneed init_lib
  105. init_lib() {
  106.  
  107.     MYretnirp = fopen("LST:","w");
  108.     OFFscript();
  109.     ONinterrupt();
  110.     Cmode = 0;
  111.     IOpread[0] = 6; IOpwrit[0] = 6;
  112.  
  113. } /* end: init_lib */
  114. #endif
  115.  
  116.  
  117.  
  118.  
  119. #ifneed random
  120. random() {      /* depends on effective random location spun by gets() */
  121.  
  122.     RNDloc = 2053 * RNDloc + 13849;
  123.     return (RNDloc);
  124. }
  125. #endif
  126.  
  127.  
  128.  
  129. #ifneed ONscript
  130. ONscript() {
  131.  
  132.     MYscrtp = TRUE; 
  133.  
  134. }
  135. #endif
  136. #ifneed OFFscript
  137. OFFscript() {
  138.  
  139.     MYscrtp = FALSE; 
  140.  
  141. }
  142. #endif
  143.  
  144.  
  145. #ifneed ONinterrupt
  146. ONinterrupt() {
  147.  
  148.     MYbstout = TRUE;
  149.  
  150. }
  151. #endif
  152. #ifneed OFFinterrupt
  153. OFFinterrupt() {
  154.  
  155.     MYbstout = FALSE;
  156.  
  157. }
  158. #endif
  159.  
  160.  
  161. #ifneed ask
  162. ask(p) char *p; {
  163.  
  164. char ch, resp[2];
  165.  
  166. loo:    puts(p);
  167.     gets(resp,1);
  168.     ch = toupper( *resp );
  169.     if ( !( ch == 'Y' || ch == 'N' )) {
  170.         puts("Please answer the question, Yes or No.\n");
  171.         goto loo;
  172.     }
  173.     return (ch == 'Y');
  174.  
  175. } /* end: ask */
  176. #endif
  177.  
  178.  
  179. #ifneed rollup
  180. rollup() {
  181.  
  182. int i;
  183.  
  184.     for (i=0; i<23; i++) puts("\n");
  185.  
  186. } /* end: rollup */ 
  187. #endif
  188.  
  189.  
  190. #ifneed sleep
  191. sleep( n )  int n;  {        /* sleep for n/10 seconds, 0 <= n < 256 */
  192.  
  193.     n;            /* get n into HL */
  194. #asm
  195.     MOV    B,L        ;delay B/10ths of a second
  196. __DL0:    MVI    A,100        ;100 milliseconds, 1/10 second
  197. __DL1:    MVI    C,249        ;1 millisecond per unit of A at 4 MHz
  198. __DL2:    DCR    C        ;Leventhal, Z80 Assembly Language Programming
  199.     JNZ    __DL2
  200.     DCR    A
  201.     JNZ    __DL1
  202.     DCR    B
  203.     JNZ    __DL0        ;on exit, HL has FALSE if n was 0, else TRUE
  204. #endasm
  205. } /* end: sleep */
  206. #endif
  207.  
  208.  
  209. /*========================================*/
  210. /*  GETS(p, maxinput)                     */
  211. /*  Local getline function with special   *---* WARNING:                    */
  212. /*  input handling,  1 <= len <= maxinput *---* Execute INIT_LIB() first !! */
  213. /*  Updates effective random, RNDloc,     */
  214. /*  Forces input from CONSOLE only!       */
  215. /*========================================*/ 
  216.  
  217. #ifneed gets
  218. gets(p,maxinput) char *p; int maxinput; {
  219.  
  220. /* This function depends on BDOS Function #6.  Init_lib() sets Cmode=0 and
  221.    IOpread[0]=6 and IOpwrit[0]=6 (courtesy of and peculiar to C/80 3.1)
  222.    YOU must ensure that the target string is long enough to collect the
  223.    entire maximum input allowed and specified, INCLUDING FINAL NULL!    */
  224.  
  225. static int len;
  226. static char ch;
  227.  
  228.     len = -1;
  229.     if (maxinput < 1 || maxinput > 127) maxinput = 79;
  230.  
  231.         /*--------------------------------*/
  232.         /* SPECIAL ROUTINE FOR BACKGAMMON */
  233.         /*--------------------------------*/
  234.  
  235. loo:    while ( !(ch = getc(0)) ) acg(); /* keep the game lively */
  236.  
  237.     if (len < 0) len = 0;    /* don't destroy prompt by backing up */
  238.     if (ch == '\n') {      /* end of line?  don't store newline */
  239.         *p = '\0';    /* mark it with a B for baby and me */
  240.         /* chrout('\n'); */ /* but DON'T echo newline */
  241.         return ( len ); /* <--- HERE IS THE FUNCTION EXIT! */
  242.     }
  243.     else if (ch == '\b' || ch == 0x7F) {      /* backspace? rubout? */
  244.         if (len--) {            /* where's the prompt?  */
  245.             puts("\008 \008");    /* we're ok, echo erase */
  246.             p--;            /* delete from string */
  247.         }
  248.     }
  249.  
  250.         /*--------------------------------*/
  251.         /* SPECIAL ROUTINE FOR BACKGAMMON */
  252.         /*--------------------------------*/
  253.  
  254.     else if (ch == '\003') {  /* user bailout key is Ctrl-C, not ESC */
  255.         if (MYbstout) exit();
  256.         else {
  257.                      haltgame(); /* sets whofirst flag and does jumpjack() */
  258.         }
  259.     }
  260.  
  261.     else if (ch == '\025' || ch == '\030') {  /* Ctl-U, Ctl-X */
  262.         while (len--) {
  263.             p--;
  264.             puts("\008 \008");
  265.         }
  266.     }
  267.     else if (len == maxinput) { /* test specials before testing len */
  268.         chrout('\007');
  269.     }
  270.     else if (ch > 31 && ch < 127) { /* printable char? */
  271.         chrout(ch);        /* yes, echo it */
  272.         *p++ = ch;        /* collect it */
  273.         len++;            /* keep track of it */
  274.     }
  275.     else {              /* control chars? */
  276.         chrout('\007');
  277.     }        
  278.     goto loo;
  279.  
  280. } /* end: gets */
  281. #endif
  282.  
  283.  
  284.  
  285.  
  286. #ifneed hide_input
  287. hide_input(s,len) char *s; int len; {
  288.  
  289. /* receive at most len chars in s buffer, 
  290.    terminate string with zero, 
  291.    but echo each char with 1, 2, or 3 meaningless dots */
  292.  
  293. char ch; int num;
  294.  
  295.     if ((len < 1) || (len > 127)) len = 127;
  296.     num = 0;
  297.     for (;;) { /* forever */ 
  298.     while ((ch = getc(0)) == 0) /* bdos 6 does not wait, so we do */
  299.     ;
  300.     if ((ch == '\r') || (ch == '\n') || (num++ > len)) { 
  301.         /* not sure what the CR key actually is to bdos 6 & C/80 */
  302.         *s++ = '\0';
  303.         return;  /* this way out */
  304.     }
  305.     if ((num % 2) == 0) putc('.',0); /* deception, illusion */
  306.     if ((num % 5) == 0) putc('.',0);
  307.     putc('.',0);
  308.     *s++ = ch;
  309.     }
  310.  
  311. } /* end: hide_input */
  312. #endif
  313.  
  314.  
  315.  
  316.  
  317. /*------------------------ kpro stuff -------------------------*/
  318.  
  319. #ifneed shadow_box
  320. /* like box, but with horizontal & vertical displacement for shadow */
  321. shadow_box(h,v,x1,y1,x2,y2) int h,v,x1,y1,x2,y2;
  322. {
  323.     box(x1+h,y1+v,x2+h,y2+v);   /* draw the shadow  */
  324.     box(x1,y1,x2,y2);        /* draw the box     */
  325.     ldraw(x1+h,y1+v,x1,y1);     /* draw the corners */
  326.     ldraw(x2+h,y2+v,x2,y2);
  327.     ldraw(x2+h,y1+v,x2,y1);
  328.     ldraw(x1+h,y2+v,x1,y2);
  329. }
  330. #endif
  331.  
  332.  
  333. #ifneed box
  334. /* parameters are topleft X,Y and bottomright X,Y 
  335.    X ranges from 0 to 159, Y ranges from 0 to 99, top left is 0,0
  336.    */
  337. box(x1,y1,x2,y2) int x1,y1,x2,y2; {
  338.     ldraw(x1,y1,x1,y2);
  339.     ldraw(x1,y2,x2,y2); /* appears to draw the box anticlockwise */ 
  340.     ldraw(x2,y1,x2,y2);
  341.     ldraw(x1,y1,x2,y1);
  342. }
  343. #endif
  344.  
  345.  
  346.  
  347. #ifneed gotoxy
  348. gotoxy (xpos,ypos) int xpos,ypos; {  /* 0,0 is top left corner */
  349.     putscreen("\033=");
  350.     putc(ypos+' ',0);
  351.     putc(xpos+' ',0);
  352.     }
  353. #endif
  354.  
  355.  
  356. #ifneed beep
  357. beep()        { putc(7,0); }     /* send bell character */
  358. #endif
  359.  
  360. #ifneed home
  361. home()        { putc(30,0); }    /* home cursor to top left */
  362. #endif
  363.  
  364. #ifneed clr_screen
  365. clr_screen()     { putc(26,0); }    /* home and erase screen */
  366. #endif
  367.  
  368.  
  369. #ifneed clr_lend
  370. clr_lend()     { putc(24,0); }    /* clear to end of line */
  371. #endif
  372.  
  373. #ifneed clr_send
  374. clr_send()     { putc(23,0); }    /* clear to end of screen */
  375. #endif
  376.  
  377.  
  378.  
  379. #ifneed rev_vid
  380. rev_vid()     { putscreen ("\033B0"); }    /* reverse background */
  381. #endif
  382.  
  383. #ifneed nor_vid
  384. nor_vid()     { putscreen ("\033C0"); }
  385. #endif
  386.  
  387.  
  388.  
  389. #ifneed dim_vid
  390. dim_vid()     { putscreen ("\033B1"); }    /* low intensity */
  391. #endif
  392.  
  393. #ifneed bri_vid
  394. bri_vid()     { putscreen ("\033C1"); }
  395. #endif
  396.  
  397.  
  398.  
  399. #ifneed on_blink
  400. on_blink()    { putscreen ("\033B2"); }    /* blinking characters */
  401. #endif
  402.  
  403. #ifneed off_blink
  404. off_blink()     { putscreen ("\033C2"); }
  405. #endif
  406.  
  407.  
  408.  
  409. #ifneed ul_start
  410. ul_start()     { putscreen ("\033B3"); }    /* underline */
  411. #endif
  412.  
  413. #ifneed ul_stop
  414. ul_stop()     { putscreen ("\033C3"); }
  415. #endif
  416.  
  417.  
  418. #ifneed save_cursor
  419. save_cursor()    { putscreen ("\033B6"); }  /* remember cursor position */
  420. #endif
  421. #ifneed retn_cursor
  422. retn_cursor()    { putscreen ("\033C6"); }  /* return to remembered pos */
  423. #endif
  424.  
  425.  
  426. #ifneed on_status
  427. on_status()    { putscreen ("\033B7"); }    /* status line preservation on */
  428. #endif
  429. #ifneed off_status
  430. off_status()    { putscreen ("\033C7"); }
  431. #endif
  432.  
  433.  
  434. #ifneed ins_line
  435. ins_line() {                /* insert text line */
  436.     putscreen("\033R");
  437.     }
  438. #endif
  439. #ifneed del_line
  440. del_line() {                /* delete text line */
  441.     putscreen("\033E");
  442.     }
  443. #endif
  444.  
  445.  
  446. #ifneed on_cursor
  447. on_cursor()     { putscreen ("\033B4"); }    /* (in)visible cursor */
  448. #endif
  449. #ifneed off_cursor
  450. off_cursor()     { putscreen ("\033C4"); }
  451. #endif
  452.  
  453.  
  454.  
  455. /* Video Mode ON/OFF: video WORD, 8 bit video if 15 and 7 are both high */
  456. /*                    VM-ON 10000001 11111111 VM-OFF                    */
  457. /*                          ^video ^x^video                             */
  458. /* otherwise, video BYTE, high bit 7 interprets bits 0-6 as screen dots */
  459. /*                    11111111                                          */
  460. /*                    ^video                                            */
  461. /* e.g.,                                                                */
  462. /*        Non-VideoMode                 VideoMode                       */
  463. /*    xx     1 11:0     where % is    1 01:0  1 11:0      xx            */
  464. /*    xx     3 11 2     the video     3 00 2  3 11 2      xx            */
  465. /*    xx     5 11 4     flag bit,     5 00 4  5 11 4      xx            */
  466. /*     x     7:%1 6     x is pixel    7:%0 6  7:%1 6      xx            */
  467. /*             ^                        ^       ^                       */
  468. /* to set the pixels, first do a gotoxy to character screen position    */
  469. /* this mode is faster than Pixel ON/OFF if values are drawn from table */
  470. #ifneed vm_on
  471. vm_on()        { putscreen ("\033B5"); }    /* video mode on */
  472. #endif
  473. #ifneed vm_off
  474. vm_off()    { putscreen ("\033C5"); }
  475. #endif
  476.  
  477.  
  478. #ifneed pixel
  479. pixel(x,y) int x,y; {            /* x <= 159, y <= 99 */
  480.     putscreen("\033*");
  481.     putc(y+' ',0); putc(x+' ',0);
  482. }
  483. #endif
  484.  
  485. #ifneed no_pixel
  486. no_pixel(x,y) int x,y; {        /* x <= 159, y <= 99 */
  487.     putscreen("\033 ");
  488.     putc(y+' ',0); putc(x+' ',0);
  489. }
  490. #endif
  491.  
  492.  
  493. #ifneed ldraw
  494. /* use x1 <= x2, y1 <= y2, order is significant (Kaypro bug?) */
  495. ldraw(x1,y1,x2,y2) int x1,x2,y1,y2; {    /* x <= 159, y <= 99 */
  496.     putscreen("\033L");
  497.     putc(y1+' ',0); putc(x1+' ',0); 
  498.     putc(y2+' ',0); putc(x2+' ',0);
  499. }
  500. #endif
  501.  
  502.  
  503. #ifneed lwipe
  504. lwipe(x1,y1,x2,y2) int x1,x2,y1,y2; {    /* x <= 159, y <= 99 */
  505.     putscreen("\033D");
  506.     putc(y1+' ',0); putc(x1+' ',0); 
  507.     putc(y2+' ',0); putc(x2+' ',0);
  508. }
  509. #endif
  510.  
  511.  
  512. #ifneed putscreen
  513. putscreen(p) char *p; {
  514.  
  515.     while (*p) putc(*p++,0);
  516.  
  517. } /* end: putscreen */
  518. #endif
  519.  
  520.  
  521. #ifneed puts
  522. puts(p) char *p; {
  523.  
  524.     while (*p) chrout(*p++);
  525.  
  526. } /* end: puts */
  527. #endif
  528.  
  529.  
  530. #ifneed chrout
  531. chrout(c) char c; {  /* SPECIAL FOR SCRIPT OPTION WITH LST: */
  532.  
  533.     putc(c,0);
  534.     if ( MYscrtp ) putc(c,MYretnirp);
  535.  
  536. } /* end: chrout */
  537. #endif
  538.  
  539. /* end: MYLIB.C */
  540.  
  541.