Hi,
I would like to open a menu by clicking on a button.
Theoretically it works so far, but it does open only for a fraction of a second and then goes away. How to prevent it from disappearing.
Thx in advance.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow mMainWindow(sf::VideoMode(400, 400), "Window");
mMainWindow.setFramerateLimit(40);
sf::RectangleShape rect1;
rect1.setSize(sf::Vector2f(200, 200));
sf::RectangleShape rect2;
rect2.setSize(sf::Vector2f(200, 200));
rect2.setPosition(100, 100);
while (mMainWindow.isOpen())
{
sf::Event event;
bool leftclick = false;
bool rect1clicked = false;
sf::Vector2i mousePos;
while (mMainWindow.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
mMainWindow.close();
break;
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Left)
{
leftclick = true;
mousePos = sf::Vector2i (event.mouseButton.x, event.mouseButton.y);
break;
}
}
}
if (leftclick)
{
sf::Vector2f mousecoords(mMainWindow.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)));
if (rect1.getGlobalBounds().contains(mousecoords))
{
rect1clicked = true;
}
}
mMainWindow.clear();
mMainWindow.draw(rect1);
if(rect1clicked)
{
mMainWindow.draw(rect2);
}
mMainWindow.display();
}
return 0;
}