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

Author Topic: when my cursor goes out of window my red circle goes to top left corner  (Read 2039 times)

0 Members and 2 Guests are viewing this topic.

rajhansk

  • Newbie
  • *
  • Posts: 21
    • View Profile
when my cursor goes out of window my red circle goes to top left corner. Also when I click in the window the red circle goes to extreme left.

#include <SFML/Graphics.hpp>
#include<iostream>
using namespace std;
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Circle Cursor");
    sf::CircleShape circle(30);
    circle.setOrigin(30,30);
   
    circle.setFillColor(sf::Color::Red);

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

        switch(event.type)
        {
        case sf::Event::EventType::MouseMoved:

         cout<<event.mouseMove.x <<" : "<<event.mouseMove.y<<endl;

        }
        }
        //if(sf::Event::)
        window.clear();
        circle.setPosition(event.mouseMove.x, event.mouseMove.y);
        window.draw(circle);
        window.display();
    }

    return 0;
}
« Last Edit: September 04, 2017, 06:57:28 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: when my cursor goes out of window my red circle goes to top left corner
« Reply #1 on: September 04, 2017, 06:57:16 pm »
Please read this carefully:
https://www.sfml-dev.org/tutorials/2.4/window-events.php#the-sfevent-type

(and next time use [ code ] tags when you post code)
Laurent Gomila - SFML developer

rajhansk

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: when my cursor goes out of window my red circle goes to top left corner
« Reply #2 on: September 05, 2017, 06:47:25 am »
Thanks for the reply.

I read it carefully but I did not get how to implement it on my code..


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: when my cursor goes out of window my red circle goes to top left corner
« Reply #3 on: September 05, 2017, 07:56:17 am »
You cannot use events outside the event loop, this is undefined behaviour.
Laurent Gomila - SFML developer

rajhansk

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: when my cursor goes out of window my red circle goes to top left corner
« Reply #4 on: September 05, 2017, 09:29:24 am »
Thanks I just moved
This line
[circle.setPosition(event.mouseMove.x, event.mouseMove.y);]

and it is working like a charm..