📄 input.c
字号:
/*==========================================================================
*
* Copyright (C) 1995-1996 Microsoft Corporation. All Rights Reserved.
*
* File: input.c
* Content: DirectInput functionality for Multi-player duel
*
*
***************************************************************************/
#include "input.h"
#define KEY_RETURN 0x00000001
#define KEY_ESC 0x00000002
#define KEY_UP 0x00000004
#define KEY_DOWN 0x00000008
#define KEY_LEFT 0x00000010
#define KEY_RIGHT 0x00000020
#define KEY_TAB 0x00000040
#define KEY_STOP 0x00000080
#define KEY_FIRE 0x00000100
//#include"global.h"
//#include "gameproc.h"
//extern HINSTANCE ghinst; // program instance
//extern HWND ghWndMain; // app window handle
//extern DWORD gdwKeys;
//extern DIMOUSESTATE mousedata;
static LPDIRECTINPUT lpdi; // DirectInput interface
static LPDIRECTINPUTDEVICE lpdiKeyboard; // keyboard device interface
static LPDIRECTINPUTDEVICE lpdiMouse;
static BOOL fKeybdAcquired; // has the keyboard been acquired?
static BOOL fMouseAcquired;
//static BOOL bRepeat=FALSE;
//static DWORD Time;
//static DWORD KeyPressTime;
//extern DWORD gdwKeys; // gameplay keys
//extern DIMOUSESTATE mousedata;
/*
*
* InitInput
*
* Initialize DirectInput objects & devices
*
*/
BOOL InitInput(HANDLE ghinst, HWND ghWndMain)
{
GUID guid = GUID_SysKeyboard;
GUID guidm=GUID_SysMouse;
HRESULT hRes;
// try to create di object
if(DirectInputCreate(ghinst, DIRECTINPUT_VERSION, &lpdi, NULL) != DI_OK)
{
MessageBox(NULL,"Error","1",MB_OK);//debug
return FALSE;
}
// try to create keyboard device
if(lpdi->lpVtbl->CreateDevice(lpdi, &guid, &lpdiKeyboard, NULL) !=DI_OK)
{
MessageBox(NULL,"Error","2",MB_OK);//debug
return FALSE;
}
//try to create mouse device
if(lpdi->lpVtbl->CreateDevice(lpdi, &guidm, &lpdiMouse, NULL) !=DI_OK)
{
MessageBox(NULL,"Error","Mouse",MB_OK);//debug
return FALSE;
}
// Tell DirectInput that we want to receive data in keyboard format
if (lpdiKeyboard->lpVtbl->SetDataFormat(lpdiKeyboard, &c_dfDIKeyboard) != DI_OK)
{
MessageBox(NULL,"Error","3",MB_OK);//debug
return FALSE;
}
// Tell DirectInput that we want to receive data in mouse format
if (lpdiMouse->lpVtbl->SetDataFormat(lpdiMouse, &c_dfDIMouse) != DI_OK)
{
MessageBox(NULL,"Error","mouse format",MB_OK);//debug
return FALSE;
}
if(lpdiKeyboard->lpVtbl->SetCooperativeLevel(lpdiMouse, ghWndMain,
DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK)
{
MessageBox(NULL,"Error","Mouse SC",MB_OK);//debug
return FALSE;
}
// set cooperative level
if(lpdiKeyboard->lpVtbl->SetCooperativeLevel(lpdiKeyboard, ghWndMain,
DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK)
{
MessageBox(NULL,"Error","4",MB_OK);//debug
return FALSE;
}
// try to acquire the keyboard
hRes = lpdiKeyboard->lpVtbl->Acquire(lpdiKeyboard);
if(SUCCEEDED(hRes))
{
// keyboard was acquired
fKeybdAcquired = TRUE;
}
else
{
// keyboard was NOT acquired
fKeybdAcquired = FALSE;
}
// try to acquire the keyboard
hRes = lpdiMouse->lpVtbl->Acquire(lpdiMouse);
if(SUCCEEDED(hRes))
{
// keyboard was acquired
fMouseAcquired = TRUE;
}
else
{
// keyboard was NOT acquired
fMouseAcquired = FALSE;
}
// if we get here, all objects were created successfully
return TRUE;
}
DIMOUSESTATE DI_ReadMouse(void)
{ DIMOUSESTATE mousedata;
HRESULT hRes;
hRes = lpdiMouse->lpVtbl->GetDeviceState(lpdiMouse, sizeof(DIMOUSESTATE), &mousedata);
if(hRes != DI_OK)
{
if(hRes == DIERR_INPUTLOST)
{
// we lost control of the keyboard, reacquire
fMouseAcquired = FALSE;
if(SUCCEEDED(lpdiMouse->lpVtbl->Acquire(lpdiMouse)))
{
fMouseAcquired = TRUE;
}
}
// failed to read the keyboard, just return
ZeroMemory(&mousedata,sizeof(mousedata));
}
return(mousedata);
}
int ReadChar(void)
{ BYTE rgbKeybd[256];
HRESULT hRes;
int i;
hRes = lpdiKeyboard->lpVtbl->GetDeviceState(lpdiKeyboard, sizeof(rgbKeybd), rgbKeybd);
if(hRes != DI_OK)
{
if(hRes == DIERR_INPUTLOST)
{
// we lost control of the keyboard, reacquire
fKeybdAcquired = FALSE;
if(SUCCEEDED(lpdiKeyboard->lpVtbl->Acquire(lpdiKeyboard)))
{
fKeybdAcquired = TRUE;
}
}
// failed to read the keyboard, just return
//return(NULL);
}
if(rgbKeybd[DIK_A] & 0x80)return('A');
if(rgbKeybd[DIK_B] & 0x80)return('B');
if(rgbKeybd[DIK_C] & 0x80)return('C');
if(rgbKeybd[DIK_D] & 0x80)return('D');
if(rgbKeybd[DIK_E] & 0x80)return('E');
if(rgbKeybd[DIK_F] & 0x80)return('F');
if(rgbKeybd[DIK_G] & 0x80)return('G');
if(rgbKeybd[DIK_H] & 0x80)return('H');
if(rgbKeybd[DIK_I] & 0x80)return('I');
if(rgbKeybd[DIK_J] & 0x80)return('J');
if(rgbKeybd[DIK_K] & 0x80)return('K');
if(rgbKeybd[DIK_L] & 0x80)return('L');
if(rgbKeybd[DIK_M] & 0x80)return('M');
if(rgbKeybd[DIK_N] & 0x80)return('N');
if(rgbKeybd[DIK_O] & 0x80)return('O');
if(rgbKeybd[DIK_P] & 0x80)return('P');
if(rgbKeybd[DIK_Q] & 0x80)return('Q');
if(rgbKeybd[DIK_R] & 0x80)return('R');
if(rgbKeybd[DIK_S] & 0x80)return('S');
if(rgbKeybd[DIK_T] & 0x80)return('T');
if(rgbKeybd[DIK_U] & 0x80)return('U');
if(rgbKeybd[DIK_V] & 0x80)return('V');
if(rgbKeybd[DIK_W] & 0x80)return('W');
if(rgbKeybd[DIK_X] & 0x80)return('X');
if(rgbKeybd[DIK_Y] & 0x80)return('Y');
if(rgbKeybd[DIK_Z] & 0x80)return('Z');
if(rgbKeybd[DIK_RETURN] & 0x80)return(0xff);
return(0);
}
/*
*
* DI_ReadKeys
*
* Use DirectInput to read game-play keys
*
*/
DWORD DI_ReadKeys(DWORD gdwKeys)
{ //DWORD LastKey;
BYTE rgbKeybd[256];
HRESULT hRes;
hRes = lpdiKeyboard->lpVtbl->GetDeviceState(lpdiKeyboard, sizeof(rgbKeybd), rgbKeybd);
if(hRes != DI_OK)
{
if(hRes == DIERR_INPUTLOST)
{
// we lost control of the keyboard, reacquire
fKeybdAcquired = FALSE;
if(SUCCEEDED(lpdiKeyboard->lpVtbl->Acquire(lpdiKeyboard)))
{
fKeybdAcquired = TRUE;
}
}
// failed to read the keyboard, just return
//return(NULL);
}
// reset key states
//LastKey=gdwKeys;
gdwKeys = gdwKeys ^ gdwKeys;
// check & update key states
if(rgbKeybd[DIK_RETURN] & 0x80)
gdwKeys |= KEY_RETURN;
if((rgbKeybd[DIK_UP] & 0x80) )
gdwKeys |= KEY_UP;
if((rgbKeybd[DIK_DOWN] & 0x80) )
gdwKeys |= KEY_DOWN;
/* if(KeyPressTime==0)Time=timeGetTime();
if(gdwKeys==LastKey)
{Time=timeGetTime()-Time;
KeyPressTime+=Time;
Time=timeGetTime();
if(KeyPressTime>200);
{bRepeat=TRUE;
KeyPressTime-=100;}
if(bRepeat==FALSE)gdwKeys &= ~KEY_DOWN;
}
}
else {
KeyPressTime=0;
bRepeat=FALSE;
}*/
if((rgbKeybd[DIK_LEFT] & 0x80))
gdwKeys |= KEY_LEFT;
if((rgbKeybd[DIK_RIGHT] & 0x80))
gdwKeys |= KEY_RIGHT;
if(rgbKeybd[DIK_SPACE] & 0x80)
gdwKeys |= KEY_FIRE;
if(rgbKeybd[DIK_ESCAPE] & 0x80)
gdwKeys |= KEY_ESC;
gdwKeys=gdwKeys;
if(rgbKeybd[DIK_NUMPAD5] & 0x80)
gdwKeys |= KEY_STOP;
//if((gdwKeys==LastKey)&&(!(gdwKeys & 0x80000000))
// return(0);
return gdwKeys;
}
/*
*
* CleanupInput
*
* Cleans up DirectInput objects
*
*/
void CleanupInput(void)
{
if(fKeybdAcquired)
{
lpdiKeyboard->lpVtbl->Unacquire(lpdiKeyboard);
fKeybdAcquired = FALSE;
}
if(lpdiKeyboard != NULL)
lpdiKeyboard->lpVtbl->Release(lpdiKeyboard);
if(fMouseAcquired)
{
lpdiMouse->lpVtbl->Unacquire(lpdiMouse);
fMouseAcquired = FALSE;
}
if(lpdiMouse != NULL)
lpdiMouse->lpVtbl->Release(lpdiMouse);
if(lpdi!= NULL)
lpdi->lpVtbl->Release(lpdi);
}
/*
*
* ReacquireInputDevices
*
* Reacquires DirectInput devices as needed
*
*/
void UnAcquireKeyboard(void)
{if(lpdiKeyboard != NULL)
{
lpdiKeyboard->lpVtbl->Unacquire(lpdiKeyboard);
}
}
BOOL ReacquireInputDevices(void)
{
// try to acquire the keyboard
if(lpdiKeyboard != NULL)
{
lpdiKeyboard->lpVtbl->Acquire(lpdiKeyboard);
}
else
{
// keyboard device has not been created.
fKeybdAcquired = FALSE;
return FALSE;
}
// if we get here, we are acquired again
fKeybdAcquired = TRUE;
// try to acquire the mouse
if(lpdiMouse != NULL)
{
lpdiMouse->lpVtbl->Acquire(lpdiMouse);
}
else
{
// Mouse device has not been created.
fMouseAcquired = FALSE;
return FALSE;
}
// if we get here, we are acquired again
fMouseAcquired = TRUE;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -