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!
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. =\