c++user-interfacewinapigame-developmentdirectx-11

Unable to see render when I use the .exe


I have been trying to get this to work but I am at a loss as to why its not displaying the red dot, this is a test before I replace the dot with an actual gui. any help would be appreciated.

I have checked to make sure the libs are there and all correct as well as the .h files, I build and then try to click the exe and nothing shows up. I know that I am missing something here, not all my code by the way but any help would be appreciated. I have followed a TY tutorial here 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

Solution

  • Your issue is caused by this:

    MSG msg;
    while (PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE)) {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    
      if (msg.message = WM_QUIT) { // assignment !!!
        running = false;
      }
    }
    

    So, you're just quitting as soon as you've started. Change it to

    msg.message == WM_QUIT
    

    and it works.

    Additional notes:

    Your current warning level is 3, you can get to level 4:

    enter image description here

    And visual studio will tell you this:

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

    Also you should check for all errors for all Windows API and DirectX calls (HRESULTs).