home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / directx / duel / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-15  |  4.3 KB  |  203 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1997 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File:       input.c
  6.  *  Content:    DirectInput functionality for Multi-player duel
  7.  *
  8.  *
  9.  ***************************************************************************/
  10.  
  11.  
  12. #include "input.h"
  13. #include "gameproc.h"
  14.  
  15. extern HINSTANCE                ghinst;                // program instance
  16. extern HWND                     ghWndMain;              // app window handle
  17.  
  18. static LPDIRECTINPUT            lpdi;                // DirectInput interface
  19. static LPDIRECTINPUTDEVICE      lpdiKeyboard;           // keyboard device interface
  20. static BOOL                        fKeybdAcquired;        // has the keyboard been acquired?
  21.  
  22. extern DWORD                    gdwKeys;            // gameplay keys
  23.  
  24. /*
  25. *
  26. * InitInput
  27. *
  28. * Initialize DirectInput objects & devices
  29. *
  30. */
  31. BOOL InitInput(void)
  32. {
  33.     GUID        guid = GUID_SysKeyboard;
  34.    HRESULT  hRes;
  35.  
  36.     // try to create di object (DIRECTINPUT_VERSION == DX5)
  37.     if(DirectInputCreate(ghinst, DIRECTINPUT_VERSION, &lpdi, NULL) != DI_OK)
  38.     {
  39.       // creation failed, try DX3 compatibility
  40.       if(DirectInputCreate(ghinst, 0x0300, &lpdi, NULL) != DI_OK)
  41.       {
  42.          ShowError(IDS_DINPUT_ERROR_DIC);
  43.          return FALSE;
  44.       }
  45.     }
  46.  
  47.  
  48.     // try to create keyboard device
  49.         if(lpdi->lpVtbl->CreateDevice(lpdi, &guid, &lpdiKeyboard, NULL) !=DI_OK)
  50.     {
  51.         ShowError(IDS_DINPUT_ERROR_CD);
  52.         return FALSE;
  53.     }
  54.  
  55.         // Tell DirectInput that we want to receive data in keyboard format
  56.         if (lpdiKeyboard->lpVtbl->SetDataFormat(lpdiKeyboard, &c_dfDIKeyboard) != DI_OK)
  57.         {
  58.                 ShowError(IDS_DINPUT_ERROR_DF);
  59.                 return FALSE;
  60.         }
  61.  
  62.         // set cooperative level
  63.         if(lpdiKeyboard->lpVtbl->SetCooperativeLevel(lpdiKeyboard, ghWndMain,
  64.                          DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK)
  65.     {
  66.         ShowError(IDS_DINPUT_ERROR_SP);
  67.         return FALSE;
  68.         }
  69.  
  70.     // try to acquire the keyboard
  71.    hRes = lpdiKeyboard->lpVtbl->Acquire(lpdiKeyboard);
  72.    if(SUCCEEDED(hRes))
  73.    {
  74.       // keyboard was acquired
  75.       fKeybdAcquired = TRUE;
  76.     }
  77.    else
  78.    {
  79.       // keyboard was NOT acquired
  80.       fKeybdAcquired = FALSE;
  81.    }
  82.  
  83.     // if we get here, all objects were created successfully
  84.     return TRUE;    
  85.         
  86. }
  87.  
  88.  
  89. /*
  90. *
  91. * DI_ReadKeys
  92. *
  93. * Use DirectInput to read game-play keys
  94. *
  95. */
  96. void DI_ReadKeys(void)
  97. {
  98.     BYTE rgbKeybd[256];
  99.    HRESULT hRes;
  100.  
  101.    hRes = lpdiKeyboard->lpVtbl->GetDeviceState(lpdiKeyboard, sizeof(rgbKeybd), rgbKeybd);
  102.    if(hRes != DI_OK)
  103.     {
  104.       if(hRes == DIERR_INPUTLOST)
  105.       {
  106.          // we lost control of the keyboard, reacquire
  107.          fKeybdAcquired = FALSE;
  108.          if(SUCCEEDED(lpdiKeyboard->lpVtbl->Acquire(lpdiKeyboard)))
  109.          {
  110.             fKeybdAcquired = TRUE;
  111.          }
  112.       }
  113.  
  114.       // failed to read the keyboard, just return
  115.         return;
  116.     }
  117.  
  118.     // reset key states
  119.     gdwKeys = gdwKeys ^ gdwKeys;
  120.  
  121.     // check & update key states
  122.     if(rgbKeybd[DIK_NUMPAD5] & 0x80)
  123.         gdwKeys |= KEY_STOP;
  124.  
  125.     if((rgbKeybd[DIK_NUMPAD2] & 0x80) || (rgbKeybd[DIK_DOWN] & 0x80))
  126.         gdwKeys |= KEY_DOWN;
  127.  
  128.     if((rgbKeybd[DIK_NUMPAD4] & 0x80) || (rgbKeybd[DIK_LEFT] & 0x80))
  129.         gdwKeys |= KEY_LEFT;
  130.  
  131.     if((rgbKeybd[DIK_NUMPAD6] & 0x80) || (rgbKeybd[DIK_RIGHT] & 0x80))
  132.         gdwKeys |= KEY_RIGHT;
  133.  
  134.     if((rgbKeybd[DIK_NUMPAD8] & 0x80) || (rgbKeybd[DIK_UP] & 0x80))
  135.         gdwKeys |= KEY_UP;
  136.  
  137.     if(rgbKeybd[DIK_SPACE] & 0x80)
  138.         gdwKeys |= KEY_FIRE;
  139.  
  140. }
  141.  
  142. /*
  143. *
  144. * CleanupInput
  145. *
  146. * Cleans up DirectInput objects
  147. *
  148. */
  149. void CleanupInput(void)
  150. {
  151.     if(fKeybdAcquired)
  152.     {
  153.         lpdiKeyboard->lpVtbl->Unacquire(lpdiKeyboard);
  154.         fKeybdAcquired = FALSE;
  155.     }
  156.  
  157.     if(lpdiKeyboard != NULL)
  158.         lpdiKeyboard->lpVtbl->Release(lpdiKeyboard);
  159.  
  160.     if(lpdi!= NULL)
  161.         lpdi->lpVtbl->Release(lpdi);
  162.     
  163. }
  164.  
  165.  
  166. /*
  167. *
  168. * ReacquireInputDevices
  169. *
  170. * Reacquires DirectInput devices as needed
  171. *
  172. */
  173. BOOL ReacquireInputDevices(void)
  174. {
  175.     // try to acquire the keyboard
  176.         if(lpdiKeyboard != NULL)
  177.         {
  178.                 lpdiKeyboard->lpVtbl->Acquire(lpdiKeyboard);
  179.          }
  180.         else
  181.         {
  182.                 // keyboard device has not been created.
  183.                 fKeybdAcquired = FALSE;
  184.                 return FALSE;
  185.         }
  186.  
  187.         // if we get here, we are acquired again
  188.         fKeybdAcquired = TRUE;
  189.         return TRUE;
  190. }
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.