gameprogcpp / code

Game Programming in C++ Code
Other
1.01k stars 354 forks source link

Serious bug in mouse button detection #52

Closed LeeTeng2001 closed 7 months ago

LeeTeng2001 commented 2 years ago

Hello, first of all I really thank you for writing this book. When I was doing the camera part I noticed the weird behaviour of mouse detection in the InputSystem, turns out it's wrong to test equality for mouse button because valid result should be non-negative but doesn't guarantee 1.

bool MouseState::GetButtonValue(int button) const
{
    return (SDL_BUTTON(button) & mCurrButtons) == 1;  // Will not work!!!
}

The correct code should be

bool MouseState::GetButtonValue(int button) const
{
    return (SDL_BUTTON(button) & mCurrButtons);
}
chalonverse commented 2 years ago

This is a great find. Funny I missed that, but I probably didn't notice it since I always used GetButtonState instead of the GetButtonValue function, which would work correctly because of the tests against 0 instead of 1.