home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzCE2_src.ZIP / FrotzCE / Frotz / INPUT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-23  |  5.7 KB  |  298 lines

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