I'll try to give you a really simplify version of the issue I am encountering. This is just to show you where is my problem :
// windowMain and windowAux are two sf::renderwindow created
while (windowMain.isOpen())
{
sf::Event event;
while (windowMain.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed: // closing using the cross button of the window
windowMain.close(); // close the main window and gets out of this loop
break;
case sf::Event::MouseButtonPressed: // clicking
createWindowAux(windowAux); // pops windowAux
windowAux.display();
break;
default:
break;
}
}
while (windowAux.pollEvent(event)) // loop on editing/reading attributes
{
switch (event.type)
{
case sf::Event::Closed:
windowAux.clear(COLOR_BACKGROUND_WINDOW);
windowAux.close();
break;
case sf::Event::LostFocus:
cout << "LOSTFOCUS" << endl;
break;
case sf::Event::GainedFocus :
windowAux.clear(COLOR_BACKGROUND_WINDOW);
// add objects here
windowAux.display();
cout << "GAINFOCUS" << endl;
break;
case sf::Event::Resized :
cout << "RESIZED" << endl;
break;
default:
break;
}
}
}
I just re-explain the problem I got (not so easy to explain sorry). By clicking on the mainWindow while the windowAux is opened, i do not get the event LOSTFOCUS in window2 (maybe I need to make 2 distincts event but I do not think so - events should be all equal at same time I suppose).
However, clicking on an other window - (not from SFML) as MicrosoftVisualStudios (for instance) - will give me the lostfocus event.
Also, this is the code for the creation of the window :
void createWindowForAttributes(sf::RenderWindow& window)
{
if (!window.isOpen()) // if not already opened, creation of the window
{
window.create(sf::VideoMode(WIDTH_WINDOW_ATTRIBUTE,
HEIGHT_WINDOW_ATTRIBUTE),
"Editing", sf::Style::Titlebar | sf::Style::Close);
}
window.setTitle("Editing");
window.setSize(sf::Vector2u(WIDTH_WINDOW_ATTRIBUTE,
HEIGHT_WINDOW_ATTRIBUTE));
window.clear(COLOR_BACKGROUND_WINDOW);
}
Thanks,
Tell me if you need anything else for the explanation,
Gabriel
Hello,
I managed to get exactly the same issue (wasn't so easy actually, I do believe there's a problem of mine somewhere - Well here is the minimal code :
#include <iostream>
#include <string>
using namespace std;
void createWindowAux(sf::RenderWindow& window)
{
if (!window.isOpen())
window.create(sf::VideoMode(400,400),
"2nd window", sf::Style::Titlebar | sf::Style::Close);
}
int main ()
{
sf ::RenderWindow windowMain;
windowMain.create(sf::VideoMode(700,800), "Simple Test");
sf::RenderWindow windowAux;
bool mIsComponentActive = false;
while (windowMain.isOpen())
{
sf::Event event;
while (windowMain.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed: // closing using the cross button of the window
windowMain.close(); // close the main window and gets out of this loop
break;
case sf::Event::MouseButtonPressed:
mIsComponentActive = false;
sf::Vector2i localPosition = sf::Mouse::getPosition(windowMain);
if (localPosition.y < 30)
{
mIsComponentActive = true;
createWindowAux(windowAux);
}
windowAux.display();
break;
}
}
while (windowAux.pollEvent(event))
{
if (mIsComponentActive)
{
switch (event.type)
{
case sf::Event::Closed: // closing using the cross button of the window
windowAux.close(); // close the main window and gets out of this loop
break;
case sf::Event::LostFocus:
cout << "LOSTFOCUS" << endl;
windowAux.clear(COLOR_BACKGROUND_WINDOW);
break;
case sf::Event::GainedFocus :
windowAux.clear(COLOR_BACKGROUND_WINDOW);
// put objects here
windowAux.display();
cout << "GAINFOCUS" << endl;
break;
}
}
}
}
return 0;
}
The sequence of events I am doing to get the 'bug' is the following one :
- first click on the top of the first window which displayed ("simple test" window)
You should have windowAux opened now - Then click on windowMain : you don't get 'LOSTFOCUS' in cout.
Moreover, if you try to open windowAux from the taskbar of window, you will only be able to minimize it (closing it will not work)
Now, do exactly the same things, but instead of clicking on windowMain to get back to this window, just click on another program opened : this time you get the even 'LOSTFOCUS'.
I hope it's clear enough. Again, the bug is not that easy to explain too, please ask me questions if you do not understand something I'm saying.
Thanks
Edit : I do see the bug... It comes from my boolean. Mmhh, I'll try to manage this ;-)