3
\$\begingroup\$

I'm having a bad time trying to change the window view by dragging the mouse. I can precisely do that with the method:

void PlayScreen::MenuView(sf::RenderWindow& window)
{
    while (sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        old_mouse_position = new_mouse_position;
        new_mouse_position = sf::Mouse::getPosition(window);

        game_view.move(new_mouse_position.x - old_mouse_position.x, new_mouse_position.y - old_mouse_position.y);
    }
}

The problem is that the view is updated, (when I release the mouse, it's drawn in the correct position) but it's not drawn as I move the mouse. My gameLoop only draw after all the updates are made, and I was trying to keep the logic that way. Everytihng works fine if I draw inside MenuView while.

my Gameloop is quite simple:

while (main_window.isOpen())
{
    while (main_window.pollEvent(event))
    {
        GameOptions(event);
        DrawGame();
    }
}

Inside the GameOptions(event) there is a:

if (event.type == sf::Event::MouseButtonPressed)
{
    new_mouse_position = sf::Mouse::getPosition(window);
    if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        MenuView(window);
    }
    break;
}

I've tried without the while inside the MenuView, but it just don't work.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
  • The event MouseButtonPressed is sent only once (that's how SFML works), so when you check this statement : if (event.type == sf::Event::MouseButtonPressed) it is only true when you click, it won't be true again unless you click again

Instead of doing this if (event.type == sf::Event::MouseButtonPressed)

You could do this if (sf::Mouse::isButtonPressed(sf::Mouse::Left))

or set a bool to true like this:

in GameOption(event)

if (event.type == sf::Event::MouseButtonPressed)
    buttonPressed = true;
else if (event.type == sf::Event::MouseButtonReleased)
    buttonPressed = false;
if (buttonPressed == true)
{
    MenuView(window);
}
  • The while in MenuView will stop your game from rendering for all the time your Left mouse button is pressed, so get rid of this while. MenuView should only change the view position once.

Hope it solves your problem, if not I'll update my answer (I can't comment yet)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .