Afegida la funció getNewPosition a la classe Screen per a respectar la posició de la finestra al canviarla de tamany

This commit is contained in:
2024-10-15 18:12:16 +02:00
parent 3fdd60c9e2
commit 089da99b5b
3 changed files with 41 additions and 2 deletions

View File

@@ -1424,7 +1424,6 @@ void Game::createItemScoreSprite(int x, int y, std::shared_ptr<Texture> texture)
smart_sprites_.emplace_back(std::make_unique<SmartSprite>(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);

View File

@@ -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_, &current_position.x, &current_position.y);
// Obtiene las dimensiones actuales de la ventana
int current_width, current_height;
SDL_GetWindowSize(window_, &current_width, &current_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;
}

View File

@@ -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