Исходник External overlay that draws in fullscreen

Участник
Статус
Оффлайн
Регистрация
6 Апр 2021
Сообщения
343
Реакции[?]
216
Поинты[?]
122K
saw that so many need this and i am out of the cs scene
credis: re.s, a1uk4 and corsair
.hpp:
class c_emulation {
public: // private
    WNDCLASSEXW m_window_class{};
    ID3D11Device* m_device{ nullptr };
    ID3D11DeviceContext* m_deivce_context{ nullptr };
    IDXGISwapChain* m_swap_chain{ nullptr };
    ID3D11RenderTargetView* m_render_target{ nullptr };



public:   
    bool create_overlay();
    void destroy();
    void run_overlay(std::function<void()> inner_data);

    void streamproof(bool var);
    void clickable(bool value);

    int width, height;
    bool initialized = false, rendering_stopepd = false;

    HWND m_hwnd;
};

// tryint that with shared pointer
inline std::shared_ptr< c_emulation > g_emulation = std::make_shared<c_emulation>();
C++:
bool c_emulation::create_overlay()
{
    if (this->initialized)
        return true;

    this->width = GetSystemMetrics(SM_CXSCREEN);
    this->height = GetSystemMetrics(SM_CYSCREEN);

    m_window_class.cbSize = sizeof(WNDCLASSEX);
    m_window_class.style = 0;
    m_window_class.lpfnWndProc = window_process;
    m_window_class.hInstance = nullptr;
    m_window_class.lpszClassName = L"nvidia overlay emulation";

    RegisterClassEx(&m_window_class);

    m_hwnd = CreateWindowExW(
        WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED,
        m_window_class.lpszClassName, (L""), WS_POPUP | WS_VISIBLE,
        0, 0, width, height, nullptr, nullptr, nullptr /* instance */, nullptr);

    if (!SetLayeredWindowAttributes(m_hwnd, RGB(0, 0, 0), BYTE(255), LWA_ALPHA)) {
        // throw std::runtime_error(("failed to get window rect"));
        g_log->error("failed to get window rect");
    }
        // throw std::runtime_error(("failed to set layered window attributes"));

    {
        RECT clientArea = { };
        if (!GetClientRect(m_hwnd, &clientArea)) {
            // throw std::runtime_error(("failed to get window rect"));
            g_log->error("failed to get window rect");
        }
           // throw std::runtime_error(("failed to get client rect"));

        RECT windowArea = { };
        if (!GetWindowRect(m_hwnd, &windowArea))
        {
            // throw std::runtime_error(("failed to get window rect"));
            g_log->error("failed to get window rect");
        }

        POINT diff = { };
        if (!ClientToScreen(m_hwnd, &diff)) {
            // throw std::runtime_error(("failed to get client to screen"));
            g_log->error("failed to get client to screen");
        }

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

        if (FAILED(DwmExtendFrameIntoClientArea(m_hwnd, &margins))) {
            // throw std::runtime_error(("failed to extend frame into client area"));
            g_log->error("failed to extend frame into client area");
        }
    }

   // c_gui::m_context->m_hwnd = m_hwnd;

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

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

    D3D_FEATURE_LEVEL level = { };

    if (D3D11CreateDeviceAndSwapChain(
        nullptr,
        D3D_DRIVER_TYPE_HARDWARE,
        nullptr,
        0U,
        levels,
        2U,
        D3D11_SDK_VERSION,
        &swapChainDesc,
        &m_swap_chain,
        &m_device,
        &level,
        &m_deivce_context)) return false;

    ID3D11Texture2D* pBackBuffer = nullptr;
    m_swap_chain->GetBuffer(0U, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);

    if (pBackBuffer) {
        m_device->CreateRenderTargetView(pBackBuffer, nullptr, &m_render_target);
        pBackBuffer->Release();
    }
    else {
        throw std::runtime_error("failed to get back buffer");
    }

    SetWindowLongPtr(m_hwnd, GWL_EXSTYLE,
        GetWindowLongPtr(m_hwnd, GWL_EXSTYLE) & ~WS_EX_TOOLWINDOW | WS_EX_APPWINDOW);
    ShowWindow(m_hwnd, SW_MAXIMIZE);
    UpdateWindow(m_hwnd);

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

    ImGui_ImplWin32_Init(m_hwnd);
    ImGui_ImplDX11_Init(m_device, m_deivce_context);

    g_render_engine->intialize_renderer(m_device, m_deivce_context, m_hwnd);

    initialized = true;
}

void c_emulation::destroy()
{
    ImGui_ImplDX11_Shutdown();
    ImGui_ImplWin32_Shutdown();

    ImGui::DestroyContext();

    if (m_swap_chain)
        m_swap_chain->Release();

    if (m_deivce_context)
        m_deivce_context->Release();

    if (m_device)
        m_device->Release();

    if (m_render_target)
        m_render_target->Release();

    if (!DestroyWindow(m_hwnd))
        throw std::runtime_error(("failed to destroy window"));

    if (!UnregisterClassW(m_window_class.lpszClassName, m_window_class.hInstance))
        throw std::runtime_error(("failed to destroy window"));
}

void c_emulation::run_overlay(std::function<void()> inner_data)
{
   //this->width = GetSystemMetrics(SM_CXSCREEN);
   //this->height = GetSystemMetrics(SM_CYSCREEN);

    if (!initialized) {
        rendering_stopepd = true;
        return;
    }

    static bool bRunning = true;
    if (bRunning) {
        MSG message;
        ZeroMemory(&message, sizeof(message));

        while (PeekMessage(&message, m_hwnd, 0U, 0U, PM_REMOVE)) {
            TranslateMessage(&message);
            DispatchMessage(&message);
        }
        if (message.message == WM_QUIT)
            bRunning = false;

        ImGui_ImplDX11_NewFrame();
        ImGui_ImplWin32_NewFrame();
        ImGui::NewFrame();

        if (c_gui::m_context->m_opened) {
            LONG_PTR windowStyle = GetWindowLongPtr(m_hwnd, GWL_EXSTYLE);
            windowStyle &= ~WS_EX_TRANSPARENT;
            SetWindowLongPtr(m_hwnd, GWL_EXSTYLE, windowStyle);
        }
        else if (!c_gui::m_context->m_opened) {
            LONG_PTR windowStyle = GetWindowLongPtr(m_hwnd, GWL_EXSTYLE);
            SetWindowLongPtr(m_hwnd, GWL_EXSTYLE, windowStyle | WS_EX_TRANSPARENT);
        }

        g_menu->draw();
#ifndef gui_test
        g_listener->listen_for_entities();
#endif // !gui_test

        ImGui::Render();

        constexpr float flColor[4] = { 0.f, 0.f, 0.f, 0.f };
        m_deivce_context->OMSetRenderTargets(1U, &m_render_target, NULL);
        m_deivce_context->ClearRenderTargetView(m_render_target, flColor);

        ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
        m_swap_chain->Present(1U, 0U);
    }

    if (!bRunning) {
        destroy();
        rendering_stopepd = true;
        return;
    }

    rendering_stopepd = false;
}
 
Сверху Снизу