Advertisement
Guest User

MyWindow.cpp

a guest
Jul 5th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.59 KB | None | 0 0
  1. #include "MyWindow.h"
  2. #include "Engine.h"
  3. HDC CMyWindow::MainHDC = NULL;
  4. HGLRC CMyWindow::MainHGLRC = NULL;
  5. HPALETTE CMyWindow::MainPalette = NULL;
  6. HWND CMyWindow::hWindow = NULL;
  7. RECT CMyWindow::WindowRect = RECT{ 0 };
  8. HWND CMyWindow::hTargetWindow = NULL;
  9. RECT CMyWindow::TargetWindowRect = RECT{ 0 };
  10. RECT CMyWindow::TargetClientRect = RECT{ 0 };
  11. HINSTANCE* CMyWindow::WindowInstance = NULL;
  12. char* CMyWindow::WindowName = NULL;
  13. int CMyWindow::RefreshRate = (1000/200);
  14. float CMyWindow::Width = NULL;
  15. float CMyWindow::Height = NULL;
  16. float CMyWindow::CenterX = NULL;
  17. float CMyWindow::CenterY = NULL;
  18. MSG CMyWindow::Msg;
  19. GLuint  CMyWindow::base = NULL;
  20. HFONT CMyWindow::oldfont;
  21. ABC CMyWindow::glyphInfo[256];
  22.  
  23. float CMyWindow::FontHeight = 0;
  24. float CMyWindow::FontSpacing = 0;
  25. TEXTMETRIC CMyWindow::tm;
  26.  
  27. POINT CMyWindow::MousePos;
  28. MARGINS Margin = { -1, -1, -1, -1 };
  29. LRESULT APIENTRY MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  30. {
  31.     switch (message)
  32.     {
  33.     case WM_CREATE:
  34.     {
  35.         CMyWindow::MainHDC = GetDC(hWnd);
  36.         CMyWindow::SetupGL();
  37.         DwmExtendFrameIntoClientArea(CMyWindow::hWindow, &Margin);
  38.         return 0;
  39.     }
  40.     case WM_CLOSE:
  41.     {
  42.         DestroyWindow(hWnd);
  43.     }
  44.     case WM_DESTROY:
  45.     {
  46.         wglMakeCurrent(NULL, NULL);
  47.         wglDeleteContext(CMyWindow::MainHGLRC);
  48.         DeleteDC(CMyWindow::MainHDC);
  49.         ReleaseDC(hWnd, CMyWindow::MainHDC);
  50.         PostQuitMessage(0);
  51.         exit(1);
  52.         UnregisterClass(CMyWindow::WindowName, *CMyWindow::WindowInstance);
  53.     }
  54.     case WM_PAINT:
  55.     {
  56.         PAINTSTRUCT ps;
  57.         BeginPaint(hWnd, &ps);
  58.         glClear(GL_COLOR_BUFFER_BIT);
  59.  
  60.         /*
  61.         Render
  62.         */
  63.  
  64.         Engine->Render();
  65.  
  66.         SwapBuffers(CMyWindow::MainHDC);
  67.         EndPaint(hWnd, &ps);
  68.         return 0;
  69.     }
  70.     }
  71.     return DefWindowProc(hWnd, message, wParam, lParam);
  72. }
  73.  
  74. CMyWindow::~CMyWindow()
  75. {
  76. }
  77.  
  78. CMyWindow::CMyWindow()
  79. {
  80.     WindowInstance = nullptr;
  81.     MainHDC = NULL;
  82.     MainHGLRC = NULL;
  83.     MainPalette = NULL;
  84.     if (WindowName)
  85.         delete[] WindowName;
  86.     int Length = (rand() % 15) + 5;
  87.     WindowName = new char[Length + 1];
  88.     for (int i = 0; i < Length; i++)
  89.         WindowName[i] = (rand() % 200) + 50;
  90.     WindowName[Length] = 0;
  91.  
  92. }
  93.  
  94. void CMyWindow::Run()
  95. {
  96.     while (GetMessage(&Msg, NULL, 0, 0) > 0 && !GlobalVariables::ExitCheat)
  97.     {
  98.         Sleep(1);
  99.         TranslateMessage(&Msg);
  100.         DispatchMessage(&Msg);
  101.     }
  102. };
  103.  
  104. DWORD WINAPI CMyWindow::UpdateDrawings()
  105. {
  106.     while (!GlobalVariables::ExitCheat)
  107.     {
  108.         InvalidateRect(CMyWindow::hWindow, NULL, false);
  109.         Sleep(RefreshRate);
  110.     };
  111.     ExitThread(0);
  112. }
  113.  
  114. bool CMyWindow::SetInstance(HINSTANCE *Instance)
  115. {
  116.     if (WindowInstance)
  117.         return false;
  118.  
  119.     if (Instance)
  120.     {
  121.         WindowInstance = Instance;
  122.         return true;
  123.     }
  124.  
  125.     return false;
  126. }
  127.  
  128. bool CMyWindow::InitWindow()
  129. {
  130.     WNDCLASSEX WClass;
  131.     WClass.cbSize = sizeof(WNDCLASSEX);
  132.     WClass.style = 0;
  133.     WClass.lpfnWndProc = MyWndProc;
  134.     WClass.cbClsExtra = 0;
  135.     WClass.cbWndExtra = 0;
  136.     WClass.hInstance = *WindowInstance;
  137.     WClass.hIcon = NULL;
  138.     WClass.hCursor = NULL;
  139.     WClass.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
  140.     WClass.lpszMenuName = NULL;
  141.     WClass.lpszClassName = WindowName;
  142.     WClass.hIconSm = NULL;
  143.  
  144.     if (!RegisterClassEx(&WClass))
  145.     {
  146.         return false;
  147.     }
  148.     /*
  149.     hWindow = CreateWindowEx(
  150.         WS_EX_TRANSPARENT | WS_EX_TOPMOST | WS_EX_LAYERED,
  151.         WindowName,
  152.         WindowName,
  153.         WS_POPUP | WS_VISIBLE | WS_MAXIMIZE,
  154.         WindowRect.left,
  155.         WindowRect.top - 1,
  156.         Width,
  157.         Height + 1,
  158.         NULL, NULL, *WindowInstance, NULL);
  159.     */
  160.  
  161.     hWindow = CreateWindowEx(WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED, WindowName, WindowName, WS_POPUP, 1, 1, Width, Height, 0, 0, 0, 0);
  162.    
  163.     if (hWindow == NULL)
  164.     {
  165.         UnregisterClass(WindowName, *WindowInstance);
  166.         return false;
  167.     }
  168.  
  169.     if (!SetLayeredWindowAttributes(hWindow, RGB(0, 0, 0), 255, LWA_COLORKEY | LWA_ALPHA))
  170.     {
  171.         UnregisterClass(WindowName, *WindowInstance);
  172.         return false;
  173.     }
  174.  
  175.     BOOL IsEnabled;
  176.     if (DwmIsCompositionEnabled(&IsEnabled) != S_OK)
  177.     {
  178.         UnregisterClass(WindowName, *WindowInstance);
  179.         return false;
  180.     }
  181.  
  182.     if (!IsEnabled)
  183.     {
  184.         UnregisterClass(WindowName, *WindowInstance);
  185.         return false;
  186.     }
  187.  
  188.     DWM_BLURBEHIND bb = { DWM_BB_ENABLE | DWM_BB_BLURREGION, true, CreateRectRgn(0, 0, -1, -1), true };
  189.     if (DwmEnableBlurBehindWindow(hWindow, &bb) != S_OK)
  190.     {
  191.         UnregisterClass(WindowName, *WindowInstance);
  192.         return false;
  193.     }
  194.  
  195.     ShowWindow(hWindow, 1);
  196.  
  197.     return true;
  198. }
  199.  
  200. void CMyWindow::Resize()
  201. {
  202.     GetWindowRect(hTargetWindow, &TargetWindowRect);
  203.     GetClientRect(hTargetWindow, &TargetClientRect);
  204.     Width = TargetClientRect.right - TargetClientRect.left;
  205.     Height = TargetClientRect.bottom - TargetClientRect.top;
  206.     CenterX = Width / 2;
  207.     CenterY = Height / 2;
  208.     SetWindowPos(hWindow, 0, TargetWindowRect.left, TargetWindowRect.top, Width, Height, 0);
  209.     glViewport(0, 0, Width, Height);
  210.     glOrtho(TargetClientRect.left, TargetClientRect.right, TargetClientRect.bottom, TargetClientRect.top, 0, 1);
  211.  
  212. }
  213.  
  214. void CMyWindow::Resize(RECT WindowRect, RECT ClientRect)
  215. {
  216.  
  217.     Width = ClientRect.right - ClientRect.left;
  218.     Height = ClientRect.bottom - ClientRect.top;
  219.     SetWindowPos(hWindow, 0, WindowRect.left, WindowRect.top, Width, Height, 0);
  220.     glViewport(WindowRect.left, WindowRect.top, Width, Height);
  221.     glOrtho(ClientRect.left, ClientRect.right, ClientRect.bottom, ClientRect.top, 0, 1);
  222. }
  223.  
  224. bool CMyWindow::SetupGL()
  225. {
  226.     PIXELFORMATDESCRIPTOR pfd = {
  227.         sizeof(PIXELFORMATDESCRIPTOR),
  228.         1,                                  // Version Number
  229.         PFD_DRAW_TO_WINDOW |              // Format Must Support Window
  230.         PFD_SUPPORT_OPENGL |              // Format Must Support OpenGL
  231.         //        PFD_SUPPORT_GDI|                  // Format Must Support GDI
  232.         PFD_SUPPORT_COMPOSITION |         // Format Must Support Composition
  233.         PFD_DOUBLEBUFFER,                 // Must Support Double Buffering
  234.         PFD_TYPE_RGBA,                    // Request An RGBA Format
  235.         32,                               // Select Our Color Depth
  236.         0, 0, 0, 0, 0, 0,                 // Color Bits Ignored
  237.         8,                                // An Alpha Buffer
  238.         0,                                // Shift Bit Ignored
  239.         0,                                // No Accumulation Buffer
  240.         0, 0, 0, 0,                       // Accumulation Bits Ignored
  241.         0,                               // No Z-Buffer (Depth Buffer)
  242.         8,                                // Some Stencil Buffer
  243.         0,                                // No Auxiliary Buffer
  244.         PFD_MAIN_PLANE,                   // Main Drawing Layer
  245.         0,                                // Reserved
  246.         0, 0, 0                           // Layer Masks Ignored
  247.     };
  248.     int PixelFormat = ChoosePixelFormat(MainHDC, &pfd);
  249.     if (!PixelFormat)
  250.     {
  251.         return false;
  252.     }
  253.     if (!SetPixelFormat(MainHDC, PixelFormat, &pfd))
  254.     {
  255.         return false;
  256.     }
  257.     if (!DescribePixelFormat(MainHDC, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd))
  258.     {
  259.         return false;
  260.     }
  261.     MainHGLRC = wglCreateContext(MainHDC);
  262.     if (!MainHGLRC)
  263.     {
  264.         return false;
  265.     }
  266.     if (!wglMakeCurrent(MainHDC, MainHGLRC))
  267.     {
  268.         return false;
  269.     }
  270.  
  271.     glEnable(GL_ALPHA_TEST);
  272.     //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  273.     //glEnable(GL_BLEND);
  274.     glEnable(GL_LINE_SMOOTH);
  275.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  276.     glEnable(GL_BLEND);
  277.  
  278.     glMatrixMode(GL_PROJECTION);
  279.     Resize();
  280.     glClearColor(0.f, 0.f, 0.f, 0.f);
  281.  
  282.     return true;
  283. }
  284.  
  285. bool CMyWindow::Init(HINSTANCE *Instance, HWND _hTargetWindow)
  286. {
  287.     if (!_hTargetWindow)
  288.         return false;
  289.    
  290.     hTargetWindow = _hTargetWindow;
  291.  
  292.     if (!SetInstance(Instance))
  293.         return false;
  294.     Resize();
  295.    
  296.     if (!InitWindow())
  297.         return false;
  298.  
  299.     BuildFont();
  300.  
  301.     return true;
  302. }
  303.  
  304. GLvoid CMyWindow::BuildFont(GLvoid)
  305. {
  306.     /**/
  307.     HFONT   font;                                   // Used For Good House Keeping
  308.  
  309.     base = glGenLists(1000);                                // Storage For 96 Characters
  310.  
  311.     font = CreateFont(14,                           // Height Of Font
  312.         0,                              // Width Of Font
  313.         0,                              // Angle Of Escapement
  314.         0,                              // Orientation Angle
  315.         FW_BOLD,                        // Font Weight
  316.         FALSE,                          // Italic
  317.         FALSE,                          // Underline
  318.         FALSE,                          // Strikeout
  319.         ANSI_CHARSET,                   // Character Set Identifier
  320.         OUT_TT_PRECIS,                  // Output Precision
  321.         CLIP_DEFAULT_PRECIS,            // Clipping Precision
  322.         ANTIALIASED_QUALITY,            // Output Quality
  323.         FF_DONTCARE | DEFAULT_PITCH,        // Family And Pitch
  324.         "Comic Sans");
  325.         //"Courier New");                   // Font Name
  326.  
  327.     //oldfont = (HFONT)SelectObject(MainHDC, font);           // Selects The Font We Want
  328.     //wglUseFontBitmaps(MainHDC, 32, 96, base);             // Builds 96 Characters Starting At Character 32                                // Delete The Font
  329.  
  330.     oldfont = (HFONT)SelectObject(MainHDC, font);           // Selects The Font We Want
  331.     wglUseFontBitmaps(MainHDC, 0, 255, base);  
  332.     GetCharABCWidths(MainHDC, 0, 255, &glyphInfo[0]);
  333.     GetTextMetrics(MainHDC, &tm);
  334.  
  335.     FontHeight = tm.tmHeight/2;
  336.     FontSpacing = FontHeight+3;
  337.  
  338.     if (oldfont)
  339.         SelectObject(MainHDC, oldfont);                         // Selects The Font We Want
  340.    
  341.     DeleteObject(font);
  342. }
  343.  
  344. GLvoid CMyWindow::KillFont(GLvoid)                                 
  345. {
  346.     glDeleteLists(base, 255);                          
  347. }
  348.  
  349. GLvoid CMyWindow::glPrint(const char *fmt, ...)                 // Custom GL "Print" Routine
  350. {
  351.     char        text[256];                              // Holds Our String
  352.     va_list     ap;                                     // Pointer To List Of Arguments
  353.  
  354.     if (fmt == NULL)                                    // If There's No Text
  355.         return;                                         // Do Nothing
  356.  
  357.     va_start(ap, fmt);                                  // Parses The String For Variables
  358.     vsprintf(text, fmt, ap);                        // And Converts Symbols To Actual Numbers
  359.     va_end(ap);                                         // Results Are Stored In Text
  360.  
  361.     glPushAttrib(GL_LIST_BIT);                          // Pushes The Display List Bits
  362.     glListBase(base - 32);                              // Sets The Base Character to 32
  363.     glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);  // Draws The Display List Text
  364.     glPopAttrib();                                      // Pops The Display List Bits
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement