From e8bcb727712612d388dbda177448371b3ffed277 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Sat, 30 Oct 2021 15:24:54 +0100 Subject: [PATCH] Save / load window SDL2 window position. Signed-off-by: Andrea Odetti --- source/frontends/common2/programoptions.cpp | 1 + source/frontends/common2/programoptions.h | 1 + source/frontends/common2/utils.cpp | 37 +++++++++++++++++++++ source/frontends/common2/utils.h | 3 ++ source/frontends/sdl/main.cpp | 3 ++ source/frontends/sdl/sdlframe.cpp | 12 +++++++ source/frontends/sdl/sdlframe.h | 1 + 7 files changed, 58 insertions(+) diff --git a/source/frontends/common2/programoptions.cpp b/source/frontends/common2/programoptions.cpp index 96ea2b94..7c4a73a8 100644 --- a/source/frontends/common2/programoptions.cpp +++ b/source/frontends/common2/programoptions.cpp @@ -189,6 +189,7 @@ namespace common2 if (vm.count("geometry")) { + options.geometry.empty = false; parseGeometry(vm["geometry"].as(), options.geometry); } diff --git a/source/frontends/common2/programoptions.h b/source/frontends/common2/programoptions.h index e5f9579b..29e68833 100644 --- a/source/frontends/common2/programoptions.h +++ b/source/frontends/common2/programoptions.h @@ -8,6 +8,7 @@ namespace common2 struct Geometry { + bool empty = true; int width; int height; int x; diff --git a/source/frontends/common2/utils.cpp b/source/frontends/common2/utils.cpp index 0c264ea2..98438566 100644 --- a/source/frontends/common2/utils.cpp +++ b/source/frontends/common2/utils.cpp @@ -1,5 +1,6 @@ #include "StdAfx.h" #include "frontends/common2/utils.h" +#include "frontends/common2/programoptions.h" #include "linux/network/uthernet2.h" @@ -24,6 +25,7 @@ #include "SaveState.h" #include "RGBMonitor.h" #include "Riff.h" +#include "Registry.h" #include "Utilities.h" #include "Interface.h" #include "Debugger/Debug.h" @@ -133,4 +135,39 @@ namespace common2 RiffFinishWriteFile(); } + void loadGeometryFromRegistry(const std::string §ion, Geometry & geometry) + { + if (geometry.empty) // otherwise it was user provided + { + const std::string path = section + "\\geometry"; + const auto loadValue = [&path](const char * name, int & dest) + { + DWORD value; + if (RegLoadValue(path.c_str(), name, TRUE, &value)) + { + dest = value; + } + }; + + loadValue("width", geometry.width); + loadValue("height", geometry.height); + loadValue("x", geometry.x); + loadValue("y", geometry.y); + } + } + + void saveGeometryToRegistry(const std::string §ion, const Geometry & geometry) + { + const std::string path = section + "\\geometry"; + const auto saveValue = [&path](const char * name, const int source) + { + RegSaveValue(path.c_str(), name, TRUE, source); + }; + + saveValue("width", geometry.width); + saveValue("height", geometry.height); + saveValue("x", geometry.x); + saveValue("y", geometry.y); + } + } diff --git a/source/frontends/common2/utils.h b/source/frontends/common2/utils.h index 9cf3d42c..5986d9d9 100644 --- a/source/frontends/common2/utils.h +++ b/source/frontends/common2/utils.h @@ -4,6 +4,7 @@ namespace common2 { + struct Geometry; void setSnapshotFilename(const std::string & filename, const bool load); @@ -11,4 +12,6 @@ namespace common2 void InitialiseEmulator(); void DestroyEmulator(); + void loadGeometryFromRegistry(const std::string §ion, Geometry & geometry); + void saveGeometryToRegistry(const std::string §ion, const Geometry & geometry); } diff --git a/source/frontends/sdl/main.cpp b/source/frontends/sdl/main.cpp index d5d0a544..51619084 100644 --- a/source/frontends/sdl/main.cpp +++ b/source/frontends/sdl/main.cpp @@ -67,6 +67,7 @@ void run_sdl(int argc, const char * argv []) const int sw = video.GetFrameBufferBorderlessWidth(); const int sh = video.GetFrameBufferBorderlessHeight(); + options.geometry.empty = true; options.geometry.width = sw * 2; options.geometry.height = sh * 2; options.geometry.x = SDL_WINDOWPOS_UNDEFINED; @@ -79,6 +80,8 @@ void run_sdl(int argc, const char * argv []) const LoggerContext logger(options.log); const RegistryContext registryContext(CreateFileRegistry(options)); + common2::loadGeometryFromRegistry("sa2", options.geometry); + std::shared_ptr frame; if (options.imgui) { diff --git a/source/frontends/sdl/sdlframe.cpp b/source/frontends/sdl/sdlframe.cpp index 0c5f7795..f708a4a9 100644 --- a/source/frontends/sdl/sdlframe.cpp +++ b/source/frontends/sdl/sdlframe.cpp @@ -136,6 +136,18 @@ namespace sa2 { } + void SDLFrame::Destroy() + { + if (!myFullscreen) + { + common2::Geometry geometry; + SDL_GetWindowPosition(myWindow.get(), &geometry.x, &geometry.y); + SDL_GetWindowSize(myWindow.get(), &geometry.width, &geometry.height); + saveGeometryToRegistry("sa2", geometry); + common2::CommonFrame::Destroy(); + } + } + void SDLFrame::setGLSwapInterval(const int interval) { const int current = SDL_GL_GetSwapInterval(); diff --git a/source/frontends/sdl/sdlframe.h b/source/frontends/sdl/sdlframe.h index 57281df7..9c69448c 100644 --- a/source/frontends/sdl/sdlframe.h +++ b/source/frontends/sdl/sdlframe.h @@ -19,6 +19,7 @@ namespace sa2 SDLFrame(const common2::EmulatorOptions & options); void Initialize() override; + void Destroy() override; void FrameRefreshStatus(int drawflags) override; int FrameMessageBox(LPCSTR lpText, LPCSTR lpCaption, UINT uType) override;