Hello,
I've got a small problem, I need to get the position of the mouse to check if a certain button is hovered (and clicked if
sf::Mouse::isButtonPressed(sf::Mouse::Left) is true). But the problem is, that my code has a pretty complex structure.
From the main function, I launch a function (deviation), the meaning of this function is a login-box. If the login was successful (in this example it isn't checked) thread show (which shows the whole GUI), and mousePosition (which lifetime gets the position of the cursor).
The actual problem now is, that mousePosition can't get the defined values of 'window'. So the position always is (0,0). It is working if I just make a new window in the mousePosition function, but that isn't the wanted solution because then I don't have the definitions (colours, other buttons,...) and I'd have to change 80% of my code (variable places, classes,...) which have cost me a lot of hours.
This is an exact representation of my code (with lot of parts leaved out because they don't have anything to do with this problem).
Code:#include <iostream>
#include <thread>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
using namespace std;
class A {
public:
sf::RenderWindow window;
void show();
int mousePosition();
};
void A::show() {
cout << "show Successfully launched!" <<endl;
window.create(sf::VideoMode(800,600), "Title");
while(window.isOpen()) {
sf::Event windowEvent;
while(window.pollEvent(windowEvent)) {
if(windowEvent.type == sf::Event::Closed) {
window.close();
}
window.clear(sf::Color::Black);
window.display();
}
}
}
int A::mousePosition() {
A a;
//Not the wanted solution:
//sf::RenderWindow window(sf::VideoMode(800, 600), "Title");
cout << "mousePosition Successfully launched!" <<endl;
while (true) {
//ERROR HERE: How to just get position of mouse relative to the window declared in class A, and defined in function show() ??
int x = sf::Mouse::getPosition(a.window).x;
int y = sf::Mouse::getPosition(a.window).y;
cout << "x: " << x << endl << "y: " << y << endl << endl;
}
return 0;
}
class B {
public:
void deviation() {
A a;
thread show(&A::show, &a);
thread mousePosition(&A::mousePosition, &a);
mousePosition.join();
show.join();
}
};
int main() {
A a;
B b;
b.deviation();
}
There is also some explanation in the code comments.
To avoid mistakes, I don't get any compiling errors. I probably just made a logical error.
Thanks a lot for reading and helping!