📄 cinput.cpp
字号:
//-----------------------------------------------------------------------------
// File: cInput.cpp
// Desc: Init of all the DirectInput System.
//-----------------------------------------------------------------------------
#include "Main.h"
//-----------------------------------------------------------------------------
// Func: cInput()
// Desc: Constructor of our cInput Class.
//-----------------------------------------------------------------------------
cInput::cInput()
{
m_lpDI = NULL;
m_lpDIKB = NULL;
m_lpDIMouse = NULL;
MPos.fX = 0;
MPos.fY = 0;
InitDI();
InitDIKB();
InitDIMouse();
}
//-----------------------------------------------------------------------------
// Func: InitDI()
// Desc: Initializing the DI object here.
//-----------------------------------------------------------------------------
HRESULT cInput::InitDI()
{
if(FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_lpDI, NULL)))
{
MessageBox( hWnd,"DirectInput8Create() failed!","cInput::InitDI()",MB_OK );
return E_FAIL;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Func: InitKeyBoard()
// Desc: Initializing the KeyBoard Device.
//-----------------------------------------------------------------------------
HRESULT cInput::InitDIKB()
{
if(FAILED(m_lpDI->CreateDevice(GUID_SysKeyboard, &m_lpDIKB, NULL)))
{
MessageBox( hWnd,"CreateDevice() failed!","cInput::InitDIKB()",MB_OK );
return E_FAIL;
}
if(FAILED(m_lpDIKB->SetDataFormat(&c_dfDIKeyboard)))
{
MessageBox( hWnd,"SetDataFormat() failed!","cInput::InitDIKB()",MB_OK );
return E_FAIL;
}
if(FAILED(m_lpDIKB->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
{
MessageBox( hWnd,"SetCooperativeLevel() failed!","cInput::InitDIKB()",MB_OK );
return E_FAIL;
}
if(FAILED(m_lpDIKB->Acquire()))
{
MessageBox(hWnd, "Acquire() Failed!", "cInput::InitDIKB()", MB_OK);
return E_FAIL;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Func: InitDIMouse()
// Desc: Initializing the Mouse Device.
//-----------------------------------------------------------------------------
HRESULT cInput::InitDIMouse()
{
if(FAILED(m_lpDI->CreateDevice(GUID_SysMouse, &m_lpDIMouse, NULL)))
{
MessageBox( hWnd,"CreateDevice() failed!","cInput::InitDIMouse()",MB_OK );
return E_FAIL;
}
if(FAILED(m_lpDIMouse->SetDataFormat(&c_dfDIMouse)))
{
MessageBox( hWnd, "SetDataFormat() failed!", "cInput::InitDIMouse()", MB_OK);
return E_FAIL;
}
if(FAILED(m_lpDIMouse->SetCooperativeLevel( hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
{
MessageBox( hWnd, "SetCooperativeLevel() failed!", "cInput::InitDIMouse()", MB_OK);
return E_FAIL;
}
if(FAILED(m_lpDIMouse->Acquire()))
{
MessageBox(hWnd, "Acquire() Failed!", "cInput::InitDIMouse()", MB_OK);
return E_FAIL;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Func: GetDeviceState()
// Desc: Updates the devices for any changes.
//-----------------------------------------------------------------------------
HRESULT cInput::GetDeviceState()
{
if(FAILED(m_lpDIKB->GetDeviceState(sizeof(chKB_Buffer),(LPVOID)&chKB_Buffer)))
{
if(FAILED(m_lpDIKB->Acquire()))
{
MessageBox(hWnd, "Acquire() Failed!", "cInput::GetDeviceState()", MB_OK);
return E_FAIL;
}
}
if(FAILED(m_lpDIMouse->GetDeviceState(sizeof(M_Buffer),(LPVOID)&M_Buffer)))
{
if(FAILED(m_lpDIMouse->Acquire()))
{
MessageBox(hWnd, "Acquire() Failed!", "cInput::GetDeviceState()", MB_OK);
return E_FAIL;
}
}
MPos.fX += M_Buffer.lX;
MPos.fY += M_Buffer.lY;
return S_OK;
}
//-----------------------------------------------------------------------------
// Func: Render()
// Desc: Render function. calling for move() and rotate() before rendering.
//-----------------------------------------------------------------------------
HRESULT cInput::Render()
{
char FontBuffer[255];
sprintf(FontBuffer, "MPos(%d, %d), M_Buffer(%f, %f, %f)", MPos.fX, MPos.fY, (FLOAT)M_Buffer.lX, (FLOAT)M_Buffer.lY, (FLOAT)M_Buffer.lZ );
if(FAILED(g_pApp->RenderFont( FontBuffer, 0 )))
{
MessageBox(hWnd, "g_pApp->RenderFont( FontBuffer, 0 ) Failed!", "cInput::Render()", MB_OK);
return E_FAIL;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Func: BKeyDown(int Key)
// Desc: Checks if a spesific Board Key pressed.
//-----------------------------------------------------------------------------
HRESULT cInput::BKeyDown(int Key)
{
if(chKB_Buffer[Key] & 0x80)
{
return S_OK;
}
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Func: MKeyDown(int Key)
// Desc: Checks if a spesific Mouse Key pressed.
//-----------------------------------------------------------------------------
HRESULT cInput::MKeyDown(int Key)
{
if(M_Buffer.rgbButtons[Key] & 0x80)
{
return S_OK;
}
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Func: ~cInput()
// Desc: Deconstructor of our cInput Class.
//-----------------------------------------------------------------------------
cInput::~cInput()
{
SafeRelease(m_lpDI);
if( m_lpDIKB != NULL)
{
m_lpDIKB->Unacquire();
m_lpDIKB->Release();
m_lpDIKB = NULL;
}
if( m_lpDIMouse != NULL)
{
m_lpDIMouse->Unacquire();
m_lpDIMouse->Release();
m_lpDIMouse = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -