SDL Mouse States

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

SDL Mouse States

Post 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!
pritam
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 991
Joined: Thu Nov 13, 2008 3:16 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Amiga, PSOne, NDS
Programming Language of Choice: C++
Location: Sweden

Re: SDL Mouse States

Post 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;
}
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: SDL Mouse States

Post 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)
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: SDL Mouse States

Post 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.
Post Reply