ImGui 独立运行时崩溃

问题描述 投票:0回答:0

我的 ImGui 窗口 (dx11 winapi32) 由 visual studio 本地 windows 调试器启动时按预期运行但是当通过双击 .exe 文件启动时它运行 4 秒然后它没有响应。

这里是窗口设置的代码:

WNDCLASSEXW WndCLASS{};
ID3D11Device* device{ nullptr };
ID3D11DeviceContext* device_ctx{ nullptr };
IDXGISwapChain* swap_chain{ nullptr };
ID3D11RenderTargetView* g_mainRenderTargetView{ nullptr };
D3D_FEATURE_LEVEL levels{};
HWND window;
DXGI_SWAP_CHAIN_DESC swap_description{};
ID3D11Texture2D* pBackBuffer{ nullptr };
ImGuiContext* context1;

extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
        return 0L;

    if (msg == WM_DESTROY) {
        PostQuitMessage(0);
        return 0L;
    }

    return DefWindowProcW(hWnd, msg, wParam, lParam);
}

int screen_width;
int screen_height;

int setupRenderer(HINSTANCE hInstance) {

    screen_width = GetSystemMetrics(0);
    screen_height = GetSystemMetrics(1);

    WndCLASS.cbSize = sizeof(WNDCLASSEXW);
    WndCLASS.style = CS_HREDRAW | CS_VREDRAW;
    WndCLASS.lpfnWndProc = WndProc;
    WndCLASS.hInstance = hInstance;
    WndCLASS.lpszClassName = L"Overlay";

    RegisterClassExW(&WndCLASS); // register the class

    // create window
    window = CreateWindowExW(WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED, WndCLASS.lpszClassName, L"", WS_POPUP, 0, 0, screen_width, screen_height, nullptr, nullptr, WndCLASS.hInstance, nullptr);

    std::cout << "-> Window created.\n";

    SetLayeredWindowAttributes(window, RGB(0, 0, 0), BYTE(255), LWA_COLORKEY); // i don't know what the fuck is this but it's important

    // Device

    swap_description.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    swap_description.BufferDesc.RefreshRate.Numerator = 60U;
    swap_description.BufferDesc.RefreshRate.Denominator = 1U;
    swap_description.SampleDesc.Count = 1U;
    swap_description.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swap_description.BufferCount = 2U;
    swap_description.OutputWindow = window;
    swap_description.Windowed = 1;
    swap_description.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
    swap_description.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

    constexpr D3D_FEATURE_LEVEL d3d_levels[2]{
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_0,
    };



    D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0U, d3d_levels, 2U, D3D11_SDK_VERSION, &swap_description, &swap_chain, &device, &levels, &device_ctx);
    // Render Target

    swap_chain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
    if (pBackBuffer) {
        device->CreateRenderTargetView(pBackBuffer, nullptr, &g_mainRenderTargetView);
        pBackBuffer->Release();
    }
    else {
        exit(1);
        return 1;
    }

    ShowWindow(window, true);
    UpdateWindow(window);

    ImGui::CreateContext();
    std::cout << "-> Created context.\n";
    ImGui::StyleColorsDark();

    ImGui_ImplWin32_Init(window);
    ImGui_ImplDX11_Init(device, device_ctx);
    std::cout << "-> Overlay initialized.\n\n";
}

这是渲染代码:

        bool running = true;
    while (running) {
        
        // Start the Dear ImGui frame
        ImGui_ImplDX11_NewFrame();
        ImGui_ImplWin32_NewFrame();
        ImGui::NewFrame();

        renderWatermark();
        renderCrosshair();
        renderESP();
        renderHDOT();

        ImGui::Render();

        constexpr float color[4] = { 0, 0, 0, 0 };
        device_ctx->OMSetRenderTargets(1U, &g_mainRenderTargetView, nullptr);
        device_ctx->ClearRenderTargetView(g_mainRenderTargetView, color);
        ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());

        swap_chain->Present(1U, 0U);
        
    }

    // Cleanup after exit
    ImGui_ImplDX11_Shutdown();
    ImGui_ImplWin32_Shutdown();
    ImGui::DestroyContext();

    swap_chain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));

    //device->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
    pBackBuffer->Release();
    if (swap_chain) { swap_chain->Release(); swap_chain = NULL; }
    if (device_ctx) { device_ctx->Release(); device_ctx = NULL; }
    if (device) { device->Release(); device = NULL; }

    ::DestroyWindow(window);
    ::UnregisterClassW(WndCLASS.lpszClassName, WndCLASS.hInstance);

    return;

同样,奇怪的是,我只有在 visual studio 外启动程序时才会遇到这个问题。

试过 std::this_thread::sleep_for(std::chrono::milliseconds(10));但它没有帮助

winapi directx-11 imgui
© www.soinside.com 2019 - 2024. All rights reserved.