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

Author Topic: Can't get position of mouse in window  (Read 3449 times)

0 Members and 1 Guest are viewing this topic.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Can't get position of mouse in window
« on: October 03, 2015, 02:07:14 pm »
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!

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Wow...
« Reply #1 on: October 03, 2015, 04:52:42 pm »
Quote
int x = sf::Mouse::getPosition(a.window).x;
Why not:
int x = sf::Mouse::getPosition(window).x;
??? ::) ???
« Last Edit: October 03, 2015, 04:54:46 pm by G. »

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Can't get position of mouse
« Reply #2 on: October 03, 2015, 07:43:57 pm »
 :o

You're my hero! I was stuck for three days on this, tried like one million things, besides this...

But, why is this working?
I thought that when you use a variable declared/defined in another class, you have to refer to that class using className[dot]VariableName?

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Can't get position of mouse in window
« Reply #3 on: October 03, 2015, 08:03:36 pm »
A::mousePosition is a method of A.
window is a member of A.
How is that a different class?  :o

And you also use window in A::show, it's exactly the same thing.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Can't get position of mouse in window
« Reply #4 on: October 03, 2015, 08:23:26 pm »
Oh yeah, that's true. Looked over that. Thanks a lot!

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Can't get position of mouse in window
« Reply #5 on: October 05, 2015, 12:36:09 am »
It also means that:
A a;
inside A::mouseposition() makes no sense  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*