Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: is there a way in the library to make this more efficient?  (Read 1522 times)

0 Members and 1 Guest are viewing this topic.

codinglexernewbie

  • Newbie
  • *
  • Posts: 4
    • View Profile
is there a way in the library to make this more efficient?
« 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?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: is there a way in the library to make this more efficient?
« Reply #1 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
}
 
Visit my game site (and hopefully help funding it? )
Website | IndieDB

PupperGump

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: is there a way in the library to make this more efficient?
« Reply #2 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.