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;
}
}
}
}
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