Mouse handling in SDL

Started by
3 comments, last by Mizipzor 18 years, 1 month ago
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?
Advertisement
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).
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?
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.
Solved... had another function later that also polled events, removing that made it work.

This topic is closed to new replies.

Advertisement