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.