int main()
{
cout << "enter width: ";
cin >> x;
cout << endl;
cout << "enter height: ";
cin >> y;
sf::RenderWindow window(sf::VideoMode(x * 4, y * 4), "Window");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
sf::IntRect r1(100, 100, 100, 100);
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(100, 100));
rectangle.setOutlineColor(sf::Color::Red);
rectangle.setFillColor(sf::Color::Blue);
rectangle.setOutlineThickness(1);
rectangle.setPosition(100, 100);
window.draw(rectangle);
if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
mx = event.mouseButton.x;
my = event.mouseButton.y;
bool b1 = r1.contains(mx, my);
if (b1 == true)
{
rectangle.setFillColor(sf::Color::Red);
window.draw(rectangle);
cout << mx << my << endl;
}
}
}
window.display();
}
system("pause");
}
I've try to write my code
Result is my rectangle change to red only I pressed my mouse
How an I fix this?