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

Sample Code C#/XNA : Mouse Control Tip

Discussion in 'Programming & Webmastering' started by Kreij, Apr 12, 2008.

  1. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (3.97/day)
    Thanks Received:
    5,632
    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 says thanks.
  2. Oliver_FF

    Oliver_FF New Member

    Joined:
    Oct 15, 2006
    Messages:
    546 (0.15/day)
    Thanks Received:
    65
    Nice one! Myself and several friends are planning a large game in XNA after our university exams are out - should be fun XD
     
  3. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (3.97/day)
    Thanks Received:
    5,632
    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.
     
  4. Xietep New Member

    Joined:
    Apr 11, 2012
    Messages:
    2 (0.00/day)
    Thanks Received:
    0
    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: Apr 11, 2012
  5. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (3.97/day)
    Thanks Received:
    5,632
    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
     
  6. Xietep New Member

    Joined:
    Apr 11, 2012
    Messages:
    2 (0.00/day)
    Thanks Received:
    0
    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 ;)
     
  7. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (3.97/day)
    Thanks Received:
    5,632
    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 !!
     
  8. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    18,310 (6.36/day)
    Thanks Received:
    7,585
    Location:
    IA, USA
    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.
     
    Crunching for Team TPU
  9. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (3.97/day)
    Thanks Received:
    5,632
    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.
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)