Skip to main content
GameDev.net gamedev.net
🔒 Locked

Direct-X 9 mouse problems

Started by PaperFace Aug 9, 2007 at 1:26 PM 1 replies 1.1k views
Original Post
PaperFace
PaperFace
I followed the directx 8 mouse tutorial on the site and it all compiles perfectly, only the mouse position always seems to be off. I've created a mouse_x and mouse_y variable at start-up, and then everytime it calls the GetDeviceState I..

	//Keep track of movement
	mouse_x += mousestate.lX;
	mouse_y += mousestate.lY;

only, it seems to be off. I'm running in windowed mode but even when I toggled it to full screen it does the same, it seems to randomly get the mouse position wrong by about 400 pixels each axis, it's consistantly offset until I restart it, the amount it's offset seems to change everytime I start it up. Is it something to do with where the cursor is when the application starts? or should I be letting directx do the input buffering? Thanks!
galapogos22
galapogos22
I inagine it would work only if the mouse was at 0,0 at the start as you are most likely recording the changes but not the start conditions. You need to get them during init.

If this doent help you try keeping the mouse inside the window during update to see if that helps?

	//Keep mouse pointer within window	if(x < viewport.left)		x = viewport.left;	if(y < viewport.top)		y = viewport.top;	if(x > viewport.right)	x = viewport.right;	if(y > viewport.bottom)	y = viewport.bottom;


You could try setting the mouse to a specified position on initialisation also.

Below is my init function for a mouse from an old project. I probably pilfered this from somewhere as it doesnt look like i made it. Hope it was something without any copywrite issues.

        try	{		//Get directinput object		LPDIRECTINPUT8 directInput = NULL;		DirectInput8Create(GetModuleHandle(NULL),	// Retrieves the application handle						   0x0800,					// v.8.0							   IID_IDirectInput8,		// Interface ID						   (VOID**)&directInput,	// Our DirectInput Object						   NULL);		//Acquire Default System mouse		directInput->CreateDevice(GUID_SysMouse, &MouseDevice, NULL);				MouseDevice->SetDataFormat(&c_dfDIMouse);		MouseDevice->SetCooperativeLevel(mainWindow, DISCL_EXCLUSIVE | DISCL_FOREGROUND);		MouseDevice->Acquire();	        		//Get viewport size and init mouse location		D3DVIEWPORT9 v;		Device->GetViewport(&v);		viewport.left = v.X;		viewport.top = v.Y;		viewport.right = v.X + v.Width;		viewport.bottom = v.Y + v.Height;		x = (int)(v.X + v.Width / 2.0f);		y = (int)(v.Y + v.Height / 2.0f);		//Release Direct Input Object		directInput->Release();	}	catch(...)	{		debug.Print("Error in MOUSE::InitMouse()");	}	



To update it like you said just use:-

        x += (int)(mouseState.lX * speed);	y += (int)(mouseState.lY * speed);


or something similar. Note speed is just what it says, i used it change mouse speed.

Hope this helps.
'I is what I is. Eggegegegegeg' - The wisdom of Popeye
devronious
devronious
I don't know if this will help in any way but I don't use that at all in my system. I just use (where form1 is your direct3d form):

[source lang = "c#"]Point loc = form1.PointToClient(Cursor.Position);


I find that using the window's system for mouse location and keyboard buttons is easiest for me.

Joystick and extra mouse buttons I use DirectX.

-Devin

Topic Locked

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

Sign in to reply to this topic.