• We've upgraded our forums. Please post any issues/requests in this thread.

Sample Code C#/XNA : Mouse Control Tip

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#1
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.13/day)
Likes
65
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
#2
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
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#3
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)
Likes
0
#4
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
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#5
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)
Likes
0
#6
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
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#7
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
20,921 (6.24/day)
Likes
10,022
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#8
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
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#9
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.