Skip to main content
gamedev.net
🔒 Locked

DirectInput problem...

Started by OrcSlayer Apr 27, 2004 at 9:50 AM 2 replies 400+ views
Original Post
OrcSlayer
OrcSlayer
I''m trying to get what position the mouse is going (forward or backward) I''m doing this by taking the DIMOUSESTATE::lX but it dosent seems to work... can you tell me what is wrong with this code?:
class CInput
{
	private:
		char Keys[256];
		DIMOUSESTATE mouse_state;
		IDirectInput8 *di;
		IDirectInputDevice8 *di_keyboard;
		IDirectInputDevice8 *di_mouse;
		POINT pt;
	public:
		~CInput();
		bool Init(HWND hWnd,HINSTANCE hInst);
		void Update(void);
		bool KeyDown(UINT Key);
		long GetMouseX(void);
		long GetMouseY(void);
		bool MouseDown(UINT Button);
		long GetMouseDeltaY(void);
		long GetMouseDeltaX(void);
	
};
#include "global.h"

bool CInput::Init(HWND hWnd,HINSTANCE hInst)
{
	if(FAILED(DirectInput8Create(hInst,DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&di,NULL)))
		return FALSE;
	if (FAILED(di->CreateDevice(GUID_SysKeyboard, &di_keyboard, NULL)))
	    return FALSE;
	if (FAILED(di_keyboard->SetDataFormat(&c_dfDIKeyboard)))
	    return FALSE;
	if (FAILED(di_keyboard->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
		return FALSE;
	if (FAILED(di_keyboard->Acquire()))
	    return FALSE;
	if (FAILED(di->CreateDevice(GUID_SysMouse, &di_mouse, NULL)))
	    return FALSE;
	if (FAILED(di_mouse->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
		return FALSE;
	if (FAILED(di_mouse->SetDataFormat(&c_dfDIMouse)))
	    return FALSE;
	if (FAILED(di_mouse->Acquire()))
	    return FALSE;
	return TRUE;
}

void CInput::Update(void)
{
	GetCursorPos(&pt);
	if (FAILED(di_keyboard->GetDeviceState(sizeof(char[256]), (LPVOID)Keys)))
	    PostQuitMessage(0);
	if (FAILED(di_mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouse_state)))
	    PostQuitMessage(0);
}

long CInput::GetMouseX(void)
{
	this->Update();
	return pt.x;
}
long CInput::GetMouseY(void)
{
	this->Update();
	return pt.y;
}

bool CInput::KeyDown(UINT Key)
{
	this->Update();
	if (Keys[Key] & 0x80)
		return TRUE;
	return FALSE;
}

bool CInput::MouseDown(UINT Button)
{
	if(mouse_state.rgbButtons[Button] & 0x80)
		return TRUE;
	return FALSE;
}
long CInput::GetMouseDeltaY(void)
{
    return mouse_state.lY;
}
long CInput::GetMouseDeltaX(void)
{
    return mouse_state.lX;
}
CInput::~CInput()
{
	
	di_mouse->Unacquire();
	di_mouse->Release();
	di_mouse = NULL;
	di_keyboard->Unacquire();
	di_keyboard->Release();
	di_keyboard = NULL;
	di->Release();
	di = NULL;
}
Alvarny
Alvarny
The problem is that when you call update it gives you the diffence for x and y since you last called it. You are calling update when you get the x then calling it again for y. You need to call it once then get both x and y.
cpprulz
cpprulz
never mind, hehe
Alvarny's right.

[edited by - cpprulz on April 27, 2004 8:20:38 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.