Jump to content
  • Advertisement
Sign in to follow this  
dream0331

SDL mouse event

This topic is 3551 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
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;

Share this post


Link to post
Share on other sites
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!