home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / animutil / fastgfx / fg303b / manuals.arj / USER14.DOC < prev    next >
Text File  |  1993-10-02  |  68KB  |  1,430 lines

  1. Chapter 14
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Input Device Support
  8. 260   Fastgraph User's Guide
  9.  
  10. Overview
  11.  
  12.      The selection of application input devices is an important part of
  13. designing a program for the IBM PC and PS/2 family of systems.  The keyboard
  14. and mouse are the most popular, and in fact more and more applications,
  15. especially those that use a graphical interface, actually require a mouse to
  16. use the product.  Another input device, primarily used in entertainment
  17. software, is the joystick.  Although not as popular as the mouse, joysticks
  18. nevertheless can simplify the use of certain applications.  Fastgraph
  19. provides support for these three types of input devices, and this chapter
  20. will discuss this in detail.
  21.  
  22.  
  23. Keyboard Support
  24.  
  25.      Fastgraph's keyboard support includes routines to read keystrokes, check
  26. the state of certain keys, and set the state of these keys.  In addition,
  27. Fastgraph provides a low-level keyboard handler that replaces the BIOS
  28. keyboard handler to increase keyboard responsiveness.  These routines are
  29. independent of the other parts of Fastgraph and thus do not require that you
  30. call fg_setmode.  All keyboard-related routines work in text and graphics
  31. video modes.
  32.  
  33.      The IBM PC and PS/2 keyboards produce two types of character codes --
  34. standard codes and extended codes (extended codes are sometimes called
  35. auxiliary codes).  The standard codes correspond to the 128 characters in the
  36. ASCII character set.  In general, pressing keys on the main part of the
  37. keyboard, or on the numeric keypad with NumLock turned on, will generate a
  38. standard code.  The 128 extended codes are specific to the IBM PC and PS/2
  39. keyboards.  Some common keystrokes that produce extended codes are keys on
  40. the numeric keypad with NumLock turned off, the function keys, or pressing
  41. Alt with another key.  The following tables show the standard and extended
  42. keyboard codes.
  43.  
  44.                        Table of standard keyboard codes
  45.  
  46.           key     code   key     code   key     code   key     code
  47.  
  48.           (none)    0    space    32    @        64    `        96
  49.           Ctrl+A    1    !        33    A        65    a        97
  50.           Ctrl+B    2    "        34    B        66    b        98
  51.           Ctrl+C    3    #        35    C        67    c        99
  52.           Ctrl+D    4    $        36    D        68    d       100
  53.           Ctrl+E    5    %        37    E        69    e       101
  54.           Ctrl+F    6    &        38    F        70    f       102
  55.           Ctrl+G    7    '        39    G        71    g       103
  56.           Ctrl+H    8    (        40    H        72    h       104
  57.           Ctrl+I    9    )        41    I        73    i       105
  58.           Ctrl+J   10    *        42    J        74    j       106
  59.           Ctrl+K   11    +        43    K        75    k       107
  60.           Ctrl+L   12    ,        44    L        76    l       108
  61.           Ctrl+M   13    -        45    M        77    m       109
  62.           Ctrl+N   14    .        46    N        78    n       110
  63.           Ctrl+O   15    /        47    O        79    o       111
  64.           Ctrl+P   16    0        48    P        80    p       112
  65.           Ctrl+Q   17    1        49    Q        81    q       113
  66.                                        Chapter 14:  Input Device Support   261
  67.           Ctrl+R   18    2        50    R        82    r       114
  68.           Ctrl+S   19    3        51    S        83    s       115
  69.           Ctrl+T   20    4        52    T        84    t       116
  70.           Ctrl+U   21    5        53    U        85    u       117
  71.           Ctrl+V   22    6        54    V        86    v       118
  72.           Ctrl+W   23    7        55    W        87    w       119
  73.           Ctrl+X   24    8        56    X        88    x       120
  74.           Ctrl+Y   25    9        57    Y        89    y       121
  75.           Ctrl+Z   26    :        58    Z        90    z       122
  76.           Ctrl+[   27    ;        59    [        91    {       123
  77.           Ctrl+\   28    <        60    \        92    |       124
  78.           Ctrl+]   29    =        61    ]        93    }       125
  79.           Ctrl+^   30    >        62    ^        94    ~       126
  80.           Ctrl+-   31    ?        63    _        95    Ctrl+BS 127
  81.  
  82.  
  83.                        Table of extended keyboard codes
  84.  
  85.               code        key
  86.  
  87.                3          Ctrl+@
  88.               15          Shift+Tab (back tab)
  89.               16-25       Alt+Q to Alt+P (top row of letters)
  90.               30-38       Alt+A to Alt+L (middle row of letters)
  91.               44-50       Alt+Z to Alt+M (bottom row of letters)
  92.               59-68       F1 to F10
  93.               71          Home
  94.               72          up arrow
  95.               73          PgUp
  96.               75          left arrow
  97.               77          right arrow
  98.               79          End
  99.               80          down arrow
  100.               81          PgDn
  101.               82          Ins
  102.               83          Del
  103.               84-93       Shift+F1 to Shift+F10
  104.               94-103      Ctrl+F1 to Ctrl+F10
  105.               104-113     Alt+F1 to Alt+F10
  106.               114         Ctrl+PrtSc
  107.               115         Ctrl+left arrow
  108.               116         Ctrl+right arrow
  109.               117         Ctrl+End
  110.               118         Ctrl+PgDn
  111.               119         Ctrl+Home
  112.               120-131     Alt+1 to Alt+= (top row of keys)
  113.               132         Ctrl+PgUp
  114.  
  115. In addition, four keys generate the same standard codes as other control key
  116. combinations.  These keys are:
  117.  
  118.                          key       same as   code
  119.  
  120.                          Backspace Ctrl+H      8
  121.                          Tab       Ctrl+I      9
  122.                          Enter     Ctrl+M     13                         Escape    Ctrl+[     27
  123. 262   Fastgraph User's Guide
  124.  
  125.      The CapsLock, NumLock, and ScrollLock keys do not generate a standard or
  126. extended code when pressed.  Instead, they toggle between off and on states.
  127.  
  128.  
  129. Reading Keystrokes
  130.  
  131.      When you press a key or key combination, the standard or extended code
  132. representing that keystroke is stored in the ROM BIOS keyboard buffer.  This
  133. buffer can hold up to 16 keystrokes and thus provides a type-ahead
  134. capability.  Fastgraph includes three routines for reading keystroke
  135. information from the keyboard buffer.  The fg_getkey routine reads the next
  136. item in the keyboard buffer if one is available (that is, if a key has been
  137. pressed).  If the keyboard buffer is empty (meaning no key has been pressed),
  138. fg_getkey waits for a keystroke and then reports information about it.
  139. Another routine, fg_intkey, reads the next keystroke from the keyboard buffer
  140. if one is available.  If the keyboard buffer is empty, fg_intkey immediately
  141. returns and reports this condition.  The fg_intkey routine is useful when a
  142. program must continue performing a task until a key is pressed.  We've
  143. already seen the third routine, fg_waitkey, which flushes the keyboard buffer
  144. and then waits for another keystroke.  Unlike fg_getkey and fg_intkey,
  145. fg_waitkey does not return any keystroke information.  It is most useful in
  146. "press any key to continue" situations.
  147.  
  148.      Both fg_getkey and fg_intkey require two one-byte arguments passed by
  149. reference.  If the keystroke is represented by a standard keyboard code,
  150. fg_getkey and fg_intkey return its code in the first argument and set the
  151. second argument to zero.  Similarly, if the keystroke generates an extended
  152. code, the routines return its code in the second argument and set the first
  153. argument to zero.  If the fg_intkey routine detects an empty keyboard buffer,
  154. it sets both arguments to zero.
  155.  
  156.      Example 14-1 is a simple program that uses the fg_getkey routine.  It
  157. solicits keystrokes and then displays the two values returned by fg_getkey,
  158. one of which will always be zero.  The variable key receives the key's
  159. standard code, while aux receives its extended code.  Note that fg_getkey is
  160. the only Fastgraph routine in the program; this can be done because the
  161. keyboard support routines are logically independent from the rest of
  162. Fastgraph.  The program returns to DOS when you press the Escape key.
  163.  
  164.                                 Example 14-1.
  165.  
  166.                #include <fastgraf.h>
  167.                #include <stdio.h>
  168.                void main(void);
  169.  
  170.                #define ESC 27
  171.  
  172.                void main()
  173.                {
  174.                   unsigned char key, aux;
  175.  
  176.                   do {
  177.                      fg_getkey(&key,&aux);
  178.                      printf("key = %3d  aux = %3d\n",key,aux);
  179.                      }
  180.  
  181.                                        Chapter 14:  Input Device Support   263
  182.  
  183.                   while (key != ESC);
  184.                }
  185.  
  186.  
  187.      Example 14-2 reads keystrokes using the fg_intkey routine at half-second
  188. intervals (18 fg_waitfor units equals one second).  As in the previous
  189. example, the program displays the standard and extended codes for each
  190. keystroke.  However, example 14-2 will continuously execute the while loop
  191. even if no keystrokes are available, in which case the key and aux values
  192. will both be zero.  The program returns to DOS when you press the Escape key.
  193.  
  194.                                 Example 14-2.
  195.  
  196.                #include <fastgraf.h>
  197.                #include <stdio.h>
  198.                void main(void);
  199.  
  200.                #define ESC 27
  201.  
  202.                void main()
  203.                {
  204.                   unsigned char key, aux;
  205.  
  206.                   do {
  207.                      fg_waitfor(9);
  208.                      fg_intkey(&key,&aux);
  209.                      printf("key = %3d  aux = %3d\n",key,aux);
  210.                      }
  211.                   while (key != ESC);
  212.                }
  213.  
  214.      When you use fg_intkey in a "tight" loop that does little else, you
  215. should force a small delay within the loop by calling fg_waitfor as in
  216. example 14-2.  Typically a delay of one or two clock ticks is enough.
  217. Without this delay, the BIOS may not be able to handle all keyboard activity,
  218. and thus some keystrokes may not be available to your program.
  219.  
  220.  
  221. Testing and Setting Key States
  222.  
  223.      As mentioned earlier, the CapsLock, NumLock, and ScrollLock keys do not
  224. generate a standard or extended code when pressed but instead toggle between
  225. off and on states.  Fastgraph includes routines for checking the state of
  226. these keys, as well as setting the state of the CapsLock and NumLock keys.
  227.  
  228.      The Fastgraph routines fg_capslock, fg_numlock, and fg_scrlock
  229. respectively read the state of the CapsLock, NumLock, and ScrollLock keys.
  230. Each routine has no arguments and returns the key state as its function
  231. value.  A return value of 0 means the associated key is in the off state,
  232. while 1 indicates the key is in the on state.  If the keyboard does not have
  233. a ScrollLock key, fg_scrlock considers the key off and returns a value of
  234. zero.
  235.  
  236.      Example 14-3 is a simple program that uses the fg_capslock, fg_numlock,
  237. and fg_scrlock routines to print messages describing the current state of
  238. these three keys.
  239. 264   Fastgraph User's Guide
  240.  
  241.  
  242.                                 Example 14-3.
  243.  
  244.                     #include <fastgraf.h>
  245.                     #include <stdio.h>
  246.                     void main(void);
  247.  
  248.                     void main()
  249.                     {
  250.                        if (fg_capslock())
  251.                           printf("CapsLock is on.\n");
  252.                        else
  253.                           printf("CapsLock is off.\n");
  254.  
  255.                        if (fg_numlock())
  256.                           printf("NumLock is on.\n");
  257.                        else
  258.                           printf("NumLock is off.\n");
  259.  
  260.                        if (fg_scrlock())
  261.                           printf("ScrollLock is on.\n");
  262.                        else
  263.                           printf("ScrollLock is off.\n");
  264.                     }
  265.  
  266.      You also can set the state of the CapsLock and NumLock keys within a
  267. program.  Fastgraph includes two routines, fg_setcaps and fg_setnum, for this
  268. purpose.  Each routine requires an integer argument that specifies the new
  269. key state.  If the argument value is 0, the key will be turned off; if the
  270. value is 1, the key will be turned on.  Example 14-4 uses fg_setcaps and
  271. fg_setnum to turn off CapsLock and NumLock.
  272.  
  273.                                 Example 14-4.
  274.  
  275.                             #include <fastgraf.h>
  276.                             void main(void);
  277.  
  278.                             void main()
  279.                             {
  280.                                fg_setcaps(0);
  281.                                fg_setnum(0);
  282.                             }
  283.  
  284.      On most keyboards, changing key states with fg_setcaps or fg_setnum also
  285. will change the keyboard state light to reflect the new key state.  However,
  286. some older keyboards, especially when used on PC, PC/XT, or Tandy 1000
  287. systems, do not update the state light.  This makes the state light
  288. inconsistent with the true key state.
  289.  
  290.  
  291. Low-Level Keyboard Handler
  292.  
  293.      Fastgraph includes a low-level keyboard handler that replaces the BIOS
  294. keyboard handler.  The replacement handler intercepts keystrokes ahead of the
  295. BIOS and thus eliminates the annoying beep that sounds upon filling the BIOS
  296.                                        Chapter 14:  Input Device Support   265
  297.  
  298. keyboard buffer.  Fastgraph's keyboard handler is especially well-suited to
  299. game development because it increases keyboard responsiveness in high-speed
  300. action games.  However, when the low-level keyboard handler is enabled, it is
  301. not possible to use fg_getkey, fg_intkey, fg_waitkey, or any third party
  302. functions that use BIOS or DOS services for keyboard activity.  For this
  303. reason, a program that enables the low-level keyboard handler must disable it
  304. before exiting to DOS.
  305.  
  306.      The low-level keyboard handler can be enabled and disabled at any time.
  307. The fg_kbinit routine is provided for this purpose.  To enable Fastgraph's
  308. low-level keyboard handler, pass the value 1 to fg_kbinit.  To disable
  309. Fastgraph's handler and re-enable the BIOS keyboard handler, pass the value
  310. zero.  No harm is caused if you try to enable Fastgraph's keyboard handler
  311. when it is already active, or if you try to disable it when the BIOS handler
  312. is active.
  313.  
  314.      When the low-level keyboard handler is enabled, you can use the
  315. fg_kbtest routine to check if keys are currently pressed or released.  This
  316. routine provides the only mechanism for accessing the keyboard when the low-
  317. level handler is enabled.  It specifies the keys through the use of scan
  318. codes.  If the corresponding key is pressed, fg_kbtest returns 1.  If it is
  319. released, the routine returns zero.  The following table lists the scan codes
  320. returned by pressing the keys on a standard PC keyboard.
  321.  
  322.                              Table of scan codes
  323.  
  324.                   scan           scan           scan           scan
  325.           key     code   key     code   key     code   key     code
  326.  
  327.           Esc       1    I        23    X        45    F9       67
  328.           1         2    O        24    C        46    F10      68
  329.           2         3    P        25    V        47    NumLock  69
  330.           3         4    [        26    B        48    ScrLock  70
  331.           4         5    ]        27    N        49    Home     71
  332.           5         6    Enter    28    M        50    Up arrow 72
  333.           6         7    Ctrl     29    ,        51    PgUp     73
  334.           7         8    A        30    .        52    KP-      74
  335.           8         9    S        31    /        53    L arrow  75
  336.           9        10    D        32    R shift  54    KP5      76
  337.           0        11    F        33    KP*      55    R arrow  77
  338.           -        12    G        34    Alt      56    KP+      78
  339.           =        13    H        35    Space    57    End      79
  340.           BS       14    J        36    CapsLock 58    Dn arrow 80
  341.           Tab      15    K        37    F1       59    PgDn     81
  342.           Q        16    L        38    F2       60    Ins      82
  343.           W        17    ;        39    F3       61    Del      83
  344.           E        18    '        40    F4       62    (unused) 84
  345.           R        19    `        41    F5       63    (unused) 85
  346.           T        20    L shift  42    F6       64    (unused) 86
  347.           Y        21    \        43    F7       65    F11      87
  348.           U        22    Z        44    F8       66    F12      88
  349.  
  350.      There are actually more scan codes defined for PC keyboards than listed
  351. in this table.  Such scan codes are generated when a key is pressed as a
  352. combination of one or more keys, such as when the shift and slash keys are
  353. pressed together to produce a question mark (?) character.  Fastgraph's low-
  354. 266   Fastgraph User's Guide
  355.  
  356. level keyboard handler is designed to report the pressing or releasing of
  357. keys themselves, as opposed to reporting the actual characters so produced.
  358. It is thus not possible for the keyboard handler to report the scan code for
  359. a multi-key character.  If needed, such characters can be identified by the
  360. scan codes generated for each individual key in the sequence.  For example, a
  361. question mark character would be reported as a forward slash (scan code 53)
  362. generated while pressing the left shift (42) or right shift (54) keys.
  363.  
  364.      Example 14-5 illustrates the use of Fastgraph's low-level keyboard
  365. handler.  It first uses fg_kbinit to enable Fastgraph's keyboard handler and
  366. displays a message stating this.  Then, at approximately one second
  367. intervals, the program calls fg_kbtest to check which of the four arrow keys
  368. are pressed and displays an appropriate message.  Pressing Escape restores
  369. the BIOS keyboard handler and exits to DOS.
  370.  
  371.                                 Example 14-5.
  372.  
  373.                 #include <fastgraf.h>
  374.                 #include <stdio.h>
  375.                 void main(void);
  376.  
  377.                 #define ESC    1
  378.                 #define LEFT  75
  379.                 #define RIGHT 77
  380.                 #define UP    72
  381.                 #define DOWN  80
  382.  
  383.                 void main()
  384.                 {
  385.                    fg_kbinit(1);
  386.                    printf("Keyboard handler enabled.\n");
  387.  
  388.                    do {
  389.                       printf("keys pressed:  ");
  390.                       if (fg_kbtest(LEFT))   printf("Left ");
  391.                       if (fg_kbtest(RIGHT))  printf("Right ");
  392.                       if (fg_kbtest(UP))     printf("Up ");
  393.                       if (fg_kbtest(DOWN))   printf("Down ");
  394.                       printf("\n");
  395.                       fg_waitfor(18);
  396.                    } while (fg_kbtest(ESC) == 0);
  397.  
  398.                    fg_kbinit(0);
  399.                    printf("Keyboard handler disabled.\n");
  400.                 }
  401.  
  402. Mouse Support
  403.  
  404.      The mouse is a very popular input and pointing device, especially in
  405. graphically-oriented programs.  Fastgraph contains several routines to
  406. support mice.  These routines perform such tasks as mouse initialization,
  407. controlling and defining the mouse cursor, and reporting information about
  408. the mouse position and button status.
  409.  
  410.      The underlying software that controls the mouse is called the mouse
  411. driver.  Fastgraph's mouse support routines provide a high-level interface to
  412.                                        Chapter 14:  Input Device Support   267
  413. this driver.  The Microsoft Mouse and its accompanying mouse driver have
  414. become an industry standard, and other manufacturers of mice have also made
  415. their mouse drivers Microsoft compatible.  For this reason, the Fastgraph
  416. mouse support routines assume you are using a Microsoft or compatible mouse
  417. driver.
  418.  
  419.      Unfortunately, not all mouse drivers are created equal.  That is, some
  420. drivers are not Microsoft compatible, even though they may be advertised as
  421. such.  In some cases, these incompatibilities are rather trivial, but others
  422. are significant.  For example, early versions of some third party mouse
  423. drivers had real problems in the EGA graphics modes.  The Microsoft mouse
  424. driver, the Logitech mouse driver (version 3.2 or above), and the DFI mouse
  425. driver (version 3.00 or above) are known to work well with Fastgraph's mouse
  426. support routines.  Any other Microsoft compatible mouse driver also should
  427. work properly.
  428.  
  429.  
  430. Initializing the Mouse
  431.  
  432.      There are two steps required to use Fastgraph's mouse support routines
  433. within an application program.  First, you must install the mouse driver.
  434. This is done before running the application, typically by entering the
  435. command MOUSE at the DOS command prompt.  Second, you must use the Fastgraph
  436. routine fg_mouseini to initialize the mouse within the program.
  437.  
  438.      The fg_mouseini routine has no arguments and returns a "success or
  439. failure" indicator as its function value.  If the return value is -1, it
  440. means fg_mouseini could not initialize the mouse (either because the mouse
  441. driver is not installed, or the driver is installed but the mouse is
  442. physically disconnected).  If fg_mouseini returns a positive integer value,
  443. the mouse initialization was successful.  The value itself indicates the
  444. number of buttons (either 2 or 3) on the mouse.  If you don't call
  445. fg_mouseini, or if fg_mouseini can't initialize the mouse, none of
  446. Fastgraph's other mouse support routines will have any effect.3
  447.  
  448.      Example 14-6 illustrates how to initialize the mouse.  Unlike the
  449. keyboard support routines, Fastgraph's mouse support routines require that
  450. fg_setmode be called first.  In this example, we simply pass fg_setmode the
  451. value -1 to initialize Fastgraph for whatever video mode is in effect when we
  452. run the program.  The program then calls fg_mouseini and prints a message
  453. indicating whether or not the initialization was successful.  If it was, the
  454. message includes the number of buttons on the mouse.
  455.  
  456.                                 Example 14-6.
  457.  
  458.                #include <fastgraf.h>
  459.                #include <stdio.h>
  460.                void main(void);
  461.  
  462.                void main()
  463.                {
  464. ____________________
  465.  
  466.      (3) If you use another mouse library or communicate directly with the
  467.      mouse driver, you must still call fg_mouseini if your program runs in
  468.      modes 13 through 18.  Otherwise, Fastgraph won't know that your program
  469.      is using a mouse and may display graphics incorrectly.
  470. 268   Fastgraph User's Guide
  471.  
  472.                   int status;
  473.  
  474.                   fg_setmode(-1);
  475.                   status = fg_mouseini();
  476.  
  477.                   if (status < 0)
  478.                      printf("Mouse not available.\n");
  479.                   else
  480.                      printf("%d button mouse found.\n",status);
  481.                }
  482.  
  483.  
  484.      You should be aware that certain mouse drivers do not fully initialize
  485. the mouse when a program changes video modes.  This problem most frequently
  486. occurs when you restore the original video mode at the end of a program that
  487. has called fg_mouseini.  When changing video modes, you must first make the
  488. mouse cursor invisible (this is described in the next section), change the
  489. video mode, and then call fg_mouseini again to initialize the mouse for the
  490. new video mode.
  491.  
  492.  
  493. XVGA and SVGA Mouse Considerations
  494.  
  495.      Mouse drivers do not have the ability to directly display the mouse
  496. cursor in XVGA and SVGA graphics modes (modes 20 to 29).  Hence, Fastgraph
  497. must display the mouse cursor through an interrupt handler that is activated
  498. whenever the mouse moves.  The handler is automatically installed when you
  499. call fg_mouseini in modes 20 to 29.
  500.  
  501.      If you do not disable this handler before your program exits, it will
  502. remain "hooked" to the mouse driver.  This will most likely hang your system
  503. the next time you try doing anything of consequence.  The fg_mousefin routine
  504. removes Fastgraph's mouse interrupt handler from the mouse driver.  In XVGA
  505. and SVGA graphics modes, you should call fg_mousefin just before restoring
  506. the original video mode, as illustrated below:
  507.  
  508.  
  509.                             fg_mousefin();
  510.                             fg_setmode(old_mode);
  511.                             fg_reset();
  512.  
  513.  
  514. Again, calling fg_mousefin is required only in XVGA and SVGA graphics modes.
  515. Calling it in other video modes is not applicable, though it causes no
  516. problems.
  517.  
  518.      In the standard VGA/MCGA 256-color mode (mode 19), white pixels in the
  519. mouse cursor are displayed in color 15.  This is inconsistent with other
  520. graphics modes, where the mouse cursor's white pixels are displayed in the
  521. highest-numbered color value available in that mode.  Fastgraph corrects this
  522. inconsistency in XVGA and SVGA 256-color graphics modes by displaying white
  523. mouse cursor pixels in color 255.  Like color 15, color 255 is white by
  524. default.  This allows you to redefine color 15 in your 256-color applications
  525. without interfering with the mouse cursor colors.
  526.                                        Chapter 14:  Input Device Support   269
  527.  
  528. Controlling the Mouse Cursor
  529.  
  530.      The mouse cursor indicates the current position of the mouse.  By
  531. default, the cursor is a small white arrow in graphics modes and a one-
  532. character rectangle in text modes.  After you use fg_mouseini to initialize
  533. the mouse, the mouse cursor is invisible.  To make it visible, you must use
  534. the fg_mousevis routine.  This routine has a single integer argument that
  535. defines the mouse cursor visibility.  If it is 0, the mouse cursor will be
  536. invisible; if it is 1, the mouse cursor becomes visible.
  537.  
  538.      If the mouse cursor is in an area of the screen that is being updated,
  539. or if it moves into this area during the update process, you must make the
  540. mouse cursor invisible.  Additionally, when performing any video output in
  541. the native EGA and VGA graphics modes (modes 13 through 18), you also must
  542. make the mouse cursor invisible.  Instead of checking for these conditions,
  543. it is more convenient and efficient to make the mouse cursor invisible during
  544. all screen updates and then make it visible again when the updating is
  545. finished.  Finally, you must make the mouse cursor invisible whenever you
  546. change the visual page number with fg_setvpage.
  547.  
  548.      After you initialize the mouse, the cursor is positioned in the center
  549. of the screen.  Moving the mouse of course changes the cursor position, but
  550. you also can position the mouse cursor with the Fastgraph routine
  551. fg_mousemov.  This routine has two arguments that specify the new horizontal
  552. and vertical cursor position.  The position is expressed in screen space
  553. units for graphics modes, while it is expressed in character cells for text
  554. modes.  The fg_mousemov routine moves the cursor whether or not it is
  555. visible.
  556.  
  557.      Sometimes it is useful to restrict the mouse cursor to a specific area
  558. of the screen.  The Fastgraph routine fg_mouselim prevents the mouse cursor
  559. from moving outside the specified rectangular area.  It requires four
  560. arguments that specify the minimum horizontal coordinate, maximum horizontal
  561. coordinate, minimum vertical coordinate, and maximum vertical coordinate of
  562. this area.  Again, the coordinates are expressed in screen space units for
  563. graphics modes and character cells for text modes.
  564.  
  565.      One of the most important functions of the mouse driver is to translate
  566. the horizontal and vertical mouse movements into a position on the screen.
  567. The mouse reports these movements to the mouse driver in units called mickeys
  568. (one mickey is about 1/200 of an inch).  By default, moving the mouse 8
  569. mickeys in the horizontal direction moves the mouse cursor one horizontal
  570. pixel.  Similarly, moving the mouse 16 mickeys vertically moves the cursor
  571. one vertical pixel.  Fastgraph provides a routine named fg_mousespd that can
  572. change these values, which effectively allows you to control the speed at
  573. which the mouse cursor moves relative to the movement of the mouse itself.
  574. The fg_mousespd routine requires two arguments that define the number of
  575. mickeys required for eight pixels of mouse cursor movement.  The first
  576. argument specifies this for the horizontal direction, and the second for the
  577. vertical direction.
  578.  
  579.      Example 14-7, which runs in any graphics mode, demonstrates the
  580. fg_mousevis, fg_mousemov, fg_mouselim, and fg_mousespd routines.  The program
  581. first establishes the video mode, initializes the mouse, and fills the screen
  582. with a white rectangle.  Next, the program calls fg_mousevis to make the
  583. mouse cursor visible and then calls fg_mouselim to restrict the mouse cursor
  584. 270   Fastgraph User's Guide
  585. to an area one-fourth the size of the screen, centered in the middle of the
  586. screen.  At this point you should move the mouse cursor around the screen to
  587. see the effect of fg_mouselim and note the speed at which the cursor moves
  588. relative to the mouse itself.  The program continues when you press any key.
  589.  
  590.      The program then uses fg_mousemov to move the mouse cursor to each
  591. corner of the region established by fg_mouselim.  The call to fg_waitfor
  592. keeps the cursor in each corner for two seconds, unless you move the mouse.
  593. Note how the program tries to move the mouse cursor to each corner of the
  594. screen, but since doing so would move the cursor outside the defined region
  595. of movement, fg_mousemov just positions the cursor at the nearest point
  596. possible within this region.  The last call to fg_mousemov moves the cursor
  597. back to the middle of the screen.  After doing this, the program calls
  598. fg_mousespd to change the mouse cursor speed.  The values passed to
  599. fg_mousespd (16 and 32) are twice the defaults and therefore make you move
  600. the mouse twice as far as before to move the mouse cursor the same distance.
  601. When you run the program, compare the mouse sensitivity to the original
  602. speed.  After a keystroke, the program returns to DOS.
  603.  
  604.                                 Example 14-7.
  605.  
  606.                #include <fastgraf.h>
  607.                #include <stdio.h>
  608.                #include <stdlib.h>
  609.                void main(void);
  610.  
  611.                void main()
  612.                {
  613.                   int maxx, maxy;
  614.                   int old_mode;
  615.  
  616.                   old_mode = fg_getmode();
  617.                   fg_setmode(fg_automode());
  618.  
  619.                   if (fg_mouseini() < 0) {
  620.                      fg_setmode(old_mode);
  621.                      fg_reset();
  622.                      exit(1);
  623.                      }
  624.  
  625.                   maxx = fg_getmaxx();
  626.                   maxy = fg_getmaxy();
  627.                   fg_setcolor(15);
  628.                   fg_rect(0,maxx,0,maxy);
  629.  
  630.                   fg_mousevis(1);
  631.                   fg_mouselim(maxx/4,3*maxx/4,maxy/4,3*maxy/4);
  632.                   fg_waitkey();
  633.  
  634.                   fg_mousemov(0,0);
  635.                   fg_waitfor(36);
  636.                   fg_mousemov(maxx,0);
  637.                   fg_waitfor(36);
  638.                   fg_mousemov(maxx,maxy);
  639.                   fg_waitfor(36);
  640.                   fg_mousemov(0,maxy);
  641.                   fg_waitfor(36);
  642.                                        Chapter 14:  Input Device Support   271
  643.                   fg_mousemov(maxx/2,maxy/2);
  644.                   fg_mousespd(16,32);
  645.                   fg_waitkey();
  646.  
  647.                   fg_setmode(old_mode);
  648.                   fg_reset();
  649.                }
  650.  
  651.  
  652.  
  653. Reporting the Mouse Status
  654.  
  655.      It is obviously important to be able to track the mouse position and
  656. button status.  The Fastgraph routines fg_mousepos and fg_mousebut enable you
  657. to do this.
  658.  
  659.      The fg_mousepos routine returns information about the current mouse
  660. cursor position and button status.  It requires three integer arguments, all
  661. passed by reference.  The first two arguments respectively receive the
  662. horizontal and vertical coordinates of the mouse cursor.  These values are
  663. expressed in screen space units for graphics modes and character cells for
  664. text modes.  The third argument receives a three-bit mask containing the
  665. button status as indicated below.
  666.  
  667.                    bit
  668.                  number  meaning
  669.  
  670.                     0    1 if left button pressed, 0 if not
  671.                     1    1 if right button pressed, 0 if not
  672.                     2    1 if middle button pressed, 0 if not
  673.  
  674. For example, if both the left and right buttons are pressed, the button
  675. status will be set to 3.  If the mouse only has two buttons, bit 2 will
  676. always be zero.
  677.  
  678.      Another routine, fg_mousebut, is available for returning the number of
  679. button press or release counts that have occurred since the last check, or
  680. since calling fg_mouseini.  Each mouse button maintains its own separate
  681. counters, so fg_mousebut returns this information for a specific button.
  682. Additionally, fg_mousebut returns the horizontal and vertical position of the
  683. mouse cursor at the time the specified button was last pressed or released.
  684.  
  685.      The fg_mousebut routine takes four integer arguments, of which the last
  686. three are passed by reference.  The first argument specifies the button of
  687. interest (1 means the left button, 2 is the right button, and 3 is the middle
  688. button).  If this value is positive, button press counts will be reported.
  689. If it is negative, release counts will be reported.  The second, third, and
  690. fourth arguments respectively receive the press or release count, the
  691. horizontal mouse cursor position at the time of the last press or release,
  692. and the vertical position at that same time.  If the press or release count
  693. is zero, the mouse cursor position is returned as (0,0).  The coordinate
  694. positions are expressed in screen space units for graphics modes and
  695. character cells for text modes.
  696.  
  697.      Example 14-8 runs in any graphics video mode and illustrates the use of
  698. the fg_mousepos and fg_mousebut routines.  The program first establishes the
  699. video mode and then initializes the mouse (the program exits if the
  700. 272   Fastgraph User's Guide
  701. initialization fails).  It next fills the entire screen with a white
  702. rectangle and then calls fg_mousevis to make the mouse cursor visible.
  703.  
  704.      The main part of example 14-8 is a while loop that polls the mouse at
  705. three-second intervals (the call fg_waitfor(54) delays the program for three
  706. seconds).  Within the loop, the program first uses fg_mousebut to get the
  707. number of times the left mouse button was pressed in the last three seconds.
  708. Following this, the fg_mousepos routine gets the current mouse position.  The
  709. program then displays this information in the upper left corner of the
  710. screen; note how fg_mousevis is used to make the cursor invisible during
  711. graphics operations.  The program continues until you press the right mouse
  712. button, checked by the call to fg_mousebut at the end of the loop.
  713.  
  714.                                 Example 14-8.
  715.  
  716.           #include <fastgraf.h>
  717.           #include <stdio.h>
  718.           #include <stdlib.h>
  719.           void main(void);
  720.  
  721.           void main()
  722.           {
  723.              int old_mode;
  724.              int buttons, count;
  725.              int x, y;
  726.              char string[25];
  727.  
  728.              old_mode = fg_getmode();
  729.              fg_setmode(fg_automode());
  730.  
  731.              if (fg_mouseini() < 0) {
  732.                 fg_setmode(old_mode);
  733.                 fg_reset();
  734.                 exit(1);
  735.                 }
  736.  
  737.              fg_setcolor(15);
  738.              fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  739.              fg_mousevis(1);
  740.  
  741.              do {
  742.                 fg_waitfor(54);
  743.                 fg_mousebut(1,&count,&x,&y);
  744.                 fg_mousepos(&x,&y,&buttons);
  745.                 sprintf(string,"X=%3d  Y=%3d  count=%4d",x,y,count);
  746.                 fg_mousevis(0);
  747.                 fg_setcolor(15);
  748.                 fg_rect(0,fg_xconvert(25),0,fg_yconvert(1));
  749.                 fg_setcolor(0);
  750.                 fg_locate(0,0);
  751.                 fg_text(string,24);
  752.                 fg_mousevis(1);
  753.                 fg_mousebut(2,&count,&x,&y);
  754.                 }
  755.              while (count == 0);
  756.  
  757.              fg_setmode(old_mode);
  758.                                        Chapter 14:  Input Device Support   273
  759.  
  760.              fg_reset();
  761.           }
  762.  
  763.  
  764. Defining the Mouse Cursor
  765.  
  766.      By default, the mouse cursor is a small white arrow in graphics modes
  767. and a one-character rectangle in text modes.  In graphics modes, you can
  768. change the mouse cursor to any 16 by 16 pixel image with the Fastgraph
  769. routine fg_mouseptr (in the CGA four-color graphics modes, the cursor size is
  770. 8 by 16 pixels).  You cannot change the mouse cursor shape in text modes, but
  771. you can use the Fastgraph routine fg_mousecur to define how it interacts with
  772. existing characters on the screen.
  773.  
  774. Text Modes
  775.  
  776.      To change the mouse cursor in text modes, you must first define two 16-
  777. bit quantities called the screen mask and cursor mask.  The following figure
  778. defines the format of each mask.
  779.  
  780.                     bits      meaning
  781.  
  782.                     0 to 7    ASCII character value
  783.                     8 to 11   foreground color
  784.                     12 to 14  background color
  785.                     15        blink
  786.  
  787. Notice how this structure parallels the character and attribute bytes
  788. associated with each character cell.  The default screen mask is 77FF hex,
  789. and the default cursor mask is 7700 hex.
  790.  
  791.      When you position the mouse over a specific character cell, the mouse
  792. driver uses the current screen and cursor masks to determine the mouse
  793. cursor's appearance.  First, the mouse driver logically ANDs the screen mask
  794. with the existing contents of that character cell.  It then XORs that result
  795. with the cursor mask to display the mouse cursor.
  796.  
  797.      For example, consider how the mouse cursor is produced in the 80-column
  798. color text mode (mode 3).  Suppose a specific character cell contains the
  799. ASCII character 0 (48 decimal, 30 hex) and an attribute byte that specifies a
  800. white (color 15) foreground on a blue background (color 1) and does not blink
  801. (blink bit 0).  The binary structure of the character and its attribute are:
  802.  
  803.                             attribute    character
  804.  
  805.                             0 001 1111   00110000
  806.  
  807. Now let's see what happens when we apply the screen and cursor masks to the
  808. character and its attribute.
  809.  
  810.             attribute/character   0001 1111 0011 0000   (1F30 hex)
  811.             default screen mask   0111 0111 1111 1111   (77FF hex)
  812.                                   -------------------
  813.             result of AND         0001 0111 0011 0000   (1730 hex)
  814.  
  815. 274   Fastgraph User's Guide
  816.  
  817.             default cursor mask   0111 0111 0000 0000   (7700 hex)
  818.                                   -------------------
  819.             result of XOR         0110 0000 0011 0000   (6030 hex)
  820.  
  821. The resulting character (30 hex) is the original character, but the new
  822. attribute (60 hex) represents a black foreground with a brown background and
  823. does not blink.  As long as the mouse cursor remains positioned on this
  824. character cell, it would appear black on brown.
  825.  
  826.      When we use the default screen and cursor masks, the mouse cursor will
  827. always display the original character and it will not blink.  The cursor
  828. foreground color will be 15-F, where F is the displayed character's
  829. foreground color.  Similarly, the cursor background color will be 7-B, where
  830. B is the displayed character's background color.  The default masks will
  831. virtually always produce a satisfactory mouse cursor.
  832.  
  833.      It is possible, however, to change the appearance of the mouse cursor in
  834. text modes by using your own screen and cursor masks.  The Fastgraph routine
  835. fg_mousecur does just that.  It expects two arguments, the first being the
  836. cursor mask and the second the screen mask.  Example 14-9 demonstrates the
  837. use of fg_mousecur.  The program displays some text and uses the default
  838. mouse cursor.  After waiting for a keystroke, the program calls fg_mousecur
  839. to define a new mouse cursor.  The new cursor is similar to the default
  840. cursor, but it displays the foreground colors in the opposite intensity as
  841. the default cursor.  The program then waits for another keystroke before
  842. returning to DOS.
  843.  
  844.                                 Example 14-9.
  845.  
  846.                   #include <fastgraf.h>
  847.                   #include <stdio.h>
  848.                   #include <stdlib.h>
  849.                   void main(void);
  850.  
  851.                   void main()
  852.                   {
  853.                      int old_mode;
  854.                      int row;
  855.  
  856.                      old_mode = fg_getmode();
  857.                      fg_setmode(3);
  858.  
  859.                      if (fg_mouseini() < 0) {
  860.                         fg_setmode(old_mode);
  861.                         fg_reset();
  862.                         exit(1);
  863.                         }
  864.  
  865.                      fg_setattr(7,0,0);
  866.                      fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  867.  
  868.                      fg_setattr(12,7,0);
  869.                      for (row = 0; row < 25; row++) {
  870.                         fg_locate(row,34);
  871.                         fg_text("example 14-9",12);
  872.                         }
  873.                                        Chapter 14:  Input Device Support   275
  874.  
  875.  
  876.                      fg_mousevis(1);
  877.                      fg_waitkey();
  878.                      fg_mousecur(0x7FFF,0x7F00);
  879.                      fg_waitkey();
  880.  
  881.                      fg_setmode(old_mode);
  882.                      fg_reset();
  883.                   }
  884.  
  885. Graphics Modes
  886.  
  887.      Defining the mouse cursor in graphics video modes also requires creating
  888. a screen mask and cursor mask, but as one might expect, the structure of
  889. these masks is vastly different from for text modes.  In fact, it closely
  890. resembles the mode-independent bit map format used by the fg_drawmap routine.
  891. Although their structure differs, the way the mouse driver applies the masks
  892. is the same as in the text modes.  That is, the driver displays the mouse
  893. cursor by first logically ANDing video memory with the screen mask, and then
  894. XORing that result with the cursor mask.
  895.  
  896.      Let's begin by looking at the masks for the default mouse cursor in
  897. graphics modes.  The size of each mask (and hence the mouse cursor) is 16
  898. pixels wide and 16 pixels high.  As mentioned earlier, the default cursor is
  899. a small white arrow with a black outline around it.  Here are its screen and
  900. cursor masks expressed as binary values.
  901.  
  902.               screen                cursor                cursor
  903.                mask                  mask               appearance
  904.  
  905.          1001111111111111      0000000000000000       **
  906.          1000111111111111      0010000000000000       *x*
  907.          1000011111111111      0011000000000000       *xx*
  908.          1000001111111111      0011100000000000       *xxx*
  909.          1000000111111111      0011110000000000       *xxxx*
  910.          1000000011111111      0011111000000000       *xxxxx*
  911.          1000000001111111      0011111100000000       *xxxxxx*
  912.          1000000000111111      0011111110000000       *xxxxxxx*
  913.          1000000000011111      0011111111000000       *xxxxxxxx*
  914.          1000000000001111      0011111000000000       *xxxxx*****
  915.          1000000011111111      0011011000000000       *xx*xx*
  916.          1000100001111111      0010001100000000       *x* *xx*
  917.          1001100001111111      0000001100000000       **  *xx*
  918.          1111110000111111      0000000110000000            *xx*
  919.          1111110000111111      0000000110000000            *xx*
  920.          1111111000111111      0000000000000000             ***
  921.  
  922.      The mouse driver first ANDs the screen mask with video memory at the
  923. mouse cursor position.  This means the screen mask 1 bits leave video memory
  924. intact, while the 0 bits change the corresponding pixels to black.  Next, the
  925. mouse driver XORs the result with the cursor mask.  This time the cursor mask
  926. 0 bits leave video memory unchanged, while the 1 bits change the
  927. corresponding pixels to white.  This produces a mouse cursor as shown above
  928. on the right, where a dot ( ) represents an unchanged pixel, an asterisk (*)
  929. a black pixel, and an x a white pixel.  The following table summarizes the
  930. cursor appearance for all possible combinations of mask bits.
  931. 276   Fastgraph User's Guide
  932.  
  933.         screen mask bit     cursor mask bit resulting cursor pixel
  934.  
  935.                0                   0                 black
  936.                0                   1                 white
  937.                1                   0               unchanged
  938.                1                   1               inverted
  939.  
  940.      The color of an "inverted" pixel is n-k, where n is the maximum color
  941. number in the current video mode, and k is the color of the pixel being
  942. replaced.  Also, "black" and "white" pixels are not necessarily these colors
  943. in 16-color and 256-color modes.  More correctly, "black" pixels are
  944. displayed in the color assigned to palette 0, and "white" pixels are the
  945. displayed in the color assigned to palette 15 (255 in XVGA and SVGA 256-color
  946. modes).  If you're using the CGA color modes, "black" pixels are displayed in
  947. the background color, and "white" pixels appear in color 3 (whose actual
  948. color is determined by the selected CGA palette).
  949.  
  950.      With an understanding of the way the default mouse cursor works in
  951. graphics modes, we're now ready to define our own mouse cursor.  Shown below
  952. are the screen mask, cursor mask, and resulting appearance for a solid plus-
  953. shaped cursor.  The hexadecimal equivalents of the binary mask values are
  954. also given.
  955.  
  956.    ----- screen mask ----      ----- cursor mask ----
  957.                                                                 cursor
  958.         binary       hex            binary       hex          appearance
  959.  
  960.    1110000000111111  E03F      0000000000000000  0000      ...*******......
  961.    1110000000111111  E03F      0000111110000000  0F80      ...*xxxxx*......
  962.    1110000000111111  E03F      0000111110000000  0F80      ...*xxxxx*......
  963.    0000000000000111  0007      0000111110000000  0F80      ****xxxxx****...
  964.    0000000000000111  0007      0111111111110000  7FF0      *xxxxxxxxxxx*...
  965.    0000000000000111  0007      0111111111110000  7FF0      *xxxxxxxxxxx*...
  966.    0000000000000111  0007      0111111111110000  7FF0      *xxxxxxxxxxx*...
  967.    0000000000000111  0007      0111111111110000  7FF0      *xxxxxxxxxxx*...
  968.    0000000000000111  0007      0111111111110000  7FF0      *xxxxxxxxxxx*...
  969.    0000000000000111  0007      0000111110000000  0F80      ****xxxxx****...
  970.    1110000000111111  E03F      0000111110000000  0F80      ...*xxxxx*......
  971.    1110000000111111  E03F      0000111110000000  0F80      ...*xxxxx*......
  972.    1110000000111111  E03F      0000000000000000  0000      ...*******......
  973.    1111111111111111  FFFF      0000000000000000  0000      ................
  974.    1111111111111111  FFFF      0000000000000000  0000      ................
  975.    1111111111111111  FFFF      0000000000000000  0000      ................
  976.  
  977. If we wanted to make the mouse cursor hollow rather than solid, the masks and
  978. resulting cursor appearance would look like this.
  979.  
  980.    ----- screen mask ----      ----- cursor mask ----
  981.                                                                 cursor
  982.         binary       hex            binary       hex          appearance
  983.  
  984.    1110000000111111  E03F      0000000000000000  0000      ...*******......
  985.    1110111110111111  EFBF      0000000000000000  0000      ...*.....*......
  986.    1110111110111111  EFBF      0000000000000000  0000      ...*.....*......
  987.    0000111110000111  0F87      0000000000000000  0000      ****.....****...
  988.    0111111111110111  7FF7      0000000000000000  0000      *...........*...
  989.                                        Chapter 14:  Input Device Support   277
  990.    0111111111110111  7FF7      0000000000000000  0000      *...........*...
  991.    0111111111110111  7FF7      0000001000000000  0200      *.....x.....*...
  992.    0111111111110111  7FF7      0000000000000000  0000      *...........*...
  993.    0111111111110111  7FF7      0000000000000000  0000      *...........*...
  994.    0000111110000111  0F87      0000000000000000  0000      ****.....****...
  995.    1110111110111111  EFBF      0000000000000000  0000      ...*.....*......
  996.    1110111110111111  EFBF      0000000000000000  0000      ...*.....*......
  997.    1110000000111111  E03F      0000000000000000  0000      ...*******......
  998.    1111111111111111  FFFF      0000000000000000  0000      ................
  999.    1111111111111111  FFFF      0000000000000000  0000      ................
  1000.    1111111111111111  FFFF      0000000000000000  0000      ................
  1001.  
  1002. Note that the center bit defined in the cursor mask causes the corresponding
  1003. pixel in video memory to be inverted.
  1004.  
  1005.      There is one more item needed to define a graphics mode mouse cursor
  1006. completely.  That item is the hot spot, or the actual screen position used or
  1007. reported by the mouse driver.  For the plus-shaped cursors just constructed,
  1008. it would be sensible to define the hot spot in the center of the plus.  The
  1009. hot spot is specified relative to the upper left corner of the cursor, so its
  1010. position within the cursor would be (6,6) -- that is, six pixels to the right
  1011. and six pixels below the upper left corner.  You can specify the hot spot
  1012. offsets using negative values or values above 15 to position it outside the
  1013. mouse cursor matrix if desired.
  1014.  
  1015.      The Fastgraph routine fg_mouseptr defines a mouse cursor in graphics
  1016. modes.  The first of its three arguments is a 32-element integer array,
  1017. passed by reference.  The array's first 16 elements contain the screen mask,
  1018. and its second 16 elements contain the cursor mask.  The remaining two
  1019. arguments respectively specify the horizontal and vertical offsets for the
  1020. hot spot.  The fg_mouseptr routine has no effect in a text video mode.
  1021.  
  1022.      Example 14-10 is similar to example 14-9.  It shows how to define a
  1023. graphics mode mouse cursor using fg_mouseptr.  The values stored in the solid
  1024. and hollow arrays define the screen and cursor masks for the solid and hollow
  1025. plus-shaped mouse cursors discussed earlier.  After making the mouse cursor
  1026. visible, the program uses the default mouse cursor until a key is pressed.
  1027. Following this, it changes to the solid cursor.  After another keystroke, the
  1028. program changes to the hollow cursor.  When you run example 14-10, compare
  1029. the differences among the three mouse cursors.
  1030.  
  1031.                                 Example 14-10.
  1032.  
  1033.   #include <fastgraf.h>
  1034.   #include <stdio.h>
  1035.   #include <stdlib.h>
  1036.   void main(void);
  1037.  
  1038.   int solid[]  = {0xE03F,0xE03F,0xE03F,0x0007,0x0007,0x0007,0x0007,0x0007,
  1039.                   0x0007,0x0007,0xE03F,0xE03F,0xE03F,0xFFFF,0xFFFF,0xFFFF,
  1040.                   0x0000,0x0F80,0x0F80,0x0F80,0x7FF0,0x7FF0,0x7FF0,0x7FF0,
  1041.                   0x7FF0,0x0F80,0x0F80,0x0F80,0x0000,0x0000,0x0000,0x0000};
  1042.  
  1043.   int hollow[] = {0xE03F,0xEFBF,0xEFBF,0x0F87,0x7FF7,0x7FF7,0x7FF7,0x7FF7,
  1044.                   0x7FF7,0x0F87,0xEFBF,0xEFBF,0xE03F,0xFFFF,0xFFFF,0xFFFF,
  1045.                   0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0200,0x0000,
  1046.                   0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
  1047. 278   Fastgraph User's Guide
  1048.  
  1049.   void main()
  1050.   {
  1051.      int old_mode;
  1052.      int column, row, last_row;
  1053.  
  1054.      old_mode = fg_getmode();
  1055.      fg_setmode(fg_automode());
  1056.  
  1057.      if (fg_mouseini() < 0) {
  1058.         fg_setmode(old_mode);
  1059.         fg_reset();
  1060.         exit(1);
  1061.         }
  1062.  
  1063.      fg_setcolor(15);
  1064.      fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  1065.  
  1066.      fg_setcolor(12);
  1067.      column = fg_xalpha(fg_getmaxx()/2) - 6;
  1068.      last_row = fg_yalpha(fg_getmaxy()) + 1;
  1069.  
  1070.      for (row = 0; row < last_row; row++) {
  1071.         fg_locate(row,column);
  1072.         fg_text("example 14-10",13);
  1073.         }
  1074.  
  1075.      fg_mousevis(1);
  1076.      fg_waitkey();
  1077.      fg_mouseptr(solid,6,6);
  1078.      fg_waitkey();
  1079.      fg_mouseptr(hollow,6,6);
  1080.      fg_waitkey();
  1081.  
  1082.      fg_setmode(old_mode);
  1083.      fg_reset();
  1084.   }
  1085.  
  1086.  
  1087. CGA Considerations
  1088.  
  1089.      The mouse driver treats the screen and cursor masks differently in the
  1090. CGA four-color graphics modes (modes 4 and 5) than in the other graphics
  1091. modes.  In the CGA modes, each pair of mask bits corresponds to one pixel.
  1092. This means the masks more closely resemble the mode-specific format used by
  1093. fg_drwimage instead of the mode-independent format of fg_drawmap.
  1094.  
  1095.      Fastgraph uses a different default mouse cursor for modes 4 and 5.  Its
  1096. screen and cursor masks, as well as the resulting cursor appearance, are
  1097. shown in the following diagram.
  1098.  
  1099.                 screen                  cursor              cursor
  1100.                 mask                    mask             appearance
  1101.  
  1102.            0000111111111111        0000000000000000        **
  1103.            0000001111111111        0011000000000000        ***
  1104.            0000000011111111        0011110000000000        ****
  1105.                                        Chapter 14:  Input Device Support   279
  1106.  
  1107.            0000000000111111        0011111100000000        *****
  1108.            0000000000001111        0011111111000000        ******
  1109.            0000000000000011        0011111111110000        *******
  1110.            0000000000000011        0011111100000000        *******
  1111.            0000000000111111        0011111110000000        *****
  1112.            0000000000001111        0011000011000000        ******
  1113.            0000110000001111        0000000011000000        ** ***
  1114.            1111111100000011        0000000000110000            ***
  1115.            1111111100000011        0010000000110000            ***
  1116.            1111111111000011        0000000000000000             **
  1117.            1111111111111111        0000000000000000
  1118.            1111111111111111        0000000000000000
  1119.            1111111111111111        0000000000000000
  1120.  
  1121. As you can see, the resulting mouse cursor is eight pixels wide instead of
  1122. 16.
  1123.  
  1124.      Another important point concerning mouse cursors in modes 4 and 5 is the
  1125. chance of pixel bleeding, or the changing of colors within the mouse cursor
  1126. as it moves horizontally.  Bleeding will occur if you use the bit pairs 01 or
  1127. 10 in either mask to represent a pixel.  In the default masks for modes 4 and
  1128. 5, note that only the binary values 00 and 11 appear as bit pairs.  Keep this
  1129. in mind if you create your own masks in these video modes.
  1130.  
  1131.  
  1132. Joystick Support
  1133.  
  1134.      The third type of input device supported by Fastgraph is the joystick.
  1135. Although joysticks are not as popular as mice, they are often preferable when
  1136. a user's reactions are critical, such as in an arcade-style game.  Fastgraph
  1137. includes routines for initializing a joystick, reading a joystick's position
  1138. or button status, and making a joystick behave analogously to the keyboard.
  1139. These routines are independent of the rest of Fastgraph and thus do not
  1140. require that you first call the fg_setmode routine.
  1141.  
  1142.      Joysticks are connected to a system through a game port.  The PCjr and
  1143. Tandy 1000 systems come equipped with two game ports, and hence support two
  1144. joysticks.  On other systems in the IBM family, you can install a game port
  1145. card that contains either one or two game ports.  If the card only has one
  1146. game port, you can use a splitter cable to fork two joysticks into the port.
  1147.  
  1148.  
  1149. Initializing Joysticks
  1150.  
  1151.      Before you can use any of Fastgraph's joystick support routines with a
  1152. specific joystick, you must initialize that joystick.  The fg_initjoy routine
  1153. performs this task.  This routine requires a single integer argument that
  1154. specifies which joystick to initialize, either 1 or 2.  If successful,
  1155. fg_initjoy returns 0 as the function value.  If the machine has no game port,
  1156. or if the requested joystick is not connected to the game port, fg_initjoy
  1157. returns -1.  When you use fg_initjoy, the joystick being initialized must be
  1158. centered (that is, the stick itself must not be tilted in either direction).
  1159.  
  1160.      Example 14-11 uses the fg_initjoy routine to try to initialize both
  1161. joysticks.  For each joystick, the program prints a message stating whether
  1162. or not the initialization was successful.
  1163. 280   Fastgraph User's Guide
  1164.  
  1165.                                 Example 14-11.
  1166.  
  1167.                  #include <fastgraf.h>
  1168.                  #include <stdio.h>
  1169.                  void main(void);
  1170.  
  1171.                  void main()
  1172.                  {
  1173.                     if (fg_initjoy(1) < 0)
  1174.                        printf("Joystick 1 not available.\n");
  1175.                     else
  1176.                        printf("Joystick 1 found.\n");
  1177.  
  1178.                     if (fg_initjoy(2) < 0)
  1179.                        printf("Joystick 2 not available.\n");
  1180.                     else
  1181.                        printf("Joystick 2 found.\n");
  1182.                  }
  1183.  
  1184.  
  1185.  
  1186. Reporting Joystick Status
  1187.  
  1188.      Each joystick can report three items:  its horizontal position, its
  1189. vertical position, and the button status.  Fastgraph includes routines for
  1190. obtaining each of these quantities.
  1191.  
  1192.      The fg_getxjoy and fg_getyjoy routines respectively return the
  1193. horizontal and vertical position of the indicated joystick.  Both routines
  1194. require a single integer argument, whose value is either 1 or 2, to identify
  1195. the joystick.  The requested position is returned as the function value.
  1196. Horizontal coordinates increase as the joystick moves to the right, while
  1197. vertical coordinates increase as the joystick moves downward.  If fg_initjoy
  1198. did not initialize the specified joystick, or if your program hasn't yet
  1199. called fg_initjoy, both fg_getxjoy and fg_getyjoy will return the value -1.
  1200.  
  1201.      Joystick characteristics vary more than those of any other input device.
  1202. The values returned by fg_getxjoy and fg_getyjoy depend on the system's
  1203. processor speed and the brand of joystick used.  It often suffices to know
  1204. the joystick position relative to its previous position, in which case the
  1205. actual coordinate values do not matter.  However, if you must rely on
  1206. specific coordinate values, your program must perform some type of manual
  1207. joystick calibration and then scale the coordinates reported by fg_getxjoy
  1208. and fg_getyjoy as needed.
  1209.  
  1210.      The other piece of information joysticks provide is the button status.
  1211. Most joysticks have two buttons, called the top and bottom buttons.  Others
  1212. have three buttons, but one of them duplicates the functionality of another
  1213. (for example, a joystick might have one bottom button on its left side and
  1214. another on its right side).  The Fastgraph routine fg_button returns the
  1215. joystick button status as its function value.  Like fg_getxjoy and
  1216. fg_getyjoy, the fg_button routine requires a single argument that specifies
  1217. the joystick number.  The meaning of the returned value is shown below.
  1218.                                        Chapter 14:  Input Device Support   281
  1219.                   value  meaning
  1220.  
  1221.                     0    neither button pressed
  1222.                     1    top button pressed
  1223.                     2    bottom button pressed
  1224.                     3    top and bottom buttons pressed
  1225.  
  1226.      You don't need to call fg_initjoy before using fg_button.  If the
  1227. specified joystick is not present, the fg_button routine will return zero.
  1228.  
  1229.      Example 14-12 uses fg_getxjoy, fg_getyjoy, and fg_button to poll both
  1230. joysticks at half-second intervals.  It then displays the joystick number (1
  1231. or 2), horizontal position, vertical position, and button status for each
  1232. joystick.  As the program runs, you can move the joysticks and watch how the
  1233. movements affect the displayed coordinate values.  The program continues
  1234. doing this until you press Ctrl/C or Ctrl/Break to stop it.
  1235.  
  1236.                                 Example 14-12.
  1237.  
  1238.                   #include <fastgraf.h>
  1239.                   #include <stdio.h>
  1240.                   void main(void);
  1241.                   void main()
  1242.                   {
  1243.                      int b, x, y;
  1244.  
  1245.                      fg_initjoy(1);
  1246.                      fg_initjoy(2);
  1247.  
  1248.                      while (1) {
  1249.                         x = fg_getxjoy(1);
  1250.                         y = fg_getyjoy(1);
  1251.                         b = fg_button(1);
  1252.                         printf("1:  %3d %3d %1d\n",x,y,b);
  1253.                         x = fg_getxjoy(2);
  1254.                         y = fg_getyjoy(2);
  1255.                         b = fg_button(2);
  1256.                         printf("2:  %3d %3d %1d\n\n",x,y,b);
  1257.                         fg_waitfor(9);
  1258.                         }
  1259.                   }
  1260.  
  1261.      There are two ways of effectively monitoring joystick button status.
  1262. One is to call fg_button at many places in your program and then take the
  1263. necessary action depending on the button status.  However, the preferable
  1264. method is to extend the BIOS time-of-day interrupt to check the button status
  1265. at each clock tick (there are 18.2 clock ticks per second), set a flag if a
  1266. button is pressed, and then check the flag as needed in your program.
  1267. Information on changing the BIOS time-of-day interrupt appears in Appendix C
  1268. of this document.
  1269.  
  1270.  
  1271. Keyboard Emulation
  1272.  
  1273.      Although we can use the fg_getxjoy and fg_getyjoy routines to monitor
  1274. relative joystick movements, it is usually easier to do this with another
  1275. Fastgraph routine, fg_intjoy.  This routine is similar to the fg_intkey
  1276. 282   Fastgraph User's Guide
  1277.  
  1278. routine in that it returns two values that are equivalent to the standard or
  1279. extended keyboard codes for analogous keystrokes.
  1280.  
  1281.      The fg_intjoy routine needs three arguments.  The first argument
  1282. specifies the joystick number, either 1 or 2.  The second and third
  1283. arguments, both one-byte quantities passed by reference, receive the standard
  1284. and extended keyboard codes analogous to the joystick movement and button
  1285. status.  The second argument receives a value of 13 (the standard keyboard
  1286. code for the Enter key) if any joystick button is pressed; it receives a
  1287. value of 0 if not.  The third argument receives a value corresponding to the
  1288. extended keyboard code for one of the directional keys on the numeric keypad,
  1289. as summarized in the following table.
  1290.  
  1291.        joystick position   corresponding key   extended key code
  1292.  
  1293.           up and left            Home                 71
  1294.               up               up arrow               72
  1295.          up and right            PgUp                 73
  1296.              left             left arrow              75
  1297.            centered           (no action)              0
  1298.              right            right arrow             77
  1299.          down and left            End                 79
  1300.              down             down arrow              80
  1301.         down and right           PgDn                 81
  1302.  
  1303. The fg_intjoy routine will set both key code arguments to zero if the
  1304. specified joystick has not yet been initialized.
  1305.  
  1306.      Example 14-13 is similar to example 14-11, but it uses fg_intjoy in
  1307. place of fg_getxjoy and fg_getyjoy to report relative joystick position.
  1308. This program does not report the joystick button status as example 14-11
  1309. does, but you could readily add this feature to it.
  1310.  
  1311.                                 Example 14-13.
  1312.  
  1313.                    #include <fastgraf.h>
  1314.                    #include <stdio.h>
  1315.                    void main(void);
  1316.  
  1317.                    void main()
  1318.                    {
  1319.                       char key, aux;
  1320.  
  1321.                       fg_initjoy(1);
  1322.                       fg_initjoy(2);
  1323.  
  1324.                       while (1) {
  1325.                          fg_intjoy(1,&key,&aux);
  1326.                          printf("1: %2d %2d\n",key,aux);
  1327.                          fg_intjoy(2,&key,&aux);
  1328.                          printf("2: %2d %2d\n\n",key,aux);
  1329.                          fg_waitfor(9);
  1330.                          }
  1331.                    }
  1332.  
  1333.                                        Chapter 14:  Input Device Support   283
  1334.  
  1335. Special Joystick Considerations
  1336.  
  1337.      If you develop a program that supports only one joystick, you should use
  1338. joystick 1.  The reasons for this are twofold.  First, it will make your
  1339. program consistent with most other products that support joysticks.  Second,
  1340. and perhaps more importantly, many Tandy 1000 series machines cannot
  1341. determine if joystick 2 is present when neither joystick is connected.  This
  1342. means if you use joystick 2 instead of joystick 1 in a single joystick
  1343. program, you won't be able to tell if a joystick is available when running on
  1344. a Tandy 1000.
  1345.  
  1346.  
  1347. Summary of Input Routines
  1348.  
  1349.      This section summarizes the functional descriptions of the Fastgraph
  1350. routines presented in this chapter.  More detailed information about these
  1351. routines, including their arguments and return values, may be found in the
  1352. Fastgraph Reference Manual.
  1353.  
  1354.      FG_BUTTON returns information about the state of either joystick's
  1355. buttons.
  1356.  
  1357.      FG_CAPSLOCK determines the state of the CapsLock key.
  1358.  
  1359.      FG_GETKEY waits for a keystroke (or reads the next entry from the BIOS
  1360. keyboard buffer). It returns the keystroke's standard or extended keyboard
  1361. code.
  1362.  
  1363.      FG_GETXJOY and FG_GETYJOY return the horizontal and vertical coordinate
  1364. position of the specified joystick.  The actual coordinates depend on the
  1365. processor speed and brand of joystick used.
  1366.  
  1367.      FG_INITJOY initializes joystick 1 or 2 and must be called before using
  1368. fg_getxjoy, fg_getyjoy, or fg_intjoy.  It returns a status code indicating
  1369. whether or not the initialization was successful.
  1370.  
  1371.      FG_INTJOY returns the standard and extended keyboard codes analogous to
  1372. the current position and button status of the specified joystick.
  1373.  
  1374.      FG_INTKEY reads the next entry from the BIOS keyboard buffer and returns
  1375. the keystroke's standard or extended keyboard code.  It is similar to
  1376. fg_getkey, but it does not wait for a keystroke if the keyboard buffer is
  1377. empty.
  1378.  
  1379.      FG_KBINIT enables or disables the Fastgraph low-level keyboard handler.
  1380. If the keyboard handler is already in the requested state, nothing happens.
  1381.  
  1382.      FG_KBTEST determines if the key having the specified scan code is now
  1383. pressed or released.  The low-level keyboard handler must be enabled for this
  1384. routine to work properly.
  1385.  
  1386.      FG_MOUSEBUT returns information about mouse button press or release
  1387. counts, as well as the mouse cursor position at the time of the last button
  1388. press or release.
  1389. 284   Fastgraph User's Guide
  1390.  
  1391.      FG_MOUSECUR defines the appearance of the mouse cursor in text video
  1392. modes.
  1393.  
  1394.      FG_MOUSEFIN unhooks Fastgraph's XVGA or SVGA mouse handler from the
  1395. mouse driver.  This routine should be used just before reverting to text mode
  1396. in programs that have called fg_mouseini in XVGA or SVGA graphics modes.  It
  1397. has no effect in other video modes.
  1398.  
  1399.      FG_MOUSEINI initializes the mouse and must be called before any of
  1400. Fastgraph's other mouse support routines.  It returns an error status if the
  1401. mouse driver has not been loaded, or if the mouse is not connected.
  1402.  
  1403.      FG_MOUSELIM defines the rectangular area in which the mouse cursor may
  1404. move.
  1405.  
  1406.      FG_MOUSEMOV moves the mouse cursor to the specified character cell (in
  1407. text modes) or screen space position (in graphics modes).
  1408.  
  1409.      FG_MOUSEPOS returns the current mouse position and button status.
  1410.  
  1411.      FG_MOUSEPTR defines the shape and appearance of the mouse cursor in
  1412. graphics video modes.
  1413.  
  1414.      FG_MOUSESPD defines the number of mickey units per eight pixels of
  1415. cursor movement.  This effectively controls the speed at which the mouse
  1416. cursor moves relative to the movement of the mouse itself.
  1417.  
  1418.      FG_MOUSEVIS makes the mouse cursor visible or invisible.
  1419.  
  1420.      FG_NUMLOCK determines the state of the NumLock key.
  1421.  
  1422.      FG_SCRLOCK determines the state of the ScrollLock key (which is not
  1423. present on some keyboards).
  1424.  
  1425.      FG_SETCAPS controls the state of the CapsLock key.
  1426.  
  1427.      FG_SETNUM controls the state of the NumLock key.
  1428.  
  1429.      FG_WAITKEY flushes the BIOS keyboard buffer (that is, removes any type-
  1430. ahead characters) and then waits for another keystroke.