Direct3D窗口打开为白色屏幕

Direct3D window opens to white screen

本文关键字:白色 屏幕 窗口 Direct3D      更新时间:2023-10-16

在我使用的源代码中,我的程序打开了一个显示白色屏幕的窗口,该窗口立即变得没有响应。请帮助我查找代码的问题。

Windows.cpp

#include "DirectX.h"
using namespace std;
bool gameover = false;
//Windows event handler
LRESULT WINAPI WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
    case WM_DESTROY:
        gameover = true;
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Windows entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow) {
    //initializes window settings
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WinProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = L"MainWindowClass";
    wc.hIconSm = NULL;
    RegisterClassEx(&wc);
    //create a new window
    HWND window = CreateWindow(L"MainWindowClass", APPTITLE,
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        SCREENW, SCREENH, NULL, NULL, hInstance, NULL);
    if (window == 0) return 0;
    //display the window
    ShowWindow(window, nCmdShow);
    UpdateWindow(window);
    //initialize the game
    if (!Game_Init(window)) return 0;
    //main message loop
    MSG message;
    while (!gameover) {
        if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) {
            TranslateMessage(&message);
            DispatchMessage(&message);
        }
        //process game loop
        Game_Run(window);
    }
    //shutdown game
    Game_End();
    return message.wParam;
}

DirectX.cpp我想这就是问题发生的地方

bool Direct3D_Init(HWND window, int width, int height, bool fullscreen) 
{
    //initialize Direct3D
    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    if (!d3d) return false;
    //set Direct3D presentation parameters
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = !fullscreen;
    d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferWidth = width;
    d3dpp.BackBufferHeight = height;
    d3dpp.hDeviceWindow = window;
    //create Direct3D device
    d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
    if (!d3ddev) return false;
    //get a pointer to the back buffer surface
    d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
    //create sprite object
    D3DXCreateSprite(d3ddev, &spriteobj);
    return true;
}

我的游戏项目也有同样的问题。游戏是成功的,但当我开始的时候,它显示的是白色屏幕。我刚刚修复了这个错误。如果你有一个GPU(NVidia或…),你应该禁用它。就像我在这张图中所做的那样。在这里输入图像描述