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

Author Topic: Mouse position relative to games window in SFML 2?  (Read 39410 times)

0 Members and 1 Guest are viewing this topic.

Ashen1900

  • Newbie
  • *
  • Posts: 1
    • View Profile
Mouse position relative to games window in SFML 2?
« on: August 30, 2011, 01:15:00 pm »
Hi

I've been following a tutorial for SFML http://redkiing.wordpress.com/category/games/sfml/ and have a problem. Although I got the game running with minimal changes for 2.0, I can't find how to get the mouse position relative to the games window rather the current desktop position.

These are the parts where i get the mouse click position. This part is changed from the tutorial due to Input getting scrapped in 2.0.

main.cpp
Code: [Select]


else if (Event.Type == Event.MouseButtonReleased && Event.MouseButton.Button == sf::Mouse::Left)
ui.placeInColumn(sf::Mouse());


uInterface.cpp
Code: [Select]


sf::Vector2f MousePosition(GetMousePosition.GetPosition(sf::RenderWindow &Game));

    column = float(MousePosition.x)/board.getWindowWidth()*BOARD_X_SIZE+1;



Hoping someone can give me a clue as to how to solve this!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Mouse position relative to games window in SFML 2?
« Reply #1 on: August 30, 2011, 01:21:40 pm »
You should read the online doc, everything is explained.

- If you have a sf::MouseButton* event then the mouse position relative to the window are simply the X and Y members of the event.

- If you want to get the mouse position with the new sf::Mouse class, use sf::Mouse::GetPosition(the_window).
Laurent Gomila - SFML developer

fg109

  • Guest
Mouse position relative to games window in SFML 2?
« Reply #2 on: November 04, 2011, 04:12:30 pm »
I found that using sf::Event::MouseButton.X and sf::Event::MouseButton.Y does not give the correct mouse position.  The sf::Mouse class does though.

Code: [Select]
#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;

int main()
{
    sf::RenderWindow TestWindow(sf::VideoMode(800, 600, 32), "Mouse Cursor Position Test");

    while (TestWindow.IsOpened())
    {
        sf::Event TestEvent;
        while(TestWindow.PollEvent(TestEvent))
        {
            if (TestEvent.Type == sf::Event::Closed)
                TestWindow.Close();

            if (TestEvent.Type == sf::Event::MouseMoved)
            {
                cout << "Method 1: (" << TestEvent.MouseButton.X << ", " << TestEvent.MouseButton.Y << ")\n";
                cout << "Method 2: (" << sf::Mouse::GetPosition(TestWindow).x << ", " << sf::Mouse::GetPosition(TestWindow).y << ")\n\n";
            }
        }
        TestWindow.Clear();
        TestWindow.Display();
    }
}

 

anything