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