SFML community forums

Help => Graphics => Topic started by: wvtrammell on June 20, 2013, 07:05:43 pm

Title: Mouse movement
Post by: wvtrammell on June 20, 2013, 07:05:43 pm
When I run the following, the buffer0 prints out the movement of the mouse in the Y direction and the buffer1 prints a constant.  When I stop mouse movement and press it's button, buffer0 prints the current X location and buffer1 prints the current Y location.  Can someone please tell me what I am doing incorrectly?  Thanks a million.
Warren Trammell

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

int main()
{
    char buffer0[25],buffer1[25];

    sf::RenderWindow window(sf::VideoMode(2300, 1300), "SFML window");
   
    sf::Font font;
    if (!font.loadFromFile("/users/warren/programming/projects/impact.ttf"))        return EXIT_FAILURE;
   
    sf::Text text;
    text.setFont(font);
    text.setCharacterSize(50);
    text.setColor(sf::Color::Red);
   
    window.clear(sf::Color(128,128,128));
       
    window.display();

    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
           switch (event.type)
            {
                case sf::Event::Closed:
                    window.close();
                    break;
            }
                 
 
        }
       
        window.clear(sf::Color(128,128,128));
       
        sprintf(buffer0,"%i",event.mouseWheel.x);
        sprintf(buffer1,"%i",event.mouseWheel.y);
       
        text.setString(buffer0);
        text.setPosition(500,600);
        window.draw(text);
       
        text.setString(buffer1);
        text.setPosition(1000,600);
        window.draw(text);
       
        window.display();
       }
      return EXIT_SUCCESS;
}
Title: Re: Mouse movement
Post by: fallahn on June 20, 2013, 09:28:51 pm
You don't say what you're trying to do, so I am assuming you want to get a realtime update of the mouse cursor position, in which case you should look at sf::Mouse::getPosition() (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Mouse.php). The mouseWheel event is designed to be handled while you poll for events, and contains the coordinates of the cursor when a mouse wheel event is received, ie when the wheel is scrolled or clicked.
Title: Re: Mouse movement
Post by: wvtrammell on June 20, 2013, 09:44:56 pm
My goal is to draw a circle on the screen, pick it up with the mouse and move it to a new location. in the process I was attempting to learn something about mouse movements.
Thanks
Warren
Title: Re: Mouse movement
Post by: Nexus on June 20, 2013, 09:52:25 pm
With 54 posts, you should meanwhile know that that you are supposed to use [code=cpp] tags... ;)

Why mouse wheel? I thought you wanted the cursor movement. The sf::Event object is not guaranteed to be valid outside the loop function. Please read the SFML tutorials and documentation to see how event polling works.
Title: Re: Mouse movement
Post by: wvtrammell on June 21, 2013, 12:32:35 am
Mr Nexus
I am sorry but everyone is not as smart as you.  I have no idea what [code = cpp] tags even are much less how to create them.  As for the tutorials & documentation I have studied them till I am blue in the face and know no more than when I started.  If I was an experienced programmer I might understand it but it is just a hobby with me and I am doing the best I can.
Thanks for your words of encouragement.
Warren Trammell
P. S. If you read all my posts you surely know I will be 90 years young in October and all my brain cells may not be firing all the time.
Title: Re: Mouse movement
Post by: zsbzsb on June 21, 2013, 01:03:36 am
Quote
I am sorry but everyone is not as smart as you.  I have no idea what [code = cpp] tags even are much less how to create them.

No one needs to be smart to learn to lookup something you have been asked to do multiple times such as a simple forum tag. Anyways apparently you do know what the code tag is considering you used it here (http://en.sfml-dev.org/forums/index.php?topic=11648.msg81027#msg81027).

Quote
As for the tutorials & documentation I have studied them till I am blue in the face and know no more than when I started.

If you would read around the forums some you would see in this thread (http://en.sfml-dev.org/forums/index.php?topic=11925.0) exactly what you are doing wrong.

For starters declare your sf::Event in your event loop - not outside and then your problem when your accessing an invalid variable will show up and not allow you to compile your code.


Quote
P. S. If you read all my posts you surely know I will be 90 years young in October and all my brain cells may not be firing all the time.

I have some very good reasons to doubt this from my personal experience, but who knows...  ::)
Title: Re: Mouse movement
Post by: wvtrammell on June 21, 2013, 02:06:27 am
If I could find and understand the answers to my questions in the tutorials or documentation or by reading the threads, I would not be bothering you nice people for help!!!!!
Title: Re: Mouse movement
Post by: Laurent on June 21, 2013, 07:57:02 am
Please read the events tutorial. It shows how to write a correct event loop, and highlights potential mistakes in red.

The formatting options can be found above the text field when writing a post. The language combo box is on the right side.
Title: Re: Mouse movement
Post by: wvtrammell on June 21, 2013, 05:59:34 pm
Thanks
 
Title: Re: Mouse movement
Post by: wvtrammell on June 21, 2013, 06:37:28 pm
Perhaps some day some one will write a book "SFML for Idiots". When it happens, I will be the first purchaser and then perhaps I will be able to learn SFML programming as I am trying to learn C++.
Title: Re: Mouse movement
Post by: Laurent on June 21, 2013, 06:43:36 pm
Have you read the events tutorial? Did you manage to improve or fix your code? Or are you still stuck with the same code as in your first post?
Title: Re: Mouse movement
Post by: Nexus on June 21, 2013, 06:44:01 pm
vwtrammel, please don't take things personally ;)

You're not stupid or anything, it's just that you have to read tutorials to know how things work. Everybody does. Same with forum rules and code tags -- it's not us who try to harass you, but it helps you to get more and better answers if you conform to them.
Title: Re: Mouse movement
Post by: wvtrammell on June 22, 2013, 09:14:42 pm
When one is not an experienced programmer, tutorials  are not always the answer.  For instance to install SFML the tutorial states "copy the contents of lib to /usr/local/lib"  simple enough yes?.  But The MAC finder does not know that directory so how does one accomplish the task?  He must be familiar with Terminal and other such things.
Same thing applies with some of the other tutorials.
Warren 
Title: Re: Mouse movement
Post by: eXpl0it3r on June 23, 2013, 12:10:25 am
When one is not an experienced programmer, tutorials  are not always the answer.  For instance to install SFML the tutorial states "copy the contents of lib to /usr/local/lib"  simple enough yes?.  But The MAC finder does not know that directory so how does one accomplish the task?  He must be familiar with Terminal and other such things.
Same thing applies with some of the other tutorials.
Programming requires you to have some "basic" knowledge in your OS, compiler and programming language. Thus if you want to use a certain library while programming, it's not the job of that library creator to explain every single detail about every single OS/compiler layout. SFML goes as far as giving multiple guides for multiple platforms and compilers, where other libraries won't even provide a makefile for anything else than their internal compiler version.
If you don't understand how to use the power tools on your Mac or how libraries work there, you might want to grab a book or tutorial specific for those subjects. ;)
I know it's annoying, but it's just how things work. You can solve problem A with the instructions for problem B. :)

As far as I see, we've done everything possible to help you. The only thing we could do more, but won't, is to write the code for you. You're here because you want to program, thus you should also be able to figure out such problems, that is one of the most important skills one has to learn while programming! ;)
Title: Re: Mouse movement
Post by: Laurent on June 23, 2013, 09:01:47 am
Quote
Same thing applies with some of the other tutorials.
But not with your specific problem.

There are examples of event loops everywhere (tutorials, documentation, SDK examples, forum posts, wiki), and none of them makes the same mistake as you. So I'm 100% sure that you can figure out what's wrong in your code by looking at these examples. And in case you don't, your mistake is explicitely mentioned in the events tutorial.

Quote from: Tutorial
sf::Event instances are filled by the pollEvent (or waitEvent) function of the sf::Window class. Only these two functions can produce valid events, any attempt to use a sf::Event without first a successful call to pollEvent (or waitEvent) will result in the same undefined behaviour that I mentioned above.

To be clear, here is what a typical event loop looks like:

sf::Event event;

// while there are pending events...
while (window.pollEvent(event))
{
    // check the type of the event...
    switch (event.type)
    {
        // window closed
        case sf::Event::Closed:
            window.close();
            break;

        // key pressed
        case sf::Event::KeyPressed:
            ...
            break;

        // we don't process other types of events
        default:
            break;
    }
}
Title: Re: Mouse movement
Post by: wvtrammell 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!!!!
Title: Re: Mouse movement
Post by: G. 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.
Title: Re: Mouse movement
Post by: Nexus 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...
Title: Re: Mouse movement
Post by: Laurent 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.
Title: Re: Mouse movement
Post by: wvtrammell 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;
}
 
Title: Re: Mouse movement
Post by: G. 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 (http://www.sfml-dev.org/tutorials/2.0/graphics-draw.php#the-drawing-window)).
Why didn't you take a look at the link I gave (http://www.sfml-dev.org/tutorials/2.0/window-events.php#the-mousemoved-event)? 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".
Title: Re: Mouse movement
Post by: Laurent 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?