Sign in to follow this  
Ancient Spirit

Rendering...

Recommended Posts

//begine message loop if(ActiveFlag) Render(); ... ... ... //end message loop the thing is that when the window is back active... meaning i return to it... the swapping of the buffers fail... how do i solve it?

Share this post


Link to post
Share on other sites
Hi there Ancient Spirit,
How are you doing?

The Problem
Device might be lost

The Solution
Ancient Spirit, I think the problem here is that you might be losing your device as a result of losing focus to the window and regaining focus.

Look into Device Lost
I hope this helps. Take care buddy.

Share this post


Link to post
Share on other sites

if(FAILED((Err = d3ddev->Present(NULL, NULL, NULL, NULL))))
{
Sleep(100);

if(Err == D3DERR_DEVICELOST)
{
DevErr = d3ddev->TestCooperativeLevel();

if(DevErr == D3DERR_DEVICENOTRESET)
if(FAILED(d3ddev->Reset(&d3dpp)))
{
MessageBox(NULL, "Unable to reset device", "Error", MB_OK | MB_ICONERROR | MB_TOPMOST);
return false;
}

if(FAILED(d3d->CreateDevice(ADAPTER, DEV_HAL, hWnd, Vertex, &d3dpp, &d3ddev)))
{
MessageBox(NULL, "Unable to create D3D Device", "Error", MB_OK | MB_ICONERROR | MB_TOPMOST);
return false;
}
else
return true;
}
else if(Err == D3DERR_DRIVERINTERNALERROR)
{
MessageBox(NULL, "Internal driver error", "Error", MB_OK | MB_ICONERROR | MB_TOPMOST);
return false;
}
else if(Err == D3DERR_INVALIDCALL)
{
MessageBox(NULL, "The method call is invalid", "Error", MB_OK | MB_ICONERROR | MB_TOPMOST);
return false;
}
}



i have made this error check... is this ok? or should i modifie something to make it more efficient?

[Edited by - Ancient Spirit on September 8, 2005 4:40:47 AM]

Share this post


Link to post
Share on other sites
Hi there Ancient Spirit

The Problem
Devices being lost

The Solution
The code really depends on how your engine is laid out and what language you are using but i'll try my best to provide you with a framework/guideline to implement it in your system/engine.

Let's cover the theory first shall we?
What constitutes as the device being lost?
"A Microsoft Direct3D device can be in either an operational state or a lost state. The operational state is the normal state of the device in which the device runs and presents all rendering as expected. The device makes a transition to the lost state when an event, such as the loss of keyboard focus in a full-screen application, causes rendering to become impossible. The lost state is characterized by the silent failure of all rendering operations, which means that the rendering methods can return success codes even though the rendering operations fail. In this situation, the error code D3DERR_DEVICELOST is returned by IDirect3DDevice9::Present."

This means that we can check if the device has been lost since the Present method will throw an exception.

So now we know when the device is lost

How do we respond to a lost device?
"A lost device must re-create resources (including video memory resources) after it has been reset. If a device is lost, the application queries the device to see if it can be restored to the operational state. If not, the application waits until the device can be restored.

If the device can be restored, the application prepares the device by destroying all video-memory resources and any swap chains. Then, the application calls the IDirect3DDevice9::Reset method. Reset is the only method that has an effect when a device is lost, and is the only method by which an application can change the device from a lost to an operational state. Reset will fail unless the application releases all resources that are allocated in D3DPOOL_DEFAULT, including those created by the IDirect3DDevice9::CreateRenderTarget and IDirect3DDevice9::CreateDepthStencilSurface methods."


Now that we know that we should call the reset method when the device has been lost and also we should recreate all the resources that have been created with the D3DPOOL_DEFAULT flag and also those using the CreateRenderTarget and CreateDepthStencilSurface methods.

"The application can determine what to do on encountering a lost device by querying the return value of the IDirect3DDevice9::TestCooperativeLevel method."

Now that we have our 2 methods for the lost device.
1) Testing for lost device
2) Responding/Recovering

Let's implement it.


private:
bool deviceLost = false;
public:
void Recover()
{
HRESULT hResult = IDirect3DDevice9::TestCooperativeLevel();
if(hResult == D3DERR_DEVICELOST)
{
//do nothing
}
else if(hResult == D3DERR_DEVICENOTRESET)
{
IDirect3DDevice9::Reset();
deviceLost = false;
}
}
void BeginRender()
{
if(deviceLost)
Recover();
if(deviceLost)
return;
if(device != null)
{
IDirect3DDevice9::Clear();
IDirect3DDevice9::BeginScene();
}
}
void EndRender()
{
public void EndRender()
{
if(deviceLost)
return;
IDirect3DDevice9::EndScene();
HRESULT hResult = IDirect3DDevice9::Present();
if(hResult == D3DERR_DEVICELOST)
deviceLost = true;
}
}





This is probably the worst implementation you might get but I just wrote it quickly. You should use it as a guideline ;)

I hope this helps buddy.

Share this post


Link to post
Share on other sites
ok thank you very much! :)

1 more questions... *feels all nooby and stuff* :P

is there a matrix stack in direct3d like in opengl or i need to set many matrices and then just choose the right one that i need?

Share this post


Link to post
Share on other sites
Hi there buddy,

The Problem
Does DirectX have a matrix stack like opengl?

The Solution
OpenGL's matrix stack
" The openGL matrix stack is a powerful construct. First some basic review: a stack is a last-in-first out structure. Think of a stack of trays in a cafeteria. The last tray added to the top is the first tray that will be removed.
OpenGL's matrix stack is quite similar. You can push a new item onto the top of the stack, or pop an item off the top. The commands for this are glPushMatrix() and glPopMatrix(). Every point that is drawn will be mulitipled by whatever is on the matrix stack.
As with all data structures, GL's matrix stack must be intialised. With a matrix, instead of initialising to 0, or NULL, we initialise to the Identity Matrix. When anything (including a point we are about to draw) is multiplied by the identity matrix, it remains the same. The command to load the identity matrix into the current top of the stack is: glLoadIdentity(). Please be aware that this will wipe out whatever is currently on the stack. If you wish to preserve your previous Matrix, use glPushMatrix() to push it down a level, before loading the identity matrix."


DirectX ID3DXMatrixStack
DirectX provides you with a similar stack in the D3DX utility library
"The D3DX utility library provides the ID3DXMatrixStack interface. It supplies a mechanism to enable matrices to be pushed onto and popped off of a matrix stack. Implementing a matrix stack is an efficient way to track matrices while traversing a transform hierarchy."

So the ID3DXMatrixStack Interface is what you are looking for :)

Hope this helps,
take care buddy.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this