Sign in to follow this  

Mouse handling in SDL

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

Im trying to do a real simple mouse interface in my program, this is the code I use:
	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_MOUSEBUTTONDOWN) {
				if(event.button.button == SDL_BUTTON_LEFT) 
					A.SetNodeValue(event.button.x, event.button.y, 10);
				if(event.button.button == SDL_BUTTON_RIGHT) 
					A.SetNodeValue(event.button.x, event.button.y, 1);
				if(event.button.button == SDL_BUTTON_MIDDLE) 
					A.SetNodeValue(event.button.x, event.button.y, 5);

				cout << "mouse!";
			}
		}

                (...)
        }

I got no response when I pressed the mouse, I just add some values to my class and print them to the screen. So to check if I even entered the if statement I added that cout. But nothing is printed so SDL apparently never knows that Im clicking the mouse. What have I done wrong?

Share this post


Link to post
Share on other sites
Just a couple of suggestions. First of all, although it's unlikely that the keysym will be 'escape' when the event is not a key event, you should probably only check the keysym when the event is in fact a key event.

Next, I would add a 'cout' in reponse to other events, such as key down, key up, mouse motion, and so on. Whatever happens will tell you more about the problem (for example, if no events are registering it's a general problem with SDL, if only mouse button events are not registering it's a problem with that particular event, and so on).

Share this post


Link to post
Share on other sites
I changed it into this:


while(running) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT)
running = false;

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

cout << "key\n";
}

if(event.type == SDL_MOUSEBUTTONDOWN) {
if(event.button.button == SDL_BUTTON_LEFT)
A.SetNodeValue(event.button.x, event.button.y, 10);
if(event.button.button == SDL_BUTTON_RIGHT)
A.SetNodeValue(event.button.x, event.button.y, 1);
if(event.button.button == SDL_BUTTON_MIDDLE)
A.SetNodeValue(event.button.x, event.button.y, 5);

cout << "mouse\n";
}

if(event.type == SDL_MOUSEMOTION)
cout << "motion\n";
}
}



Immediately when I start the program, it prints "motion" three times. After that, it doesnt print anything more. This suggests, as you say, that there is something bad with the sdl event loop on the whole. But I cant see what Im doing wrong. I create a SDL_Event, and send a reference to it to SDL_PollEvent(), and poll every event (and handle them) until the queue is empty. Is this correct?

Share this post


Link to post
Share on other sites
Try changing

Quote:

if(event.type == SDL_MOUSEMOTION)
cout << "motion\n";"


to



if(event.type == SDL_MOUSEMOTION)
{
cout << "motion\n";
cout << event.motion.state << '\n';
}



The state field shows the current buttons held while the mouse was in motion.

See what it says.

Share this post


Link to post
Share on other sites

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