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

Author Topic: OS Active window  (Read 23074 times)

0 Members and 1 Guest are viewing this topic.

dewyatt

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
    • http://dewyatt.blogspot.com
OS Active window
« on: June 29, 2008, 11:26:41 pm »
I think this has been talked about before a little.

What I would like is a function in sf::Window to tell me whether the window is the active window or not.
Not the active sf::Window, but the active OS window.

I know you can use the LostFocus/GainedFocus events but these don't seem to be reliable.
Right now, I keep a variable mHaveFocus.
It starts out as true, we have to ASSUME we are the active OS window.
I don't like to make that assumption because it isn't always true.
I really don't even like to have this variable.
I feel sf::Window should handle it.

I change this variable when I get Lost/Gained focus events.
I then use the variable to determine whether I should draw anything or just handle events and sleep() a bit.

I don't know what you'd call it though, since there's already isActive().
Anyways, let me know if this would be possible.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
OS Active window
« Reply #1 on: June 30, 2008, 03:04:40 am »
Hi

What's exactly the problem with the focus event ? It should be perfect for this kind of feature.
Laurent Gomila - SFML developer

dewyatt

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
    • http://dewyatt.blogspot.com
OS Active window
« Reply #2 on: June 30, 2008, 09:34:21 pm »
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=en
The 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:
Code: [Select]

#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();
}
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
OS Active window
« Reply #3 on: July 01, 2008, 02:53:10 am »
You're right, there's no way to know if the window has focus when it's created. However, as focus is already handled through events, I feel like it would be better to use the existing system rather than providing a new function. What do you think of sending a focus event right after window creation, to tell the user if the window initiallly has the focus or not ?
Laurent Gomila - SFML developer

dewyatt

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
    • http://dewyatt.blogspot.com
OS Active window
« Reply #4 on: July 01, 2008, 11:47:01 am »
Sounds good to me!
It's best not to change design or things will get all messy.

 

anything