From 366fe404cae50bb8a4fb497b7eaf6a4f26d7deb3 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 28 Feb 2025 09:46:55 +0100 Subject: [PATCH] =?UTF-8?q?Modes=20de=20video=20aclarits=20(sembla)=20La?= =?UTF-8?q?=20finestra=20mant=C3=A9=20la=20posici=C3=B3=20al=20canviar=20d?= =?UTF-8?q?e=20tamany=20o=20activar=20el=20borde=20La=20finestra=20ja=20po?= =?UTF-8?q?t=20creixer=20mentres=20donde=20de=20si=20el=20escriptori?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/screen.cpp | 52 ++++++++++++++++++++++++++++++++++++----------- source/screen.h | 6 ++++++ source/utils.h | 1 + 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/source/screen.cpp b/source/screen.cpp index 70e3778..f15ba77 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -40,13 +40,6 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) : window_(window), renderer_(renderer) { - // Obtiene información sobre la pantalla - SDL_DisplayMode DM; - SDL_GetCurrentDisplayMode(0, &DM); - - // Calcula el máximo factor de zoom que se puede aplicar a la pantalla - options.window.max_zoom = std::min(DM.w / options.game.width, DM.h / options.game.height); - adjustGameCanvasRect(); adjustWindowSize(); @@ -81,6 +74,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) setVideoMode(options.video.mode); // Muestra la ventana + SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); show(); } @@ -191,7 +185,7 @@ void Screen::setVideoMode(int videoMode) // Camibia entre pantalla completa y ventana void Screen::toggleVideoMode() { - options.video.mode = (options.video.mode == 0) ? SDL_WINDOW_FULLSCREEN : 0; + options.video.mode = (options.video.mode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0; setVideoMode(options.video.mode); } @@ -201,7 +195,8 @@ bool Screen::decWindowZoom() if (options.video.mode == 0) { int previous_zoom = options.window.zoom; - options.window.zoom = std::max(--options.window.zoom, 1); + --options.window.zoom; + options.window.zoom = std::max(options.window.zoom, 1); if (options.window.zoom != previous_zoom) { @@ -213,14 +208,14 @@ bool Screen::decWindowZoom() return false; } - // Aumenta el tamaño de la ventana bool Screen::incWindowZoom() { if (options.video.mode == 0) { int previous_zoom = options.window.zoom; - options.window.zoom = std::min(++options.window.zoom, options.window.max_zoom); + ++options.window.zoom; + options.window.zoom = std::min(options.window.zoom, options.window.max_zoom); if (options.window.zoom != previous_zoom) { @@ -348,11 +343,24 @@ 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); + // Establece el nuevo tamaño if (options.video.mode == 0) { + int old_width, old_height; + SDL_GetWindowSize(window_, &old_width, &old_height); + + int old_pos_x, old_pos_y; + SDL_GetWindowPosition(window_, &old_pos_x, &old_pos_y); + + int new_pos_x = old_pos_x + (old_width - (window_width_ * options.window.zoom)) / 2; + 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_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + SDL_SetWindowPosition(window_, std::max(new_pos_x, 30), std::max(new_pos_y, 10)); } + + options.window.max_zoom = getMaxZoom(); + renderBlackFrame(); } // Ajusta game_canvas_rect_ @@ -371,4 +379,24 @@ void Screen::adjustRenderLogicalSize() const int extra_width = options.video.border.enabled ? options.video.border.width * 2 : 0; const int extra_height = options.video.border.enabled ? options.video.border.height * 2 : 0; SDL_RenderSetLogicalSize(renderer_, options.game.width + extra_width, options.game.height + extra_height); +} + +// Obtiene el tamaño máximo de zoom posible para la ventana +int Screen::getMaxZoom() +{ + // Obtiene información sobre la pantalla + SDL_DisplayMode DM; + 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_); +} + +// Renderiza un frame negro +void Screen::renderBlackFrame() +{ + SDL_SetRenderTarget(renderer_, nullptr); + SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF); + SDL_RenderClear(renderer_); + SDL_RenderPresent(renderer_); } \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index ec6c55e..d34278c 100644 --- a/source/screen.h +++ b/source/screen.h @@ -51,6 +51,12 @@ 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(); + // Constructor Screen(SDL_Window *window, SDL_Renderer *renderer); diff --git a/source/utils.h b/source/utils.h index 647ae99..5fca2e7 100644 --- a/source/utils.h +++ b/source/utils.h @@ -3,6 +3,7 @@ #include // for SDL_Rect, SDL_Point #include // for Uint8 #include // for string +#include // Tipos de paleta enum class Palette : int