SFML community forums

Help => Graphics => Topic started by: Clockwork on July 24, 2014, 06:35:10 am

Title: Drawing a line segment with end points set by mouse click
Post by: Clockwork on July 24, 2014, 06:35:10 am
Hi everybody,

I did some quick googling and I think I found how to draw the line successfully, but I have a problem setting the start and end points of the line.  When I use the code that I have now, the start point is the top left corner of the screen, and it ends wherever I click.  How can I get it to start where the first click is and end at the second click? 

This is what I have now:
Player::Player()
        : isPressed(false)
{
        points[0].color = sf::Color::Red;
        points[1].color = sf::Color::Red;
}

void Player::handleInput(sf::Event event)
{
        switch (event.type)
        {
        case sf::Event::MouseButtonPressed:
                pressed = true;
                std::cout << std::to_string(pressed) << std::endl;
                break;
        case sf::Event::MouseButtonReleased:
                pressed = false;
                std::cout << std::to_string(pressed) << std::endl;
                break;
        }
}

void Player::update(sf::RenderWindow& window, sf::Time dt)
{
        mousePos = sf::Mouse::getPosition(window);

        if (isPressed)
                points[0] = sf::Vector2f(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));

        std::cout << points[0].position.x << std::endl;
}

void Player::render(sf::RenderWindow& window)
{
        window.draw(points, 2, sf::Lines);
}

Thank you for the help!
Title: Re: Drawing a line segment with end points set by mouse click
Post by: Strelok on July 24, 2014, 07:24:33 am
Are isPressed and pressed the same class member and just a typo?
Where do you assign values to points[1]?
Title: Re: Drawing a line segment with end points set by mouse click
Post by: Clockwork on July 24, 2014, 06:09:56 pm
Ah, sorry about that, pressed and isPressed are the same variable, that was just a typo.  I tried assigning points[1] to like this:

void Player::update(sf::RenderWindow& window, sf::Time dt)
{
        mousePos = sf::Mouse::getPosition(window);

        if (pressed)
                points[0] = sf::Vector2f(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
        if (!pressed)
                points[1] = sf::Vector2f(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
}

But I quickly realized that doesn't work.  At this point I'm out of ideas though. =\
Title: Re: Drawing a line segment with end points set by mouse click
Post by: Nexus on July 24, 2014, 06:33:31 pm
At this point I'm out of ideas though. =\
Please read the tutorials and the documentation. You have to map mouse pixels to world coordinates, see sf::RenderWindow.

Also, I would not mix real-time input with events. sf::Mouse allows you to check whether a button is pressed.
Title: Re: Drawing a line segment with end points set by mouse click
Post by: Hapax on July 24, 2014, 08:18:05 pm
that doesn't work.
What does it do now? As a guess, I'd say that one point follows the mouse constantly and the other follows it when then mouse button is down.

Quote
At this point I'm out of ideas though. =\
Try storing which point you are working with and changing it whenever you update the other (on a MouseButtonPressed event). You shouldn't need to check when they're released.