Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

Restoring a direct3Ddevice (Solved)

This topic is 4769 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I recognize that my D3DDevice is lost I run the following code:

class Direct3DDevice: public BaseClass {
		void BeginScene();
		void Clear (BYTE Red, BYTE Green, BYTE Blue);
		void EndScene();
		DWORD GetHeight();
		BOOL GetNonPower2();
		DWORD GetWidth();
		void Initialize (Direct3DHardware & D3DHardware, WORD Width, WORD Height, BOOL bForcePower2);
		void Present ();
		void Restore ();

		Direct3DDevice ();
		~Direct3DDevice ();

		void CreateTexture (WORD Width, WORD Height, LPDIRECT3DTEXTURE9 & pTexture);
		void CreateVertexBuffer(WORD VertexAmount, WORD VertexSize, DWORD FlexibleVertexFormat, LPDIRECT3DVERTEXBUFFER9 & VB);
		void DrawPrimitive (D3DPRIMITIVETYPE PrimitiveType, WORD StartVertex, WORD PrimitiveCount);
		void SetStreamSource (LPDIRECT3DVERTEXBUFFER9 pBuffer, WORD BytesSize, DWORD FlexibleVertexFormat);
		void SetTexture (LPDIRECT3DTEXTURE9 pTexture);
		void LocalFree();

		DWORD Width, Height;
		BOOL bNonPower2;

		Direct3DHardware * pD3DHardware;
		BOOL bForcePower2;

Direct3DDevice::Restore ()
	this->Initialize (*this->pD3DHardware, this->Width, this->Height, this->bForcePower2);

Direct3DDevice::LocalFree ()
	if (this->pD3DDevice!=NULL)

Direct3DDevice::Initialize (Direct3DHardware & D3DHardware, WORD Width, WORD Height, BOOL bForcePower2)
	D3DCAPS9 DCaps;

	D3DHardware.CreateDevice (Width, Height, this->pD3DDevice);
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_ZENABLE, FALSE ));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetSamplerState (0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetSamplerState (0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetSamplerState (0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetSamplerState (0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetSamplerState (0, D3DSAMP_ADDRESSW, D3DTADDRESS_CLAMP));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ));
	this->bNonPower2 = (DCaps.TextureCaps&D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
	if (bForcePower2)
		this->bNonPower2 = FALSE;
	this->Width = Width;
	this->Height = Height;
	this->bForcePower2 = bForcePower2;
	this->pD3DHardware = &D3DHardware;

Direct3DHardware::CreateDevice (WORD Width, WORD Height, LPDIRECT3DDEVICE9 & pDevice)

		ZeroMemory( &d3dpp, sizeof(d3dpp) );
		d3dpp.BackBufferWidth = Width;
		d3dpp.BackBufferHeight = Height;
		d3dpp.BackBufferCount = 1;
	    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
		d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
		d3dpp.MultiSampleQuality = D3DMULTISAMPLE_NONE;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.hDeviceWindow = this->hWnd;
		d3dpp.Windowed = FALSE;
		d3dpp.EnableAutoDepthStencil = FALSE;
//		d3dpp.AutoDepthStencilFormat;
		d3dpp.Flags = 0;
		d3dpp.FullScreen_RefreshRateInHz = this->GetHighestRefreshRate (Width, Height);
		d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

		GlobalError::pErrH->HandleDirect3DError(this->pD3D->CreateDevice( this->AdapterNumber, D3DDEVTYPE_HAL, this->hWnd,
                                      &d3dpp, &pDevice ));

However, I get the following error from the createDevice at the last line of the code: "The device has been lost but cannot be reset at this time. Therefore, rendering is not possible." How should I restore the device? [Edited by - The C modest god on July 28, 2005 10:08:14 AM]

Share this post

Link to post
Share on other sites
This is one of those things I never bother to memorize myself, but I know where to go find the answer [smile]

Load up the SDK samples and dig into their framework code - it's included in all but the most trivial tutorials. Granted, it's no simple piece of code - but tracing the callback declarations and looking in the DXUTMainLoop() (I think thats the right name) code will eventually point you to the right place.

The SDK framework is effectively the "text book" perfect approach to handling the lifecycle of a Direct3D application, if you do things the way it does them you should be fine [grin]


Share this post

Link to post
Share on other sites
I have added the Reset method before the Release and it worked out.
I thought Release would be enough, but the Release failed without calling reset before it.

Thanks for the help.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!