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

Author Topic: [SFML 2.0] sf::Event::MouseMoveEvent problems  (Read 2268 times)

0 Members and 1 Guest are viewing this topic.

Harsay

  • Newbie
  • *
  • Posts: 5
    • View Profile
[SFML 2.0] sf::Event::MouseMoveEvent problems
« on: June 17, 2012, 02:29:37 am »
Hi. I have a problem with sf::Event::MouseMoveEvent

#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(1280, 720, 32), "LIKE A PONG TEST", sf::Style::Fullscreen);
    window.setFramerateLimit(60);
    sf::Mouse::setPosition(sf::Vector2i(100, 200), window);
    sf::Event::MouseMoveEvent mouse;
    sf::Event event;
    sf::Text paletka("|");

    while (window.isOpen())
    {
         while (window.pollEvent(event))
         {
             if (event.type == sf::Event::Closed)
                 window.close();

             if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
                 window.close();

             if (event.type == sf::Event::MouseMoved) {
                 int x = mouse.x;
                 paletka.setPosition(x, 5);
                 std::cout << x << std::endl;
             }

         }

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

I'm trying to get text x equal to mouse x, but when I am moving the mouse it writes (in cmd) the same x always!
I don't know what to do...

Thanks in advance for help.
« Last Edit: June 17, 2012, 09:22:53 am by Laurent »

Hazematman

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [SFML 2.0] sf::Event::MouseMoveEvent problems
« Reply #1 on: June 17, 2012, 05:08:54 am »
Hey Harsay, you don't need to declare an sf::Event:MouseMoveEvent, as I believe that is static. When you call window.pollEvent(event) you send all the event data to the event variable. So in order to get the mouse's x all you need to do is call it from the event variable.

if (event.type == sf::Event:MouseMove) {
 int x = event.mouseMove.x;
 paletka.setPosition(x, 5);
 std::cout << x << std::endl;
}
 

Harsay

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: [SFML 2.0] sf::Event::MouseMoveEvent problems
« Reply #2 on: June 17, 2012, 01:06:33 pm »
Code: [Select]
            if (event.type == sf::Event::MouseMoved) {
             int x = event.mouseMove.x;
             paletka.setPosition(x, 5);
             std::cout << x << std::endl;
            }

It works! Thanks :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: [SFML 2.0] sf::Event::MouseMoveEvent problems
« Reply #3 on: June 17, 2012, 01:08:19 pm »
As Hatematman stated you don't need an sf::Event::MouseMoveEvent object and since you have a sf::Event object you can access the MouseMoveEvent.
Unfortunatly the suggested code isn't fully correct:

if(event.type == sf::Event::MouseMoved)
{
 int x = event.MouseMoveEvent.x;
 paletka.setPosition(x, 5);
 std::cout << x << std::endl;
}

An alternative would be to use the independet mouse object sf::Mouse with the static function getPosition(window), where window is used to get the relative position to the window and not the global position to the top left corner of your screen.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything