c++ - direct3d 初始化失败/c++

标签 c++ winapi directx

这是创建一个简单窗口并初始化一个简单 direct3d 设备的代码。但每次程序到达 render() 函数时,应用程序都会终止。我不知道为什么会这样。有人可以向我解释这种奇怪的行为吗?谢谢!!

//====================================================================================================

#include <windows.h>
#include <d3d9.h>

//====================================================================================================

HINSTANCE hInst;
HWND wndHandle;

//====================================================================================================

LPDIRECT3D9 pD3D; // the Direct3D object
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device

//====================================================================================================

bool initWindow(HINSTANCE hInstance);
bool initDirect3D(void);
void cleanUp (void);
void render(void);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

//====================================================================================================

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    if (!initWindow(hInstance)) return false;

    if (!initDirect3D()) return false;

    MSG msg;
    ZeroMemory(&msg, sizeof(msg));

    if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    } else {
        render();  // i think this is the problem ...
    }

    return static_cast<int>(msg.wParam);
}

bool initWindow(HINSTANCE hInstance )
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(0, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(0, IDC_ARROW);
    wcex.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
    wcex.lpszMenuName = 0L;
    wcex.lpszClassName = L"DirectXTemplate";
    wcex.hIconSm = 0;
    RegisterClassEx(&wcex);

    wndHandle = CreateWindow(L"DirectXTemplate", L"DirectX Template", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);

    if (!wndHandle) return false;

    ShowWindow(wndHandle, SW_SHOW);
    UpdateWindow(wndHandle);

    return true;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }   
}

bool initDirect3D(void)
{
    pD3D = NULL;
    pd3dDevice = NULL;

    // create the DirectX object
    if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))) return false;

    // fill the presentation parameters structure
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));

    d3dpp.Windowed          = TRUE;
    d3dpp.SwapEffect        = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat  = D3DFMT_UNKNOWN;
    d3dpp.BackBufferCount   = 1;
    d3dpp.BackBufferHeight  = 480;
    d3dpp.BackBufferWidth   = 640;
    d3dpp.hDeviceWindow     = wndHandle;

    // create a default DirectX device
    if (FAILED(pD3D -> CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice))) return false;

    return true;
}

void render(void)
{
    // Check to make sure you have a valid Direct3D device
    if(NULL == pd3dDevice) return;  // clear the back buffer to a blue color
    pd3dDevice -> Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);

    // Present the back buffer contents to the display
    pd3dDevice -> Present(NULL, NULL, NULL, NULL);
}

void cleanUp (void)
{
    // Release the device and the Direct3D object
    if (pd3dDevice != NULL) pd3dDevice -> Release();
    if (pD3D != NULL) pD3D -> Release();
}

最佳答案

@DuckMaestro 是对的。您的程序正在经历一次消息/渲染过程,然后结束。它应该只在 msg 是退出程序时结束。尝试放入这样的循环:

while(msg.message!=WM_QUIT){
  if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  {
      TranslateMessage (&msg);
      DispatchMessage (&msg);
  } else {
      render();  // i think this is the problem ...
  }
}

关于c++ - direct3d 初始化失败/c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8598197/

相关文章:

c++ - 如何从 FAILED(HRESULT) 引发 std::system_error 异常?

c++ - 你如何在 C++/directx 中设置菜单系统(可点击按钮)?

c++ - 标题困惑。编译器不识别数据类型

c++ - 如何在复合键控提升多索引容器的一个键上执行 equal_range 并在第二个键上执行 lower_bound?

c++ - 将 C++11 lambda 与 boost::multi_index 结合使用

c++ - 是什么导致十六进制到二进制转换器(C++ 代码)中出现这些奇怪的输出?

c++ - 如何在 C++ 中将结构写入文件并读取文件?

c++ - 在 C++ 中每 10 毫秒执行一次函数

winapi - Win32 中的 BackupWrite() 和 BackupRead()

c++ - Directx11 - DeviceContext::ClearRenderTargetView 崩溃