Неправильная отрисовка ImGui

Делаю оверлей для приложения и элементы ImGui отрисовываются не относительно окна, созданного ImGui, а относительно окна приложения, где рисуется ImGui.

Примерная схема: Схема

Так же квадрат 2 не виден на основном окне если на него не навести ImGui window.

// globals
ID3D11Device* pDevice;
ID3D11DeviceContext* pCtx;
_Present fn_Present;
HWND hwnd;
_Wndproc wndproc;
vector<const char*> messages;
ID3D11RenderTargetView* mainRenderTargetView;
bool init_graph = false;
bool init_imgui = false;
bool enable_menu = false;
// end of globals

bool InitImGui() {
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO();
    ImGui_ImplWin32_Init(hwnd);
    ImGui_ImplDX11_Init(pDevice, pCtx);

    return true;
}

void DrawImGui() {
    ImGui_ImplDX11_NewFrame();
    ImGui_ImplWin32_NewFrame();
    ImGui::NewFrame();

    ImGui::Begin("Overlay window", 0, ImGuiWindowFlags_::ImGuiWindowFlags_NoResize | ImGuiWindowFlags_::ImGuiWindowFlags_NoTitleBar);
    ShowMetricsWindow();

    ImGuiWindow* window = ImGui::GetCurrentWindow();
    ImDrawList* draw = window->DrawList;

    ImGui::SetWindowSize({ 400,300 });
    draw->AddRectFilled(ImVec2(), ImVec2(670, 50), ImColor(56, 56, 56));

    ImGui::End();

    ImGui::Render();

    pCtx->OMSetRenderTargets(1, &mainRenderTargetView, NULL);
    ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
}

#define PREVIOUS_KEY_STATE(lParam) (lParam & 0x40000000)

LRESULT CALLBACK CustomWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    if (ImGui_ImplWin32_WndProcHandler(hwnd, msg, wParam, lParam))
        return true;

    switch (msg) {
    case WM_KEYDOWN:
        switch (wParam) {
        case VK_INSERT:
            if (!PREVIOUS_KEY_STATE(lParam)) enable_menu = !enable_menu;

            break;
        }

        break;
    }

    return CallWindowProc(wndproc, hwnd, msg, wParam, lParam);
}

// hooked IDXGISwapChain::Present function
HRESULT CustomPresent(IDXGISwapChain* swapchain, UINT reserved1, UINT reserved2) {
    if (!init_graph) {
        HRESULT hr = swapchain->GetDevice(__uuidof(ID3D11Device), (void**)&pDevice);

        if (FAILED(hr)) {
            pDevice = nullptr;

            printf("ID3D11Device error: %p\n", hr);

            return fn_Present(swapchain, reserved1, reserved2);
        }

        pDevice->GetImmediateContext(&pCtx);

        printf("Device: %p\n", (uintptr_t*)pDevice);
        printf("DeviceContext: %p\n", (uintptr_t*)pCtx);

        ID3D11Texture2D* pBackBuffer;
        swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&pBackBuffer);
        pDevice->CreateRenderTargetView(pBackBuffer, 0, &mainRenderTargetView);
        pBackBuffer->Release();

        printf("Swapping wndproc...");
        wndproc = (_Wndproc)SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)CustomWndProc);
        printf("ok\n");

        if (InitImGui()) {
            printf("ImGui inited\n");
            init_imgui = true;
        }

        init_graph = true;
    }

    if (enable_menu && init_imgui) DrawImGui();

    return fn_Present(swapchain, reserved1, reserved2);
}

Добавил весь код, который мог бы вызвать какие-либо ошибки.


Ответы (0 шт):