⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameinput.cpp

📁 一个类似坦克大战的小小游戏
💻 CPP
字号:

#include "GameInput.h"
#include "GlobalDefs.h"

CGameInput::CGameInput(void)
{
	m_pDirectInput = NULL;
	m_pKeyboard = NULL;
	m_pMouse = NULL;
	memset((void *)m_keyboardState, 0, sizeof(m_keyboardState));
}

CGameInput::~CGameInput(void)
{
	Release();
}

// Create directinput devices /////////////////////////////////////////////

BOOL CGameInput::Create(HWND hWnd, HINSTANCE hInstance)
{
	// Create IDirectInput8 interface first.
	if (FAILED(DirectInput8Create(hInstance, DIRECTINPUT_VERSION, 
					IID_IDirectInput8, (void **)&m_pDirectInput, NULL)))
	{
		Release();
		MessageBox(NULL, "Create DirectInput failed!", "ERROR!", MB_OK);
		return FALSE;
	}

	// Create the keyboard device
	if (FAILED(m_pDirectInput->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL)))
	{
		Release();
		MessageBox(NULL, "Create Keyboard failed!", "ERROR!", MB_OK);
		return FALSE;
	}

	// Set Cooperative Level
	if (FAILED(m_pKeyboard->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
	{
		Release();
		return FALSE;
	}

	// Set data format of the keyboard
	if (FAILED(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard)))
	{
		Release();
		return FALSE;
	}

	// Acquire the keyboard
	if (FAILED(m_pKeyboard->Acquire()))
	{
		Release();
		return FALSE;
	}

	// Create mouse 
	if (FAILED(m_pDirectInput->CreateDevice(GUID_SysMouse, &m_pMouse, NULL)))
	{
		Release();
		MessageBox(NULL, "Create Mouse failed!", "ERROR!", MB_OK);
		return FALSE;
	}

	// Set Cooperative Level
	if (FAILED(m_pMouse->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
	{
		Release();
		return FALSE;
	}

	// Set data format of the mouse
	if (FAILED(m_pMouse->SetDataFormat(&c_dfDIMouse)))
	{
		Release();
		return FALSE;
	}

	// Acquire the Mouse
	if (FAILED(m_pMouse->Acquire()))
	{
		Release();
		return FALSE;
	}

	return TRUE;
}

// Release ////////////////////////////////////////////////////////////////

void CGameInput::Release()
{
	if (NULL != m_pMouse)
	{
		m_pMouse->Unacquire();
		SAFE_RELEASE(m_pMouse);
	}

	if (NULL != m_pKeyboard)
	{
		m_pKeyboard->Unacquire();
		SAFE_RELEASE(m_pKeyboard);
	}

	if (NULL != m_pDirectInput)
	{
		SAFE_RELEASE(m_pDirectInput);
	}
}

// Get state //////////////////////////////////////////////////////////////

HRESULT CGameInput::GetKeyboardState()
{
	if (NULL != m_pKeyboard)
	{
		if (FAILED(m_pKeyboard->GetDeviceState(sizeof(m_keyboardState), (LPVOID)m_keyboardState)))
		{
			return E_FAIL;
		}
	}
	return S_OK;
}

HRESULT CGameInput::GetMouseState()
{
	if (NULL != m_pMouse)
	{
		if (FAILED(m_pMouse->GetDeviceState(sizeof(m_mouseState), (LPVOID)&m_mouseState)))
		{
			return E_FAIL;
		}
	}
	return S_OK;
}

// Is key | mouse down ? //////////////////////////////////////////////////

BOOL CGameInput::IsKeyDown(int inKey)
{
	return (m_keyboardState[inKey] & 0x80);
}

BOOL CGameInput::IsMouseDown(int inKey)
{
	return (m_mouseState.rgbButtons[inKey] & 0x80);
}

// Get Cursor position ////////////////////////////////////////////////////

void CGameInput::GetCursorPos(long &outX, long &outY, long &outZ)
{
	outX = m_mouseState.lX;
	outY = m_mouseState.lY;
	outZ = m_mouseState.lZ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -