From 089da99b5ba1bf9238a8fff2cede6cdeff23f1c3 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 15 Oct 2024 18:12:16 +0200 Subject: [PATCH] =?UTF-8?q?Afegida=20la=20funci=C3=B3=20getNewPosition=20a?= =?UTF-8?q?=20la=20classe=20Screen=20per=20a=20respectar=20la=20posici?= =?UTF-8?q?=C3=B3=20de=20la=20finestra=20al=20canviarla=20de=20tamany?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/game.cpp | 1 - source/screen.cpp | 39 ++++++++++++++++++++++++++++++++++++++- source/screen.h | 3 +++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/source/game.cpp b/source/game.cpp index 0d7e440..59aeeba 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -1424,7 +1424,6 @@ void Game::createItemScoreSprite(int x, int y, std::shared_ptr texture) smart_sprites_.emplace_back(std::make_unique(texture)); // Inicializa - smart_sprites_.back()->init(); smart_sprites_.back()->setPos({0, 0, texture->getWidth(), texture->getHeight()}); smart_sprites_.back()->setSpriteClip(smart_sprites_.back()->getPos()); smart_sprites_.back()->setPosX(x); diff --git a/source/screen.cpp b/source/screen.cpp index c7e2026..d374568 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -200,8 +200,9 @@ void Screen::setVideoMode(ScreenVideoMode videoMode) SDL_ShowCursor(SDL_ENABLE); #endif // Modifica el tamaño de la ventana + SDL_Point pos = getNewPosition(); SDL_SetWindowSize(window_, param.game.width * options.video.window.size, param.game.height * options.video.window.size); - SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + SDL_SetWindowPosition(window_, pos.x, pos.y); break; } @@ -494,4 +495,40 @@ void Screen::displayInfo() bool Screen::notificationsAreActive() const { return notify_->active(); +} + +// Calcula la nueva posición de la ventana a partir de la antigua al cambiarla de tamaño +SDL_Point Screen::getNewPosition() +{ + // Obtiene la posición actual de la ventana + SDL_Point current_position; + SDL_GetWindowPosition(window_, ¤t_position.x, ¤t_position.y); + + // Obtiene las dimensiones actuales de la ventana + int current_width, current_height; + SDL_GetWindowSize(window_, ¤t_width, ¤t_height); + + // Obtiene las dimesiones que tendrá la ventana + const int new_width = param.game.width * options.video.window.size; + const int new_height = param.game.height * options.video.window.size; + + // Obtiene el centro de la ventana actual + SDL_Point center; + center.x = current_position.x + current_width / 2; + center.y = current_position.y + current_height / 2; + + // Calcula la nueva posición a partir del centro y las nuevas diemsiones + SDL_Point new_pos; + new_pos.x = center.x - new_width / 2; + new_pos.y = center.y - new_height / 2; + + // Obtiene las dimensiones del escritorio + SDL_DisplayMode DM; + SDL_GetCurrentDisplayMode(0, &DM); + + // Evita que la ventana quede fuera del escritorio + new_pos.x = std::clamp(new_pos.x, 30, DM.w - new_width); + new_pos.y = std::clamp(new_pos.y, 30, DM.h - new_height); + + return new_pos; } \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index 6379421..3450e97 100644 --- a/source/screen.h +++ b/source/screen.h @@ -85,6 +85,9 @@ private: // Muestra información por pantalla void displayInfo(); + // Calcula la nueva posición de la ventana a partir de la antigua al cambiarla de tamaño + SDL_Point getNewPosition(); + // [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos screen desde fuera // Constructor