Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Laroche

DirectInput - Mouse innacurate

This topic is 5559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is the definition of my CInput class, which is based on directinput8, and handles the keyboard and mouse:
  
// header

class CInput
{
public:
	CInput(){	lpdi = NULL; Keyboard = NULL; }
	~CInput(){	lpdi->Release();lpdi = NULL;
				Keyboard->Unacquire();
				Keyboard->Release();Keyboard = NULL;
				Mouse->Unacquire();
				Mouse->Release();Mouse = NULL; }

	//Methods

	int Initialize(HWND hWnd, int ScreenX, int ScreenY);
	int Read_Keyboard();
	int Read_Mouse();
	bool GetKeyState(int Num);
	int GetMouseState(int Num);
private:
	LPDIRECTINPUT8 lpdi;
	LPDIRECTINPUTDEVICE8 Keyboard;
	LPDIRECTINPUTDEVICE8  Mouse;
	char KeyState[256];

	DIMOUSESTATE MouseState;
	int MouseX, MouseY, ScreenX, ScreenY;
};

// source

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


CInput::Initialize(HWND hWnd, int Screenx, int Screeny)
{
	MouseX = 0;
	MouseY = 0;
	ScreenX = Screenx;
	ScreenY = Screeny;

	if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, 
    IID_IDirectInput8, (void**)&lpdi, NULL)))
  {
	  MessageBox(NULL, "DirectInputCreate Failed", "damn", MB_OK);
  }
  
  if (FAILED(lpdi->CreateDevice(GUID_SysKeyboard, &Keyboard, NULL)))
  {
	  MessageBox(NULL, "CreateDevice Failed", "damn", MB_OK);
  }
  
  if (FAILED(Keyboard->SetDataFormat(&c_dfDIKeyboard)))
  {
	  MessageBox(NULL, "SetDataFormat() Failed", "damn", MB_OK);
  }
   
  if (FAILED(Keyboard->SetCooperativeLevel(hWnd, DISCL_BACKGROUND |
    DISCL_NONEXCLUSIVE)))
    {
	  MessageBox(NULL,"SetCooperative Level Failed", "damn", MB_OK);
	}
  
  if (FAILED(Keyboard->Acquire()))
    {
	  MessageBox(NULL, "Acquire() Failed", "damn", MB_OK);
	}

  if (FAILED(lpdi->CreateDevice(GUID_SysMouse, &Mouse, NULL)))
    return false;
  if (FAILED(Mouse->SetCooperativeLevel(hWnd, DISCL_BACKGROUND |
                                          DISCL_NONEXCLUSIVE)))
    return false;
  if (FAILED(Mouse->SetDataFormat(&c_dfDIMouse)))
    return false;

  if (FAILED(Mouse->Acquire()))
    return false;

  return (TRUE);
}

int CInput::Read_Keyboard()
{
	Keyboard->GetDeviceState(sizeof(unsigned char[256]), KeyState);
	return (TRUE);
}

int CInput::Read_Mouse()
{
	Mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&MouseState);
	MouseX += MouseState.lX;
	if (MouseX >= ScreenX)
		MouseX = ScreenX;

	if (MouseX < 0)
		MouseX = 0;

	MouseY += MouseState.lY;
	if (MouseY >= ScreenY)
		MouseY = ScreenY;

	if (MouseY < 0)
		MouseY = 0;

	return (TRUE);
}

bool CInput::GetKeyState(int Num)
{
  return ((KeyState[Num] & 0x80) ? TRUE : FALSE);
}

int CInput::GetMouseState(int Num)
{
	switch (Num)
	{
	case 4:
		{
			return MouseX;
		}
	case 5:
		{
			return MouseY;
		}
	default:break;
	}
	
	return ((MouseState.rgbButtons[Num] & 0x80) ? TRUE : FALSE);
}
  
The keyboard works fine, and the mouse buttons work fine, but tracking the mouse coordinates is innacurate.. If i wiggle my mouse a bit then check, its usually fairly off, by even as much as 100 pixels.. I tried doing this:
	POINT MouseMovement;
	GetCursorPos(&MouseMovement);
 
and this is accurate and works fine. But why is my mouse movement tracking innaccurate? It seems to me to be fairly straightforward, but I really dont understand.. Any help is appreciated!

Share this post


Link to post
Share on other sites
Advertisement
What are you comparing the movement to? The system cursor? If you are then your code is not inaccurate. The system cursor employs a more complex algorithm to calculate location instead of just using relative movement.

I don''t see anything wrong with your code.



Qui fut tout, et qui ne fut rien
Invader''s Realm

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!