From 92f488986e8d56625cd0794ae0f9538b0d41923a Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Mon, 19 Feb 2024 05:41:14 -0600 Subject: [PATCH] Implemented window size saving/loading for Linux. Co-authored-by: sigonasr2 --- Adventures in Lestoria/SaveFile.cpp | 1 + Adventures in Lestoria/olcPixelGameEngine.h | 34 +++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Adventures in Lestoria/SaveFile.cpp b/Adventures in Lestoria/SaveFile.cpp index 64a1f198..36625b5e 100644 --- a/Adventures in Lestoria/SaveFile.cpp +++ b/Adventures in Lestoria/SaveFile.cpp @@ -156,6 +156,7 @@ const void SaveFile::SaveGame(){ #pragma endregion #pragma region Save System Settings + //HACK ALERT! It's possible that on X11 the actual reported window position is offset later when we set it. We will set it now to see if it moves or adjusts, and use that new position instead as the position we save. saveSystemFile["Window Pos"].SetInt(game->GetActualWindowPos().x,0); saveSystemFile["Window Pos"].SetInt(game->GetActualWindowPos().y,1); saveSystemFile["Window Size"].SetInt(game->GetWindowSize().x,0); diff --git a/Adventures in Lestoria/olcPixelGameEngine.h b/Adventures in Lestoria/olcPixelGameEngine.h index c2ccb24b..3f3ce16d 100644 --- a/Adventures in Lestoria/olcPixelGameEngine.h +++ b/Adventures in Lestoria/olcPixelGameEngine.h @@ -6575,10 +6575,12 @@ namespace olc private: X11::Display* olc_Display = nullptr; X11::Window olc_WindowRoot; + X11::Window olc_WindowParent; X11::Window olc_Window; X11::XVisualInfo* olc_VisualInfo; X11::Colormap olc_ColourMap; X11::XSetWindowAttributes olc_SetWindowAttribs; + vi2d desiredPos{30,30}; public: virtual olc::rcode ApplicationStartUp() override @@ -6718,9 +6720,21 @@ namespace olc } virtual void SetWindowPos(vi2d pos)override{ + using namespace X11; if(!ptrPGE->IsFullscreen()){ + Window rootW,parentW; + Window*childWindows; + unsigned int children; + XQueryTree(olc_Display,olc_Window,&rootW,&olc_WindowParent,&childWindows,&children); + if(childWindows!=nullptr)XFree(childWindows); + XWindowAttributes gwa; + XGetWindowAttributes(olc_Display, olc_Window, &gwa); + XWindowAttributes cwa; + XGetWindowAttributes(olc_Display, olc_WindowParent, &cwa); + int diffX=gwa.x-cwa.x; //The border is some defined size, and the window manager may be accounting for that when deciding window positions. If we take the difference of window between the two and subtract, we probably end up with a window at the right spot! + int diffY=gwa.y-cwa.y; XMapWindow(olc_Display, olc_Window); - XMoveWindow(olc_Display, olc_Window, pos.x, pos.y); + XMoveWindow(olc_Display, olc_Window, pos.x-diffX, pos.y-diffY); } } virtual void SetWindowSize(vi2d size)override{ @@ -6782,17 +6796,31 @@ namespace olc XNextEvent(olc_Display, &xev); if (xev.type == Expose) { + Window rootW,parentW; + Window*childWindows; + unsigned int children; + XQueryTree(olc_Display,olc_Window,&rootW,&olc_WindowParent,&childWindows,&children); + if(childWindows!=nullptr)XFree(childWindows); + XWindowAttributes cwa; + XGetWindowAttributes(olc_Display, olc_WindowParent, &cwa); XWindowAttributes gwa; XGetWindowAttributes(olc_Display, olc_Window, &gwa); ptrPGE->olc_UpdateWindowPos(gwa.x, gwa.y); - ptrPGE->olc_UpdateActualWindowPos(gwa.x, gwa.y); + ptrPGE->olc_UpdateActualWindowPos(cwa.x, cwa.y); ptrPGE->olc_UpdateWindowSize(gwa.width, gwa.height); } else if (xev.type == ConfigureNotify) { + Window rootW,parentW; + Window*childWindows; + unsigned int children; + XQueryTree(olc_Display,olc_Window,&rootW,&olc_WindowParent,&childWindows,&children); + if(childWindows!=nullptr)XFree(childWindows); + XWindowAttributes gwa; + XGetWindowAttributes(olc_Display, olc_WindowParent, &gwa); XConfigureEvent xce = xev.xconfigure; ptrPGE->olc_UpdateWindowPos(xce.x, xce.y); - ptrPGE->olc_UpdateActualWindowPos(xce.x, xce.y); + ptrPGE->olc_UpdateActualWindowPos(gwa.x, gwa.y); ptrPGE->olc_UpdateWindowSize(xce.width, xce.height); } else if (xev.type == KeyPress)