Directx9:Ctrl + Alt + Del后重置设备

时间:2014-01-07 05:13:26

标签: c++ directx device direct3d directx-9

我正在使用无边框窗口并从Youtube视频中复制设备重置代码并在那里工作但我只是从那里收到消息:

if(FAILED(hr)){
    MessageBox(0, "Failed to reset device!", 0, 0);
    return;
}

我哪里出错了?我在InvalidateDeviceObjects()忘记了什么吗?我可以给你更多的代码而不是一切,因为它太长了。 我真的需要帮助......

重置设备:

void Render(){

if(HandleDeviceLost/*VK_F1*/){
    if(DeviceLost){
        Sleep(100);
        if(FAILED(hr=d3ddev->TestCooperativeLevel())){

            if(hr==D3DERR_DEVICELOST){
                return;
            }
            if(hr==D3DERR_DEVICENOTRESET){

                //clean
                InvalidateDeviceObjects();

                //reset device
                hr=d3ddev->Reset(&d3dpp);

                if(FAILED(hr)){
                    MessageBox(0, "Failed to reset device!", 0, 0);
                    return;
                }

                //restore
                RestoreDeviceObjects();
            }
            return;
        }
    }
}
DeviceLost=0;

/*
   Stuff
*/

hr=d3ddev->Present(NULL, NULL, NULL, NULL);
if(hr==D3DERR_DEVICELOST){
    DeviceLost=1;
}
}


发布对象:

void InvalidateDeviceObjects(){

    buffShipMaterial->Release();
    Wall_large->Release();
    Wall_small->Release();
    space_text->Release();
    meshWall->Release();
    menuText->Release();
    menuText2->Release();
    menuText3->Release();
    text_cpu->Release();
    text_player->Release();
    text_player2->Release();
    number_0->Release();
    number_1->Release();
    number_2->Release();
    number_3->Release();
    number_4->Release();
    number_5->Release();
    number_6->Release();
    number_7->Release();
    number_8->Release();
    number_9->Release();
    number_10->Release();
}

1 个答案:

答案 0 :(得分:2)

在您关联的示例中,Reset上的错误会导致return,而render函数会再次被调用。这是正常的 - 没有理由Reset必须在第一次调用时成功,因此通常会继续重试,而不是像在代码中那样显示错误消息。

如果您在消息循环中进行渲染,就像在该示例中一样,只需执行相同的操作 - 当您收到错误时不要停止。

如果你没有在消息循环中渲染,而是使用WM_PAINT方法,那么这就是我使用的一般模式 - 虽然有时我设置了一个计时器而不是调用InvalidateRect,它取决于应用程序 - 但这已经足够强大的许多应用程序。您可以看到Reset如何在失败时重复,而不是在第一次失败时抛出错误消息。采用这种模式可能是个主意:

void CMyClass::DrawScene()
{
    // perform all dx9 scene drawing
    HRESULT hr;

    // if device was lost, try to restore it
    if (m_bDeviceLost)
    {
        // is it ok to render again yet?
        if (FAILED(hr = m_pD3DDevice->TestCooperativeLevel()))
        {
            // the device has been lost but cannot be reset at this time
            if (hr == D3DERR_DEVICELOST)
            {
                // request repaint and exit
                InvalidateRect(NULL);
                return;
            }

            // the device has been lost and can be reset
            if (hr == D3DERR_DEVICENOTRESET)
            {
                // do lost/reset/restore cycle
                OnLostDevice();
                hr = m_pD3DDevice->Reset(&m_pD3Dpp);
                if (FAILED(hr))
                {
                    // reset failed, try again later
                    InvalidateRect(NULL);
                    return;
                }
                OnResetDevice();
            }
        }

        // flag device status ok now
        m_bDeviceLost = false;
    }

    // ... clear to background and do the drawing ...

    // display scene
    hr = m_pD3DDevice->Present(NULL, NULL, GetSafeHwnd(), NULL);
    m_bDeviceLost = (hr == D3DERR_DEVICELOST);

    // request repaint if device has been lost
    if (m_bDeviceLost) 
    {
        InvalidateRect(NULL);
    }
}

此外,您必须确保从用于创建设备的同一线程调用TestCooperativeLevelReset