SFML community forums

Help => Graphics => Topic started by: codinglexernewbie on December 24, 2022, 08:58:12 pm

Title: is there a way in the library to make this more efficient?
Post by: codinglexernewbie on December 24, 2022, 08:58:12 pm
   I'm planning on adding a text-highlighting feature to my little project, (I wrote one before, but it was spaghetti code and very buggy)
I started with   
 
case sf::Event::MouseButtonReleased:
    {
    if(sf::Mouse::getPosition(MaWind).y != Mcoords[1]||sf::Mouse::getPosition(MaWind).x != Mcoords[0])
    {
//Mcoords[0] is the x pos stored from the mouse button being clicked and you get it for Mcoords[1]
    }
    }
but then I realized having to check an if-statement every time the mouse button seems rather inefficient, is there a way i should be doing it other than this or this is the best possible?
Title: Re: is there a way in the library to make this more efficient?
Post by: Stauricus on December 26, 2022, 12:35:33 pm
i don't know if I understand what you are trying to do
but you could use a sf::vector2f instead. and the way you're doing its not inefficient at all, its simply conditionals

sf::vector2f mcoords(10, 20); //you can acces individual values with mcoords.x or mcoords.y
if (mcoords != sf::Mouse::getPosition(MaWind){
//code
}
 
Title: Re: is there a way in the library to make this more efficient?
Post by: PupperGump on January 22, 2023, 02:40:55 am
switch the event.mouseButton.button because the MouseButtonPressed and Released events don't distinguish between mouse buttons. It'll even trigger if you press mouse buttons 3 (scroll wheel) and 4 and 5 (the ones on the side).

As for your condition it looks like you're just checking whether the cursor was moved since you last clicked. There's also a MouseMoved event you can use to set a flag, then just check that flag if you click.