• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Sample Code C#/XNA : Mouse Control Tip

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
When you want to update your game logic in XNA you do it in the Update method (which is overridden). This is the ideal spot to check for mouse events, like left click, right click or whatever. XNA uses a class called "Mouse" which contains a "GetState" method that returns the state of the mouse into a "MouseState" type variable.

The problem is that the Update method is called so freakin' fast (this is a good thing because it is part of your render loop) that it is almost impossible to click the mouse fast enough to get it to see only one click.

There is a pretty easy workaround for simple cases.

Create two MouseState variables.
Code:
MouseState mouseStateCurrent, mouseStatePrevious;

In the Update method check for conditions in a logical "and" statement.
Code:
[color=blue]protected override void[/color] Update([color=teal]GameTime[/color] gameTime)
{
    mouseStateCurrent = [color=teal]Mouse[/color].GetState();

    if (mouseStateCurrent.LeftButton = [color=teal]ButtonState[/color].Pressed &&
        mouseStatePrevious.LeftButton = [color=teal]ButtonState[/color].Released)
    {
        // Do your mouse state logic here
    }

    mouseStatePrevious = mouseStateCurrent;

    [color=blue]base[/color].Update(gameTime);
}

Basically what this will do is only run the logic when it sees that the current mouse state is different than the previous state. This prevents it from running the logic in the event you are holding down the mouse button. Since the Update method runs so fast, it appears to the program that you are holding down the button even when you are just clicking, and this prevents flickering and odd things from happening.

Do note however, this will prevent your logic from running when you are dragging the mouse. If you want to be able to drag the mouse around, the logic will have to change.
You would need to also check for some other condition to have changed. For example, the mouse has moved far enough away from it's initial position that the logic needs to be run again.

If anyone would like to see an example of something like that, just let me know.

Have fun coding!
 

Oliver_FF

New Member
Joined
Oct 15, 2006
Messages
544 (0.08/day)
Processor Intel q9400 @ stock
Motherboard Lanparty P45-T2RS
Cooling Zalman CNPS-9500
Memory 8GB OCZ PC2-6400
Video Card(s) BFG Nvidia GTX285 OC
Storage 1TB, 500GB, 500GB
Display(s) 20" Samsung T200HD
Case Antec Mini P180
Audio Device(s) Sound Blaster X-Fi Elite Pro
Power Supply 700w Hiper
Software Ubuntu x64 virtualising Vista
Nice one! Myself and several friends are planning a large game in XNA after our university exams are out - should be fun XD
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
Have fun !

I'm wrting a little 2D, AI path-finding simulation and I wanted the user to be able to change the tiles to different types (open, clostly, blocked) by clicking on them with the different mouse buttons before the simulation starts.

Once I finish this I think I will write a 3D one.
 

Xietep

New Member
Joined
Apr 11, 2012
Messages
2 (0.00/day)
Ah, I came here, because I was curious if you could possibly click the mouse so fast that the click wouldn't be picked up by a mouseState. I guess that could never happen?
 
Last edited:

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
I suppose that anything is possible, but it shouldn't happen. When you click the mouse, the input is stored in a buffer (you can see this in the explorer when the computer is busy and there is a slight delay between a mouse click and the OS's response).

The update method (loop) for a game runs very fast and would check the buffer every time you request it to do so (query the mouse state), but you could potentially have so much happening in the update loop that you could experience mouse lag, especially if your game is running on a single thread. If that were to occur, you would then be looking at optimization of existing code or pushing certain functionality off to separate threads.

Quite the zombie thread. I forgot I even wrote this one 4 years ago. lol
 

Xietep

New Member
Joined
Apr 11, 2012
Messages
2 (0.00/day)
Haha, thanks for the quick reply. I was debating whether or not I should resurrect this one or not, but I figured if I found it, someone else looking for an answer might, too. And it's nice for all the answers being on one thread!

Was surprised to see you were the one to respond, too! I guess you've been around a while. I know who to go to for questions now ;)
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
Always happy to help. :toast:

Kinda funny that you kicked this one up as I haven't done anything in XNA for awhile and was just pondering a game idea today.

Oh and btw ... Welcome to TPU !!
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
I know my code that uses user32.dll (mouse_event) can be ignored by games if it happens too quickly (or just completely outright ignore it like Fallout New Vegas). At the same time, it might be merging multiple clicks into a single event.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
But that's an app/utility outside of the game using PInvoke, isn't it Ford?
I've had weird things happen in managed code when calling unmanaged functions.
 
Top