www.pudn.com > notWow.rar > Input.cpp
#include "Input.h"
////////////////////////////////////
//Input
BOOL Input::Init(HWND hWnd, HINSTANCE hInst)
{
// Free a prior Init
Free();
// Record parent Window handle
m_hWnd = hWnd;
// Create a DirectInput interface
if(FAILED(DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_pDI, NULL)))
return FALSE;
// Return a success
return TRUE;
}
BOOL Input::Free()
{
if(m_pDI != NULL)
{
m_pDI->Release();
m_pDI = NULL;
}
// Clear parent Window handle
m_hWnd = NULL;
// Return a success
return TRUE;
}
/////////////////////////////////////////////////////////////////
// Keyboard Class
BOOL Keyboard::Init(Input *Input)
{
if((m_pInput = Input) == NULL)
return FALSE;
if(m_pInput->GetDirectInput() == NULL)
return FALSE;
if( FAILED(m_pInput->GetDirectInput()->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL)))
return FALSE;
if(FAILED(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard)))
return FALSE;
if(FAILED(m_pKeyboard->SetCooperativeLevel(m_pInput->GethWnd(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND)))
return FALSE;
m_pKeyboard->Acquire();
ZeroMemory(m_Lock,sizeof(m_Lock));
return TRUE;
}
BOOL Keyboard::Free()
{
//if(m_pInput == NULL)
// return FALSE;
//if(m_pInput->GetDirectInput() == NULL)
// return FALSE;
if(m_pKeyboard == NULL)
return FALSE;
// Always unacquire the device before calling Release().
m_pKeyboard->Unacquire();
m_pKeyboard->Release();
m_pKeyboard = NULL;
m_pInput = NULL;
return TRUE;
}
BOOL Keyboard::Read()
{
HRESULT hr;
short i;
if(m_pInput == NULL)
return FALSE;
if(m_pInput->GetDirectInput() == NULL)
return FALSE;
ZeroMemory(m_State,sizeof(m_State));
// Loop polling and reading until succeeded or unknown error
// Also take care of lost-focus problems
while(1)
{
// Read in state
if(SUCCEEDED(hr = m_pKeyboard->GetDeviceState(sizeof(m_State), (LPVOID)&m_State)))
break;
// Return on an unknown error
if(hr != DIERR_INPUTLOST && hr != DIERR_NOTACQUIRED)
return FALSE;
// Reacquire and try again
if(FAILED(m_pKeyboard->Acquire()))
return FALSE;
}
// Released keys and button need to be unlocked
for(i=0;i<256;i++)
{
if(!(m_State[i] & 0x80))
{
m_Lock[i] = FALSE;
}
}
return TRUE;
}
/////////////////////////////////////////////////////////////////
// Mouse Class
BOOL Mouse::Init(Input *Input,BOOL Windowed)
{
if((m_pInput = Input) == NULL)
return FALSE;
if(m_pInput->GetDirectInput() == NULL)
return FALSE;
if( FAILED(m_pInput->GetDirectInput()->CreateDevice(GUID_SysMouse, &m_pMouse, NULL)))
return FALSE;
if(FAILED(m_pMouse->SetDataFormat(&c_dfDIMouse)))
return FALSE;
if(FAILED(m_pMouse->SetCooperativeLevel(m_pInput->GethWnd(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND)))
return FALSE;
m_Windowed = Windowed;
ZeroMemory(m_Lock,sizeof(m_Lock));
m_pMouse->Acquire();
m_Pos.x = 0;
m_Pos.y = 0;
return TRUE;
}
BOOL Mouse::Free()
{
// if(m_pInput == NULL)
// return FALSE;
// if(m_pInput->GetDirectInput() == NULL)
// return FALSE;
if(m_pMouse == NULL)
return FALSE;
// Always unacquire the device before calling Release().
m_pMouse->Unacquire();
m_pMouse->Release();
m_pMouse = NULL;
m_pInput = NULL;
return TRUE;
}
BOOL Mouse::Read()
{
HRESULT hr;
short i;
if(m_pInput == NULL)
return FALSE;
if(m_pInput->GetDirectInput() == NULL)
return FALSE;
ZeroMemory(&m_State,sizeof(m_State));
// Loop polling and reading until succeeded or unknown error
// Also take care of lost-focus problems
while(1)
{
// Read in state
if(SUCCEEDED(hr = m_pMouse->GetDeviceState(sizeof(m_State), (LPVOID)&m_State)))
break;
// Return on an unknown error
if(hr != DIERR_INPUTLOST && hr != DIERR_NOTACQUIRED)
return FALSE;
// Reacquire and try again
if(FAILED(m_pMouse->Acquire()))
return FALSE;
}
// If windowed usage, ask windows for coordinates
if(m_Windowed == TRUE)
{
POINT pt;
GetCursorPos(&pt);
ScreenToClient(m_pInput->GethWnd(), &pt);
m_Pos.x = pt.x;
m_Pos.y = pt.y;
}
else
{
m_Pos.x += m_State.lX;
m_Pos.y += m_State.lY;
}
for(i = 0; i < 4; i++)
{
if(!(m_State.rgbButtons[i]))
m_Lock[i] = FALSE;
}
return TRUE;
}