Sign in to follow this  

SDL mouse event

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

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
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
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
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

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