WIndows fix bug in RestoreFromFullscreen
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m39s

This commit is contained in:
2025-07-12 17:56:33 -04:00
parent 13dfa1216d
commit 3862fb602f
5 changed files with 144 additions and 92 deletions

View File

@@ -220,13 +220,12 @@ public:
/// @note Execution of the parent window is stopped while the message box is up. /// @note Execution of the parent window is stopped while the message box is up.
void DialogOK(const std::string& title, const std::string& message); void DialogOK(const std::string& title, const std::string& message);
/// Sets whether or not to make the window fullscreen. /// Sets whether the window is fullscreen.
/// @note This is implemented per-OS, and as such, it simply requests the OS to do what we want. No guarantee about follow-through can be given.
void SetFullscreen(bool fs); void SetFullscreen(bool fs);
/// Sets whether or not to make the window resizable. /// Sets whether the window can be resized by the user.
/// @note This is implemented per-OS, and as such, it simply requests the OS to do what we want. No guarantee about follow-through can be given. /// @note Resize events will still be sent if fullscreen is toggled.
void SetResizable(bool resizable); void DisableResizing();
/// Sets the title of this window. /// Sets the title of this window.
void SetTitle(const std::string& title); void SetTitle(const std::string& title);
@@ -265,7 +264,7 @@ public:
void SetSize(const std::pair<int, int>& size); void SetSize(const std::pair<int, int>& size);
/// Returns the position of the window's top-left corner relative to the display /// Returns the position of the window's top-left corner relative to the display
[[nodiscard]] std::pair<int, int> GetPos() const; [[nodiscard]] std::pair<int, int> GetPosition() const;
/// Returns the known size of the window, in {x,y} pixel measurement. /// Returns the known size of the window, in {x,y} pixel measurement.
[[nodiscard]] std::pair<int, int> GetSize() const; [[nodiscard]] std::pair<int, int> GetSize() const;

View File

@@ -14,8 +14,10 @@ class MyWindow : public OpenGLWindow {
void OnMouseMove(const MouseMoveEvent& e) override {} void OnMouseMove(const MouseMoveEvent& e) override {}
void OnKeyDown(const KeyDownEvent& e) override { void OnKeyDown(const KeyDownEvent& e) override {
if (e.key == Keys::F11) if (e.key == Keys::F11) {
SetFullscreen(!IsFullscreen()); SetFullscreen(!IsFullscreen());
std::cout << "ran" << std::endl;
}
} }
bool OnResizeRequest(const WindowResizeRequestEvent& e) override { return true; } bool OnResizeRequest(const WindowResizeRequestEvent& e) override { return true; }
@@ -53,7 +55,7 @@ int main() {
Logger::Debug("TODO: Cannot set flags until after window is open"); Logger::Debug("TODO: Cannot set flags until after window is open");
window->SetVsyncEnabled(true); window->SetVsyncEnabled(true);
window->SetResizable(true); window->DisableResizing();
window->SetKeyRepeatEnabled(false); window->SetKeyRepeatEnabled(false);
Logger::Debug(std::format Logger::Debug(std::format

View File

@@ -23,6 +23,7 @@ public:
XWMHints* wm_hints = nullptr; XWMHints* wm_hints = nullptr;
bool window_visible = true; bool window_visible = true;
std::pair<int, int> position = { 0, 0 }; std::pair<int, int> position = { 0, 0 };
std::pair<int, int> size_before_fullscreen {0, 0};
}; };
@@ -197,17 +198,15 @@ void RWindow::SetCursorVisible(bool cursor_enable) {
XUndefineCursor(platform->display, platform->window); XUndefineCursor(platform->display, platform->window);
} }
void RWindow::SetResizable(bool sizable) { void RWindow::DisableResizing() {
XGetWindowAttributes(platform->display, platform->window, &platform->windowAttributes); XGetWindowAttributes(platform->display, platform->window, &platform->windowAttributes);
this->resizable = sizable; platform->hints.flags = PMinSize | PMaxSize;
if (!sizable) { platform->hints.min_width = platform->hints.max_width = platform->windowAttributes.width;
Logger::Debug("Once you've done this you cannot make it resizable again."); platform->hints.min_height = platform->hints.max_height = platform->windowAttributes.height;
platform->hints.flags = PMinSize | PMaxSize; XSetWMNormalHints(platform->display, platform->window, &platform->hints);
platform->hints.min_width = platform->hints.max_width = platform->windowAttributes.width;
platform->hints.min_height = platform->hints.max_height = platform->windowAttributes.height; this->resizable = false;
XSetWMNormalHints(platform->display, platform->window, &platform->hints);
}
} }
void RWindow::PollEvents() { void RWindow::PollEvents() {
@@ -411,16 +410,13 @@ bool RWindow::IsVisible() const {
return platform->window_visible; return platform->window_visible;
} }
std::pair<int, int> RWindow::GetSize() const {
return { this->width, this->height };
}
// TODO: implement integer std::pair<int, int>/3 types // TODO: implement integer std::pair<int, int>/3 types
std::pair<int, int> RWindow::GetPos() const { std::pair<int, int> RWindow::GetPosition() const {
return platform->position; return platform->position;
} }
std::pair<int, int> RWindow::GetSize() const { return { this->width, this->height }; }
void RWindow::SetPosition(int x, int y) { void RWindow::SetPosition(int x, int y) {
XMoveWindow(platform->display, platform->window, x, y); XMoveWindow(platform->display, platform->window, x, y);
platform->position = {x, y}; platform->position = {x, y};
@@ -431,6 +427,19 @@ void RWindow::SetPosition(const std::pair<int, int>& pos) {
} }
void RWindow::Fullscreen() { void RWindow::Fullscreen() {
platform->size_before_fullscreen = GetSize();
if (!this->resizable) {
XSizeHints hints;
hints.flags = PMinSize | PMaxSize;
hints.min_width = 0;
hints.min_height = 0;
hints.max_width = 100000;
hints.max_height = 100000;
XSetWMNormalHints(platform->display, platform->window, &hints);
XFlush(platform->display);
}
Atom wm_state = XInternAtom(platform->display, "_NET_WM_STATE", False); Atom wm_state = XInternAtom(platform->display, "_NET_WM_STATE", False);
Atom wm_fullscreen = XInternAtom(platform->display, "_NET_WM_STATE_FULLSCREEN", False); Atom wm_fullscreen = XInternAtom(platform->display, "_NET_WM_STATE_FULLSCREEN", False);
@@ -450,13 +459,11 @@ void RWindow::Fullscreen() {
xev.xclient.data.l[3] = 1; xev.xclient.data.l[3] = 1;
xev.xclient.data.l[4] = 0; xev.xclient.data.l[4] = 0;
XSendEvent( XSendEvent(platform->display,
platform->display, DefaultRootWindow(platform->display),
DefaultRootWindow(platform->display), False,
False, SubstructureNotifyMask | SubstructureRedirectMask,
SubstructureNotifyMask | SubstructureRedirectMask, &xev);
&xev
);
XFlush(platform->display); XFlush(platform->display);
fullscreen_mode = true; fullscreen_mode = true;
@@ -491,7 +498,19 @@ void RWindow::RestoreFromFullscreen() {
&xev &xev
); );
if (!this->resizable) {
XSizeHints hints;
hints.flags = PMinSize | PMaxSize;
hints.min_width = hints.max_width = platform->size_before_fullscreen.first;
hints.min_height = hints.max_height = platform->size_before_fullscreen.second;
XSetWMNormalHints(platform->display, platform->window, &hints);
this->width = platform->size_before_fullscreen.first;
this->height = platform->size_before_fullscreen.second;
}
XFlush(platform->display); XFlush(platform->display);
fullscreen_mode = false; fullscreen_mode = false;
Logger::Debug(std::format("Restored '{}' from Fullscreen", this->title)); Logger::Debug(std::format("Restored '{}' from Fullscreen", this->title));
} }

View File

@@ -3,6 +3,7 @@
#include <ReWindow/Logger.h> #include <ReWindow/Logger.h>
using namespace ReWindow; using namespace ReWindow;
RWindow::~RWindow() { RWindow::~RWindow() {
if (open) if (open)
DestroyOSWindowHandle(); DestroyOSWindowHandle();
@@ -112,21 +113,21 @@ int RWindow::GetWidth() const { return this->width; }
int RWindow::GetHeight() const { return this->height; } int RWindow::GetHeight() const { return this->height; }
bool RWindow::IsResizable() const { return resizable; } bool RWindow::IsResizable() const { return resizable; }
bool RWindow::IsFullscreen() const { return fullscreen_mode; } bool RWindow::IsFullscreen() const { return fullscreen_mode; }
bool RWindow::IsFocused() const { return focused; } bool RWindow::IsFocused() const { return focused; }
bool RWindow::IsVsyncEnabled() const { return vsync; } bool RWindow::IsVsyncEnabled() const { return vsync; }
float RWindow::GetDeltaTime() const { return delta_time; } float RWindow::GetDeltaTime() const { return delta_time; }
float RWindow::GetRefreshRate() const { return refresh_rate; } float RWindow::GetRefreshRate() const { return refresh_rate; }
bool RWindow::SetCursorPosition(const int x, const int y) { return SetCursorPosition({x, y}); } bool RWindow::SetCursorPosition(const int x, const int y) { return SetCursorPosition({x, y}); }
unsigned long long RWindow::GetRefreshCount() const { return refresh_count; } unsigned long long RWindow::GetRefreshCount() const { return refresh_count; }
void RWindow::SetSizeWithoutEvent(const std::pair<int, int>& size) { void RWindow::SetSizeWithoutEvent(const std::pair<int, int>& size) {
width = size.first; width = size.first;
@@ -152,15 +153,14 @@ bool RWindow::IsMouseButtonDown(const MouseButton& button) const {
} }
void RWindow::ManagedRefresh() void RWindow::ManagedRefresh() {
{ auto begin = GetTimestamp();
auto begin = GetTimestamp(); Refresh();
Refresh(); auto end = GetTimestamp();
auto end = GetTimestamp();
float dt = ComputeElapsedFrameTimeSeconds(begin, end); float dt = ComputeElapsedFrameTimeSeconds(begin, end);
UpdateFrameTiming(dt); UpdateFrameTiming(dt);
} }
void RWindow::Refresh() { void RWindow::Refresh() {
@@ -187,7 +187,7 @@ float RWindow::ComputeElapsedFrameTimeSeconds(std::chrono::steady_clock::time_po
return frame_time_s; return frame_time_s;
} }
std::chrono::steady_clock::time_point RWindow::GetTimestamp() { std::chrono::steady_clock::time_point RWindow::GetTimestamp() {
return std::chrono::steady_clock::now(); return std::chrono::steady_clock::now();
} }
@@ -204,8 +204,7 @@ void RWindow::UpdateFrameTiming(float frame_time) {
} }
void RWindow::processMouseWheel(int scrolls) void RWindow::processMouseWheel(int scrolls) {
{
currentMouse.Wheel += scrolls; currentMouse.Wheel += scrolls;
auto ev = MouseWheelEvent(scrolls); auto ev = MouseWheelEvent(scrolls);
OnMouseWheel(ev); OnMouseWheel(ev);
@@ -214,46 +213,46 @@ void RWindow::processMouseWheel(int scrolls)
previousMouse.Wheel = currentMouse.Wheel; previousMouse.Wheel = currentMouse.Wheel;
} }
void RWindow::Close() { void RWindow::Close() {
closing = true; closing = true;
processOnClose(); processOnClose();
} }
void RWindow::ForceClose() { void RWindow::ForceClose() {
Close(); Close();
DestroyOSWindowHandle(); DestroyOSWindowHandle();
} }
bool MouseState::IsDown(const MouseButton& btn) const { bool MouseState::IsDown(const MouseButton& btn) const {
if (btn == MouseButtons::Left) return Buttons.LMB; if (btn == MouseButtons::Left) return Buttons.LMB;
if (btn == MouseButtons::Right) return Buttons.RMB; if (btn == MouseButtons::Right) return Buttons.RMB;
if (btn == MouseButtons::Middle) return Buttons.MMB; if (btn == MouseButtons::Middle) return Buttons.MMB;
if (btn == MouseButtons::Mouse4) return Buttons.SideButton1; if (btn == MouseButtons::Mouse4) return Buttons.SideButton1;
if (btn == MouseButtons::Mouse5) return Buttons.SideButton2; if (btn == MouseButtons::Mouse5) return Buttons.SideButton2;
//if (btn == MouseButtons::MWheelUp) return Buttons.MWheelUp; //if (btn == MouseButtons::MWheelUp) return Buttons.MWheelUp;
//if (btn == MouseButtons::MWheelDown) return Buttons.MWheelDown; //if (btn == MouseButtons::MWheelDown) return Buttons.MWheelDown;
return false; // Unknown button? return false; // Unknown button?
} }
void MouseState::Set(const MouseButton& btn, bool state) { void MouseState::Set(const MouseButton& btn, bool state) {
if (btn == MouseButtons::Left) Buttons.LMB = state; if (btn == MouseButtons::Left) Buttons.LMB = state;
if (btn == MouseButtons::Right) Buttons.RMB = state; if (btn == MouseButtons::Right) Buttons.RMB = state;
if (btn == MouseButtons::Middle) Buttons.MMB = state; if (btn == MouseButtons::Middle) Buttons.MMB = state;
if (btn == MouseButtons::Mouse4) Buttons.SideButton1 = state; if (btn == MouseButtons::Mouse4) Buttons.SideButton1 = state;
if (btn == MouseButtons::Mouse5) Buttons.SideButton2 = state; if (btn == MouseButtons::Mouse5) Buttons.SideButton2 = state;
//if (btn == MouseButtons::MWheelUp) Buttons.MWheelUp = state; //if (btn == MouseButtons::MWheelUp) Buttons.MWheelUp = state;
//if (btn == MouseButtons::MWheelDown) Buttons.MWheelDown = state; //if (btn == MouseButtons::MWheelDown) Buttons.MWheelDown = state;
} }
bool &MouseState::operator [](const MouseButton& btn) { bool &MouseState::operator [](const MouseButton& btn) {
if (btn == MouseButtons::Left) return Buttons.LMB; if (btn == MouseButtons::Left) return Buttons.LMB;
if (btn == MouseButtons::Right) return Buttons.RMB; if (btn == MouseButtons::Right) return Buttons.RMB;
if (btn == MouseButtons::Middle) return Buttons.MMB; if (btn == MouseButtons::Middle) return Buttons.MMB;
if (btn == MouseButtons::Mouse4) return Buttons.SideButton1; if (btn == MouseButtons::Mouse4) return Buttons.SideButton1;
if (btn == MouseButtons::Mouse5) return Buttons.SideButton2; if (btn == MouseButtons::Mouse5) return Buttons.SideButton2;
//if (btn == MouseButtons::MWheelUp) return Buttons.MWheelUp; //if (btn == MouseButtons::MWheelUp) return Buttons.MWheelUp;
//if (btn == MouseButtons::MWheelDown) return Buttons.MWheelDown; //if (btn == MouseButtons::MWheelDown) return Buttons.MWheelDown;
throw std::invalid_argument("Attempted to handle unmapped mouse button."); throw std::invalid_argument("Attempted to handle unmapped mouse button.");
} }

View File

@@ -9,6 +9,8 @@ public:
HINSTANCE hInstance; HINSTANCE hInstance;
HWND hwnd; HWND hwnd;
HDC hdc; HDC hdc;
std::pair<int, int> window_size_before_fullscreen;
std::pair<int, int> window_position_before_fullscreen;
}; };
using namespace ReWindow; using namespace ReWindow;
@@ -253,16 +255,18 @@ void RWindow::DestroyOSWindowHandle() {
DestroyWindow(platform->hwnd); DestroyWindow(platform->hwnd);
} }
void RWindow::SetResizable(bool resizable) { void RWindow::DisableResizing() {
if (!resizable) { RECT rect;
RECT rect; GetWindowRect(platform->hwnd, &rect);
GetWindowRect(platform->hwnd, &rect);
LONG style = GetWindowLong(platform->hwnd, GWL_STYLE); LONG style = GetWindowLong(platform->hwnd, GWL_STYLE);
style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX); style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
SetWindowLong(platform->hwnd, GWL_STYLE, style);
SetWindowPos(platform->hwnd, nullptr, rect.left, rect.top, rect.right - rect.left, SetWindowLong(platform->hwnd, GWL_STYLE, style);
rect.bottom - rect.top, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER); SetWindowPos(platform->hwnd, nullptr, rect.left, rect.top, rect.right - rect.left,
} rect.bottom - rect.top, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER);
this->resizable = false;
} }
void RWindow::SetSize(int newWidth, int newHeight) { void RWindow::SetSize(int newWidth, int newHeight) {
@@ -292,13 +296,20 @@ void RWindow::SetKeyRepeatEnabled(bool state) {
key_repeat = state; key_repeat = state;
} }
std::pair<int, int> RWindow::GetSize() const { std::pair<int, int> RWindow::GetSize() const {
RECT rect; RECT rect;
GetClientRect(platform->hwnd, &rect); GetClientRect(platform->hwnd, &rect);
return { (rect.right - rect.left), (rect.bottom - rect.top) }; return { (rect.right - rect.left), (rect.bottom - rect.top) };
} }
std::pair<int, int> RWindow::GetPosition() const {
RECT rect;
if (GetWindowRect(platform->hwnd, &rect))
return { rect.left, rect.top };
return { -1, -1 };
}
void RWindow::SetPosition(int x, int y) { void RWindow::SetPosition(int x, int y) {
SetWindowPos(platform->hwnd, nullptr, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER); SetWindowPos(platform->hwnd, nullptr, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
} }
@@ -308,16 +319,38 @@ void RWindow::SetPosition(const std::pair<int, int>& pos) {
} }
void RWindow::Fullscreen() { void RWindow::Fullscreen() {
// Implement fullscreen platform->window_position_before_fullscreen = GetPosition();
platform->window_size_before_fullscreen = GetSize();
platform->window_position_before_fullscreen = this->GetPosition();
SetWindowLong(platform->hwnd, GWL_STYLE, GetWindowLong(platform->hwnd, GWL_STYLE) & ~WS_OVERLAPPEDWINDOW); SetWindowLong(platform->hwnd, GWL_STYLE, GetWindowLong(platform->hwnd, GWL_STYLE) & ~WS_OVERLAPPEDWINDOW);
SetWindowPos(platform->hwnd, HWND_TOP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_FRAMECHANGED | SWP_NOOWNERZORDER); SetWindowPos(platform->hwnd, HWND_TOP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_FRAMECHANGED | SWP_NOOWNERZORDER);
fullscreen_mode = true; fullscreen_mode = true;
} }
void RWindow::RestoreFromFullscreen() { void RWindow::RestoreFromFullscreen() {
// Implement restore from fullscreen
SetWindowLong(platform->hwnd, GWL_STYLE, GetWindowLong(platform->hwnd, GWL_STYLE) | WS_OVERLAPPEDWINDOW); SetWindowLong(platform->hwnd, GWL_STYLE, GetWindowLong(platform->hwnd, GWL_STYLE) | WS_OVERLAPPEDWINDOW);
SetWindowPos(platform->hwnd, nullptr, 0, 0, width, height, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER);
RECT rect = {0, 0, platform->window_size_before_fullscreen.first, platform->window_size_before_fullscreen.second };
AdjustWindowRectEx(&rect, GetWindowLong(platform->hwnd, GWL_STYLE), FALSE, GetWindowLong(platform->hwnd, GWL_EXSTYLE));
int window_width = rect.right - rect.left;
int window_height = rect.bottom - rect.top;
SetWindowPos(
platform->hwnd,
nullptr,
platform->window_position_before_fullscreen.first,
platform->window_position_before_fullscreen.second,
window_width,
window_height,
SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER
);
DisableResizing();
this->width = platform->window_position_before_fullscreen.first;
this->height = platform->window_position_before_fullscreen.second;
fullscreen_mode = false; fullscreen_mode = false;
} }