Jump to content
  • Advertisement
Sign in to follow this  
Ulognep

DirectInput and mouse control of sprites

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

Hi, I'm making a top-down shooter in C#, based off the MSDN C# Game development class engine (http://www.microsoft.com/events/series/msdnvideodev.mspx). I'm still relatively new to programming but I've been able to make some modifications so far. One thing I'm having trouble with is making the game take input from the mouse rather than the keyboard. Whenever I sift through DirectInput documentation, I get lost. Can someone(s) give me a run-down on the best ways to implement mouse control in this game?

Share this post


Link to post
Share on other sites
Advertisement
Create an input device as you did for the keyboard but use the
GUID_SysMouse for using mouse.

Next set the data format to use for getting mouse input using
the SetDataFormat() method with the c_dfDIMouse parameter (You could also use the c_dfDIMouse2 parameter if you desire to use more than 4 buttons)

Than you set the cooperative level like you did with the keyboard

Now there are two approaches for getting mouse data
1. Getting immidiate data - As the name implies you get the current state of the mouse buttons and axes.

2. Getting buffered data - You get all mouse data information that occured till the call to the GetDeviceData method in a buffer you set beforehand.

For now I can tell you about the first approach since its the one I use (the second is a bit more complicated, and you can read about it in the sdk which explains its pretty good, check for the GetDeviceData() method for DirectInput8)

Whenever you want to get mouse data you call the GetDeviceState method.
You pass a buffer, and the size of the buffer for this method.
The buffer is a DIMOUSESTATE structure which contains the x,y,z (z = wheel)
changes of the mouse axes and the current state of the mouse buttons.

i.e. if you want to check if the left button was pressed you check
the rgbButtons field as follows :

if (mouseState.rgbButtons[0] & 0x80 == 0x80)
Left button is down... do something
else
Left button is up

etc... for the other buttons.

For more information check the SDK under Contents->DirectInput->Using DirectInput and you'll find all your answers(I hope :) ) there.



Share this post


Link to post
Share on other sites
Thanks! This is exactly the kind of info I need to get started. The tricky thing is, we were supplied with the game engine for the online class, and so keyboard handling was pretty much in there and we didn't need to touch it. Except these are things I want to learn :) go figure.

I understand most of what you're telling me, but I'm a little shaky on this part

Quote:
if (mouseState.rgbButtons[0] & 0x80 == 0x80)
Left button is down... do something
else
Left button is up


what exactly is "0x80"?

Share this post


Link to post
Share on other sites
0x80 is hexadecimal notation in C++ representing 10000000 binary. If a button is pressed down you check for it by verifying that the high-order bit is set to 1 for that particular button.

So, DesignerX did if (mouseState.rgbButtons[0] & 0x80 == 0x80) which is:
  mouseState.rgbButtons[0] is the first button on the mouse
  & 0x80 is bitwise AND of the previous statement and 0x80
  == 0x80 checks that the result is in fact 10000000 binary (and is a little redundant in this case).

I don't know if this is how it's done in C#, though.

[edit] for C#/MDX, look at the MouseState structure.

[edit] looks like hex literals are the same in C# as in C++, so you would probably use the CompareTo method of the first element of the array returned by MouseState.GetMouseButtons(). [edit] Then again, it looks like you can use the & operator the same way in C#. Obviously I'm not a C# programmer.

jfl.

[Edited by - jflanglois on June 7, 2005 5:54:46 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Ulognep
what exactly is "0x80"?


The 0x denotes that this is a hexadecimal figure where each following digit (0-F) represents 4 bits. 0x80 therefore represents the bit sequence 10000000.

If the left mouse button is down, the first bit of the rgbButtons[0] struct will be set to 1, else it will be zero. The code simply tests for whether this is the case using a bitwise logical and operation.

- Oscar [smile]

Share this post


Link to post
Share on other sites
All right... I'm working through this... slowly, but you guys seem to be on top of this stuff so I'll capitalize on your knowledge. I really appreciate this.

I'm reading through the source code and am having trouble finding the spot to call the SetDataFormat() method. Should I do this immediately after I initialize my mouse as a device?

This is a little tougher because I'm working with someone else's code. If anyone has C# express edition and wants to dig in with me, the url for the engine is http://www.digipen.edu/webcast/files/webcast-8.zip

Share this post


Link to post
Share on other sites
Wow, all the MDX documentation on MSDN kinda sucks. I would recommend that you use the C++ documentation at the same time as there seems to be a lot of correlation between the two systems. For instance, the C++ docs tell you to SetDataFormat() before you Acquire() the DirectInput device.

HTH


jfl.

Share this post


Link to post
Share on other sites
Quote:
Original post by jflanglois
Wow, all the MDX documentation on MSDN kinda sucks. I would recommend that you use the C++ documentation at the same time as there seems to be a lot of corrolation between the two systems. For instance, the C++ docs tell you to SetDataFormat() before you Acquire() the DirectInput device.


jfl.


The interesting thing is, I don't think I need to call SetDataFormat() in C# at all... It seems to be working okay, now I'm just having trouble reading the values of MouseState.X and MouseState.Y.

int vx = 0, vy = 0;
vx = MouseState.X; //this line is giving a build error
vy = MouseState.Y; //so is this one
Velocity = new Vector2(vx, vy);

The errors are telling me I need an object reference... This is like my 3rd month coding and I'm way over my head

Share this post


Link to post
Share on other sites
Can we get the exact error?

Whoops, I didn't read the code well enough. You need an instance of MouseState. MouseState is just the structure. So I assume you would get it from Device.GetDeviceState().

[edit] By the way, you most probably do need to call SetDataFormat() even if it is working without.


jfl.

[Edited by - jflanglois on June 7, 2005 5:32:22 PM]

Share this post


Link to post
Share on other sites
I see. I'm slowly understanding structures and methods and classes. I created a new instance of MouseState so now the code reads:

int vx = 0, vy = 0;
MouseState ms = Game.DIMouse.CurrentMouseState;
vx = ms.X;
vy = ms.Y;
Velocity = new Vector2(vx, vy);

And I have mouse control! Now I just need to map the buttons to my attacks, and disable keyboard functionality.

One bug I'm seeing now: The mouse cursor is still being drawn. Does anyone know how to get the cursor to disappear without changing cooperative level flags?

Share this post


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

  • 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!