Need help with SDL mouse input

Started by
2 comments, last by Lost 18 years, 2 months ago
I need help with SDL mouse input. I have been using "Focus on SDL" which is good, but is a bit lacking (or I'm just not seeing/understanding it) on how to get which mouse button was pushed and where. This is what I have now (note the fprint isn't long term needed, just nice to know it is working).
if(SDL_PollEvent(&g_Event)==0)
{
	//no event, so blit image onto display
	RenderUI();
	SDL_UpdateRect(g_pDisplaySurface,0,0,0,0);
}
else
{
	if(g_Event.type == SDL_MOUSEBUTTONDOWN){
		//report event  SDL_MouseButtonEvent
		fprintf(stdout,"Button press event\n");
	}

	if(g_Event.type==SDL_QUIT){
		break;
	}
}
Advertisement
I think this is what you're looking after:

SDL_Event event;while (SDL_PollEvent(&event)) {  switch (event.type) {    case SDL_MOUSEBUTTONDOWN:      switch (event.button.button) {        case SDL_BUTTON_LEFT:          printf("Left mouse button pressed.\n");          printf("At position %dx%d\n", event.button.x, event.button.y); // Get position of the button press          break;        case SDL_BUTTON_MIDDLE:          printf("Middle mouse button pressed.\n";          break;        case SDL_BUTTON_RIGHT:          printf("Right mouse button pressed.\n";          break;        default:break;      } break;    default:break;  } }
- Jezper Blixt
I have a tutorial on that.
Lazy Foo's SDL tutorials

</yet another shameless plug>

[Edited by - Lazy Foo on August 9, 2007 11:55:08 PM]

Learn to make games with my SDL 2 Tutorials

Thanks to you both! [smile]
Between the two, I got what I need.

This topic is closed to new replies.

Advertisement