I thought I had already written a response to this, but I guess I closed my browser or something before posting.
Anyways, the problem is this:
There is simply no way to determine whether your app has focus on start up.
Actually, after writing a small test program, I would say the LostFocus/GainedFocus events are a little buggy.
I shot a quick video to show you:
http://video.google.com/videoplay?docid=4311287987679040491&hl=enThe app should not react this way.
The problem illustrated in the video is:
-Somebody starts an SFML app
-They go to another window before the app can create its window
-They switch back to the SFML app
-The app does not receive a GainedFocus event notification until the user switches away from, and back to, the window.
Really, this is all beside my original point.
It would just be much more reliable to add a direct haveFocus() method rather than making assumptions on whether your app starts with focus.
Here is the test code:
#include <SFML/Graphics.hpp>
int main(int argc, char *argv[])
{
sf::RenderWindow Window;
bool Done = false;
sf::Event Event;
sf::Font Font;
sf::String String;
Font.LoadFromFile("Font.ttf", 32);
String.SetFont(Font);
Window.Create(sf::VideoMode(800, 600, 32), "sfmlFocus", sf::Style::Close);
while (!Done)
{
while (Window.GetEvent(Event))
{
switch (Event.Type)
{
case sf::Event::Closed:
Done = true;
break;
case sf::Event::KeyPressed:
if (Event.Key.Code == sf::Key::Escape)
Done = true;
break;
case sf::Event::GainedFocus:
String.SetText("Have focus");
break;
case sf::Event::LostFocus:
String.SetText("Do not have focus");
break;
}
}
Window.Draw(String);
Window.Display();
}
}