
#include "Input.h"




class Input : public Singleton<Input>
{
public:
	Input()
	{
		HRESULT hresult = DirectInput8Create(g_hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_pDI, NULL); 
	}

	LPDIRECTINPUT8 DirectInput()
	{
		return m_pDI;
	}

private:
	LPDIRECTINPUT8  m_pDI;
};




KeyboardInput::KeyboardInput()
{
	Input::Instance()->DirectInput()->CreateDevice(GUID_SysKeyboard, &m_pDevice, NULL);
	HRESULT hr = m_pDevice->SetDataFormat(&c_dfDIKeyboard);

	m_pDevice->Acquire();

	memset(m_diKeys, 0, 256);
}


void KeyboardInput::Update()
{
	memcpy(m_LastKeys, m_diKeys, 256);
	m_pDevice->GetDeviceState(256, m_diKeys);

	//uzenetek szetkuldese
	for(int i=0; i<256; i++)
	{
		if(m_diKeys[i]!=m_LastKeys[i])
		{
			if(m_diKeys[i]==0)	//felengedtek egy billentyut
			{

			}
			else
			{
				
				int size=m_KeyPressedMessages.size();
				for(std::list<KeyPressedMessageCallback>::iterator it =m_KeyPressedMessages.begin(); size--; it++)
				{
					(*it)(i);
				}
			}
		}
	}
};


bool KeyboardInput::IsPressed(int key)
{
	return (m_diKeys[key] & 0x80)!=0;
};


MouseInput::MouseInput()
{
	Input::Instance()->DirectInput()->CreateDevice(GUID_SysMouse, &m_pDevice, NULL);

	HRESULT hr = m_pDevice->SetDataFormat(&c_dfDIMouse);

#ifndef _DEBUG
	hr = m_pDevice->SetCooperativeLevel(g_hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
#endif

	m_pDevice->Acquire();
}
