Click here to Skip to main content
15,878,852 members
Articles / Desktop Programming / Win32

Half Life Game Level Viewer

Rate me:
Please Sign up or sign in to vote.
4.61/5 (23 votes)
7 Feb 2009CPOL26 min read 79.5K   2.3K   60  
DirectX based application to open and view Half Life 1 game files
//
//	CInput.cpp
//
//	Copyright 2005 Paul Higinbotham
//


#include "CInput.h"
#include <cassert>


#ifndef SAFE_RELEASE
	#define SAFE_RELEASE(p)       { if(p) { (p)->Release(); (p)=0; } }
#endif


CInput::CInput() : m_pDXInput(0), m_pDXKeyboard(0), m_pDXMouse(0)
{
	memset(m_keyBoardBuffer, 0, sizeof(m_keyBoardBuffer));
}

CInput::~CInput()
{
	_cleanUp();
}

bool CInput::Init(HWND hWnd, HINSTANCE hInst)
{
	HRESULT hr;

	_cleanUp();

	// Create DirectInput object
	hr = DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_pDXInput, NULL); 
	if (!SUCCEEDED(hr))
	{
		_cleanUp();
		return false;
	}

	// Create Keyboard device object
	hr = m_pDXInput->CreateDevice(GUID_SysKeyboard, &m_pDXKeyboard, NULL);
	if (SUCCEEDED(hr))
	{
		hr = m_pDXKeyboard->SetDataFormat(&c_dfDIKeyboard);
		if (!SUCCEEDED(hr))
		{
			_cleanUp();
			return false;
		}

		hr = m_pDXKeyboard->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
		if (!SUCCEEDED(hr))
		{
			_cleanUp();
			return false;
		}
	}
	else
	{
		_cleanUp();
		return false;
	}

	// Create Mouse device object
	hr = m_pDXInput->CreateDevice(GUID_SysMouse, &m_pDXMouse, NULL);
	if (SUCCEEDED(hr))
	{
		hr = m_pDXMouse->SetDataFormat(&c_dfDIMouse);
		if (!SUCCEEDED(hr))
		{
			_cleanUp();
			return false;
		}

		hr = m_pDXMouse->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
		if (!SUCCEEDED(hr))
		{
			_cleanUp();
			return false;
		}
	}
	else
	{
		_cleanUp();
		return false;
	}

	return true;
}

void CInput::AcquireKeyboard()
{
	if (m_pDXKeyboard)
	{
		HRESULT hr = m_pDXKeyboard->Acquire();
		assert(SUCCEEDED(hr));
	}
}

void CInput::UnacquireKeyboard()
{
	if (m_pDXKeyboard)
	{
		HRESULT hr = m_pDXKeyboard->Unacquire();
		assert(SUCCEEDED(hr));
	}
}

void CInput::AcquireMouse()
{
	if (m_pDXMouse)
	{
		HRESULT hr = m_pDXMouse->Acquire();
		assert(SUCCEEDED(hr));
	}
}

void CInput::UnacquireMouse()
{
	if (m_pDXMouse)
	{
		HRESULT hr = m_pDXMouse->Unacquire();
		assert(SUCCEEDED(hr));
	}
}

void CInput::Update()
{
	// Update keyboard state
	assert(m_pDXKeyboard);
	memset(m_keyBoardBuffer, 0, sizeof(m_keyBoardBuffer));
	
	HRESULT hr = m_pDXKeyboard->GetDeviceState(sizeof(m_keyBoardBuffer), (LPVOID)&m_keyBoardBuffer);
	assert(SUCCEEDED(hr));
}

bool CInput::IsKeydown(unsigned char cDIKey)
{
	return ((m_keyBoardBuffer[cDIKey] & 0x80) != 0);
}

void CInput::GetMouseInfo(long & lXPos, long & lYPos, bool & bLeftButton, bool & bRightButton)
{
	assert(m_pDXMouse);

	DIMOUSESTATE	mouseState;
	HRESULT			hr;
	hr = m_pDXMouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouseState);
	if (!SUCCEEDED(hr))
	{
		lXPos = 0;
		lYPos = 0;
		bLeftButton = false;
		bRightButton = false;
		assert(0);
		return;
	}

	lXPos = mouseState.lX;
	lYPos = mouseState.lY;
	bLeftButton = (mouseState.rgbButtons[0] & 0x80) != 0;
	bRightButton = (mouseState.rgbButtons[1] & 0x80) != 0;
}


//
//	Private class methods
//

void CInput::_cleanUp()
{
	if (m_pDXKeyboard)
		m_pDXKeyboard->Unacquire();
	if (m_pDXMouse)
		m_pDXMouse->Unacquire();

	SAFE_RELEASE(m_pDXKeyboard);
	SAFE_RELEASE(m_pDXMouse);
	SAFE_RELEASE(m_pDXInput);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I am a senior software developer currently doing contract work for Microsoft. My educational background is in electrical engineering and I hold a masters degree from the University of Washington. I have experience in hardware and systems design but have done primarily software development for the last two decades. I have worked for various small companies as well as start-up companies, and have worked as a full time employee SDE at Microsoft Corporation.

Comments and Discussions