使用.exe时无法看到渲染

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

我一直在尝试让它工作,但我不知道为什么它不显示红点,这是我用实际的 GUI 替换点之前的测试。任何帮助将不胜感激。

我已检查以确保库存在并且所有正确以及 .h 文件,我构建然后尝试单击 exe,但没有显示任何内容。我知道我在这里遗漏了一些东西,顺便说一下,并不是我所有的代码,但任何帮助将不胜感激。我在这里遵循了 TY 教程 https://www.youtube.com/watch?v=aY8MNCNN-cY&t=1638s

#include <Windows.h>

#include <dwmapi.h>
#include <d3d11.h>
#include <imgui.h>
#include <imgui_impl_dx11.h>
#include <imgui_impl_win32.h>

extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

LRESULT CALLBACK window_procedure(HWND window, UINT message, WPARAM w_param, LPARAM l_param) {
    if (ImGui_ImplWin32_WndProcHandler(window, message, w_param, l_param)) {
        return 0L;
    }

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

    return DefWindowProc(window, message, w_param, l_param);

}

INT APIENTRY WinMain(HINSTANCE instance, HINSTANCE, PSTR, INT cmd_show) {
    WNDCLASSEXW wc{};
    wc.cbSize = sizeof(WNDCLASSEXW);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = window_procedure;
    wc.hInstance = instance;
    wc.lpszClassName = L"External Overlay Class";

    RegisterClassExW(&wc);

    const HWND window = CreateWindowExW(
        WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED,
        wc.lpszClassName,
        L"Overlay",
        NULL,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        1920,
        1080,
        nullptr,
        nullptr,
        wc.hInstance,
        nullptr
    );

    SetLayeredWindowAttributes(window, RGB(255, 0, 0), BYTE(255), LWA_ALPHA);

    
    
  {

      RECT client_area{};
      GetClientRect(window, &client_area);

      RECT window_area{};
      GetWindowRect(window, &window_area);

      POINT diff{};
      ClientToScreen(window, &diff);

     const MARGINS margins{
         window_area.left + (diff.x - window_area.left),
         window_area.top + (diff.y - window_area.top),
         client_area.right,
         client_area.bottom
     };

    DwmExtendFrameIntoClientArea(window, &margins);

  }

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

  constexpr D3D_FEATURE_LEVEL levels[2]{
      D3D_FEATURE_LEVEL_11_1,
      D3D_FEATURE_LEVEL_10_0
  };

  ID3D11Device* device{ nullptr };
  ID3D11DeviceContext* device_context{ nullptr };
  IDXGISwapChain* swap_chain{ nullptr };
  ID3D11RenderTargetView* render_target_view{ nullptr };
  D3D_FEATURE_LEVEL level{};

  D3D11CreateDeviceAndSwapChain(
      nullptr,
      D3D_DRIVER_TYPE_HARDWARE,
      nullptr,
      0U,
      levels,
      2U,
      D3D11_SDK_VERSION,
      &sd,
      &swap_chain,
      &device,
      &level,
      &device_context
  );

  ID3D11Texture2D* back_buffer{ nullptr };
  swap_chain->GetBuffer(0U, IID_PPV_ARGS(&back_buffer));

  if (back_buffer) {
      device->CreateRenderTargetView(back_buffer, nullptr, &render_target_view);
      back_buffer->Release();
  }
  else {
      return 1;
  }

  ShowWindow(window, cmd_show);
  UpdateWindow(window);

  ImGui::CreateContext();
  ImGui::StyleColorsDark();

  ImGui_ImplWin32_Init(window);
  ImGui_ImplDX11_Init(device, device_context);

  bool running = true;

  while (running) {
      MSG msg;
      while (PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE)) {
          TranslateMessage(&msg);
          DispatchMessage(&msg);

          if (msg.message = WM_QUIT) {
              running = false;
          }
      }

      if (!running) {
          break;
      }

      ImGui_ImplDX11_NewFrame();
      ImGui_ImplWin32_NewFrame();

      ImGui::NewFrame();

      ImGui::GetBackgroundDrawList()->AddCircleFilled({500, 500}, 20.f, ImColor (1.f, 0.f, 0.f, 0.5f));

      // rendering

      ImGui::Render();

      constexpr float color[4]{0.f, 0.f, 0.f, 0.f};
      device_context->OMSetRenderTargets(1U, &render_target_view, nullptr);
      device_context->ClearRenderTargetView(render_target_view, color);

      ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());

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

  }


  ImGui_ImplDX11_Shutdown();
  ImGui_ImplWin32_Shutdown();

  ImGui::DestroyContext();

  if (swap_chain) {
      swap_chain->Release();
  }
  if (device_context) {
      device_context->Release();
  }
  if (device) {
      device->Release();
  }

  if (render_target_view) {
      render_target_view->Release();
  }

  DestroyWindow(window);
  UnregisterClassW(wc.lpszClassName, wc.hInstance);


  return 0;
}

written in visual studio cpp
c++ user-interface render game-development directx-11
1个回答
0
投票

您的问题是由以下原因引起的:

MSG msg;
while (PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE)) {
  TranslateMessage(&msg);
  DispatchMessage(&msg);

  if (msg.message = WM_QUIT) { // assignment !!!
    running = false;
  }
}

所以,你一开始就放弃了。改为

msg.message == WM_QUIT

并且有效。

补充说明:

您当前的警告级别为3级,您可以升级到4级:

视觉工作室会告诉你:

1>E:\kilroywashere\Gameystuff\IESP\src\main.cpp(142):
    warning C4706: assignment within conditional expression

此外,您还应该检查所有 Windows API 和 DirectX 调用(HRESULT)的all错误。

© www.soinside.com 2019 - 2024. All rights reserved.