home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Game Programming Gurus / Tricks_of_the_Game_Programming_Gurus_SAMS_Publishing_1994.iso / source / chap_03 / mouse.c < prev    next >
Text File  |  1994-01-17  |  5KB  |  204 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <dos.h>
  5. #include <bios.h>
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <conio.h>
  9. #include <graph.h>
  10.  
  11.  
  12. // D E F I N E S  ////////////////////////////////////////////////////////////
  13.  
  14. // mouse sub-function calls
  15.  
  16. #define MOUSE_INT                0x33 //mouse interrupt number
  17. #define MOUSE_RESET              0x00 // reset the mouse
  18. #define MOUSE_SHOW               0x01 // show the mouse
  19. #define MOUSE_HIDE               0x02 // hide the mouse
  20. #define MOUSE_BUTT_POS           0x03 // get buttons and postion
  21. #define MOUSE_SET_SENSITIVITY    0x1A // set the sensitivity of mouse 0-100
  22. #define MOUSE_MOTION_REL         0x0B // query motion counters to compute
  23.                                       // relative motion
  24.  
  25. // defines to make reading buttons easier
  26.  
  27. #define MOUSE_LEFT_BUTTON        0x01 // left button mask
  28. #define MOUSE_RIGHT_BUTTON       0x02 // right button mask
  29. #define MOUSE_CENTER_BUTTON      0x04 // center button mask
  30.  
  31.  
  32. // G L O B A L S  ////////////////////////////////////////////////////////////
  33.  
  34.  
  35.  
  36. // F U N C T I O N S /////////////////////////////////////////////////////////
  37.  
  38.  
  39. int Squeeze_Mouse(int command, int *x, int *y,int *buttons)
  40. {
  41. // mouse interface, we'll use _int86 instead of inline asm...Why? No real reason.
  42. // what function is caller requesting?
  43.  
  44. union _REGS inregs, outregs;
  45.  
  46. switch(command)
  47.       {
  48.  
  49.       case MOUSE_RESET:
  50.            {
  51.  
  52.            inregs.x.ax = 0x00; // subfunction 0: reset
  53.            _int86(MOUSE_INT, &inregs, &outregs);
  54.            *buttons = outregs.x.bx; // return number of buttons
  55.            return(outregs.x.ax);    // return overall success/failure
  56.  
  57.            } break;
  58.  
  59.       case MOUSE_SHOW:
  60.            {
  61.            // this function increments the internal show mouse counter.
  62.            // when it is equal to 0 then the mouse will be displayed.
  63.  
  64.            inregs.x.ax = 0x01; // subfunction 1: increment show flag
  65.            _int86(MOUSE_INT, &inregs, &outregs);
  66.  
  67.            return(1);
  68.  
  69.            } break;
  70.  
  71.       case MOUSE_HIDE:
  72.            {
  73.            // this function decrements the internal show mouse counter.
  74.            // when it is equal to -1 then the mouse will be hidden.
  75.  
  76.            inregs.x.ax = 0x02; // subfunction 2: decrement show flag
  77.            _int86(MOUSE_INT, &inregs, &outregs);
  78.  
  79.            return(1);
  80.  
  81.            } break;
  82.  
  83.       case MOUSE_BUTT_POS:
  84.            {
  85.            // this functions gets the buttons and returns the absolute mouse
  86.            // positions in the vars x,y, and buttons, respectively
  87.  
  88.            inregs.x.ax = 0x03; // subfunction 3: get position and buttons
  89.            _int86(MOUSE_INT, &inregs, &outregs);
  90.  
  91.            // extract the info and send back to caller via pointers
  92.            *x       = outregs.x.cx;
  93.            *y       = outregs.x.dx;
  94.            *buttons = outregs.x.bx;
  95.  
  96.            return(1);
  97.  
  98.            } break;
  99.  
  100.       case MOUSE_MOTION_REL:
  101.            {
  102.  
  103.            // this functions gets the relative mouse motions from the last
  104.            // call and puts them in the vars x,y respectively
  105.  
  106.            inregs.x.ax = 0x03; // subfunction 11: get relative motion
  107.            _int86(MOUSE_INT, &inregs, &outregs);
  108.  
  109.            // extract the info and send back to caller via pointers
  110.            *x       = outregs.x.cx;
  111.            *y       = outregs.x.dx;
  112.  
  113.            return(1);
  114.  
  115.            } break;
  116.  
  117.       case MOUSE_SET_SENSITIVITY:
  118.            {
  119.            // this function sets the overall "sensitivity" of the mouse.
  120.            // each axis can have a sensitivity from 1-100.  So the caller
  121.            // should put 1-100 in both "x" and "y" before calling/
  122.            // also "buttons" is used to send in the doublespeed value which
  123.            // ranges from 1-100 also.
  124.  
  125.            inregs.x.bx = *x;
  126.            inregs.x.cx = *y;
  127.            inregs.x.dx = *buttons;
  128.  
  129.            inregs.x.ax = 0x1A; // subfunction 26: set sensitivity
  130.            _int86(MOUSE_INT, &inregs, &outregs);
  131.  
  132.            return(1);
  133.  
  134.            } break;
  135.  
  136.       default:break;
  137.  
  138.       } // end switch
  139.  
  140. } // end Squeze_Mouse
  141.  
  142. //////////////////////////////////////////////////////////////////////////////
  143.  
  144. void main(void)
  145. {
  146.  
  147. int x,y,buttons,num_buttons;
  148. int color=1;
  149.  
  150. // put the computer into graphics mode
  151.  
  152. _setvideomode(_VRES16COLOR); //  640x480 in 16 colors
  153.  
  154. // initialize mouse
  155.  
  156. Squeeze_Mouse(MOUSE_RESET,NULL,NULL,&num_buttons);
  157.  
  158. // show the mouse
  159.  
  160. Squeeze_Mouse(MOUSE_SHOW,NULL,NULL,NULL);
  161.  
  162. while(!kbhit())
  163.      {
  164.  
  165.      _settextposition(2,0);
  166.  
  167.      Squeeze_Mouse(MOUSE_BUTT_POS,&x,&y,&buttons);
  168.  
  169.      printf("mouse x=%d y=%d buttons=%d    ",x,y,buttons);
  170.  
  171.      // video easel
  172.  
  173.      if (buttons==1)
  174.         {
  175.         _setcolor(color);
  176.         _setpixel(x-1,y-2);
  177.         _setpixel(x,y-2);
  178.         _setpixel(x-1,y-1);
  179.         _setpixel(x,y-1);
  180.         } // end if draw on
  181.  
  182.      if (buttons==2)
  183.         {
  184.         if (++color>15) color=0;
  185.  
  186.         // wait for mouse release
  187.  
  188.         while(buttons==2)
  189.              {
  190.              Squeeze_Mouse(MOUSE_BUTT_POS,&x,&y,&buttons);
  191.              } // end while
  192.  
  193.         } // end if draw on
  194.  
  195.      } // end while
  196.  
  197. // place the computer back into text mode
  198.  
  199. _setvideomode(_DEFAULTMODE);
  200.  
  201. } // end main
  202.  
  203.  
  204.