1

Question

SFML windows implement the method hasFocus() as a convenient way of checking whether the window has focus or is a background window.

It seems strange to me that this method does not appear to be implemented for sf::RenderWindow's, particularly as the following code compiles, but does not link. (Perhaps this is a bug or an oversight of the developer? If so it might be an idea to implement this in the next set of bug fixes.)

Code example:

sf::RenderWindow window;

while(window.isOpen())
{

    if(window.hasFocus())
    {
        // do something
    }

    window.clear();
    // etc drawing code
}

This compiles, using: g++ --std=c++11 main.cpp -o ratwatch -lsfml-graphics -lsfml-window -lsfml-system

but unfortunately will not link, with the following error:

/tmp/ccWhfqtT.o: In function `main':
main.cpp:(.text+0xc3b): undefined reference to `sf::Window::hasFocus() const'
collect2: error: ld returned 1 exit status

Have I made a mistake with my linking libraries? Did I miss another -lsfml...?

Workaround

I managed to do what I was trying to do wit the following code, but this is clearly a messy and unnecessarily convoluted workaround:

sf::RenderWindow window(sf::VideoMode(800, 600), "text");
bool WINDOW_HAS_FOCUS = false;

while(window.isOpen())
{

    sf::Event event;
    while(window.pollEvent(event))
    {

        if(event.type == sf::Event::LostFocus)
        {
            WINDOW_HAS_FOCUS = false;
        }
        else if(event.type == sf::Event::GainedFocus)
        {
            WINDOW_HAS_FOCUS = true;
        }

    }

    // ... later in program ...

    if(WINDOW_HAS_FOCUS)
    {
        // do something
    }

    window.clear();
    // etc drawing code
}

Hope that's helpful to anyone who wasn't able to do what I was trying to, if a solution to my original question cannot be found.

Community
  • 1
  • 1
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • Have you looked at: http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix? – NathanOliver Aug 17 '15 at 13:04
  • This compiles fine for me, do you have the latest SFML? Are you using the right version of the library for your gcc version? Maybe if you are using an IDE you could share your project settings so we can see if you have done anything wrong? – tom Aug 17 '15 at 13:04
  • @username_unavailable I am using version 2.1 ? Which version do I require? I compile with g++, terminal. – FreelanceConsultant Aug 17 '15 at 17:24
  • Ah, i will look into the possibility of upgrading to that - 2.1 is latest in ubuntu repos – FreelanceConsultant Aug 17 '15 at 19:41

1 Answers1

2

The functionality you request is already available in the latest version of SFML.

void requestFocus(); 
// to give the window the focus

bool hasFocus() const; 
// to check whether the window is currently focused

As issue for this request was opened on GitHub here.

The request was merged into master with this request.

This is the final pull request.


The error you're encountering is probably caused by a version mismatch between your SFML headers and your compiled SFML binaries. I suggest you to make sure your SFML binaries are updated or recompile SFML.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416