Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. static Window* s_Window = nullptr; 
  2.  
  3. static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  4.  
  5.         WindowsWindow::WindowsWindow(WindowDescriptor& a_WindowDescriptor)
  6.             : Window(a_WindowDescriptor)
  7.         {
  8.             // Set static
  9.             s_Window = this;
  10.         }
  11.  
  12.         bool WindowsWindow::Initialize()
  13.         {
  14.             // Zero out the memory
  15.             WNDCLASSEX wc;
  16.             ZeroMemory(&wc, sizeof(WNDCLASSEX));
  17.  
  18.             // Fill in the struct with the needed information
  19.             wc.cbSize           = sizeof(WNDCLASSEX);
  20.             wc.style            = CS_HREDRAW | CS_VREDRAW;
  21.             wc.lpfnWndProc      = WndProc;
  22.             wc.hInstance        = GetModuleHandle(NULL);
  23.             wc.hIcon            = LoadIcon(wc.hInstance, MAKEINTRESOURCE(IDI_ICON1));
  24.             wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  25.             wc.hbrBackground    = (HBRUSH)COLOR_WINDOW;
  26.             wc.lpszClassName    = m_WindowClass.c_str();
  27.             wc.hIconSm          = LoadIcon(wc.hInstance, IDI_APPLICATION);
  28.  
  29.             // Register window class
  30.             RegisterClassEx(&wc);
  31.  
  32.             // Create the actual window instance
  33.             m_WindowHandle = CreateWindowEx(NULL, m_WindowClass.c_str(), m_WindowTitle.c_str(), WS_OVERLAPPEDWINDOW,
  34.                 m_WindowPositionX, m_WindowPositionY, m_Dimensions[m_CurrentDimension].GetWidth(), m_Dimensions[m_CurrentDimension].GetHeight(), NULL, NULL, GetModuleHandle(NULL), NULL);
  35.  
  36.             return true;
  37.         }
  38.  
  39.         bool WindowsWindow::Terminate()
  40.         {
  41.             // Destroys the window
  42.             if (m_WindowHandle)
  43.             {
  44.                 DestroyWindow(m_WindowHandle);
  45.                 m_WindowHandle = nullptr;
  46.             }
  47.  
  48.             return true;
  49.         }
  50.  
  51.         void WindowsWindow::UpdateWindow(const float a_DeltaTime)
  52.         {
  53.             MSG msg;
  54.             while (PeekMessage(&msg, m_WindowHandle, 0, 0, PM_REMOVE))
  55.             {
  56.                 // Return when painting
  57.                 // I know this is a sin...
  58.                 if (msg.message == WM_PAINT)
  59.                     return;
  60.  
  61.                 TranslateMessage(&msg);
  62.                 DispatchMessage(&msg);
  63.             }
  64.         }
  65.  
  66.         void WindowsWindow::Show()
  67.         {
  68.             ::ShowWindow(m_WindowHandle, SW_SHOW);
  69.         }
  70.  
  71.         void WindowsWindow::Hide()
  72.         {
  73.             ::ShowWindow(m_WindowHandle, SW_HIDE);
  74.         }
  75.  
  76.         static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  77.         {
  78.             // sort through and find what code to run for the message given
  79.             switch (message)
  80.             {
  81.                 // close the applCcation entirely
  82.             case WM_DESTROY:
  83.             {
  84.                 // Broadcast event
  85.                 Event::EventSystem::Broadcast<Event::WindowCloseEvent>();
  86.  
  87.                 PostQuitMessage(0);
  88.                 Chlorine::Core::Engine::GetInstance()->RequestStop();
  89.                 return 0;
  90.             }
  91.             break;
  92.             case WM_WINDOWPOSCHANGED:
  93.             case WM_WINDOWPOSCHANGING:
  94.             {
  95.                 WINDOWPOS* w = (WINDOWPOS*)(lParam);
  96.  
  97.                 std::cout << "CX: " << w->cx << ", CY: " << w->cy << std::endl;
  98.             }
  99.             break;
  100.             case WM_SIZE:
  101.             {
  102.                 if (hwnd != (HWND)s_Window->GetNativeWindowHandle())
  103.                 {
  104.                     std::cout << "hello";
  105.                 }
  106.  
  107.                 // Will only happen if it is a resizable window
  108.                 unsigned int width = LOWORD(lParam);
  109.                 unsigned int height = HIWORD(lParam);
  110.                 char         type = char(wParam);
  111.  
  112.                 // Minimize
  113.                 if (width == 0 || height == 0)
  114.                     break;
  115.  
  116.                 // Maximize
  117.                 if (width == s_Window->GetWidth() && height == s_Window->GetHeight())
  118.                     break;
  119.  
  120.                 // Set the custom dimensions
  121.                 s_Window->SetWindowDimensionID(CHLORINE_WINDOW_DIMENSION_CUSTOM);
  122.                 s_Window->SetWindowCustomDimension(width, height);
  123.  
  124.                 // Broadcast event
  125.                 Event::EventSystem::Broadcast<Event::WindowResizeEvent>((Event::WindowResizeType)type, width, height);
  126.             }
  127.             break;
  128.             case WM_MOVE:
  129.             {
  130.                 unsigned int positionX = LOWORD(lParam);
  131.                 unsigned int positionY = HIWORD(lParam);
  132.  
  133.                 // Broadcast event
  134.                 Event::EventSystem::Broadcast<Event::WindowMoveEvent>(positionX, positionY);
  135.             //  s_Window->SetWindowPosition(positionX, positionY);
  136.             }
  137.             break;
  138.             case WM_LBUTTONDOWN:
  139.             {
  140.                 // Broadcast event
  141.                 Event::EventSystem::Broadcast<Event::InputMousePressEvent>(CHLORINE_MOUSE_BUTTON_LEFT);
  142.             }
  143.             break;
  144.             case WM_LBUTTONUP:
  145.             {
  146.                 // Broadcast event
  147.                 Event::EventSystem::Broadcast<Event::InputMouseReleaseEvent>(CHLORINE_MOUSE_BUTTON_LEFT);
  148.             }
  149.             break;
  150.             case WM_INPUT:
  151.             {
  152.  
  153.             }
  154.             break;
  155.             }
  156.             // Handle any messages the switch statement didn't
  157.             return DefWindowProc(hwnd, message, wParam, lParam);
  158.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement