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

Author Topic: Mouse movement  (Read 6901 times)

0 Members and 2 Guests are viewing this topic.

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Mouse movement
« 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;
}

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Mouse movement
« Reply #1 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(). 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.

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Mouse movement
« Reply #2 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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Mouse movement
« Reply #3 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.
« Last Edit: June 20, 2013, 09:54:05 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Mouse movement
« Reply #4 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.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Mouse movement
« Reply #5 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.

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 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...  ::)
« Last Edit: June 21, 2013, 01:09:30 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Mouse movement
« Reply #6 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!!!!!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouse movement
« Reply #7 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.
Laurent Gomila - SFML developer

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Mouse movement
« Reply #8 on: June 21, 2013, 05:59:34 pm »
Thanks
 

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Mouse movement
« Reply #9 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++.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouse movement
« Reply #10 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?
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Mouse movement
« Reply #11 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Mouse movement
« Reply #12 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 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Mouse movement
« Reply #13 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! ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouse movement
« Reply #14 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;
    }
}
Laurent Gomila - SFML developer

 

anything