INPUT.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:4k
源码类别:

Windows编程

开发平台:

Visual C++

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