home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzS5_src.ZIP / INPUT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-11  |  6.2 KB  |  311 lines

  1. /*
  2.  * input.c
  3.  *
  4.  * High level input functions
  5.  *
  6.  */
  7.  
  8. #include "frotz.h"
  9. #include "s5api.h"
  10.  
  11. extern short save_undo (struct sg *g);
  12.  
  13. extern zchar stream_read_key (struct sg *g, zword, zword, short);
  14. extern zchar stream_read_input (struct sg *g, short, zchar *, zword, zword, short, short);
  15.  
  16. extern void tokenise_line (struct sg *g, zword, zword, zword, short);
  17.  
  18. /*
  19.  * is_terminator
  20.  *
  21.  * Check if the given key is an input terminator.
  22.  *
  23.  */
  24.  
  25. short is_terminator (struct sg *g, short key)
  26. {
  27.  
  28.     if (key == ZC_TIME_OUT)
  29.     return TRUE;
  30.     if (key == ZC_RETURN)
  31.     return TRUE;
  32.     switch(key)
  33.           {
  34.           case 18: case 16:
  35.           case 19: case 21:
  36.           case 14: case 24:
  37.           case 4:  return TRUE;
  38.           default:break;
  39.           }
  40.  
  41.     if (g->h_terminating_keys != 0)
  42.  
  43.     if (key >= ZC_ARROW_MIN && key <= ZC_MENU_CLICK) {
  44.  
  45.         zword addr = g->h_terminating_keys;
  46.         zbyte c;
  47.  
  48.         do {
  49.         LOW_BYTE (addr, c)
  50.         if (c == 255 || key == translate_from_zscii (g, c))
  51.             return TRUE;
  52.         addr++;
  53.         } while (c != 0);
  54.  
  55.     }
  56.  
  57.     return FALSE;
  58.  
  59. }/* is_terminator */
  60.  
  61. /*
  62.  * z_make_menu, add or remove a menu and branch if successful.
  63.  *
  64.  *     zargs[0] = number of menu
  65.  *    zargs[1] = table of menu entries or 0 to remove menu
  66.  *
  67.  */
  68.  
  69. void z_make_menu (struct sg *g)
  70. {
  71.  
  72.     /* This opcode was only used for the Macintosh version of Journey.
  73.        It controls menus with numbers greater than 2 (menus 0, 1 and 2
  74.        are system menus). Frotz doesn't implement menus yet. */
  75.  
  76.     branch (g,FALSE);
  77.  
  78. }/* z_make_menu */
  79.  
  80. /*
  81.  * read_yes_or_no
  82.  *
  83.  * Ask the user a question; return true if the answer is yes.
  84.  *
  85.  */
  86.  
  87. short read_yes_or_no (struct sg *g, const char *s)
  88. {
  89.     zchar key;
  90.  
  91.     print_string (g, s);
  92.     print_string (g, "? (y/n) >");
  93.  
  94.     key = stream_read_key (g, 0, 0, FALSE);
  95.  
  96.     if (key == 'y' || key == 'Y') {
  97.     print_string (g, "y\n");
  98.     return TRUE;
  99.     } else {
  100.     print_string (g, "n\n");
  101.     return FALSE;
  102.     }
  103.  
  104. }/* read_yes_or_no */
  105.  
  106. /*
  107.  * read_string
  108.  *
  109.  * Read a string from the current input stream.
  110.  *
  111.  */
  112.  
  113. void read_string (struct sg *g, short max, zchar *buffer)
  114. {
  115.     zchar key;
  116.  
  117.     buffer[0] = 0;
  118.  
  119.     do {
  120.  
  121.     key = stream_read_input (g, max, buffer, 0, 0, FALSE, FALSE);
  122.  
  123.     } while (key != ZC_RETURN);
  124.  
  125. }/* read_string */
  126.  
  127. /*
  128.  * read_number
  129.  *
  130.  * Ask the user to type in a number and return it.
  131.  *
  132.  */
  133.  
  134. short read_number (struct sg *g)
  135. {
  136.     zchar buffer[6];
  137.     short value = 0;
  138.     short i;
  139.  
  140.     read_string (g, 5, buffer);
  141.  
  142.     for (i = 0; buffer[i] != 0; i++)
  143.     if (buffer[i] >= '0' && buffer[i] <= '9')
  144.         value = 10 * value + buffer[i] - '0';
  145.  
  146.     return value;
  147.  
  148. }/* read_number */
  149.  
  150. /*
  151.  * z_read, read a line of input and (in V5+) store the terminating key.
  152.  *
  153.  *    zargs[0] = address of text buffer
  154.  *    zargs[1] = address of token buffer
  155.  *    zargs[2] = timeout in tenths of a second (optional)
  156.  *    zargs[3] = packed address of routine to be called on timeout
  157.  *
  158.  */
  159.  
  160. void z_read (struct sg *g)
  161. {
  162.     zchar buffer[INPUT_BUFFER_SIZE];
  163.     zword addr;
  164.     zchar key;
  165.     zbyte max, size;
  166.     zbyte c;
  167.     short i;
  168.  
  169.     /* Supply default arguments */
  170.  
  171.     if (g->zargc < 3)
  172.         g->zargs[2] = 0;
  173.  
  174.     /* Get maximum input size */
  175.  
  176.     addr = g->zargs[0];
  177.  
  178.     LOW_BYTE (addr, max)
  179.  
  180.     if (g->h_version <= V4)
  181.     max--;
  182.  
  183.     if (max >= INPUT_BUFFER_SIZE)
  184.     max = INPUT_BUFFER_SIZE - 1;
  185.  
  186.     /* Get initial input size */
  187.  
  188.     if (g->h_version >= V5) {
  189.     addr++;
  190.     LOW_BYTE (addr, size)
  191.     } else size = 0;
  192.  
  193.     /* Copy initial input to local buffer */
  194.  
  195.     for (i = 0; i < size; i++) {
  196.     addr++;
  197.     LOW_BYTE (addr, c)
  198.     buffer[i] = (unsigned char)translate_from_zscii (g,c);
  199.     }
  200.  
  201.     buffer[i] = 0;
  202.  
  203.     /* Draw status line for V1 to V3 games */
  204.  
  205.     if (g->h_version <= V3)
  206.     z_show_status (g);
  207.  
  208.     /* Read input from current input stream */
  209.  
  210.     key = stream_read_input (g,
  211.     max, buffer,        /* buffer and size */
  212.     g->zargs[2],        /* timeout value   */
  213.     g->zargs[3],        /* timeout routine */
  214.     TRUE,                /* enable hot keys */
  215.     g->h_version == V6);    /* no script in V6 */
  216.  
  217.     if (key == ZC_BAD)
  218.     return;
  219.  
  220.     /* Perform save_undo for V1 to V4 games */
  221.  
  222.     if (g->h_version <= V4)
  223.     save_undo (g);
  224.  
  225.     /* Copy local buffer back to dynamic memory */
  226.  
  227.     for (i = 0; buffer[i] != 0; i++) {
  228.  
  229.     if (key == ZC_RETURN) {
  230.  
  231.         if (buffer[i] >= 'A' && buffer[i] <= 'Z')
  232.         buffer[i] += 'a' - 'A';
  233.         if (buffer[i] >= 0xc0 && buffer[i] <= 0xde && buffer[i] != 0xd7)
  234.         buffer[i] += 0x20;
  235.  
  236.     }
  237.  
  238.     storeb (g, (zword) (g->zargs[0] + ((g->h_version <= V4) ? 1 : 2) + i), (unsigned char)translate_to_zscii (g, buffer[i]));
  239.  
  240.     }
  241.  
  242.     /* Add null character (V1-V4) or write input length into 2nd byte */
  243.  
  244.     if (g->h_version <= V4)
  245.     storeb (g,(zword) (g->zargs[0] + 1 + i), 0);
  246.     else
  247.     storeb (g,(zword) (g->zargs[0] + 1), (unsigned char)i);
  248.  
  249.     /* Tokenise line if a token buffer is present */
  250.  
  251.     if (key == ZC_RETURN && g->zargs[1] != 0)
  252.     tokenise_line (g,g->zargs[0], g->zargs[1], 0, FALSE);
  253.  
  254.     /* Store key */
  255.  
  256.     if (g->h_version >= V5)
  257.     store (g, translate_to_zscii (g, key));
  258.  
  259. }/* z_read */
  260.  
  261. /*
  262.  * z_read_char, read and store a key.
  263.  *
  264.  *    zargs[0] = input device (must be 1)
  265.  *    zargs[1] = timeout in tenths of a second (optional)
  266.  *    zargs[2] = packed address of routine to be called on timeout
  267.  *
  268.  */
  269.  
  270. void z_read_char (struct sg *g)
  271. {
  272.     zchar key;
  273.  
  274.     /* Supply default arguments */
  275.  
  276.     if (g->zargc < 2)
  277.     g->zargs[1] = 0;
  278.  
  279.     /* Read input from the current input stream */
  280.  
  281.     key = stream_read_key (g,
  282.     g->zargs[1],    /* timeout value   */
  283.     g->zargs[2],    /* timeout routine */
  284.     TRUE);      /* enable hot keys */
  285.  
  286.     if (key == ZC_BAD)
  287.     return;
  288.  
  289.     /* Store key */
  290.  
  291.     store (g, translate_to_zscii (g, key));
  292.  
  293. }/* z_read_char */
  294.  
  295. /*
  296.  * z_read_mouse, write the current mouse status into a table.
  297.  *
  298.  *    zargs[0] = address of table
  299.  *
  300.  */
  301.  
  302. void z_read_mouse (struct sg *g)
  303. {
  304.  
  305.     storew (g, (zword) (g->zargs[0] + 0), g->hx_mouse_y);
  306.     storew (g, (zword) (g->zargs[0] + 2), g->hx_mouse_x);
  307.     storew (g, (zword) (g->zargs[0] + 4), 1);        /* mouse button bits */
  308.     storew (g, (zword) (g->zargs[0] + 6), 0);        /* menu selection */
  309.  
  310. }/* z_read_mouse */
  311.