• Advertisement
Sign in to follow this  

Mouse movement in managed dx c# help!

This topic is 4793 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

Okey I am struggeling with this problem, I recently got managed dx kickstart (btw isn't there supposed to be a cd with this book the company I bought it from said the cd was sold sepperatly, wtf?) Awell to my problem, I am making a small game now and I got some trouble making the mouse x, y values correct. When I start the game they seem to be slightly off and I don't know what is causing it.
namespace WindowsApplication4
{
	/// <summary>
	/// Summary description for Input.
	/// </summary>
	public class Input
	{
		int x = 0;
		int y = 0;
		Device device;
 
		private System.Threading.AutoResetEvent deviceUpdated;
		private System.Threading.ManualResetEvent appShutdown;

		MouseState state;
		byte[] buttons;


		public Input( System.Windows.Forms.Control This )
		{
			device =  new Device( SystemGuid.Mouse );
			device.SetCooperativeLevel( This, 
				CooperativeLevelFlags.Background | 
				CooperativeLevelFlags.NonExclusive );
			
			deviceUpdated = new System.Threading.AutoResetEvent(false);
			appShutdown	  = new System.Threading.ManualResetEvent(false);
	
			device.SetEventNotification( deviceUpdated );
			
			
			System.Threading.Thread threadLoop = new System.Threading.Thread(
				new System.Threading.ThreadStart( this.ThreadFunction ));
			threadLoop.Start();
			device.Acquire();
		}

		public void ThreadFunction()
		{
			System.Threading.WaitHandle[] handles =
				{ deviceUpdated, appShutdown };

			while( true )
			{
				int index = System.Threading.WaitHandle.WaitAny(handles);

				if( index == 0 )
				{
					UpdateInput();
				}
				else if( index == 1 )
				{
					return;
				}
			}
		}

		public void Shutdown()
		{
			if( appShutdown != null )
				appShutdown.Set();
		}

		public void UpdateInput()
		{
			
			state = device.CurrentMouseState;
			
			x += state.X;
			y += state.Y;
			
			buttons = state.GetMouseButtons();
			
			//for( int i = 0; i < buttons.Length; i++ )
			//	mouseState += string.Format( "Button {0} {1}\r\n",
			//		i, buttons != 0 ? "Pressed" : "Not Pressed" );
			
			
		}
//crude collition detection (test)
		public bool CalculateCollision( Kort s )
		{
			if( s.spr.XPos < x && s.spr.XPos+80 > x && buttons[0] != 0)
				return true;

			return false;

		}
			


		
	}
}


It also does not seem to be consistant, sometimes I have had a large difference sometimes not. My guess is that the mouse does not get it's initial values in the beggining of the program. But what did I forget? [edit] It's a windowed application, if that has anything to do with it. [Edited by - kappa on January 9, 2005 5:14:12 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by kappa
Okey I am struggeling with this problem, I recently got managed dx kickstart (btw isn't there supposed to be a cd with this book the company I bought it from said the cd was sold sepperatly, wtf?)

Yes, sometimes publishers make the books without the CDs. My copy has the CD, though. Odd.

Quote:
Awell to my problem, I am making a small game now and I got some trouble making the mouse x, y values correct. When I start the game they seem to be slightly off and I don't know what is causing it.

Can you be more specific? What do you mean by slightly off? Also, I notice you're using names for your method parameters that aren't case-differentiable from C# keywords ("This" and "this" in the Input constructor). THat's a huge no-no.

Share this post


Link to post
Share on other sites
What I mean is that the x value of the mouse is not where it should be, it's mostly to much to the right. Aka sometimes when I click outside of the window the collision detection returns true of a sprite inside the window. That means that the mouse input works but I get the wrong values.

also 'This' is a System.Windows.Forms.Control that I have set to 'this' in the original form.

Share this post


Link to post
Share on other sites
If its a windows app then I assume you should remember two things(although im not too familiar with c#:
1.) you must set the initial coordiantes where the mouse is.
2.) There could be problems when your mouse leaves the client area.
For instances you could move the mouse out on the left side an in on the right. Hence the mouse wont be updated outside the client area and that will make the coordinates wrong.

Remeber what your using DirectInput to do is just getting the relative movement of the mouse and adding that o a variable.
It would be better to use a function that always gives you the exact coords in the client area.
Why use DirectInput in a windowed programm if your not just using the movement of the mouse?
Isn there a simple function or he like in c#?

-CProgrammer

Share this post


Link to post
Share on other sites
yeah I thought it was like that, but how???

I can't set the mouse position because I am using my windows mouse, and I cannot get the current position because I can only get relativ values.

Share this post


Link to post
Share on other sites
Its probably best you dont use DirectInput in this particular case.
Sorry I dont know enough c# to tell you how to do it but Im sure theres a function for Forms that you can override. Itll probably give you a MouseEventArgs e which you only have to access the x and Y member variables to get he exact location.
Im sure somebody here can tell you how to do this, or try msdn or google.

-CProgrammer

Share this post


Link to post
Share on other sites
The example here may be of some help:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsmouseeventargsclasstopic.asp

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement