Fixed off-by-one error in mouse button bit-masking #177
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
getMouseButtons
detects the wrong buttons because its bit mask doesn't match SDL's convention. For example, it's impossible to detect the left mouse button being held.getMouseState
, likeSDL_GetMouseState
, produces a set of Boolean flags encoded as the bits of an integer. We test the nth flag by AND-ing that integer with a bit mask: the integer 1 bit-shifted by n places.The state of the left mouse button resides in the lowest-order bit—0 places away from the lowest place. So, to test that button we have to bit-shift by 0 places.
SDL provides the macro definitions
SDL_BUTTON(X)
andSDL_BUTTON_LEFT
to do that bit-shifting. It definesSDL_BUTTON_LEFT
to be 1, not 0, and it definesSDL_BUTTON(X)
to subtract 1 from its argument before using it to construct the bit mask (see lines 203-204 in SDL_mouse.h):So,
&& SDL_BUTTON( SDL_BUTTON_LEFT )
=&& (1 << (1 - 1))
=&& (1 << 0)
, which is what we want.However, the corresponding Haskell provisions don't match that convention.
Raw.SDL_BUTTON_LEFT
(and hencetoNumber ButtonLeft
) is defined to be 1, butgetMouseButtons
doesn't do the same subtraction asSDL_BUTTON(X)
:Here,
(`testBit` fromIntegral (toNumber ButtonLeft))
=(`testBit` 1)
, but that doesn't test the left button. We want(`testBit` 0)
.