Mouse clicks inside game loop

This is a discussion on Mouse clicks inside game loop within the Game Programming forums, part of the General Programming Boards category; I’m working on an OO windows chess game w/ VS 2005 using DirectX 9.0. I have everything finished (excluding AI ...

  1. #1
    Pupil
    Join Date
    Oct 2005
    Location
    Toledo
    Posts
    27

    Question Mouse clicks inside game loop

    I’m working on an OO windows chess game w/ VS 2005 using DirectX 9.0. I have everything finished (excluding AI and En Passant). Right now I’m trying to tackle Direct Input. I’m using the right mouse button to “take back” moves. The problem is when you right click, 4 or 5 moves are taken back, for each iteration on the game loop. Is there a test expression I can use for checking when the right mouse button is released instead of being pressed?

    Code:
    #define BUTTON_DOWN(obj, button) (obj.rgbButtons[button] & 0x80)
    DIMOUSESTATE mouse_state;
    
    int Mouse_Button(int button)
    {
        return BUTTON_DOWN(mouse_state, button);
    }
    	
    if (Mouse_Button(1)){Code for take back move}

    EDIT:
    I made a quick fix.
    Code:
    static bool release = false;
    if (Mouse_Button(1))
    {
        release = true;
    }
    
    else if (release)
    {
        chessboard.unmakeMove();
        release = false;
    }
    Last edited by chad101; 04-07-2007 at 12:23 AM.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You have to call GetDeviceState() per frame for this setup to work. Otherwise the mousestate structure will reflect the mouse state as it was on the last call to GetDeviceState().

    Code:
    HRESULT CMouse::Update(void)
    {
        
        //Get current state from driver
        return m_pDID8->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&_mousestate);
        
    }

  3. #3
    Pupil
    Join Date
    Oct 2005
    Location
    Toledo
    Posts
    27
    I am, I didn't include it with the code sample.

    DirectInput framework source file
    Code:
    void Poll_Mouse()
    {
        dimouse->GetDeviceState(sizeof(mouse_state), (LPVOID)&mouse_state);
    }
    Inside game loop
    Code:
        //update mouse and keyboard
        Poll_Mouse();
        Poll_Keyboard();
    The mouse works fine; I’m looking for a way to check for a mouse button "release", not press. The above fix (in my first post) seems to be working fine but out of curiosity I'm wondering if a button “release” test expression exists.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Well if it returns 1 when it is down it returns 0 when it is not. So when it is down, set a variable, and when it is 0 check to see if said variable is 1. If it is you know that the mouse button is being pressed and has now been released.

    Code:
    if (Mouse->ButtonDown(LMB))
    {
      m_bLMBDown=true;
    }
    
    if (Mouse->ButtonUp(LMB))
    {
      if (m_bLMBDown)
      {
         //Left mouse button was pressed at some point
         //and is now no longer pressed
      }
    }
    Last edited by VirtualAce; 04-07-2007 at 06:24 PM.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    To be fair here I gave you a circular answer. To test if the mouse button is up you will need to set a variable when it is down. Then if this variable is set, you need to check the data structure for the mouse button to see if it is still set. If not, and your variable is set, then you know the mouse button is being released. The rgbButtons[] array will tell you everything you need to know. Since this information is set every time you call GetDeviceState() you can simply check the value of the button if your down variable is set.

    Code:
    #define LMB 0
    
    if (rgbButtons[LMB])
    {
      m_bLMBDown=true;
    }
    
    if (m_bLMBDown)
    {
      //This will evaluate when GetDeviceState() populates rgbButtons[] and the left mouse button is released
      if (rgbButtons[LMB]==0) 
      {
         m_bLMBDown=false;
         //Button has been released
         m_bClick[LMB]=true;
      }
    }
    You could use a similar setup to create a ButtonUp() function for your mouse class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM
  4. Replies: 1
    Last Post: 06-06-2002, 04:17 PM
  5. game loop
    By condorx in forum Game Programming
    Replies: 3
    Last Post: 03-15-2002, 02:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21