SFML community forums
Help => Graphics => Topic started by: Ashen1900 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
else if (Event.Type == Event.MouseButtonReleased && Event.MouseButton.Button == sf::Mouse::Left)
ui.placeInColumn(sf::Mouse());
uInterface.cpp
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!
-
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).
-
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.
#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();
}
}