Sign in to follow this  
buggy123

SDL mouse events

Recommended Posts

basically I made a function to detect mouse movement in SDL. However it seems like if( input.type == SDL_MOUSEMOTION ) only detects IF the mouse moves. Is there some way to "check" the position of the mouse every frame even if it's not moving? thanks, buggy123

Share this post


Link to post
Share on other sites
SDL_GetMouseState(int*x,int*y) returns the button flags, and sets whatever you pass as x and y to the position of the mouse

SDL_GetRelativeMouseState(int*x,int*y) does the same thing, but returns the change since the last query.

Share this post


Link to post
Share on other sites
I'm trying to make it so that it loads an highlighted version of the button when the mouse hovers over the button. It only seems to work if I move my mouse. Once I stop moving the mouse it returns to the non-highlighted version.

here's the code function responsible for updating the button according to mouse

void mHandle_mInput (void)
{
SDL_GetMouseState(&Mx, &My);

if( ( Mx > b_play.Pl ) && ( Mx < b_play.Pr) && ( My > b_play.Pt) && ( My < b_play.Pb) )
{
apply_surface( 475, 235, play_button_hl, screen );
}

}


here's the message handling loop:

if( SDL_PollEvent( &input ) )
{
if( input.type == SDL_QUIT )
{
game_state = QUIT;
}

mHandle_mInput();
}



any idea what I'm doing wrong here?

[Edited by - buggy123 on September 10, 2009 8:27:44 PM]

Share this post


Link to post
Share on other sites
It seems to me that the problem is you're only calling mHandle_mInput when SDL_PollEvent returns <c>true</c>, which will only happen when there's actually an event waiting (e.g. if you pressed keys on the keyboard, you'd probably notice your button would flash as well).

The usual way to handle this would be when you get a mouse event, you check whether the mouse cursor is "inside" the button, and then set a flag in the button saying "mouse_is_over". Then when you get more mouse events, if you detect the cursor is no longer inside the button, you set the flag back to false.

Next, in your render loop (which runs regardless of input events, obviously) you just use the value of the "mouse_is_over" flag to determine whether you draw the highlighted state or the unhighlighted state.

Share this post


Link to post
Share on other sites
If you're polling for events in SDL you want to say:

while ( SDL_PollEvent( &input ) )
{
if( input.type == SDL_MOUSEMOTION )
{
Uint16 xMouse = input.motion.x;
Uint16 yMouse = input.motion.y;
}

if( input.type == SDL_QUIT )
{
game_state = QUIT;
}
}


Note: I changed the 'if' to a 'while' in the SDL_PollEvent statement, so that you process all pending events.

Share this post


Link to post
Share on other sites
How does the SDL_BUTTON(X) work?

I have this code in my handle input functions:

 if( ( Mx > b_instruction.Pl ) && ( Mx < b_instruction.Pr) && ( My > b_instruction.Pt) && ( My < b_instruction.Pb) && SDL_BUTTON(1) ) 
{
game_state = INSTRUCTION;
}


but right now, if I just put my mouse over the button (not clicking) it still changes to the instructions state. confused lol

Share this post


Link to post
Share on other sites
The SDL_BUTTON(X) is a macro that makes a constant representing SDL's internal name for a particular mouse button.

It is only meaningful when you bitwise and it with the return value of SDL_GetMouseState(), SDL_GetMouseStateRelative() or the value of "button" of a SDL_MouseButtonEvent.

You might write:

void mHandle_mInput (void)
{
Uint8 button = SDL_GetMouseState(&Mx, &My);

if( ( Mx > b_play.Pl ) && ( Mx < b_play.Pr) && ( My > b_play.Pt) && ( My < b_play.Pb) && (button & SDL_BUTTON(1))
{
apply_surface( 475, 235, play_button_hl, screen );
}
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this