SDL mouse events

Started by
10 comments, last by rip-off 14 years, 7 months ago
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
Advertisement
SDL_GetMouseState
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?
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.
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
ohh. lol the wording on that website kinda confused me xD

well thanks guys hope this works :D
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]
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.
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.
thanks codeka. your post helped me out big times there

This topic is closed to new replies.

Advertisement