Archived

This topic is now archived and is closed to further replies.

shrt

(DirectInput8) Mouse clicks :/

Recommended Posts

It is very simple to check if a mousebutton is being pressed, like:
	bool CInput::MouseButtonDown(int iButton)
	{
		if (m_MouseState.rgbButtons[iButton] & 0x80) {
			return true;
		} else
			return false;
	}
But how do I check if a mousebutton is clicked ? (definition of click: mousebtn down, then mousebtn up)

Share this post


Link to post
Share on other sites
I assume you are checking the state of the mouse each frame?(although we all know what happens when you assume)

Simply keep a state of the mouse from the previous frame and then it's simple logic from there.


bool CInput::MouseButtonDown(int iButton){
if (m_MouseState.rgbButtons[iButton] & 0x80) {
return true;
} else{
return false;
}
}

// note that the above can be reduced to:
bool CInput::MouseButtonDown(int iButton){
return m_MouseState.rgbButtons[iButton] & 0x80;
}
// but you should check the value of iButton to make sure
// you don't go outside the array and crash your program

// in class declaration
bool prevButtons[MAX_BUTTONS];


// loop through and copy the current buttons(& 0x80 them when
// you copy to turn it into a bool) to the prevButtons
// before you check the new state of the mouse

// Button was down last frame, and is up this frame
bool CInput::MouseButtonUp(int iButton){
return prevButtons[iButton] && (!m_MouseState.rgbButtons[iButton] & 0x80);
}


Jesus is Lord!!

Edit: Darn tags

[edited by - CD579 on January 24, 2004 5:49:13 PM]

Share this post


Link to post
Share on other sites