Page 1 of 1

SDL Mouse States

Posted: Wed Sep 01, 2010 9:55 am
by RandomDever
All I need is a boolean that is true if the left mouse button is down and false if the left mouse button is up.
I can't find it.
Please Help!

Re: SDL Mouse States

Posted: Wed Sep 01, 2010 10:06 am
by pritam
I can't give you an example by ready-to-go code, but you need to track the mouse states, something like this:

Code: Select all

bool lmbPressed = false; // lmb == left mouse button

if( g_Event.type == SDL_MOUSEBUTTONDOWN && SDL_BUTTON(SDL_GetMouseState(NULL,NULL)) == SDL_BUTTON_LEFT ) {
lmbPressed = true;
}

if( g_Event.type == SDL_MOUSEBUTTONUP ) {
lmbPressed = false;
}

Re: SDL Mouse States

Posted: Wed Sep 01, 2010 10:22 am
by M_D_K

Code: Select all

struct Mouse
{
        int dx, dy;
        int oldX, oldY;
        unsigned int buttons;
        unsigned int oldButtons;
};
in your input code put this somewhere.

Code: Select all

        SDL_PumpEvents();
        mouse.oldButtons = mouse.buttons;
        mouse.oldX = mouse.dx;
        mouse.oldY = mouse.dy;
        mouse.buttons = SDL_GetMouseState(&mouse.dx, &mouse.dy);
you can check for buttons by doing:

Code: Select all

bool mouseDown(int button)
{
        return currMouse(button) && (!oldMouse(button));
}
bool mouseStillDown(int button)
{
        return currMouse(button) && (oldMouse(button));
}
Same for checking if a button is up but in reverse.

where currMouse and oldMouse are defines:

Code: Select all

#define currMouse(x) ((mouse.buttons&SDL_BUTTON(x)) != 0)
#define oldMouse(x) ((mouse.oldButtons&SDL_BUTTON(x)) != 0)

Re: SDL Mouse States

Posted: Wed Sep 01, 2010 5:06 pm
by RandomDever
OK I tried all of your solutions and they didn't fully work.
The only time the thang would update was when I triggered an event.
So I thought OK maybe I put the update function in the while loop that handles events.
I checked and it wasn't in there.
After toying around for 30 minutes I realized that the functions were reversed.
I had made the update() code handle events and the check() code handle the thangs update.
So in essence I had my events updated in the logic section and my logic updated in my events section.
:|
Anyways thank for all of your solutions.