Using the Mouse Device

All of the DirectInput devices use this same device class, so the differences between using the mouse and keyboard are quite simple. You will need to update the device creation method in InitializeInput() to use a mouse GUID rather than the keyboard:

 device = new Device(SystemGuid.Mouse); 

ACQUIRING THE MOUSE EXCLUSIVELY

Acquiring the mouse in exclusive mode on certain operating systems will hide the mouse cursor completely. While this behavior is sometimes desired, in many instances it is not. For example, if you are writing a menu system for your game that is in full screen mode, the user will expect to be able to use that menu via the mouse. Many times you will need to provide your own cursors in this case.

In order to get the application working, the only other thing you will really need to change is the piece of code where you actually get the data, since the keyboard data doesn't make much sense for the mouse. Update the UpdateInputState method as follows:

 private void UpdateInputState() {     // Check the mouse state     MouseState state = device.CurrentMouseState;     string mouseState = "Using CurrentMouseState: \r\n";     // Current location of the mouse     mouseState += string.Format("{0}x{1}x{2}\r\n", state.X,         state.Y, state.Z);     // Buttons     byte[] buttons = state.GetMouseButtons();     for(int i = 0; i < buttons.Length; i++)         mouseState += string.Format("Button {0} {1}\r\n",             i, buttons[i] != 0 ? "Pressed" : "Not Pressed");     textBox1.Text = mouseState; } 

Here, instead of using a "helper" function to get the current data, you just take a complete snapshot of the mouse data currently. You then output the current X, Y, and Z of the mouse (the Z axis normally refers to the mouse wheel), as well as any of the buttons.

Running the application now, you'll notice that the majority of the time, the "position" of the mouse is reported as 0x0x0, even while you move it around. It's obvious you are really moving the mouse, so why isn't the UI being updated? In actuality, it is; however, the mouse data is being reported in relative units, as opposed to absolute values. That means the values reported for the different axes of the mouse are relative to the last frame. If you want to have the mouse data reported in absolute values, you can update your device's properties as follows (do this immediately after device creation):

 device.Properties.AxisModeAbsolute = true; 

While the application is running, it's quite obvious to see it being updated as the mouse moves around; however, the data returned for the axes initially is some crazy number. It is up to the application to keep track of this data to ensure that the mouse moves correctly in the application. The majority of applications will use the relative axis mode that is the default.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net