Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "COverlay.h"
- //LogFile overlayLog = Logger::GetFile("COverlay");
- bool COverlay::init() {
- HWND t = FindWindow("Valve001", NULL);
- h_Target = t;
- if (!h_Target) {
- //overlayLog.Error("Couldn't find window Valve001.");
- return false;
- } else{
- //overlayLog.Log("Found window Valve001.");
- }
- RECT client;
- GetClientRect(h_Target, &client);
- m_Width = client.right;
- m_Height = client.bottom;
- WNDCLASSEX wc = { NULL };
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_VREDRAW | CS_HREDRAW;
- wc.lpfnWndProc = WndProc;
- wc.cbClsExtra = NULL;
- wc.cbWndExtra = NULL;
- wc.hInstance = NULL;
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
- wc.lpszMenuName = NULL;
- wc.lpszClassName = (LPCSTR)OVERLAY_NAME;
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if (!RegisterClassEx(&wc)) {
- //overlayLog.Error("Couldn't register class.");
- return false;
- }
- else {
- //overlayLog.Log("Succesfully registered classm");
- }
- h_Overlay = CreateWindowEx(WS_EX_TOPMOST | WS_EX_COMPOSITED | WS_EX_TRANSPARENT | WS_EX_LAYERED, (LPCSTR)OVERLAY_NAME, (LPCSTR)OVERLAY_NAME, WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, m_Width, m_Height, NULL, NULL, NULL, NULL);
- if (!h_Overlay) {
- //overlayLog.Error("Couldn't create window.");
- return false;
- }
- else {
- //overlayLog.Log("Created window succesfully.");
- }
- MARGINS margin = { -1, -1, -1, -1 };
- DwmExtendFrameIntoClientArea(h_Overlay, &margin);
- ShowWindow(h_Overlay, SW_SHOWDEFAULT);
- UpdateWindow(h_Overlay);
- RECT game;
- GetWindowRect(h_Target, &game);
- int w = game.right - game.left;
- int h = game.bottom - game.top;
- LONG_PTR dwStyle = GetWindowLongPtr(h_Target, GWL_STYLE);
- if (dwStyle & WS_BORDER) {
- int x = GetSystemMetrics(SM_CXBORDER);
- int y = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYBORDER);
- game.left += x;
- game.top += y;
- w -= x;
- h -= y;
- }
- MoveWindow(h_Overlay, game.left, game.top, w, h, TRUE);
- m_Width = w;
- m_Height = h;
- return initDirectX();
- }
- bool COverlay::initDirectX() {
- pPresent.EnableAutoDepthStencil = TRUE;
- pPresent.AutoDepthStencilFormat = D3DFMT_D16;
- pPresent.Windowed = TRUE;
- pPresent.BackBufferCount = 1;
- pPresent.BackBufferFormat = D3DFMT_A8R8G8B8;
- pPresent.BackBufferWidth = m_Width;
- pPresent.BackBufferHeight = m_Height;
- pPresent.MultiSampleType = D3DMULTISAMPLE_NONE;
- pPresent.SwapEffect = D3DSWAPEFFECT_DISCARD;
- pPresent.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
- pPresent.hDeviceWindow = h_Overlay;
- pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
- if (!pDirect3D) {
- //overlayLog.Error("Couldn't create d3d object.");
- return false;
- }
- else {
- //overlayLog.Log("Created d3d object succesfully.");
- }
- if (FAILED(pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, h_Overlay, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pPresent, &pDevice))) {
- //overlayLog.Error("Couldn't create d3d device.");
- return false;
- }
- if (!pDevice) {
- //overlayLog.Error("Couldn't create d3d device.");
- return false;
- }
- else {
- //overlayLog.Log("Created d3d device succesfully.");
- }
- if (!pLine)
- D3DXCreateLine(pDevice, &pLine);
- return true;
- }
- bool COverlay::preFrame() {
- MSG msg;
- if (PeekMessageA(&msg, NULL, NULL, NULL, PM_REMOVE)) {
- if (msg.message = WM_QUIT) {
- return false;
- }
- TranslateMessage(&msg);
- DispatchMessageA(&msg);
- }
- if (FAILED(pDevice->Clear(NULL, NULL, D3DCLEAR_TARGET, NULL, 1.0f, NULL))) {
- //overlayLog.Error("Couldn't clear screen");
- return false;
- }
- if (FAILED(pDevice->BeginScene())) {
- //overlayLog.Error("Couldn't begin scene");
- return false;
- }
- return true;
- }
- bool COverlay::postFrame() {
- if (FAILED(pDevice->EndScene())) {
- //overlayLog.Error("Couldn't end scene");
- return false;
- }
- if (FAILED(pDevice->Present(NULL, NULL, NULL, NULL))) {
- //overlayLog.Error("Couldn't present");
- return false;
- }
- return true;
- }
- void COverlay::size(int &w, int &h) {
- w = m_Width;
- h = m_Height;
- }
- IDirect3DDevice9* COverlay::GetDevice() {
- return pDevice;
- }
- LRESULT CALLBACK COverlay::WndProc(HWND h, UINT msg, WPARAM wPar, LPARAM lPar) {
- switch (msg) {
- case WM_DESTROY:
- //overlayLog.Log("Got WM_DESTROY");
- PostQuitMessage(NULL);
- exit(0);
- break;
- default:
- return DefWindowProcA(h, msg, wPar, lPar);
- break;
- }
- return NULL;
- }
- void COverlay::DrawLine(float x1, float y1, float x2, float y2, int thickness, int r, int g, int b, int a) {
- pLine->SetWidth(thickness);
- D3DXVECTOR2 line[2] = { { x1, y1 }, { x2, y2 } };
- pLine->Begin();
- pLine->Draw(line, 2, D3DCOLOR_RGBA(r, g, b, a));
- pLine->End();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement