Thread: DirectX and Window Focus

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    28

    DirectX and Window Focus

    Hi, im not sure if i should post this in here or the Windows section.
    If its in the wrong place could a mod please move it, thanks.


    Im making myself a game and everythings going fine.

    Its a windowed game set to maximized.
    I was getting crashes when focusing onto another app while my game was still running. So i had a search about and found out about the WM_KILLFOCUS and WM_SETFOCUS messages sent when your app gets, or loses focus. So that has stopped the crashing!

    BUT!!

    If i switch to another app thats not maximized and move it around over the top of my game, the game isn't being drawn anymore so the screen gets all messed up.

    Is there a certain message that windows sends to say the focus is lost COMPLETELY? So i can still draw my game if its still seen, but not in focus.

    Hope that makes sense.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    while (m_gameActive)
    {
        HRESULT hr = pDevice->TestCooperativeLevel();
    
        if (hr == D3D_OK)
        {
           Render();
        }
        else if (hr == D3DERR_DEVICELOST)
        {
           OnLostDevice();
        }
        else if (hr == D3DERR_DEVICENOTRESET)
        {
           OnResetDevice();
        }
    }
    Things you lose in lost device conditions:
    • ID3DXFont - you must call it's On[Lost/Reset]Device functions.
    • All resources using the D3DPOOL_DEFAULT memory pool. These must be recreated. Render target textures fall into this category since they must use D3DPOOL_DEFAULT. But this applies to all resources in Direct3D
    • All IDirect3DStateBlock(s) will be released by the device (no need to call release()). These must be-recreated inside of OnResetDevice().
    • ID3DXEffect(s) - must call their associated On[Lost/Reset]Device functions
    • DirectInput keyboard - May have to re-acquire the keyboard
    • If you are using CD3DFont you must call it's associated On[Lost/ResetDevice] functions
    • Any swap chains created with CreateAdditionalSwapChain()


    If most of your resources use D3DPOOL_MANAGED (which they should) it's not that difficult to put into your system. As an example my system can both respond to a lost device and come back as well as switch from windowed mode to full-screen mode and back on the fly.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    Thanks very much Bubba!
    That will be easy to implement into my game, i'll have a go and report back.

    Thanks again.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    Right..

    Now i've removed the WM_KILLFOCUS, WM_SETFOCUS code and used what you recommended.
    The game now renders underneath other windows fine!
    But now, when i completely switch windows (my game isnt seen at all) i get the crash again... It sometimes reboots my computer, and sometimes the gfx driver crashes and goes to low res/color depth.

    I was reading the KILLFOCUS/SETFOCUS on msdn and thats a keyboard focus.
    Is there anyway to tell if the Window is not in focus but still seen? (so i don't render)


    EDIT: Found an error in debug log that i missed. After calling TestCooperativeLevel()
    Last edited by Marc; 12-05-2009 at 05:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't get window to show
    By GlitchGuy2 in forum Game Programming
    Replies: 2
    Last Post: 06-02-2009, 06:15 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Make My Window Invisible for a Split Second
    By Brad0407 in forum Windows Programming
    Replies: 9
    Last Post: 04-10-2007, 05:58 PM
  4. Problems with my DirectX program
    By fighter92 in forum Game Programming
    Replies: 1
    Last Post: 01-23-2007, 06:28 PM
  5. Replies: 12
    Last Post: 03-27-2006, 07:03 AM