Thread: DirectX - Handling Lost Device

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    DirectX - Handling Lost Device

    On my DirectX programs, everytime i alt-tab (in fullscreen), my desktop looks distorted and there are pieces of my program hanging about, and when i try to alt-tab back into the program, nothing happens.

    So i found a tutorial on a site, and added a Lost Device handler (or whatever its called):

    Code:
    bool ValidateDevice()
    {
    	HRESULT hResult = NULL;
    
    	hResult = g_Device->TestCooperativeLevel();
    
    	if (FAILED(hResult))
    	{
    		if (hResult == D3DERR_DEVICELOST)
    			return false;
    
    		if (hResult == D3DERR_DEVICENOTRESET)
    		{
    			if (FAILED(g_Device->Reset(&g_PresentParameters)))
    			{
    				DestroyWindow(hWnd);
    				return false;
    			}
    		}
    	}
    
    	return true;
    }
    and heres the implementation in the Render Function:

    Code:
    bool Render()
    {
    	if (!ValidateDevice())
    		return false;
    
    	g_Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    
    	g_Device->BeginScene();
    
    	//.....
    
    	g_Device->EndScene();
    
    	g_Device->Present(NULL, NULL, NULL, NULL);
    
    	return true;
    }
    Using the ValidateDevice function, i can alt-tab out of my program fine, i dont see any 'visual defects', but when i try to alt-tab back into the program, it just exits by itself.

    Theres little/no error handling, but im not worried about that for the time being.

    PaYnE

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I don't know about the program just exiting, that is strange. Do you have an error logging class? This would be ideal to track down the problem. All your textures are probably flushed when you alt-tab out. You will need to re-create them unless you put them in system memory when you created them. Lot's of things are lost when you lose focus. Sounds like you have another problem though if you program is exiting like that. Try and through in some error reporting and see if you can't track it down. That, or buy a second monitor.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    I used d3dx_managed for the textures, so i guess im ok with that? and about the error logging class, it doesnt seem to show me any errors, i alt tab, then when i try to go back in it just closes, probably because i set it so that when something goes wrong it should just exit

    but is there anything wrong with the function i posted? i used that before every render loop to make sure itll reset properly, ive never done this before so im not sure if it will even work

    PaYnE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectInput Device Access (DirectX 10)
    By DarkMasterBosel in forum Game Programming
    Replies: 0
    Last Post: 12-27-2008, 11:41 PM
  2. DirectSound header issues
    By dxfoo in forum C++ Programming
    Replies: 0
    Last Post: 03-19-2006, 07:16 PM
  3. Device Driver: sysfs confusion
    By filker0 in forum Linux Programming
    Replies: 0
    Last Post: 12-02-2005, 11:36 AM
  4. DirectX - Lost Device. HELP
    By Mastadex in forum C++ Programming
    Replies: 0
    Last Post: 11-21-2004, 10:31 PM
  5. Replies: 4
    Last Post: 06-30-2004, 03:11 PM