0
\$\begingroup\$

I've been trying to figure this out all day. I'm trying to make a simple check box. But my MOUSEBUTTONUP event keeps firing until I trigger another event like moving the mouse or clicking again.

//If a mouse button was released
if (Sdl_Setup->GetMainEvent()->type == SDL_MOUSEBUTTONUP)
{
    //If the left mouse button was pressed
    if (Sdl_Setup->GetMainEvent()->button.button == SDL_BUTTON_LEFT)
    {
        //Get the mouse offsets
        SDL_GetMouseState(&mouseX, &mouseY);

        //If the mouse is over the button
        if ((mouseX > Button->GetX()) && (mouseX < Button->GetX() + Button->GetWidth()) && (mouseY > Button->GetY()) && (mouseY < Button->GetY() + Button->GetHeight()))
        {
                //Set the button sprite
                state = selected;
                Button->SetCrop(GetFrameX(), GetFrameY(), state);
                clicked = true;
                std::cout << clicked << std::endl;
        }
    }
}

This will just spam 1 in the console (if you keep the mouse still) when all I want is for it to trigger once. From what I've read MOUSEBUTTONUP is only supposed to be sent to the event queue once

I've tried adding a bool to stop it after it's clicked by putting it in an if statement and that works but the thing is I want to be able to toggle the box on and off so when I add an else statement that changes the bool back to false it spams 101010 instead of just one.

   //If a mouse button was released
if (Sdl_Setup->GetMainEvent()->type == SDL_MOUSEBUTTONUP)
{
    //If the left mouse button was pressed
    if (Sdl_Setup->GetMainEvent()->button.button == SDL_BUTTON_LEFT)
    {
        //Get the mouse offsets
        SDL_GetMouseState(&mouseX, &mouseY);

        //If the mouse is over the button
        if ((mouseX > Button->GetX()) && (mouseX < Button->GetX() + Button->GetWidth()) && (mouseY > Button->GetY()) && (mouseY < Button->GetY() + Button->GetHeight()))
        {
            if (clicked == false)
            {
                //Set the button sprite
                state = selected;
                Button->SetCrop(GetFrameX(), GetFrameY(), state);
                clicked = true;
                std::cout << clicked << std::endl;
            }
            else
            {
                //Set the button sprite
                state = noInteraction;
                Button->SetCrop(GetFrameX(), GetFrameY(), state);
                clicked = false;
                std::cout << clicked << std::endl;
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Have you checked if the system behind Sdl_Setup->GetMainEvent() could be the trouble maker here? That seems like the only possibility to me, since your code above looks okay. \$\endgroup\$ – Tyyppi_77 Apr 5 '17 at 8:53
  • \$\begingroup\$ @Tyyppi_77 That was the problem. Thanks for your help! \$\endgroup\$ – Arkyris Apr 5 '17 at 19:03
1
\$\begingroup\$

In Sdl_Setup is where I make the window I'm using and stuff like that and the variable mainEvent is = new SDL_Event(); so then when I was calling GetMainEvent() it was just sending that to PollEvent() over and over again. So all I did is create a quick void function in Sdl_Setup to set mainEvent to a blank SDL_Event() and then call that after clicked = true; and it works.

\$\endgroup\$
0
\$\begingroup\$

I suggest adapting your program to use a "mousedown" variable. Here's an example you can use as a reference:

bool mousedown=0;  // Declared outside the scope of functions that use it.
while(running) { // Main loop.
    SDL_Event event;
    while(SDL_PollEvent(&event)) {
        switch(event.type) {
            case SDL_MOUSEBUTTONDOWN:
                mousedown=1;
                break;
            case SDL_MOUSEBUTTONUP:
                if(mousedown) {
                    /* The SDL_MOUSEBUTTONUP event will be ignored if a
                       previous SDL_MOUSEBUTTONDOWN event has not occurred. */
                }
                mousedown=0;
                break;
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ I didn't try that but it looks like it might of worked. Thank you for you help \$\endgroup\$ – Arkyris Apr 5 '17 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.