Sign in to follow this  
dream0331

SDL mouse event

Recommended Posts

dream0331    122
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!

Share this post


Link to post
Share on other sites
android_808    152
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;

Share this post


Link to post
Share on other sites
Kylotan    9881
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.

Share this post


Link to post
Share on other sites
android_808    152
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.

Share this post


Link to post
Share on other sites
Kylotan    9881
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.

Share this post


Link to post
Share on other sites

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