home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / 3DENGINE.ZIP / Eventmgr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-07  |  4.6 KB  |  169 lines

  1.  
  2. // EVENTMGR.CPP
  3. //
  4. // A set of high-level functions to monitor input from
  5. // the keyboard, the joystick and the mouse. The getevent()
  6. // function is specific to the events required by GARDENS.PRJ.
  7. //
  8. // Written by Christopher Lampton for
  9. // GARDENS OF IMAGINATION (Waite Group Press,1994)
  10.  
  11. #include <iostream.h>
  12. #include    <stdio.h>
  13. #include  <dos.h>
  14. #include    "io.h"
  15. #include    "eventmgr.h"
  16.  
  17. // Constant definition:
  18.  
  19. const GAMEPORT=0x0201;
  20.  
  21. // Variable declarations
  22.  
  23. int x,y;    // All purpose coordinate variables
  24. int xmin,xmax,xcent,ymin,ymax,ycent; // Joystick cali-
  25.                                                  // bration variables
  26. char far keybuffer[256]; // Buffer for 256 scan codes
  27. int lastkey=0,keycount=0;    // Keyboard variables
  28.  
  29. void init_events()
  30. // Initialize event manager
  31. {
  32.   initkey(keybuffer); // Install alternate keyboard driver
  33.     initmouse();          // Initialize the mouse driver
  34.     rempointer();          // Remove mouse pointer from screen
  35. }
  36.  
  37. void setmin()
  38. // Set minimum joystick coordinates
  39. {
  40.   while (!readjbutton(JBUTTON1)); // Loop until joystick
  41.                                              //  button pressed
  42.   for (long i=1; i<100000; i++);  // Brief delay
  43.   xmin=readstick(JOY_X);          // Get x coordinate
  44.   ymin=readstick(JOY_Y);          // Get y coordinate
  45.   while (readjbutton(JBUTTON1));  // Loop until button
  46.                                              //  released
  47. }
  48.  
  49. void setmax()
  50. // Set maximum joystick coordinates
  51. {
  52.   while (!readjbutton(JBUTTON1)); // Loop until joystick
  53.                                              //  button pressed
  54.   for (long i=1; i<100000; i++);  // Brief delay
  55.   xmax=readstick(JOY_X);          // Get x coordinate
  56.   ymax=readstick(JOY_Y);          // Get y coordinate
  57.   while (readjbutton(JBUTTON1));  // Loop until button
  58.                                              //  released
  59. }
  60.  
  61. int joystick_present()
  62.  
  63. // Function to detect presence of joystick, as follows:
  64. //
  65. // 0 - no stick
  66. // 1 - stick 1 present
  67. // 2 - stick 2 present
  68. // 3 - both sticks present
  69. //
  70. // Adapted from code written by Mark Betz for
  71. // Flights of Fantasy (Waite Group Press, 1993)
  72.  
  73. {
  74.   int mask=0;
  75.  
  76.   // Joystick 1 present?
  77.  
  78.   outportb(GAMEPORT,0);
  79.   for(unsigned int i=0; i<65535; i++)
  80.      if ((inportb(GAMEPORT) & 1) == 0) break;
  81.   if (i !=65535) mask = mask | 1;
  82.  
  83.   // Joystick 2 present?
  84.  
  85.   outportb(GAMEPORT,0);
  86.   for(i=0; i<65535; i++)
  87.      if ((inportb(GAMEPORT) & 2) == 0) break;
  88.   if (i !=65535) mask = mask | 2;
  89.  
  90.   return (mask);
  91. }
  92.  
  93. void end_events()
  94. // Terminate event manager routines
  95. {
  96.   remkey();
  97. }
  98.  
  99. void setcenter()
  100. // Set center joystick coordinates
  101. {
  102.   while (!readjbutton(JBUTTON1)); // Loop until joystick
  103.                                              //  button pressed
  104.   for (long i=1; i<100000; i++);  // Brief delay
  105.   xcent=readstick(JOY_X);         // Get x coordinate
  106.   ycent=readstick(JOY_Y);         // Get y coordinate
  107.   while (readjbutton(JBUTTON1));  // Loop until button
  108.                                              //  released
  109. }
  110.  
  111. void calibrate_stick(void)
  112. {
  113.     cout << "Joystick detected. \n";
  114.     cout << "CENTER the joystick and press button 1:\n";
  115.     setcenter();
  116.     cout << "Move the joystick to the UPPER LEFT corner and press button 1:\n";
  117.     setmin();
  118.     cout << "Move the joystick to the LOWER RIGHT corner and press button 1:\n";
  119.     setmax();
  120. }
  121.  
  122. void getevent(int event_mask,event_struct *events)
  123. // Get events from devices selected by EVENT_MASK
  124. {
  125.  
  126.     // Clear any events in structure:
  127.  
  128.     events->go_forward = 0;
  129.     events->go_back = 0;
  130.     events->go_left = 0;
  131.     events->go_right = 0;
  132.     events->fire = 0;
  133.     events->abort = 0;
  134.     events->pause = 0;
  135.     events->resize = 0;
  136.     events->open_door = 0;
  137.  
  138.     // If joystick events requested....
  139.     if (event_mask & JOYSTICK_EVENTS) {
  140.         if (readstick(JOY_Y)<(ycent-100)) events->go_forward = 1;
  141.         if (readstick(JOY_Y)>(ycent+100)) events->go_back = 1;
  142.         if (readstick(JOY_X)<(xcent-100)) events->go_left = 1;
  143.         if (readstick(JOY_X)>(xcent+100)) events->go_right = 1;
  144.         if (readjbutton(JBUTTON1)) events->fire = 1;
  145.     }
  146.     // If mouse events requested....
  147.     if (event_mask & MOUSE_EVENTS)
  148.     {
  149.         relpos(&x,&y);    // Read relative mouse position
  150.         if (y < -5) events->go_forward = 1;
  151.         if (y > 5) events->go_back = 1;
  152.         if (x < -20) events->go_left = 1;
  153.         if (x > 20) events->go_right = 1;
  154.         int b=readmbutton();    // Read mouse button
  155.         if (b & LMBUTTON) events->fire = 1;
  156.     }
  157.     // If keyboard events requested....
  158.     if (event_mask & KEYBOARD_EVENTS) {
  159.         if (keybuffer[FORWARDKEY]) events->go_forward = 1;
  160.         if (keybuffer[BACKKEY]) events->go_back = 1;
  161.         if (keybuffer[LEFTKEY]) events->go_left = 1;
  162.         if (keybuffer[RIGHTKEY]) events->go_right = 1;
  163.         if (keybuffer[QUITKEY]) events->abort = 1;
  164.         if (keybuffer[P]) events->pause = 1;
  165.         if (keybuffer[F3]) events->resize = 1;
  166.         if (keybuffer[SPACE]) events->open_door = 1;
  167.     }
  168. }
  169.