DirectX - Last Part - Drawing into Overlay - Help

Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Banlı Üye
Katılım
28 Kas 2020
Mesajlar
9
Tepki puanı
-1
Yaş
27
5 HİZMET YILI
C++:
#pragma once
#include <string>
#include <thread>

#include <Windows.h>

#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

namespace forceinline {
    class dx_renderer {
    private:
        struct D3DTLVERTEX {
            float x, y, z, rhw;
            unsigned long color;
        };

    public:
        dx_renderer() { }
        dx_renderer(IDirect3DDevice9* device);
        ~dx_renderer();

        void begin_rendering();
        void end_rendering();

        int get_fps();

        void draw_line(int x0, int y0, int x1, int y1, unsigned long color);
        void draw_rect(int x, int y, int w, int h, unsigned long color);
        void draw_filled_rect(int x, int y, int w, int h, unsigned long color);
        void draw_outlined_rect(int x, int y, int w, int h, unsigned long color);
        void draw_text(std::wstring text, int x, int y, unsigned long color, bool center = true, bool outline = true);


        RECT get_text_dimensions(std::wstring text);

    private:
        int m_fps = 0;

        IDirect3DDevice9* m_device = nullptr;

        ID3DXFont* m_font = nullptr;
        ID3DXLine* m_line = nullptr;
    };
}


Kod:
 = {
            D3DXVECTOR2(x0, y0),
            D3DXVECTOR2(x1, y1)
        };

        m_line->Begin();
        m_line->Draw(lines, 2, color);
        m_line->End();
    }

    void dx_renderer::draw_rect(int x, int y, int w, int h, unsigned long color) {
        draw_line(x, y, x + w, y, color);
        draw_line(x, y, x, y + h, color);
        draw_line(x + w, y, x + w, y + h, color);
        draw_line(x, y + h, x + w + 1, y + h, color);
    }

    void dx_renderer::draw_filled_rect(int x, int y, int w, int h, unsigned long color) {
        D3DTLVERTEX qV[4] = {
            { float(x), float(y + h), 0.f, 1.f, color },
            { float(x), float(y), 0.f, 1.f, color },
            { float(x + w), float(y + h), 0.f, 1.f, color },
            { float(x + w), float(y) , 0.f, 1.f, color }
        };

        m_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
        m_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
        m_device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
        m_device->SetTexture(0, nullptr);
        m_device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, qV, sizeof(D3DTLVERTEX));
    }

    void dx_renderer::draw_outlined_rect(int x, int y, int w, int h, unsigned long color) {
        draw_rect(x - 1, y - 1, w + 2, h + 2, D3DCOLOR_RGBA(1, 1, 1, 255));
        draw_rect(x + 1, y + 1, w - 2, h - 2, D3DCOLOR_RGBA(1, 1, 1, 255));
        draw_rect(x, y, w, h, color);
    }

    void dx_renderer::draw_text(std::wstring text, int x, int y, unsigned long color, bool center, bool outline) {
        if (center) {
            RECT dimensions = get_text_dimensions(text);
            x -= (dimensions.right - dimensions.left) / 2;
        }

        auto _text = [&](std::wstring _text, int _x, int _y, unsigned long _color) {
            RECT r{ _x, _y, _x, _y };
            m_font->DrawTextW(NULL, _text.data(), -1, &r, DT_NOCLIP, _color);
        };

        if (outline) {
            _text(text, x - 1, y, D3DCOLOR_RGBA(1, 1, 1, 255));
            _text(text, x + 1, y, D3DCOLOR_RGBA(1, 1, 1, 255));
            _text(text, x, y - 1, D3DCOLOR_RGBA(1, 1, 1, 255));
            _text(text, x, y + 1, D3DCOLOR_RGBA(1, 1, 1, 255));
        }

        _text(text, x, y, color);
    }

    RECT dx_renderer::get_text_dimensions(std::wstring text) {
        RECT r;
        m_font->DrawTextW(NULL, text.data(), -1, &r, DT_CALCRECT, 0xFFFFFFFF);
        return r;
    }
}


C++:
#pragma once
#include "dx_renderer.h"
#include <d3d9.h>


namespace forceinline {
    class dx_overlay {
    private:
        struct wnd_rect_t : public RECT {
            int width() { return right - left; }
            int height() { return bottom - top; }
        };

    public:
        dx_overlay() { }
        dx_overlay(std::wstring target_class, std::wstring target_window, bool not_topmost = false);
        ~dx_overlay();

        dx_renderer create_renderer();
        HWND get_overlay_wnd();

        bool is_initialized();

    private:
        void create_overlay(std::wstring target_class, std::wstring target_window);
        void init_dx9();

        int m_fps = 0;
        bool m_initialized = false;

        static bool m_not_topmost;

        static HWND m_overlay_wnd, m_target_wnd;
        wnd_rect_t m_overlay_wnd_size, m_target_wnd_size;

        IDirect3D9* m_d3d = nullptr;
        IDirect3DDevice9* m_device = nullptr;

        static LRESULT CALLBACK m_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
    };
}


Kod:
[CODE=cpp]#define _CRT_NON_CONFORMING_WCSTOK
#include <iostream>
#include <Windows.h>
#include <math.h>
#include <vector>
#include <codecvt>
#include "dx_overlay.h"
#include "dx_renderer.h"

using namespace std;

typedef struct
{
    float x, y, z;
}vec3d_f;
typedef struct
{
    float x, y, z, w;
}vec4d_f;
vec4d_f clipCoords;
vec4d_f NDC;

namespace offsets
{
    DWORD viewMatrix = 0x1287A20;
    DWORD x_position = 0x30;
    DWORD y_position = x_position + 0x4;
    DWORD z_position = y_position + 0x4;
    DWORD players = 0x29CD3C;
    DWORD name = 0x250;
    DWORD health = 0X15C;
    DWORD localplayer = 0x213EA8;
}

HWND hwnd;
HANDLE handle;
DWORD pID;

template <class T>
T Read(DWORD address)
{
    T VALUE;
    ReadProcessMemory(handle, (LPVOID)address, &VALUE, sizeof(T), 0);
    return VALUE;
}



float matrix[16];
char name[999];

bool WorldToScreen(vec3d_f pos, vec3d_f& screen, float matrix[16], int windowWidth, int windowHeight)
{
    clipCoords.x = pos.x * matrix[0] + pos.y * matrix[4] + pos.z * matrix[8] + matrix[12];
    clipCoords.y = pos.x * matrix[1] + pos.y * matrix[5] + pos.z * matrix[9] + matrix[13];
    clipCoords.z = pos.x * matrix[2] + pos.y * matrix[6] + pos.z * matrix[10] + matrix[14];
    clipCoords.w = pos.x * matrix[3] + pos.y * matrix[7] + pos.z * matrix[11] + matrix[15];

    NDC.x = clipCoords.x / clipCoords.w;
    NDC.y = clipCoords.y / clipCoords.w;
    NDC.z = clipCoords.z / clipCoords.w;

    screen.x = (windowWidth / 2 * NDC.x) + (NDC.x + windowWidth / 2);
    screen.y = -(windowHeight / 2 * NDC.x) + (NDC.y + + windowHeight / 2);

    return 0;
}

std::wstring stringToWstring(const std::string& t_str)
{

    typedef std::codecvt_utf8<wchar_t> convert_type;
    std::wstring_convert<convert_type, wchar_t> converter;
    return converter.from_bytes(t_str);
}
std::wstring readpChar(DWORD address) {
    try {
        if (address != 0) {
            const size_t namesize = 200;
            char x[namesize];
            ReadProcessMemory(handle, (LPCVOID)address, &x, namesize, NULL);
            std::wstring tmpname = std::wstring(&x[0], &x[namesize]);
            wchar_t* czech = wcstok(&tmpname[0], L"\0");
            if (czech != nullptr) return czech;
        }
    }
    catch (const std::exception& exc) {}
    return std::wstring(L"\0");
}

int main()
{





    hwnd = FindWindowA("Cube 2: Sauerbraten",0);
    GetWindowThreadProcessId(hwnd, &pID);
    handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pID);

    forceinline::dx_overlay overlay(L"SDL_app", L"Cube 2: Sauerbraten", true/*sa*/);
    if (!overlay.is_initialized())
        return -1;

    forceinline::dx_renderer renderer = overlay.create_renderer();

    MSG m;
    ZeroMemory(&m, sizeof(m));

    while (m.message != WM_QUIT)
    {
        if (PeekMessage(&m, overlay.get_overlay_wnd(), NULL, NULL, PM_REMOVE)) {
            TranslateMessage(&m);
            DispatchMessage(&m);
        }

        renderer.begin_rendering();

        ReadProcessMemory(handle, (LPVOID)offsets::viewMatrix, &matrix, sizeof(matrix), 0);

        for (int i = 0; i < offsets::players; i++)
        {

         
           

        }
    }
   



}

Evet gördüğünüz üzere kodumda hiç bir sorun yoktur ancak son yerde takıldım dx_renderer.h'da bulunan draw_text çağırmak istiyorum nasıl yapabilirim tam olarak anlayamadım position olarak x,y offsets olan mı yoksa çok kafam karıştı son durumdan sonra olan oyuncu sayısı kadar onu renderlıcaktım zaten burdan sonrasını bilmiyorum konuya bilgili herkesi davet ediyorum artık öldüm bittim ne aradığım bir cevap nede bulduğum bir bilgili var iyi günler bilgili herkes yazsın :)
 
Son düzenleme:
Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Üst