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

Author Topic: Handling Mouse input  (Read 2044 times)

0 Members and 1 Guest are viewing this topic.

Schecker

  • Newbie
  • *
  • Posts: 4
    • View Profile
Handling Mouse input
« on: January 17, 2013, 03:14:59 pm »
Hello, I'm kinda new to SFML. I started with handling mouse inputs today and found this little problem:

When you click the left button on your mouse, hello will be drawn centrally to your mouse position. But if you maximze the window and click somewhere again hello is not central to your mouse position. Width seems to be central. Just the height between mouse and hello has changed.
Can someone explain this please?

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <time.h>
#include <sstream>

using namespace std;
using namespace sf;

int main()
{
    VideoMode   videoMode(1024,768);
    RenderWindow window(videoMode,"Handling Mouse Input");

    Font font;
    if (!font.loadFromFile("Fonts/comic.ttf"))
        return EXIT_FAILURE;

    Text title("Handling Mouse Input",font,48);
    FloatRect rect = title.getGlobalBounds();
    title.setPosition((1024-rect.width)/2,10);
    title.setColor(Color::Blue);

    Text hello("HELLO",font,24);
    FloatRect rect2 = hello.getGlobalBounds();
    hello.setColor(Color::Red);

    bool clicked=false;

    while(window.isOpen())
    {
        window.clear();
        window.draw(title);
        if(clicked)
        {
            window.draw(hello);
        }
        window.display();

        if(Mouse::isButtonPressed(Mouse::Left))
        {
                Vector2i mouse = Mouse::getPosition();
                hello.setPosition(mouse.x-(rect2.width/2),mouse.y-(rect2.height*2));
                clicked=true;
        }

        Event event;
        while(window.pollEvent(event))
        {
            if( (event.type == Event::Closed) ||
               ((event.type == Event::KeyPressed) && (event.key.code==Keyboard::Escape)) )
                    window.close();

        }
    }
    return EXIT_SUCCESS;
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Handling Mouse input
« Reply #1 on: January 17, 2013, 03:21:39 pm »
Two mistakes:

1. You must call Mouse::getPosition(window), not Mouse::getPosition().

2. You must convert the mouse position, which is in pixels, to a world position; both are different after resizing because the view hasn't changed (but the window did). With SFML 2.0 RC the function to call is RenderWindow::convertCoords, with recent sources it's RenderWindow::mapPixelToCoords.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10828
    • View Profile
    • development blog
    • Email
Re: Handling Mouse input
« Reply #2 on: January 17, 2013, 03:25:45 pm »
It's because your sf::View of the window stays the same, thus although you get actually more pixel on the window the theoretical space is still the same.
For example you have say 100px on your screen that represent a length of 1 unit; then you resize the window and get 200px on your screen, but the representation of those 200px is still this 1 unit, you've only increased the resolution (can also be considered as zooming in).
To get an intuition on what the sf::View is and how it works you could take a look at my tutorial: Using sf::View

As for the problem you basically have to call convertCoords() (or with the latest version from Git mapPixelToCoords()) and pass the mouse coordinates, so you get the actual coordinate position instead of the 'pixel' position.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Schecker

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Handling Mouse input
« Reply #3 on: January 17, 2013, 03:31:55 pm »
Thanks alot!
I just thought that nothing would change, becauce the window solution of my notebook is 1024x768.
I don't get how to use this converting stuff. Just mapPixelToCoords(mouse) ???
Well i think i take a look on your tut and on the documentation.
Thanks again.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10828
    • View Profile
    • development blog
    • Email
Re: Handling Mouse input
« Reply #4 on: January 17, 2013, 03:36:50 pm »
I don't get how to use this converting stuff. Just mapPixelToCoords(mouse) ???
Yes, window.mapPixelToCoords(sf::Mouse::getPosition(window)))
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything