home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / mt / writscrn.c < prev   
C/C++ Source or Header  |  1989-01-12  |  7KB  |  228 lines

  1. /* writscrn.c  utilities to write chars to any position on screen */
  2. /* uses hi res cursor functions in linedraw.c */
  3. /* `MIDI Sequencing In C', Jim Conger, M&T Books, 1989 */
  4.  
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <string.h>
  8.  
  9. #include "screenf.h"
  10. #include "standard.h"
  11.  
  12.  
  13. /* Moves highlighted cursor block based on arrow key input.  first is the */
  14. /* menu array element to start on, last is the last (usually the number of */
  15. /* array elements in the the menu deffinition - 1). */
  16. /* Menu definition is in scrnarray[] (see MTSCREEN.H) */
  17. /* Returns element no selected, -2 for ESC, -3 for help (?), -1 on error. */
  18. int
  19. movescrn(int vidmode, struct selement scrnarray[], int first, int last, 
  20.     int normal, int hilit)
  21. {
  22.     int c, k, i, oldk, curskey;
  23.  
  24.     if (first < 0)
  25.         first = 0;
  26.     i = k = first;  
  27.     to_new_csr(vidmode, scrnarray, k, -1, normal, hilit);
  28.  
  29.     while(1){
  30.         oldk = k;
  31.         while(kbhit())              /* clear keyboard buffer */
  32.             getch();
  33.         while(!kbhit())             /* wait for new keypress */
  34.                 ;
  35.  
  36.         if(c = getch()){            /* check for cursor keypress code 0 */
  37.             curskey = 0;
  38.         }
  39.         else{
  40.             curskey = 1;
  41.             c = getch();    /* note null char and get next */
  42.         }
  43.     
  44.         if(!curskey){       /* advance to first matching char */
  45.             switch(c){
  46.             case BACKSP:
  47.                 k = scrnarray[k].nleft;
  48.                 break;
  49.             case TAB:
  50.                 k = scrnarray[k].nright;
  51.                 break;
  52.             case ESC:
  53.                 return(-2);
  54.             case CR:
  55.             case '+':
  56.                 to_new_csr(vidmode, scrnarray, k, -1, normal, hilit);
  57.                 return(k);
  58.             }
  59.             if (i >= last || i <= 0)
  60.                 i = 0;
  61.             else
  62.                 i++;
  63.             for(; i <= last; i++){      /* see if letter matches command */
  64.                 if(toupper(c) == toupper(*(scrnarray[i].content))){
  65.                     k = i;
  66.                     break;
  67.                 }
  68.             }
  69.             if (k != i && oldk != 0){   /* if no match, but started after */
  70.                 for(i = 0; i <= last; i++){  /* first element - try again */
  71.                     if(toupper(c) == toupper(*(scrnarray[i].content))){
  72.                         k = i;
  73.                         break;
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.         else{           /* must be cursor key */
  79.             switch(c){
  80.             case KUP:
  81.                 k = scrnarray[k].nup;
  82.                 break;
  83.             case KDOWN:
  84.                 k = scrnarray[k].ndown;
  85.                 break;
  86.             case KLEFT:
  87.                 k = scrnarray[k].nleft;
  88.                 break;
  89.             case KRIGHT:
  90.                 k = scrnarray[k].nright;
  91.                 break;
  92.             case KHOME:
  93.             case KPGUP:
  94.                 k = 0;
  95.                 break;
  96.             case KEND:
  97.             case KPGDN:
  98.                 k = last;
  99.                 break;
  100.             }
  101.         }
  102.         to_new_csr(vidmode, scrnarray, k, oldk, normal, hilit);
  103.     }
  104.     return(k);
  105. }
  106.  
  107.  
  108.  
  109. /* Move highlight to new active menu item.  Uses vimode (video mode) to */
  110. /* determine if in graphics or character mode.  In graphics modes the */
  111. /* string is underlined.  In char. modes the string is switched from */
  112. /* normal to hilit video attribute.  k is scrnarray[] element number. */
  113. /* oldk is last item highlighted - now needs to be unhighlighted.  A -1 */
  114. /* value for oldk skips the unhighlighting step (first time on menu). */
  115. void
  116. to_new_csr(int vidmode, struct selement scrnarray[], int k, int oldk, 
  117.     int normal, int hilit)
  118. {
  119.     if(vidmode < 4 || vidmode == 7){    /* char mode */
  120.         if (oldk != -1){                /* if not first time initialization */
  121.             writeword(scrnarray[oldk].content, scrnarray[oldk].xpos, 
  122.                 scrnarray[oldk].ypos, normal);
  123.         }
  124.         writeword(scrnarray[k].content, scrnarray[k].xpos,
  125.             scrnarray[k].ypos, hilit);
  126.     }
  127.     else{                           /* graphics mode */
  128.         if (oldk != -1){            /* if not first time initialization */
  129.             wordul(vidmode, scrnarray[oldk].content, scrnarray[oldk].xpos, 
  130.                 scrnarray[oldk].ypos, hilit & 0x000F);
  131.         }
  132.         wordul(vidmode, scrnarray[k].content, scrnarray[k].xpos,
  133.             scrnarray[k].ypos, hilit & 0x000F);
  134.     }
  135. }
  136.  
  137.  
  138. /* Put an error message on screen, wait for keypress, then clear line. */
  139. /* String s is written on line lineno with hilit attribute.  After the */
  140. /* user hits a key the line is cleared using normal attribute. */
  141. void
  142. writerr(char *s, int lineno, int normal, int hilit)
  143. {
  144.     char c, str[SCRNWIDE + 1];
  145.  
  146.     while(kbhit())
  147.         getch();
  148.     
  149.     clearline(lineno, normal);
  150.     strcpy(str, s);
  151.     strcat(str, " [Hit a key]");
  152.     writeword(str, 1, lineno, hilit);
  153.     
  154.     while(!kbhit())                 /* wait for keypress */
  155.             ;
  156.  
  157.     clearline(lineno, normal);
  158. }
  159.  
  160.  
  161. /* Writes the menu item strings on the screen in color attrib.  Starts */
  162. /* on scrnarray[] element first, goes to last. */
  163. /* bios screen write (slow) version - works in all video modes. */
  164. void
  165. initscrn(struct selement scrnarray[], int first, int last, int attrib)
  166. {               
  167.     int k;      
  168.  
  169.     for(k = first; k <= last; k++){
  170.         writeword(scrnarray[k].content, scrnarray[k].xpos, scrnarray[k].ypos,
  171.             attrib);
  172.     }
  173. }
  174.  
  175.  
  176. /* direct video (fast) version of initscrn - only for char modes. */
  177. void
  178. finitscrn(struct selement scrnarray[], int first, int last, int attrib, 
  179.     int mode)
  180. {               
  181.     int k;      
  182.  
  183.     for(k = first; k <= last; k++){
  184.         fwriteword(scrnarray[k].content, scrnarray[k].xpos, scrnarray[k].ypos,
  185.             attrib, mode);
  186.     }
  187. }
  188.  
  189.  
  190. /* Write string to x,y on screen, writes to screen memory directly */
  191. /* String is written with color attrib. */ 
  192. void
  193. fwriteword(char *string, int x, int y, int attrib, int mode)
  194. {
  195.     unsigned int offset;
  196.     
  197.     x--;        /* To conform with library convention of having */
  198.     y--;        /* the first row and column position be 1,1 not 0,0. */
  199.     
  200.     offset = (x * 2) + (y * SCRNWIDE * 2);
  201.     
  202.     while(*string != '\n' && *string){
  203.         if (mode <= 6){
  204.             *(char far *)(CVIDMEM + (offset++)) = *string++;
  205.             *(char far *)(CVIDMEM + (offset++)) = attrib;
  206.         }
  207.         else if (mode == 7){
  208.             *(char far *)(HVIDMEM + (offset++)) = *string++;
  209.             *(char far *)(HVIDMEM + (offset++)) = attrib;
  210.         }
  211.         else {
  212.             *(char far *)(EVIDMEM + (offset++)) = *string++;
  213.             *(char far *)(EVIDMEM + (offset++)) = attrib;
  214.         }
  215.     }
  216. }
  217.  
  218.  
  219.  
  220. void                        /* put an integer value on screen at x,y */
  221. write_int(int value, int x, int y, int attrib)
  222. {
  223.     char buf[10];
  224.     
  225.     itoa(value, buf, 10);       
  226.     writeword(buf, x, y, attrib);
  227. }
  228.