XNA Creators Club Online
Page 1 of 1 (10 items)
Sort Posts: Previous Next

Telling is a window is active [not using windows forms]

Last post 3/15/2008 9:25 PM by therabidwombat. 9 replies.
  • 3/15/2008 4:36 AM

    Telling is a window is active [not using windows forms]

    Using the default XNA class, is there a way to determine if the window is currently active [for the purpose of not updating game information if it is not]? I'm sure the answer is dirt quick, but I tried using the Game.IsActive property and it didn't work, and I couldn't see anything else in the Game or Game.Window class that seemed particularly useful.
  • 3/15/2008 5:25 AM In reply to

    Re: Telling is a window is active [not using windows forms]

  • 3/15/2008 3:33 PM In reply to

    Re: Telling is a window is active [not using windows forms]

    I tried that, and as I said above, it didn't work.

    I guess I should be more clear. I want to know if the game is the topmost window, or if any other applications have the focus instead of the game ... not just if the game has been minimised or not.
  • 3/15/2008 3:55 PM In reply to

    Re: Telling is a window is active [not using windows forms]

    Oh whoops, sorry about that! Compelely missed that somehow. :/

    I remember trying to figure this out myself a bit back when I had my XNA window along with a few child windows, but ended up just packing it all into one window since I could not find a decent, built-in answer. Only thing I can think of is something from the Windows API like GetActiveWindow(). I used to use that back in my VB6 days and it worked great.
  • 3/15/2008 4:00 PM In reply to

    Re: Telling is a window is active [not using windows forms]

    Game.IsActive will tell you if your game is in focus. So if your game is not the topmost it will be false. Can you show the code where this doesn't work? I've tested the property quite a bit and I've never seen it not work (except back during the 2.0 beta, but they fixed it for release).

    For instance, create a new game project. Then remove the Clear command in the Draw method and replace it with this:

    if (IsActive)
      graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
    else
      graphics.GraphicsDevice.Clear(Color.Magenta);

    and you'll see when the window isn't the topmost, it will be pink and when it is in focus it will be blue. I just tested it so I do know this works.
  • 3/15/2008 4:12 PM In reply to

    Re: Telling is a window is active [not using windows forms]

    In my update loop:

    if(IsActive)
    {
    engine.Update();
    if(null != currentApplication)
    currentApplication.Update(gameTime);
    }//if(IsActive)



    but when I alt tab to another window, then alt tab back in...game logic
    has still occurred, and the state of the game is not the same.

    EDIT: I did add it to my draw loop, however, and the game DOES pause and turn pink when it loses focus...I'm going to look into it and see if I'm updating in the wrong place somewhere.
  • 3/15/2008 4:19 PM In reply to

    Re: Telling is a window is active [not using windows forms]

    (Would've got this posted quite a bit ago if the site wasn't misbehavin')

    The problem arises when you have child forms coming from the XNA form, too. For example:

            protected override void Initialize()
    {
    System.Windows.Forms.Form f = new System.Windows.Forms.Form();
    f.Show();
    base.Initialize();
    }
  • 3/15/2008 4:20 PM In reply to

    Re: Telling is a window is active [not using windows forms]

    There must be something in your engine.Update or currentApplication.Update that's causing this. Again, if you check that code I posted, the game definitely sets IsActive to false if you are not the topmost window. So there has to be some logic in your game that is causing it to recover that lost time. Perhaps some of your code not in that loop is updating things. Or perhaps you just perceive that it is still going when it really isn't (if you alt-tab away and alt-tab back really quick the motion begins automatically and it might look like it was running the whole time). Any number of things could contribute; I'm merely saying that the IsActive property is exactly what you should be using for this because it does work.

    Try this. Open your game and open some other window (like Explorer or something). Give focus to the other window and drag it off the game to see if your game is still going. That's pretty much going to show you if you have it programmed in correctly.
  • 3/15/2008 4:24 PM In reply to

    Re: Telling is a window is active [not using windows forms]

    "but when I alt tab to another window, then alt tab back in...game logic has still occurred, and the state of the game is not the same."

    How are you updating? Even if it doesn't have focus, and if you are not running the updates, time will elapse. If you are calculating based on the delta time since last update, this will not work - all you will be doing is not updating for a while then updating a huge time lapse. So make sure you are using the Elapsed time.
  • 3/15/2008 9:25 PM In reply to

    Re: Telling is a window is active [not using windows forms]

    I guess that somehow the text in my last post didn't really show up for some reason...anyways, it turns out that I had some code in my draw loop updating [something as simple as a misplaced line of code can sure cause a lot of trouble!], and since I wanted it to still draw when the game did not have focus, just not update, this caused me to believe that the game was still updating even when it didn't have focus. Problem is now solved. Thanks for all the help.
Page 1 of 1 (10 items) Previous Next