Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. #include "COverlay.h"
  2.  
  3. //LogFile overlayLog = Logger::GetFile("COverlay");
  4.  
  5. bool COverlay::init() {
  6.     HWND t = FindWindow("Valve001", NULL);
  7.     h_Target = t;
  8.  
  9.     if (!h_Target) {
  10.         //overlayLog.Error("Couldn't find window Valve001.");
  11.         return false;
  12.     } else{
  13.         //overlayLog.Log("Found window Valve001.");
  14.     }
  15.  
  16.     RECT client;
  17.  
  18.     GetClientRect(h_Target, &client);
  19.  
  20.     m_Width = client.right;
  21.     m_Height = client.bottom;
  22.  
  23.     WNDCLASSEX wc = { NULL };
  24.  
  25.     wc.cbSize = sizeof(WNDCLASSEX);
  26.     wc.style = CS_VREDRAW | CS_HREDRAW;
  27.     wc.lpfnWndProc = WndProc;
  28.     wc.cbClsExtra = NULL;
  29.     wc.cbWndExtra = NULL;
  30.     wc.hInstance = NULL;
  31.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  32.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  33.     wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  34.     wc.lpszMenuName = NULL;
  35.     wc.lpszClassName = (LPCSTR)OVERLAY_NAME;
  36.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  37.  
  38.     if (!RegisterClassEx(&wc)) {
  39.         //overlayLog.Error("Couldn't register class.");
  40.         return false;
  41.     }
  42.     else {
  43.         //overlayLog.Log("Succesfully registered classm");
  44.     }
  45.  
  46.     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);
  47.  
  48.     if (!h_Overlay) {
  49.         //overlayLog.Error("Couldn't create window.");
  50.         return false;
  51.     }
  52.     else {
  53.         //overlayLog.Log("Created window succesfully.");
  54.     }
  55.  
  56.     MARGINS margin = { -1, -1, -1, -1 };
  57.  
  58.     DwmExtendFrameIntoClientArea(h_Overlay, &margin);
  59.  
  60.     ShowWindow(h_Overlay, SW_SHOWDEFAULT);
  61.  
  62.     UpdateWindow(h_Overlay);
  63.  
  64.     RECT game;
  65.  
  66.     GetWindowRect(h_Target, &game);
  67.  
  68.     int w = game.right - game.left;
  69.     int h = game.bottom - game.top;
  70.  
  71.     LONG_PTR dwStyle = GetWindowLongPtr(h_Target, GWL_STYLE);
  72.  
  73.     if (dwStyle & WS_BORDER) {
  74.         int x = GetSystemMetrics(SM_CXBORDER);
  75.         int y = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYBORDER);
  76.  
  77.         game.left += x;
  78.         game.top += y;
  79.  
  80.         w -= x;
  81.         h -= y;
  82.     }
  83.  
  84.     MoveWindow(h_Overlay, game.left, game.top, w, h, TRUE);
  85.  
  86.     m_Width = w;
  87.     m_Height = h;
  88.     return initDirectX();
  89. }
  90.  
  91. bool COverlay::initDirectX() {
  92.     pPresent.EnableAutoDepthStencil = TRUE;
  93.     pPresent.AutoDepthStencilFormat = D3DFMT_D16;
  94.     pPresent.Windowed = TRUE;
  95.     pPresent.BackBufferCount = 1;
  96.     pPresent.BackBufferFormat = D3DFMT_A8R8G8B8;
  97.     pPresent.BackBufferWidth = m_Width;
  98.     pPresent.BackBufferHeight = m_Height;
  99.     pPresent.MultiSampleType = D3DMULTISAMPLE_NONE;
  100.     pPresent.SwapEffect = D3DSWAPEFFECT_DISCARD;
  101.     pPresent.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  102.     pPresent.hDeviceWindow = h_Overlay;
  103.  
  104.     pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
  105.  
  106.     if (!pDirect3D) {
  107.         //overlayLog.Error("Couldn't create d3d object.");
  108.         return false;
  109.     }
  110.     else {
  111.         //overlayLog.Log("Created d3d object succesfully.");
  112.     }
  113.  
  114.     if (FAILED(pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, h_Overlay, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pPresent, &pDevice))) {
  115.         //overlayLog.Error("Couldn't create d3d device.");
  116.         return false;
  117.     }
  118.  
  119.     if (!pDevice)  {
  120.         //overlayLog.Error("Couldn't create d3d device.");
  121.         return false;
  122.     }
  123.     else {
  124.         //overlayLog.Log("Created d3d device succesfully.");
  125.     }
  126.  
  127.     if (!pLine)
  128.         D3DXCreateLine(pDevice, &pLine);
  129.  
  130.     return true;
  131. }
  132.  
  133. bool COverlay::preFrame() {
  134.     MSG msg;
  135.     if (PeekMessageA(&msg, NULL, NULL, NULL, PM_REMOVE)) {
  136.         if (msg.message = WM_QUIT) {
  137.             return false;
  138.         }
  139.  
  140.         TranslateMessage(&msg);
  141.         DispatchMessageA(&msg);
  142.     }
  143.  
  144.     if (FAILED(pDevice->Clear(NULL, NULL, D3DCLEAR_TARGET, NULL, 1.0f, NULL))) {
  145.         //overlayLog.Error("Couldn't clear screen");
  146.         return false;
  147.     }
  148.  
  149.     if (FAILED(pDevice->BeginScene())) {
  150.         //overlayLog.Error("Couldn't begin scene");
  151.         return false;
  152.     }
  153.  
  154.     return true;
  155. }
  156.  
  157. bool COverlay::postFrame() {
  158.     if (FAILED(pDevice->EndScene())) {
  159.         //overlayLog.Error("Couldn't end scene");
  160.         return false;
  161.     }
  162.  
  163.     if (FAILED(pDevice->Present(NULL, NULL, NULL, NULL))) {
  164.         //overlayLog.Error("Couldn't present");
  165.         return false;
  166.     }
  167.  
  168.     return true;
  169. }
  170.  
  171. void COverlay::size(int &w, int &h) {
  172.     w = m_Width;
  173.     h = m_Height;
  174. }
  175.  
  176. IDirect3DDevice9* COverlay::GetDevice() {
  177.     return pDevice;
  178. }
  179.  
  180. LRESULT CALLBACK COverlay::WndProc(HWND h, UINT msg, WPARAM wPar, LPARAM lPar) {
  181.     switch (msg) {
  182.     case WM_DESTROY:
  183.         //overlayLog.Log("Got WM_DESTROY");
  184.         PostQuitMessage(NULL);
  185.         exit(0);
  186.         break;
  187.     default:
  188.         return DefWindowProcA(h, msg, wPar, lPar);
  189.         break;
  190.     }
  191.  
  192.     return NULL;
  193. }
  194.  
  195. void COverlay::DrawLine(float x1, float y1, float x2, float y2, int thickness, int r, int g, int b, int a) {
  196.     pLine->SetWidth(thickness);
  197.     D3DXVECTOR2 line[2] = { { x1, y1 }, { x2, y2 } };
  198.     pLine->Begin();
  199.     pLine->Draw(line, 2, D3DCOLOR_RGBA(r, g, b, a));
  200.     pLine->End();
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement