From 93707dbd7179de01b4ef67555735d5771898c783 Mon Sep 17 00:00:00 2001 From: Redacted Date: Mon, 14 Jul 2025 04:51:15 -0400 Subject: [PATCH] Remove need expose windows.h --- include/ReWindow/types/Window.h | 22 ++++++---------------- main.cpp | 1 - src/platform/windows/Window.cpp | 28 ++++++++++++---------------- 3 files changed, 18 insertions(+), 33 deletions(-) diff --git a/include/ReWindow/types/Window.h b/include/ReWindow/types/Window.h index e89f7f7..33e547a 100644 --- a/include/ReWindow/types/Window.h +++ b/include/ReWindow/types/Window.h @@ -1,19 +1,15 @@ #pragma once + #include #include -#include +#include #include -#include +#include #include #include #include #include -#include -// TODO: Hide this eventually if possible. -#ifdef WIN32 -#include -#endif namespace ReWindow { struct KeyboardState { std::map PressedKeys; }; @@ -49,10 +45,11 @@ public: /// RWindow is a class implementation of a platform-independent window abstraction. /// This library also provides abstractions for user-input devices, and their interaction with the window. class ReWindow::RWindow { - protected: class Platform; + friend class Platform; Platform* platform; + inline static RWindow* extant; int width = 640; @@ -242,9 +239,6 @@ public: /// Returns the vertical length of this window, in pixels. [[nodiscard]] int GetHeight() const; - /// This is unfortunately here because of the poor design of windows. Maybe once interfaces are done this won't be required anymore. - void SetSizeWithoutEvent(const std::pair& size); - /// Tells the underlying window manager to destroy this window and drop the handle. /// The window, in theory, can not be re-opened after this. void DestroyOSWindowHandle(); @@ -337,12 +331,7 @@ public: /// Updates internals to account for the latest calculated frame time. void UpdateFrameTiming(float frame_time); - -#ifdef WIN32 - static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); -#endif protected: -/// These unfortunately *have* to be public because of the poor design of the windows event loop. void processKeyRelease(Key key); void processKeyPress(Key key); /// @note This will be invoked **before** the window-close procedure begins. @@ -355,6 +344,7 @@ protected: void processFocusOut(); void processMouseMove(const std::pair& last_pos, const std::pair& new_pos); void processMouseWheel(int scrolls); + void SetSizeWithoutEvent(const std::pair& size); /// Virtual functions which *must* be overridden based on the Renderer. public: diff --git a/main.cpp b/main.cpp index e647779..c3fa5b3 100644 --- a/main.cpp +++ b/main.cpp @@ -73,6 +73,5 @@ int main() { if (window->IsFocused()) window->SetCursorCenter(); } - delete window; } \ No newline at end of file diff --git a/src/platform/windows/Window.cpp b/src/platform/windows/Window.cpp index eed0e0a..b293cda 100644 --- a/src/platform/windows/Window.cpp +++ b/src/platform/windows/Window.cpp @@ -3,7 +3,6 @@ #include #include - class ReWindow::RWindow::Platform { public: HINSTANCE hInstance; @@ -11,14 +10,12 @@ public: HDC hdc; std::pair window_size_before_fullscreen; std::pair window_position_before_fullscreen; + + static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); }; using namespace ReWindow; -// TODO get rid of this. -bool local_focused = true; -bool local_toggling_cursor_focused = false; - void RWindow::SetSize(int newWidth, int newHeight) { if (!resizable) return; @@ -41,7 +38,7 @@ void RWindow::SetSize(int newWidth, int newHeight) { void RWindow::SetCursorFocused(bool state) { - local_toggling_cursor_focused = state; + toggling_cursor_focus = state; if (state) cursor_focused = true; @@ -49,7 +46,7 @@ void RWindow::SetCursorFocused(bool state) { else if (!state && cursor_focused) { ClipCursor(nullptr); cursor_focused = false; - local_toggling_cursor_focused = false; + toggling_cursor_focus = false; } } @@ -92,10 +89,9 @@ void RWindow::PollEvents() { while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) TranslateMessage(&msg), DispatchMessage(&msg); - focused = local_focused; } -LRESULT CALLBACK RWindow::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { +LRESULT CALLBACK RWindow::Platform::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { auto* window = reinterpret_cast( GetWindowLongPtr(hwnd, GWLP_USERDATA) ); switch (uMsg) { case WM_CLOSE: { @@ -119,10 +115,10 @@ LRESULT CALLBACK RWindow::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM case WM_SETFOCUS: { window->processFocusIn(); - local_focused = true; + window->focused = true; if (window->GetCursorFocused()) - local_toggling_cursor_focused = true; + window->toggling_cursor_focus = true; // Cancels window flashing. // TODO check if we're flashing before this. @@ -136,9 +132,9 @@ LRESULT CALLBACK RWindow::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM if (window->GetCursorFocused()) { ClipCursor(nullptr); - local_toggling_cursor_focused = true; + window->toggling_cursor_focus = true; } - local_focused = false; + window->focused = false; } case WM_SETCURSOR: { @@ -226,7 +222,7 @@ LRESULT CALLBACK RWindow::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM //This is the same as "Motion Notify" in the X Window System. case WM_MOUSEMOVE: { - if (local_toggling_cursor_focused) { + if (window->toggling_cursor_focus) { RECT rect; if (GetClientRect(hwnd, &rect)) { POINT top_left = { rect.left, rect.top }; @@ -236,7 +232,7 @@ LRESULT CALLBACK RWindow::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM RECT clip_rect = { top_left.x, top_left.y, bottom_right.x, bottom_right.y }; if (ClipCursor(&clip_rect)) - local_toggling_cursor_focused = false; + window->toggling_cursor_focus = false; } } } @@ -415,7 +411,7 @@ bool OpenGLWindow::Open() { platform->hInstance = GetModuleHandle(nullptr); WNDCLASS wc = { }; - wc.lpfnWndProc = RWindow::WindowProc; + wc.lpfnWndProc = RWindow::Platform::WindowProc; wc.hInstance = platform->hInstance; wc.lpszClassName = "RWindowClass"; RegisterClass(&wc);