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

Author Topic: event.mouseMove.x always 0?  (Read 3049 times)

0 Members and 1 Guest are viewing this topic.

Azurium

  • Newbie
  • *
  • Posts: 2
    • View Profile
event.mouseMove.x always 0?
« on: March 28, 2013, 10:45:29 pm »
Hello everyone! I'm new to SFML - I tried to learn Allegro in the past but I lost interest. Nowadays my curiosity for programming has been reignited and I've been in search for a graphics library. After going through Allegro, SDL and SFML, I finally settled for SFML ( at least for now).

I once saw an interesting animation effect in Processing. It's called easing. It simulates a decelerated motion towards a point. I tried to adapt this to my first program in SFML, as I'm learning new features.

The problem? event.mouseMove.x always seems to return 0, so the rectangle sticks to the left side of the screen! Also, if that wasn't enough, it seems that event.mouseMove.y returns the y position! (I've added the 'cout' statements to watch the behavior).

Here's the code:

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
 
int main()
{
    float x = 200; float y = 200; float easing = 0.01; float currentPosX = x+10, currentPosY = y+10;
    bool mousePressed = 0; float dx =2 , dy= 2; // variables used for the easing part of the code
    sf::RectangleShape rectangl; rectangl.setSize(sf::Vector2f(100,100)); rectangl.setOutlineColor(sf::Color::Yellow); rectangl.setOutlineThickness(4); rectangl.setPosition(x, y);
    rectangl.setFillColor(sf::Color::Black);
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
 
    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
                break;
 
                case sf::Event::MouseButtonPressed:
                mousePressed = 1;
                                currentPosX = event.mouseMove.x; std::cout<<" IN CASE mouseMove.x: "<<event.mouseMove.x<<"\n";
                currentPosY = event.mouseMove.y;
                break;
 
                case sf::Event::KeyPressed:
                if(event.key.code == sf::Keyboard::S)
                {
                    x+=5; y+=5;
                    rectangl.setPosition( x, y );
                } // verifying if setting a new position works
                break;
            }
                }
 
            if(mousePressed || (abs(dx) > 1) || (abs(dy) > 1)) // if the mouse moved, apply the following "easing" animation ( smooth, decelerated movement)
            {
               // if(mousePressed)  // <-- seems like I don't need this condition( I don't have to get the mouse position every loop, so I moved these in the switch block)
            //    {
                 //   currentPosX = event.mouseMove.x;
                 //   currentPosY = event.mouseMove.y;
              //  }
 
                std::cout<<"Clicked at X: "<<currentPosX<<", Clicked at Y: "<<currentPosY<<" \n";
                                std::cout<<"Current X: "<<x<<", Current Y: "<<x<<" \n";
                                system("CLS");
                dx = currentPosX - x;
               
                if ( abs(dx)>1)
                {
                    x += easing*dx;
                }
 
                dy = currentPosY - y;
                if ( abs(dy)>1)
                {
                    y += easing*dy;
                }
 
                /*if((abs(dx)<1)&&(abs(dx)<1))*/
                mousePressed = 0;
            }
            rectangl.setPosition(x, y);
       
        window.draw(rectangl);
 
        window.display();
 
        window.clear();
    }
    return 0;
}


"A computer once beat me at chess, but it was no match for me at kick boxing." - Emo Philips

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: event.mouseMove.x always 0?
« Reply #1 on: March 28, 2013, 11:03:52 pm »
If the type is MouseButtonPressed then mouseButton.x and .y have position. mouseMove.x .y is for MouseMoved type.
Back to C++ gamedev with SFML in May 2023

Azurium

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: event.mouseMove.x always 0?
« Reply #2 on: March 28, 2013, 11:15:07 pm »
Thanks! That solved the problem.  But I must say, it's a bit confusing to me the fact that you need separate classes for getting a mouse position.
"A computer once beat me at chess, but it was no match for me at kick boxing." - Emo Philips

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: event.mouseMove.x always 0?
« Reply #3 on: March 29, 2013, 12:43:45 am »
You might want to check out the official tutorial. Makes things easier to understand. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/