Sign in to follow this  

SDL Mousehandling

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

I have a wierd problem I cant understand. When I try out my tilenengine, I draw some tiles with the mouse. If left/right button is clicked I call the SetTile() function and change the tile under the mouses coordinates.
	while(running) {
		while(SDL_PollEvent(&event)) {
			if(event.type == SDL_QUIT)
				running = false;

			if(event.key.keysym.sym == SDLK_ESCAPE)
				running = false;

			if(event.type == SDL_MOUSEMOTION) {
				MousePos.X = event.button.x;
				MousePos.Y = event.button.y;
			}
			if(event.button.button == SDL_BUTTON_LEFT) 
				TileEngine->SetTile("Black", TileEngine->ScreenToMapCoord(MousePos.X, MousePos.Y));
			if(event.button.button == SDL_BUTTON_RIGHT) 
				TileEngine->SetTile("White", TileEngine->ScreenToMapCoord(MousePos.X, MousePos.Y));
		}

However, if I move the mousepointer out of the window, the last tile I was on is turned black. And if I hold down the left mousebutton, all the tiles I move over is turned black. But with the white tiles, I have to click every tile, I cant just hold down the button and drag as I can with black. Is it something about sdl mouse input Ive missunderstood?

Share this post


Link to post
Share on other sites
Ideally, you should add in code so it does not process your mouse input when the app does not have 'mouse focus'.

Here's a quick example:

if (Event.type == SDL_ACTIVEEVENT)
{
if(Event.active.state & SDL_APPMOUSEFOCUS)
{
if(Event.active.gain)
{
isActiveVar = true;
}
else
{
isActiveVar = false;
}
}
}


Then before you have your button left and right code, you would have an if(isActiveVar) to see if you should process those events. Here's more information on SDL_ActiveEvent, you might have to play with it a bit to get it how you want, but you definitly don't want to handle mouse events when the mouse is not in the screen. I think that should fix your problem, but you could also just call SDL_GrabInput(SDL_GRAB_ON) to keep the mouse in the screen, but then you better have a key to escape your program! [wink]

Share this post


Link to post
Share on other sites

This topic is 4358 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.

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