[SDL] Best approach to "mouse button held"

Started by
7 comments, last by JonBonazza 12 years, 6 months ago
What is the best approach to test whether the left mouse button has been held down and not just clicked?

I am using SDL, specificaly the SDL_PollEvent() function to test for click, so should I just keep track of the time, then compare it to a pre-defined interval constant and then check for the same event?
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US
Advertisement
The best way to test it would depend on how you want to use the information. For example, if you want this information so you can do something if the mouse button is held while dragging, then you would just check the state member of the SDL_MOUSEMOTIONEVENT structure.
Thanks for the reply.

I don't really care if it's dragging or not. I just want a way to distinguish between a click and a hold.

The thing is, the new version of SDL has android support, and it works, but instead of using the SDL_TouchEvent struct like other supported touch screen devices, it uses the SDL_MouseButtonEvent struct. Why they did this, I do not know. At any rate, I am ultimately trying to distinguish between when a users taps the screen, and when the user taps and holds a point on the screen.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US
The easiest way would be..(Note this is just a Pseudo code )

//Initialize everything
const Uint8 Button = SDL_BUTTON_LEFT;
//Add
bool dragging;
int offsetX = 0;
int offsetY = 0;
bool intersects( int objectX, int objectY, int objectW, int objectH, int mouseX, int mouseY )
{
if( mouseX < objectX || mouseY < objectY )
{
return false;
}
if( mouseX > objectX + objectW || mouseY > objectY + objectH )
{
return false;
}
return true;
}
/// and then adding all other functions etc.
//Then in the handle input function you do this
if( event.type == SDL_MOUSEBUTTONDOWN )
{
if( ( event.button.button &Button ) == Button && intersects( x, y, w, h, event.button.x, event.button.y ) )
{
offsetX = event.button.x - x;
offsetY = event.button.y - y;
dragging = true;
}
}
else if( event.type == SDL_MOUSEBUTTONUP )
{
if( event.button.button &Button ) == Button && dragging )
{
offsetX = 0;
offsetY = 0;
dragging = false;
}
else if( event.type == SDL_MOUSEMOTION )
{
if( dragging )
{
x = event.motion.x - offsetX;
y = event.motion.y - offsetY;
}
}

This is just a pseudo code.. Try implementing it on your code
Hope this helps :D
Thanks, but I think you misunderstood my problem (or I am misunderstanding you). I don't care about dragging, really. What I am trying to do is differentiate between a tap ( a quick touch on the screen) and a hold (the screen is touched and held there for a second before it is released).
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US
You could have a class that receives click down / click up events (just pass the events to the class if you want) and it stores the time between the click down and up- then depending on the timing that you ordain, you can ask it if a click or a hold happened.

If you don't like that method- on click down you can update a uint32_t with SDL_GetTicks() on click down- then on click up, store the difference between the stored number and SDL_GetTicks() and you have the time of the click. If you'd like some pseudo/real code just ask.

You could have a class that receives click down / click up events (just pass the events to the class if you want) and it stores the time between the click down and up- then depending on the timing that you ordain, you can ask it if a click or a hold happened.

If you don't like that method- on click down you can update a uint32_t with SDL_GetTicks() on click down- then on click up, store the difference between the stored number and SDL_GetTicks() and you have the time of the click. If you'd like some pseudo/real code just ask.


Thanks! The first approach is pretty much what I was planning to implement, but I wanted to get a second opinion.

As for code, I am sure I can figure it out, but if not, then I will check back.

Also, it's funny you mention the SDL_GetTicks() method. I was actually just looking for a viable replacement for timeGetTime() for timing purposes., as the timeGetTime() method (and hte DWORD data type) is windows only, and the Android OS runs on top of a linux kernel. Do you think that this would be a fair replacement (provided I throttle it to make up for faster/slower PCs)? If not, can you reccommend a better solution?
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US

[quote name='ultifinitus' timestamp='1318095969' post='4870541']
You could have a class that receives click down / click up events (just pass the events to the class if you want) and it stores the time between the click down and up- then depending on the timing that you ordain, you can ask it if a click or a hold happened.

If you don't like that method- on click down you can update a uint32_t with SDL_GetTicks() on click down- then on click up, store the difference between the stored number and SDL_GetTicks() and you have the time of the click. If you'd like some pseudo/real code just ask.


Thanks! The first approach is pretty much what I was planning to implement, but I wanted to get a second opinion.

As for code, I am sure I can figure it out, but if not, then I will check back.

Also, it's funny you mention the SDL_GetTicks() method. I was actually just looking for a viable replacement for timeGetTime() for timing purposes., as the timeGetTime() method (and hte DWORD data type) is windows only, and the Android OS runs on top of a linux kernel. Do you think that this would be a fair replacement (provided I throttle it to make up for faster/slower PCs)? If not, can you reccommend a better solution?
[/quote]

SDL_GetTicks() returns the number of milliseconds that has passed since you called SDL_Init() , it should (with 1.2.14 or later atleast) use the highest resolution method available for the target platform (On windows it can choose between timegettime and qpc, on Linux between gettimeofday and clock_gettime).
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Awesome. Thanks!
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US

This topic is closed to new replies.

Advertisement