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

Author Topic: Mouse movement  (Read 5889 times)

0 Members and 1 Guest are viewing this topic.

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Mouse movement
« Reply #15 on: June 24, 2013, 12:02:30 am »
I am looking for the expression for the x coordinate of the mouse after movement.  In the documentation I find sf::Event::MouseMoveEvent::x
I entered it into my source code but it does not compile. Needs a minor change sf::Event::MouseMoveEvent().x
Now it compiles but guess what I get after execution.
The x coordinate of the origin of the window, not the mouse movement.
Oh well back to the drawing board.
Please excuse the sarcasm, just had to get that off my chest!!!!

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Mouse movement
« Reply #16 on: June 24, 2013, 12:35:19 am »
http://www.sfml-dev.org/tutorials/2.0/window-events.php#the-mousemoved-event
This piece of code obviously needs to be inside your event loop, as stated at the start of the tutorial or in Laurent last post.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Mouse movement
« Reply #17 on: June 24, 2013, 12:35:25 am »
In the documentation I find sf::Event::MouseMoveEvent::x
I entered it into my source code but it does not compile. Needs a minor change sf::Event::MouseMoveEvent().x
This is not how it works. Code in the documentation is not meant to be copy-pasted, it only shows you the API.

In C++, there is a difference between the scope operator :: and the member access operator . and it is very important to know it. I don't know why you are still using the try-and-error approach, after multiple people have already recommended to read the tutorial. Seriously, please just read it once, but carefully, and make sure you understand it. You wouldn't waste so much time asking questions you can easily answer yourself...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Mouse movement
« Reply #18 on: June 24, 2013, 08:05:13 am »
Ok, let me explain what's highlighted in my previous post.

Quote
any attempt to use a sf::Event without first a successful call to pollEvent (or waitEvent) will result in the same undefined behaviour

What does "a successful call to pollEvent" mean? A call which returns true -- I'm sure it's obvious, even for you (because it's written in the doc) ;)
Now, where do you use the event? Outside the event loop. And when does your loop exit? When pollEvent returns false.

So the conclusion is that your sf::Event is always invalid (undefined content) outside the event loop.

If it's not clear enough now, I give up.
Laurent Gomila - SFML developer

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Mouse movement
« Reply #19 on: June 24, 2013, 07:16:31 pm »
[/code

I thought it was inside?

#include <SFML/Graphics.hpp>

int main()
{
       // Create the main window
    sf::RenderWindow window(sf::VideoMode(2200, 1300), "SFML window");
 
    window.clear(sf::Color(128,128,128));
   
    sf::RectangleShape shape(sf::Vector2f(50,50));
    shape.setFillColor(sf::Color(255,0,0));
    shape.setPosition(100,650);
    window.draw(shape);
    window.display();

    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
             // Close window : exit
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }

             if((sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
                && (sf::Event::MouseMoved))
            {
                shape.setPosition(sf::Event::MouseMoveEvent().x,sf::Event::MouseMoveEvent().y);
                window.clear(sf::Color(128,128,128));
                window.draw(shape);
                window.display();
            }
         }
     }
    return EXIT_SUCCESS;
}
 

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Mouse movement
« Reply #20 on: June 24, 2013, 08:14:07 pm »
Quote
             if((sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
                && (sf::Event::MouseMoved))
            {
                shape.setPosition(sf::Event::MouseMoveEvent().x,sf::Event::MouseMoveEvent().y);
                window.clear(sf::Color(128,128,128));
                window.draw(shape);
                window.display();
            }
It doesn't look like what's in the tutorial, not even remotely. In this small part of code, your way to check the type of the event is wrong, and your way to get the positions of the mouse is also wrong.  And you don't want to put your clear/draw/display inside the event loop (if you want to know what a basic game loop is supposed to look like, read and understand the first part of the graphics tutorial).
Why didn't you take a look at the link I gave? It directly points to what you have to do to get mouse coordinates, and there's only 3 lines of code so it's easy even for copypasters.

Seriously, why does your code look like nothing you can find in the tutorial? Why don't you read them after everyone told you to? There isn't a tutorial where you can find "sf::Event::MouseMoveEvent().x".
« Last Edit: June 24, 2013, 08:16:27 pm by G. »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Mouse movement
« Reply #21 on: June 24, 2013, 08:55:30 pm »
Quote
I thought it was inside?
You never posted this code before.

Could you please explain why the hell you're not doing things as explained in the tutorial and keep posting questions about this?
Laurent Gomila - SFML developer

 

anything