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

Author Topic: Drawing a line segment with end points set by mouse click  (Read 4671 times)

0 Members and 1 Guest are viewing this topic.

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Drawing a line segment with end points set by mouse click
« 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!

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Drawing a line segment with end points set by mouse click
« Reply #1 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]?
« Last Edit: July 24, 2014, 07:26:30 am by Strelok »

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Drawing a line segment with end points set by mouse click
« Reply #2 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. =\

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Drawing a line segment with end points set by mouse click
« Reply #3 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing a line segment with end points set by mouse click
« Reply #4 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*