home *** CD-ROM | disk | FTP | other *** search
/ PC World 2006 July & August / PCWorld_2006-07-08_cd.bin / temacd / planearcade / planearcade.exe / Tank3.bmp / input.cpp < prev    next >
C/C++ Source or Header  |  2004-10-24  |  8KB  |  381 lines

  1. #include "main.h"
  2.  
  3. //-------------------------------------------------------------
  4. //GLOBAL
  5. //--------------------------------------------------------------
  6. INPUT Input;
  7.  
  8. //------------------------------------------------------------------
  9. // Name: INPUT()
  10. // Desc: konstruktor
  11. //------------------------------------------------------------------
  12. INPUT::INPUT()
  13. {
  14.     m_pKeyboard    = NULL; 
  15.     m_pMouse       = NULL;
  16.     m_pDirectInput = NULL;
  17.  
  18.     //inicializacia poli
  19.     for (int i=0;i<256;i++)
  20.     {
  21.         KeyPRESS[i] = false;
  22.         KeyDOWN[i] = false;
  23.     }
  24. }
  25.  
  26. //------------------------------------------------------------------
  27. // Name: Initialize()
  28. // Desc: inicializacia Dinput
  29. //------------------------------------------------------------------
  30. void INPUT::Initialize()
  31. {
  32.  
  33.  
  34.     LogPrint("Nastavujem Direct Input");
  35.  
  36.     //vytvori DirectInput Objekt
  37.     if(FAILED(DirectInput8Create(hIns, DIRECTINPUT_VERSION, 
  38.           IID_IDirectInput8, (void**)&m_pDirectInput, NULL))) 
  39.     {
  40.         LogPrint("   Nepodarilo sa vytvori¥ m_pDirectInput");
  41.     }
  42.     else
  43.     {
  44.         LogPrint("   m_pDirectInput vytvoreny");
  45.     }
  46.  
  47.  
  48.  
  49.     //KEYBOARD =======================================================================
  50.     //Create the keyboard device object
  51.     if(FAILED(m_pDirectInput->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL))) 
  52.     { 
  53.         LogPrint("   Nepodarilo sa nastavit klavesnicu");       
  54.     }    
  55.     else
  56.     {
  57.         LogPrint("   Klavesnica nastavena");        
  58.     }
  59.  
  60.     //Set the data format for the keyboard
  61.     if(FAILED(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard)))
  62.     { 
  63.         LogPrint("   Nepodarilo sa nastavit format klavesnice");
  64.     }
  65.     else
  66.     {
  67.         LogPrint("   Format klavesnice nastaveny");
  68.     }
  69.  
  70.     //Set the cooperative level for the keyboard
  71.     if(FAILED(m_pKeyboard->SetCooperativeLevel(hWnd, 
  72.                                            DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
  73.     { 
  74.         LogPrint("   nepodarilo sa nastavit ccoperative level pre klavesnicu");
  75.     }
  76.     else
  77.     {
  78.         LogPrint("   cooperative level pre klavesnicu nastaveny");
  79.     }    
  80.  
  81.     //Acquire the keyboard
  82.     if(m_pKeyboard)
  83.     {
  84.         LogPrint("   Acquire klavesnica");
  85.         m_pKeyboard->Acquire(); 
  86.     }
  87.  
  88.     //JOYSTICK========================================================================
  89.     if (JoyEnabled) 
  90.     {
  91.         //Create joystick device
  92.         if(FAILED(m_pDirectInput->CreateDevice( GUID_Joystick , 
  93.                                                 &m_pJoystick, 
  94.                                                 NULL )))
  95.         { 
  96.             LogPrint("  Nepodarilo sa nastavit joystick");       
  97.         }    
  98.         else
  99.         {
  100.             LogPrint("  Joystick nastaveny");
  101.         }
  102.  
  103.         if (m_pJoystick != NULL)
  104.         {
  105.             //Set the data format for the joystick
  106.             if(FAILED(m_pJoystick->SetDataFormat(&c_dfDIJoystick)))
  107.             { 
  108.                 LogPrint("   Nepodarilo sa nastavit format joystick");
  109.             }
  110.             else
  111.             {
  112.                 LogPrint("   Format joysticku nastaveny");
  113.             }
  114.  
  115.             //Set the cooperative level for the joystick
  116.             if(FAILED(m_pJoystick->SetCooperativeLevel(hWnd, 
  117.                                                    DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
  118.             { 
  119.                 LogPrint("   nepodarilo sa nastavit ccoperative level pre joystick");
  120.             }
  121.             else
  122.             {
  123.                 LogPrint("   cooperative level pre joystick nastaveny");
  124.                 
  125.             }
  126.                 
  127.             //Acquire the joystick
  128.             if(m_pJoystick)
  129.             {
  130.                 LogPrint(" Acquire joystick");
  131.                 m_pJoystick->Acquire(); 
  132.             }
  133.         }
  134.     }
  135.  
  136.  
  137.     
  138.     //MOUSE =======================================================================
  139.     //Create the mouse device object
  140.     if(FAILED(m_pDirectInput->CreateDevice(GUID_SysMouse, &m_pMouse, NULL)))
  141.     { 
  142.         LogPrint("   Nepodarilo sa nastavit mys ");
  143.     }
  144.     else
  145.     {
  146.         LogPrint("   Mys nastavena");
  147.     }
  148.  
  149.     //Set the data format for the mouse
  150.     if(FAILED(m_pMouse->SetDataFormat(&c_dfDIMouse)))
  151.     { 
  152.         LogPrint("   Nepodarilo sa nastavit formaty mysi");
  153.     }
  154.     else
  155.     {
  156.         LogPrint("   Format myse nastavena");
  157.     }
  158.  
  159.     //Set the cooperative level for the mouse
  160.     if(FAILED(m_pMouse->SetCooperativeLevel(hWnd, 
  161.                                         DISCL_FOREGROUND|DISCL_NONEXCLUSIVE)))
  162.     { 
  163.         LogPrint("   nepodarilo sa nastavit ccoperative level pre mys");
  164.     }
  165.     else
  166.     {
  167.         LogPrint("   cooperative level pre mys nastaveny");
  168.     }
  169.  
  170.     //Acquire the mouse
  171.     if(m_pMouse)
  172.     {
  173.         LogPrint("   Acquire mys");
  174.         m_pMouse->Acquire(); 
  175.     }
  176.  
  177.     
  178. }
  179.  
  180.  
  181. //------------------------------------------------------------------
  182. // Name: CleanUp()
  183. // Desc: Odstrani DInput 
  184. //------------------------------------------------------------------
  185. void INPUT::CleanUp()
  186. {
  187.     if(m_pKeyboard)
  188.     {
  189.         m_pKeyboard->Unacquire(); 
  190.         m_pKeyboard->Release();
  191.     }
  192.  
  193.     if(m_pMouse)
  194.     {
  195.         m_pMouse->Unacquire();
  196.         m_pMouse->Release();
  197.     }
  198.  
  199.     if(m_pJoystick)
  200.     {
  201.         m_pJoystick->Unacquire();
  202.         m_pJoystick->Release();
  203.     }
  204.  
  205.    if(m_pDirectInput != NULL)
  206.         m_pDirectInput->Release();
  207.  
  208. }
  209.  
  210.  
  211. //------------------------------------------------------------------
  212. // Name: RefreshKeyboard()
  213. // Desc: Ziska stlacene klavesy
  214. //------------------------------------------------------------------
  215. void INPUT::RefreshKeyboard()
  216. {
  217.     int i;
  218.  
  219.     if (m_pKeyboard == NULL)
  220.         return;
  221.  
  222.     //ziskanie KeyDown
  223.     if( FAILED(m_pKeyboard->GetDeviceState(sizeof(KeyDown),
  224.                                           (LPVOID)&KeyDown)))
  225.     {
  226.         HRESULT hr = m_pKeyboard->Acquire();
  227.         while( hr == DIERR_INPUTLOST ) 
  228.             hr = m_pKeyboard->Acquire();
  229.     }
  230.  
  231.     //ziskanie KeyDOWN a KeyPRESS
  232.     for (i=0;i<256;i++)
  233.     {
  234.         
  235.         if (KeyDOWN[i] == false)
  236.         {
  237.             KeyPRESS[i] =  (KEYDOWN(KeyDown,i));
  238.         }
  239.         else
  240.         {
  241.             KeyPRESS[i] = false;
  242.         }
  243.  
  244.         KeyDOWN[i] =  (KEYDOWN(KeyDown,i));
  245.  
  246.     }
  247.  
  248.   
  249. }
  250.  
  251. //------------------------------------------------------------------
  252. // Name: RefreshKeyboard()
  253. // Desc: Ziska stlacene klavesy
  254. //------------------------------------------------------------------
  255. void INPUT::RefreshJoystick()
  256. {
  257.     int i;
  258.  
  259.     if (JoyEnabled == 0) 
  260.         return;
  261.  
  262.     if (m_pJoystick == NULL)
  263.         return;
  264.  
  265.     DIJOYSTATE JoyState;
  266.  
  267.     m_pJoystick->Poll(); 
  268.  
  269.     if( FAILED(m_pJoystick->GetDeviceState( sizeof(DIJOYSTATE), 
  270.                                             &JoyState )))
  271.     {
  272.         ZeroMemory( &JoyState, sizeof(DIJOYSTATE) );
  273.         HRESULT hr = m_pJoystick->Acquire();
  274.         while( hr == DIERR_INPUTLOST ) 
  275.             hr = m_pJoystick->Acquire();
  276.     }
  277.         
  278.     JoyX =  ((float)JoyState.lX);
  279.     JoyY =  ((float)JoyState.lY);    
  280.  
  281.     JoyX =  (JoyX - (65535/2.0f))/(65535/2.0f);
  282.     JoyY =  (JoyY - (65535/2.0f))/(65535/2.0f);
  283.  
  284.     float cX = 1.0f;
  285.     float cY = 1.0f;
  286.     if (JoyX < 0.0f) cX = -1.0f;
  287.     if (JoyY < 0.0f) cY = -1.0f;
  288.  
  289.     JoyX = JoyX * JoyX * cX;
  290.     JoyY = JoyY * JoyY * cY;
  291.  
  292.     JoyX *= 1.4f;
  293.     JoyY *= 1.2f;
  294.  
  295.  
  296.     if ( JoyState.rgbButtons[0] & 0x80 )
  297.         JoyButton1Down = true;
  298.     else
  299.         JoyButton1Down = false;
  300.  
  301.     if ( JoyState.rgbButtons[1] & 0x80 )
  302.         JoyButton2Down = true;
  303.     else
  304.         JoyButton2Down = false;    
  305.   
  306. }
  307.  
  308. //------------------------------------------------------------------
  309. // Name: RefreshMouse()
  310. // Desc: Ziska info o mysi
  311. //------------------------------------------------------------------
  312. void INPUT::RefreshMouse()
  313. {
  314. #define MOUSEBUTTONDOWN(key) (key & 0x80)
  315. #define MOUSEBUTTON_LEFT 0
  316. #define MOUSEBUTTON_RIGHT 1
  317. #define MOUSEBUTTON_MIDDLE 2
  318.  
  319.     if (m_pMouse == NULL)
  320.         return;
  321.  
  322.     DIMOUSESTATE MouseState;
  323.  
  324.     if( FAILED(m_pMouse->GetDeviceState(sizeof(MouseState),
  325.                                         (LPVOID)&MouseState)))
  326.     {
  327.         ZeroMemory( &MouseState, sizeof(DIMOUSESTATE) );
  328.         HRESULT hr = m_pMouse->Acquire();
  329.         while( hr == DIERR_INPUTLOST ) 
  330.             hr = m_pMouse->Acquire();
  331.     }
  332.     //press-reset
  333.     MouseLeftPress = 0;
  334.     MouseRightPress = 0;
  335.  
  336.  
  337.     //Lave tlacitko
  338.     if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_LEFT]))
  339.     {
  340.         if (MouseLeftDown == 0)
  341.             MouseLeftPress = 1;
  342.  
  343.         MouseLeftDown = 1;
  344.     }
  345.     else
  346.     {
  347.         MouseLeftDown = 0;
  348.     }
  349.  
  350.  
  351.     //Prave tlacitko
  352.     if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_RIGHT]))
  353.     {
  354.         if (MouseRightDown == 0)
  355.             MouseRightPress = 1;
  356.  
  357.         MouseRightDown = 1;
  358.     }
  359.     else
  360.     {
  361.         MouseRightDown = 0;
  362.     }
  363.  
  364.  
  365.     //solid
  366.     POINT MousePos;
  367.     ScreenToClient(hWnd,&MousePos);
  368.     GetCursorPos(&MousePos);
  369.     Mouse.X = (float)  MousePos.x ;
  370.     Mouse.Y = (float) MousePos.y ;
  371.     Mouse.Z = 0.0f;
  372.  
  373.     //relativne
  374.     MouseRelative.X = (float)MouseState.lX;
  375.     MouseRelative.Y = (float)MouseState.lY;
  376.     MouseRelative.Z = (float)MouseState.lZ;
  377.     
  378.  
  379. }
  380.  
  381.