SDL mouse event

Started by
4 comments, last by Kylotan 15 years, 5 months ago
Hi, I am a beginner to SDL libraries. I have a problem with mouse handling. I am currently building a scrollbar, when the mouse pressed, holded, and moved, the button follows the mouse position. I used SDL_MOUSEBUTTONDOWN to check whether mouse is pressed, but then it only triggered when the mouse clicks, not the holding action after that. What should I do? Thank you!
Advertisement
A combination of SDL_MOUSEBUTTONDOWN and SDL_MOUSEBUTTONUP is what I think you need to look at. This issue also relates to key presses. A typical example being movement code.

Create an array of bools large enough to hold all of the buttons on the mouse. On mouse down, or key down, toggle the related value in the array to true. On mouse up set the value to false. Your scrolling code should then check the value in the array.

Here is a sample of my key handling code. mKeyBuffer is an array of 256 bools.

		case SDL_KEYDOWN:			if(SDL_event.key.keysym.sym == Keys::Escape)			{				Engine::GetSingleton().SendQuitSignal();			}			else if(SDL_event.key.keysym.sym == Keys::W)			{				RenderSystem::GetSingleton().ToggleWireframe();			}			else			{				//System::Logger::GetSingleton().Print("Key Pressed: %i", SDL_event.key.keysym.sym);				mKeyBuffer[SDL_event.key.keysym.sym] = true;			}			break;		case SDL_KEYUP:			//System::Logger::GetSingleton().Print("Key Released: %i", SDL_event.key.keysym.sym);			mKeyBuffer[SDL_event.key.keysym.sym] = false;			break;
Thanks for your help.
It works very well.
Quote:Original post by android_808
Here is a sample of my key handling code.

For the most part, you are duplicating SDL_GetKeyState. And thus, SDL_GetMouseState also.
I originally implemented it with the idea of being able to strip it out and re-write it using another API, be it native Win32, X11 or another cross platform library. It was also the way I learnt from many different tutorials.

My understanding of SDL_GetKeyState is that it returns a pointer to an internal structure array. I don't understand the parameter that is passed to the function as all the examples just pass NULL. Using this method would reduce memory consumption? I presume as you wouldn't be duplicating the array.
The documentation for SDL_GetKeyState says that it returns the size of the array. If you pass NULL, that means you don't care about having it store the size of the array for you.

There's not really much point worrying about memory consumption when we're talking about 256 bools, but the main thing is that it's not usually worthwhile writing your own version of code that already exists in the library.

This topic is closed to new replies.

Advertisement