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

Author Topic: Stuck in event loop when using Event::MouseMoved  (Read 7270 times)

0 Members and 1 Guest are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stuck in event loop when using Event::MouseMoved
« Reply #15 on: November 21, 2014, 10:54:20 pm »
Your code has no event loop, which makes the window unresponsive and makes it very hard to close it, since the cursor is stuck in the center. I had to open the task manager to close it.

Then I have no idea what it's supposed to do, since mouseOffset is not never updated in the game loop.

So I wrote a version that works as expected (and can be closed more easily):

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test");
    window.setMouseCursorVisible(false);

    sf::Vector2i center(window.getSize().x / 2, window.getSize().y / 2);

    sf::Texture texture;
    texture.loadFromFile("example.png");
    sf::Sprite sprite(texture);
    sprite.setPosition(sf::Vector2f(center));

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                window.close();
        }

        sf::Vector2i offset = sf::Mouse::getPosition(window) - center;
        sprite.move(sf::Vector2f(offset));
        sf::Mouse::setPosition(center, window);

        window.clear();
        window.draw(sprite);
        window.display();
    }

    return EXIT_SUCCESS;
}
Laurent Gomila - SFML developer

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #16 on: November 21, 2014, 11:56:40 pm »
Thats not really a metaphor but instead an analogy. Furthermore, if you did not test the code, you did not read what Laurent said, or what the rules (which Laurent wrote) say: Complete and minimal code. If you did not test the code, its probablt not an extract from your current code, so it probably doesnt contain the problem. However, you are never updating the mouse position in the code your posted, so the sprite position will never change. Also, why are you setting the mouse position yourself? Thats probably the reason why your thing doesnt work.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #17 on: November 24, 2014, 04:09:00 am »
Well my code did contain the problem, I made sure of that much. I was just trying to post quickly before I got off. Also, sorry about the no escape functionality, I forgot to include that in the post.

Anyways, I hate to say this but your posted code doesn't seem to work either. The sprite sort of jumps around the screen in the general direction the mouse is moving, at a very fast rate as well, then after a time it zooms off screen in the last direction the mouse was moving, never to be seen again. It's just weird at this point. When you ran the code you posted, was the sprite even a little bit oversensitive to the mouse movements, or is this just me?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stuck in event loop when using Event::MouseMoved
« Reply #18 on: November 24, 2014, 07:54:07 am »
My code worked exactly as expected on my computer.

Do other SFML apps (like the SDK examples) work fine on your PC?
Laurent Gomila - SFML developer

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #19 on: November 24, 2014, 02:37:29 pm »
Yes, the opengl example (with the box that follows the cursor) works just fine. Of course, that is simply following the cursor around while my code moves the sprite based on deltas, so perhaps the issue is how I'm calculating the deltas. I would have the code simply follow the cursor except I'm creating a maze for the user, and the mouse can easily bypass the maze walls when checking for collision due to its speed, so that's why I constantly set it to the center.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stuck in event loop when using Event::MouseMoved
« Reply #20 on: November 24, 2014, 02:46:07 pm »
Quote
so perhaps the issue is how I'm calculating the deltas
This is very unlikely, since my code doesn't work for you either.

Could you test my code with a framerate limit of 60?
Laurent Gomila - SFML developer

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #21 on: November 24, 2014, 02:51:40 pm »
Yeah, setting the framerate helped some. I can say that I have control over the sprite, but getting it to move in a straight line if I move the mouse in a straight line is almost impossible.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stuck in event loop when using Event::MouseMoved
« Reply #22 on: November 24, 2014, 02:55:14 pm »
What do you mean? The movement is not smooth, or the direction is sometimes wrong?
Laurent Gomila - SFML developer

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #23 on: November 24, 2014, 06:22:50 pm »
Sorry, I mean that the movement isn't smooth. A movement that would move my cursor maybe 5 pixels moves the sprite 9 or 10 pixels plus a little left or a little right, so slight movements left or right give the sprite a shaky appearence when moving.


Edit:
sf::Vector2i mouseOffset(std::floor(sf::Mouse::getPosition(window).x - mouseOffsetOrigin.x), std::floor(sf::Mouse::getPosition(window).y -                                                                      mouseOffsetOrigin.y));
using std::floor to calculate the offset seems to be a decent workaround. I still wonder what caused the issue though
« Last Edit: November 25, 2014, 06:11:16 pm by wh1t3crayon »

 

anything