Jump to content
  • Advertisement
Sign in to follow this  
buggy123

SDL mouse events

This topic is 3293 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

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
Advertisement
eh? more specific please? Anyways, I'm looking for something that will keep a constant track of the mouse even if there is no events. Is it possible?

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 not sure how to get more specific than that :o
You pass two int* to SDL_GetMouseState, and the current x and y coordinates of the mouse get assigned to the values they point to

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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!