Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

SDL Mousehandling

This topic is 4568 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
Advertisement
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
Im just doing a mini mini program to test my A* algoritm, so that simple thing will definitely do. Thanks. [smile]

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!