From 3992fc08bfccba0ed13706c23733d8edd1af8cb3 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 28 Feb 2025 11:34:51 +0100 Subject: [PATCH] =?UTF-8?q?Afegides=20comprobacions=20de=20valors=20per=20?= =?UTF-8?q?al=20fitxer=20de=20configuraci=C3=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/options.cpp | 125 +++++++++++++++++++++++++++++++++++---------- source/screen.cpp | 15 ++++-- source/screen.h | 9 ++-- 3 files changed, 115 insertions(+), 34 deletions(-) diff --git a/source/options.cpp b/source/options.cpp index aa4012d1..4ab1e5d8 100644 --- a/source/options.cpp +++ b/source/options.cpp @@ -53,8 +53,13 @@ bool loadOptionsFromFile(const std::string &file_path) while (std::getline(file, line)) { // Elimina espacios en blanco iniciales y finales - line = std::string(std::find_if(line.begin(), line.end(), [](int ch) { return !std::isspace(ch); }), line.end()); - line.erase(std::find_if(line.rbegin(), line.rend(), [](int ch) { return !std::isspace(ch); }).base(), line.end()); + line = std::string(std::find_if(line.begin(), line.end(), [](int ch) + { return !std::isspace(ch); }), + line.end()); + line.erase(std::find_if(line.rbegin(), line.rend(), [](int ch) + { return !std::isspace(ch); }) + .base(), + line.end()); // Ignora líneas vacías o comentarios if (line.empty() || line[0] == '#') @@ -107,7 +112,6 @@ bool loadOptionsFromFile(const std::string &file_path) return success; } - // Guarda las opciones en un fichero bool saveOptionsToFile(const std::string &file_path) { @@ -169,36 +173,105 @@ bool saveOptionsToFile(const std::string &file_path) return success; } -// Establece las opciones bool setOptions(const std::string &var, const std::string &value) { - static const std::unordered_map> optionHandlers = { - {"version", [](std::string v) + static const std::unordered_map> optionHandlers = { + {"version", [](const std::string &v) { options.version = v; }}, - {"keys", [](std::string v) - { options.keys = static_cast(safeStoi(v, static_cast(ControlScheme::CURSOR))); }}, - {"window.zoom", [](std::string v) - { options.window.zoom = safeStoi(v, 1); }}, - {"video.mode", [](std::string v) - { options.video.mode = safeStoi(v, 0); }}, - {"video.filter", [](std::string v) - { options.video.filter = static_cast(safeStoi(v, static_cast(DEFAULT_VIDEO_FILTER))); }}, - {"video.shaders", [](std::string v) + {"keys", [](const std::string &v) + { + int val = safeStoi(v, static_cast(DEFAULT_CONTROL_SCHEME)); + if (val == static_cast(ControlScheme::CURSOR) || val == static_cast(ControlScheme::OPQA) || val == static_cast(ControlScheme::WASD)) + { + options.keys = static_cast(val); + } + else + { + options.keys = DEFAULT_CONTROL_SCHEME; + } + }}, + {"window.zoom", [](const std::string &v) + { + int val = safeStoi(v, DEFAULT_WINDOW_ZOOM); + if (val > 0) + { + options.window.zoom = val; + } + else + { + options.window.zoom = DEFAULT_WINDOW_ZOOM; + } + }}, + {"video.mode", [](const std::string &v) + { + int val = safeStoi(v, 0); + if (val == 0 || val == SDL_WINDOW_FULLSCREEN_DESKTOP) + { + options.video.mode = val; + } + else + { + options.video.mode = 0; + } + }}, + {"video.filter", [](const std::string &v) + { + int val = safeStoi(v, static_cast(DEFAULT_VIDEO_FILTER)); + if (val == static_cast(ScreenFilter::NEAREST) || val == static_cast(ScreenFilter::LINEAR)) + { + options.video.filter = static_cast(val); + } + else + { + options.video.filter = DEFAULT_VIDEO_FILTER; + } + }}, + {"video.shaders", [](const std::string &v) { options.video.shaders = stringToBool(v); }}, - {"video.vertical_sync", [](std::string v) + {"video.vertical_sync", [](const std::string &v) { options.video.vertical_sync = stringToBool(v); }}, - {"video.integer_scale", [](std::string v) + {"video.integer_scale", [](const std::string &v) { options.video.integer_scale = stringToBool(v); }}, - {"video.keep_aspect", [](std::string v) + {"video.keep_aspect", [](const std::string &v) { options.video.keep_aspect = stringToBool(v); }}, - {"video.border.enabled", [](std::string v) + {"video.border.enabled", [](const std::string &v) { options.video.border.enabled = stringToBool(v); }}, - {"video.border.width", [](std::string v) - { options.video.border.width = safeStoi(v, 32); }}, - {"video.border.height", [](std::string v) - { options.video.border.height = safeStoi(v, 24); }}, - {"video.palette", [](std::string v) - { options.video.palette = static_cast(safeStoi(v, static_cast(DEFAULT_PALETTE))); }}}; + {"video.border.width", [](const std::string &v) + { + int val = safeStoi(v, DEFAULT_BORDER_WIDTH); + if (val > 0) + { + options.video.border.width = val; + } + else + { + options.video.border.width = DEFAULT_BORDER_WIDTH; + } + }}, + {"video.border.height", [](const std::string &v) + { + int val = safeStoi(v, DEFAULT_BORDER_HEIGHT); + if (val > 0) + { + options.video.border.height = val; + } + else + { + options.video.border.height = DEFAULT_BORDER_HEIGHT; + } + }}, + {"video.palette", [](const std::string &v) + { + int val = safeStoi(v, static_cast(DEFAULT_PALETTE)); + if (val == static_cast(Palette::ZXSPECTRUM) || val == static_cast(Palette::ZXARNE)) + { + options.video.palette = static_cast(val); + } + else + { + options.video.palette = DEFAULT_PALETTE; + } + }}}; auto it = optionHandlers.find(var); if (it != optionHandlers.end()) @@ -207,4 +280,4 @@ bool setOptions(const std::string &var, const std::string &value) return true; } return false; -} \ No newline at end of file +} diff --git a/source/screen.cpp b/source/screen.cpp index f15ba770..e1fc472e 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -40,6 +40,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) : window_(window), renderer_(renderer) { + // Ajusta los tamaños adjustGameCanvasRect(); adjustWindowSize(); @@ -343,6 +344,8 @@ void Screen::adjustWindowSize() window_width_ = options.game.width + (options.video.border.enabled ? options.video.border.width * 2 : 0); window_height_ = options.game.height + (options.video.border.enabled ? options.video.border.height * 2 : 0); + options.window.max_zoom = getMaxZoom(); + // Establece el nuevo tamaño if (options.video.mode == 0) { @@ -356,11 +359,8 @@ void Screen::adjustWindowSize() int new_pos_y = old_pos_y + (old_height - (window_height_ * options.window.zoom)) / 2; SDL_SetWindowSize(window_, window_width_ * options.window.zoom, window_height_ * options.window.zoom); - SDL_SetWindowPosition(window_, std::max(new_pos_x, 30), std::max(new_pos_y, 10)); + SDL_SetWindowPosition(window_, std::max(new_pos_x, WINDOWS_DECORATIONS_), std::max(new_pos_y, 0)); } - - options.window.max_zoom = getMaxZoom(); - renderBlackFrame(); } // Ajusta game_canvas_rect_ @@ -389,7 +389,12 @@ int Screen::getMaxZoom() SDL_GetCurrentDisplayMode(0, &DM); // Calcula el máximo factor de zoom que se puede aplicar a la pantalla - return std::min(DM.w / window_width_, DM.h / window_height_); + const int max_zoom = std::min(DM.w / window_width_, (DM.h - WINDOWS_DECORATIONS_) / window_height_); + + // Normaliza los valores de zoom + options.window.zoom = std::min(options.window.zoom, max_zoom); + + return max_zoom; } // Renderiza un frame negro diff --git a/source/screen.h b/source/screen.h index d34278c5..1c5060b8 100644 --- a/source/screen.h +++ b/source/screen.h @@ -18,6 +18,9 @@ enum class ScreenFilter : Uint32 class Screen { private: + // Constantes + static constexpr int WINDOWS_DECORATIONS_ = 35; + // [SINGLETON] Objeto privado static Screen *screen_; @@ -51,9 +54,6 @@ private: // Ajusta el tamaño lógico del renderizador void adjustRenderLogicalSize(); - // Obtiene el tamaño máximo de zoom posible para la ventana - int getMaxZoom(); - // Renderiza un frame negro void renderBlackFrame(); @@ -126,6 +126,9 @@ public: // Oculta la ventana void hide(); + // Obtiene el tamaño máximo de zoom posible para la ventana + int getMaxZoom(); + // Getters SDL_Renderer *getRenderer() { return renderer_; } }; \ No newline at end of file