Jump to content
  • Advertisement
Sign in to follow this  
multiversicationist

!SDL_GetMouseState only works for button 1

This topic is 2583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I'm a beginner learning SDL, and I noticed something which is bugging me.


alGetSourcei(source[3], AL_SOURCE_STATE, &val3);

if (SDL_GetMouseState(&x,&y)&SDL_BUTTON(1) && x<10 && y<10 && val3!=AL_PLAYING) alSourcePlay(source[3]);

else if (!SDL_GetMouseState(&x,&y)&SDL_BUTTON(1) && val3==AL_PLAYING){ alSourcePause(source[3]);}

This plays a sound file if the mouse cursor is in the upper left corner of the window and you press mouse button 1, and if you let off mouse button 1, the source is paused.

But when I use button 2, or 3 instead of 1, in the exact same code, the button press is registered, but the button release isn't, and the source will play, but not pause on release. Whats up with this?

Share this post


Link to post
Share on other sites
Advertisement
I suspect it is an operator precendence issue. Logical NOT has a higher precendence than bitwise AND.

alGetSourcei(source[3], AL_SOURCE_STATE, &val3);

if ( (SDL_GetMouseState(&x,&y)&SDL_BUTTON(1)) && x<10 && y<10 && val3!=AL_PLAYING) alSourcePlay(source[3]);

else if (!SDL_GetMouseState(&x,&y)&SDL_BUTTON(1)) && val3==AL_PLAYING){ alSourcePause(source[3]);}


I generally try to avoid relying on precedence. by using parentheses or creating additional variables. The thing about relying on precendence is that once the code is written, it is hard to determine what the programmer intended the precendence to be, you can only see what they wrote.

I would write something like this:

alGetSourcei(source[3], AL_SOURCE_STATE, &currentState);

Uint8 button = SDL_GetMouseState(&mouseX, &mouseY)
if (button & SDL_BUTTON(1))
{
if(mouseX < 10 && mouseY < 10 && currentState != AL_PLAYING)
{
alSourcePlay(source[3]);
}
}
else if (currentState == AL_PLAYING)
{
alSourcePause(source[3]);
)

Share this post


Link to post
Share on other sites

I suspect it is an operator precendence issue. Logical NOT has a higher precendence than bitwise AND.

alGetSourcei(source[3], AL_SOURCE_STATE, &val3);

if ( (SDL_GetMouseState(&x,&y)&SDL_BUTTON(1)) && x<10 && y<10 && val3!=AL_PLAYING) alSourcePlay(source[3]);

else if (!SDL_GetMouseState(&x,&y)&SDL_BUTTON(1)) && val3==AL_PLAYING){ alSourcePause(source[3]);}


I generally try to avoid relying on precedence. by using parentheses or creating additional variables. The thing about relying on precendence is that once the code is written, it is hard to determine what the programmer intended the precendence to be, you can only see what they wrote.

I would write something like this:

alGetSourcei(source[3], AL_SOURCE_STATE, &currentState);

Uint8 button = SDL_GetMouseState(&mouseX, &mouseY)
if (button & SDL_BUTTON(1))
{
if(mouseX < 10 && mouseY < 10 && currentState != AL_PLAYING)
{
alSourcePlay(source[3]);
}
}
else if (currentState == AL_PLAYING)
{
alSourcePause(source[3]);
)



Thanks man.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!