home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / pascal / rehack / event / mouse.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-09  |  4.1 KB  |  236 lines

  1. #include "..\event\mouse.hpp"
  2. #include "..\game\game.hpp"
  3. #include "..\event\eventq.hpp"
  4. #include <dos.h>
  5.  
  6. // ------------------------------------------------------------------
  7. // File:        MOUSE.CPP
  8. // Path:        ...\REHACK\EVENT\MOUSE.CPP
  9. // Version:        0.01
  10. // Author:        Pat Reilly
  11. // CIS Id:        71333,2764
  12. // Created On:    6/26/93
  13. // Modified On:
  14. // Description:    Mouse class for REHACK. See MOUSE.TXT for
  15. //                more details.
  16. // Tabs:        4
  17. // ------------------------------------------------------------------
  18.  
  19. bool  Mouse::checked = false;
  20. bool  Mouse::present = false;
  21. bool  Mouse::installed = false;
  22. Event Mouse::curEvent;
  23. word  Mouse::dblClickInterval = 8;
  24. dword Mouse::lastLBDown;
  25. dword Mouse::lastRBDown;
  26. dword Mouse::lastCBDown;
  27.  
  28. Mouse::~Mouse()
  29. {
  30.     remove();
  31. }
  32.  
  33. bool Mouse::isPresent()
  34. {
  35.     return checkFor();
  36. }
  37.  
  38. bool Mouse::install()
  39. {
  40.     if(checkFor() && !installed)
  41.         {
  42.         installed = true;
  43.         getStatus(curEvent.posDevice);
  44.         curEvent.setSystemTicks();
  45.         curEvent.type = 0;
  46.         lastLBDown = lastRBDown = lastCBDown = curEvent.ticks-dblClickInterval-40UL;
  47.  
  48.         word pseg = FP_SEG(Mouse::callbackFunction);
  49.         word pofs = FP_OFF(Mouse::callbackFunction);
  50.         asm    {
  51.             push    es
  52.             mov        ax,12
  53.             mov        cx,0xFFFF
  54.             mov        es,pseg
  55.             mov        dx,pofs
  56.             int        0x33
  57.             pop        es
  58.             }
  59.         return true;
  60.         }
  61.     else
  62.         return false;
  63. }
  64.  
  65. void Mouse::remove()
  66. {
  67.     if(checkFor() && installed)
  68.         {
  69.         installed = false;
  70.         _AX = 0;
  71.         asm    int 0x33;
  72.         }
  73. }
  74.  
  75. void Mouse::setBounds(const Rect& bounds)
  76. {
  77.     if(checkFor())
  78.         {
  79.         // For 320x200
  80.         _CX = bounds.topLeft.x*2;
  81.         _DX = bounds.bottomRight.x*2;
  82.         _AX = 7;
  83.         asm    int 0x33;
  84.  
  85.         _CX = bounds.topLeft.y;
  86.         _DX = bounds.bottomRight.y;
  87.         _AX = 8;
  88.         asm    int 0x33;
  89.         }
  90. }
  91.  
  92. void Mouse::show()
  93. {
  94.     if(checkFor())
  95.         {
  96.         _AX = 1;
  97.         asm    int 0x33;
  98.         }
  99. }
  100.  
  101. void Mouse::hide()
  102. {
  103.     if(checkFor())
  104.         {
  105.         _AX = 2;
  106.         asm    int 0x33;
  107.         }
  108. }
  109.  
  110. void Mouse::moveTo(const Point& pt)
  111. {
  112.     if(checkFor())
  113.         {
  114.         _CX = pt.x*2;
  115.         _DX = pt.y*2;
  116.         _AX = 4;
  117.         asm    int 0x33;
  118.         }
  119. }
  120.  
  121. void Mouse::setCursor(const Point& pt, void* buf)
  122. {
  123.     if(checkFor())
  124.         {
  125.         word pseg = FP_SEG(buf);
  126.         word pofs = FP_OFF(buf);
  127.         word x = pt.x;
  128.         word y = pt.y;
  129.         asm    {
  130.             push    es
  131.             mov        ax,9
  132.             mov        cx,y
  133.             mov        bx,x
  134.             mov        es,pseg
  135.             mov        dx,pofs
  136.             int        0x33
  137.             pop        es
  138.             }
  139.         }
  140. }
  141.  
  142. void Mouse::setDblClickInterval(word w)
  143. {
  144.     dblClickInterval = w;
  145. }
  146.  
  147. void Mouse::getStatus(PosDeviceEvent& pd)
  148. {
  149.     pd.buttons = 0;
  150.     pd.dblClicked = false;
  151.     pd.location.x = pd.location.y = -1;
  152.  
  153.     if(checkFor())
  154.         {
  155.         _AX = 3;
  156.         asm    int 0x33;
  157.         pd.buttons = _BL;
  158.         pd.location.x = _CX;
  159.         pd.location.y = _DX;
  160.         // For 320x200
  161.         pd.location.x >>= 1;
  162.         }
  163. }
  164.  
  165. bool Mouse::checkFor()
  166. {
  167.     if(!checked)
  168.         {
  169.         checked = true;
  170.         if(getvect(0x33) != 0)
  171.             {
  172.             _AX = 0;
  173.             asm    int 0x33;
  174.             present = bool(_AX != 0);
  175.             }
  176.         }
  177.     return present;
  178. }
  179.  
  180. const word
  181.     cbfMove     = 0x0001,
  182.     cbfLBDown   = 0x0002,
  183.     cbfLBUp        = 0x0004,
  184.     cbfRBDown    = 0x0008,
  185.     cbfRBUp        = 0x0010,
  186.     cbfCBDown    = 0x0020,
  187.     cbfCBUp        = 0x0040;
  188.  
  189. #pragma saveregs
  190.  
  191. void huge Mouse::callbackFunction()
  192. {
  193.     word ax;
  194.  
  195.     ax = _AX;
  196.  
  197.     curEvent.posDevice.buttons = _BL;
  198.     curEvent.posDevice.location.x = _CX;
  199.     curEvent.posDevice.location.y = _DX;
  200.     curEvent.posDevice.dblClicked = false;
  201.     curEvent.type = 0;
  202.     curEvent.setSystemTicks();
  203.     // For 320x200
  204.     curEvent.posDevice.location.x >>= 1;
  205.  
  206.     if(ax & cbfMove)
  207.         curEvent.type |= PosDeviceMove;
  208.     if(ax & (cbfLBDown | cbfRBDown | cbfCBDown))
  209.         {
  210.         curEvent.type |= PosDeviceBtnDown;
  211.         if(ax & cbfLBDown)
  212.             {
  213.             if(curEvent.ticks-lastLBDown <= dblClickInterval)
  214.                 curEvent.posDevice.dblClicked = true;
  215.             lastLBDown = curEvent.ticks;
  216.             }
  217.         else if(ax & cbfRBDown)
  218.             {
  219.             if(curEvent.ticks-lastRBDown <= dblClickInterval)
  220.                 curEvent.posDevice.dblClicked = true;
  221.             lastRBDown = curEvent.ticks;
  222.             }
  223.         else
  224.             {
  225.             if(curEvent.ticks-lastCBDown <= dblClickInterval)
  226.                 curEvent.posDevice.dblClicked = true;
  227.             lastCBDown = curEvent.ticks;
  228.             }
  229.         }
  230.  
  231.     if(ax & (cbfLBUp | cbfRBUp | cbfCBUp))
  232.         curEvent.type |= PosDeviceBtnUp;
  233.  
  234.     Game::eventQueue.put(curEvent);
  235.     }
  236.