diff --git a/source/director.cpp b/source/director.cpp index f64861c..47a1d5a 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -350,8 +350,10 @@ bool Director::initSDL() // Inicializa el color de renderizado SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF); - // Establece el tamaño del buffer de renderizado - SDL_RenderSetLogicalSize(renderer_, options.game.width, options.game.height); + // Modifica el tamaño del renderizador + 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); // Establece el modo de mezcla SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND); diff --git a/source/screen.cpp b/source/screen.cpp index 1fc9696..9999059 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -40,10 +40,8 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) : window_(window), renderer_(renderer) { - game_canvas_width_ = options.game.width; - game_canvas_height_ = options.game.height; - notification_logical_width_ = game_canvas_width_; - notification_logical_height_ = game_canvas_height_; + adjustGameCanvasRect(); + calculateWindowSize(); iniFade(); iniSpectrumFade(); @@ -52,7 +50,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) border_color_ = {0x00, 0x00, 0x00}; // Crea la textura donde se dibujan los graficos del juego - game_canvas_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, game_canvas_width_, game_canvas_height_); + game_canvas_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, options.game.width, options.game.height); if (game_canvas_ == nullptr) { if (options.console) @@ -62,7 +60,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) } // Crea la textura donde se dibuja el borde que rodea el area de juego - border_canvas_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, game_canvas_width_ + options.video.border.width * 2, game_canvas_height_ + options.video.border.height * 2); + border_canvas_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, options.game.width + options.video.border.width * 2, options.game.height + options.video.border.height * 2); if (border_canvas_ == nullptr) { if (options.console) @@ -121,7 +119,6 @@ void Screen::render() renderPresent(); } - // Vuelca el contenido del renderizador en pantalla void Screen::renderWithoutNotifier() { @@ -139,86 +136,43 @@ void Screen::renderWithoutNotifier() void Screen::setVideoMode(int videoMode) { // Aplica el modo de video - SDL_SetWindowFullscreen(window_, videoMode); - + // Modo ventana if (videoMode == 0) { // Muestra el puntero SDL_ShowCursor(SDL_ENABLE); - - // Modifica el tamaño de la ventana en función del borde - if (options.video.border.enabled) - { - window_width_ = game_canvas_width_ + options.video.border.width * 2; - window_height_ = game_canvas_height_ + options.video.border.height * 2; - dest_ = {options.video.border.width, options.video.border.height, game_canvas_width_, game_canvas_height_}; - } - - else - { - window_width_ = game_canvas_width_; - window_height_ = game_canvas_height_; - dest_ = {0, 0, game_canvas_width_, game_canvas_height_}; - } - + + calculateWindowSize(); + adjustGameCanvasRect(); + // Modifica el tamaño de la ventana SDL_SetWindowSize(window_, window_width_ * options.window.zoom, window_height_ * options.window.zoom); SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + // Modifica el tamaño del renderizador + 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); + SDL_SetWindowFullscreen(window_, videoMode); } - - // Si está activo el modo de pantalla completa añade el borde - else if (videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP) + + // Modo pantalla completa + else { + calculateWindowSize(); + adjustGameCanvasRect(); // Oculta el puntero SDL_ShowCursor(SDL_DISABLE); - - // Obten el alto y el ancho de la ventana - SDL_GetWindowSize(window_, &window_width_, &window_height_); - - // Aplica el escalado al rectangulo donde se pinta la textura del juego - if (options.video.integer_scale) - { - // Calcula el tamaño de la escala máxima - int scale = 0; - while (((game_canvas_width_ * (scale + 1)) <= window_width_) && ((game_canvas_height_ * (scale + 1)) <= window_height_)) - { - scale++; - } - - dest_.w = game_canvas_width_ * scale; - dest_.h = game_canvas_height_ * scale; - dest_.x = (window_width_ - dest_.w) / 2; - dest_.y = (window_height_ - dest_.h) / 2; - } - else if (options.video.keep_aspect) - { - float ratio = (float)game_canvas_width_ / (float)game_canvas_height_; - if ((window_width_ - game_canvas_width_) >= (window_height_ - game_canvas_height_)) - { - dest_.h = window_height_; - dest_.w = (int)((window_height_ * ratio) + 0.5f); - dest_.x = (window_width_ - dest_.w) / 2; - dest_.y = (window_height_ - dest_.h) / 2; - } - else - { - dest_.w = window_width_; - dest_.h = (int)((window_width_ / ratio) + 0.5f); - dest_.x = (window_width_ - dest_.w) / 2; - dest_.y = (window_height_ - dest_.h) / 2; - } - } - else - { - dest_.w = window_width_; - dest_.h = window_height_; - dest_.x = dest_.y = 0; - } + + // Modifica el tamaño del renderizador + 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); + // Habilitar el escalado entero + SDL_RenderSetIntegerScale(renderer_, options.video.integer_scale ? SDL_TRUE : SDL_FALSE); + SDL_SetWindowFullscreen(window_, videoMode); } - // Modifica el tamaño del renderizador - SDL_RenderSetLogicalSize(renderer_, window_width_, window_height_); // Actualiza las opciones options.video.mode = videoMode; @@ -244,7 +198,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_DESKTOP : 0; + options.video.mode = (options.video.mode == 0) ? SDL_WINDOW_FULLSCREEN : 0; setVideoMode(options.video.mode); } @@ -307,7 +261,7 @@ void Screen::setBorderEnabled(bool value) { options.video.border.enabled = value void Screen::toggleBorder() { options.video.border.enabled = !options.video.border.enabled; - setVideoMode(0); + setVideoMode(options.video.mode); } // Activa el fade @@ -372,7 +326,7 @@ void Screen::renderFade() return; } - const SDL_Rect rect = {0, 0, game_canvas_width_, game_canvas_height_}; + const SDL_Rect rect = {0, 0, options.game.width, options.game.height}; Color color = {0, 0, 0}; const float step = (float)fade_counter_ / (float)fade_lenght_; const int alpha = 0 + (255 - 0) * step; @@ -455,7 +409,7 @@ void Screen::gameCanvasToBorderCanvas() SDL_SetRenderTarget(renderer_, border_canvas_); SDL_SetRenderDrawColor(renderer_, border_color_.r, border_color_.g, border_color_.b, 0xFF); SDL_RenderClear(renderer_); - SDL_RenderCopy(renderer_, game_canvas_, nullptr, &dest_); + SDL_RenderCopy(renderer_, game_canvas_, nullptr, &game_canvas_rect_); SDL_SetRenderTarget(renderer_, temp); } @@ -463,7 +417,7 @@ void Screen::gameCanvasToBorderCanvas() void Screen::renderPresent() { SDL_SetRenderTarget(renderer_, nullptr); - SDL_SetRenderDrawColor(renderer_, border_color_.r, border_color_.g, border_color_.b, 0xFF); + SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF); SDL_RenderClear(renderer_); if (options.video.shaders) @@ -479,7 +433,7 @@ void Screen::renderPresent() } else { - SDL_RenderCopy(renderer_, game_canvas_, nullptr, &dest_); + SDL_RenderCopy(renderer_, game_canvas_, nullptr, &game_canvas_rect_); } SDL_RenderPresent(renderer_); } @@ -509,4 +463,32 @@ void Screen::show() void Screen::hide() { SDL_HideWindow(window_); +} + +// Calcula el tamaño de la ventana +void Screen::calculateWindowSize() +{ + if (options.video.border.enabled) + { + window_width_ = options.game.width + options.video.border.width * 2; + window_height_ = options.game.height + options.video.border.height * 2; + } + else + { + window_width_ = options.game.width; + window_height_ = options.game.height; + } +} + +// Ajusta game_canvas_rect_ +void Screen::adjustGameCanvasRect() +{ + if (options.video.border.enabled) + { + game_canvas_rect_ = {options.video.border.width, options.video.border.height, options.game.width, options.game.height}; + } + else + { + game_canvas_rect_ = {0, 0, options.game.width, options.game.height}; + } } \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index 1a91c3a..22b2608 100644 --- a/source/screen.h +++ b/source/screen.h @@ -28,14 +28,10 @@ private: SDL_Texture *border_canvas_; // Textura donde se dibuja el borde del juego // Variables - int window_width_; // Ancho de la pantalla o ventana - int window_height_; // Alto de la pantalla o ventana - int game_canvas_width_; // Resolución interna del juego. Es el ancho de la textura donde se dibuja el juego - int game_canvas_height_; // Resolución interna del juego. Es el alto de la textura donde se dibuja el juego - SDL_Rect dest_; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana - Color border_color_; // Color del borde añadido a la textura de juego para rellenar la pantalla - int notification_logical_width_; // Ancho lógico de las notificaciones en relación al tamaño de pantalla - int notification_logical_height_; // Alto lógico de las notificaciones en relación al tamaño de pantalla + int window_width_; // Ancho de la pantalla o ventana + int window_height_; // Alto de la pantalla o ventana + SDL_Rect game_canvas_rect_; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana + Color border_color_; // Color del borde añadido a la textura de juego para rellenar la pantalla // Variables - Efectos bool fade_; // Indica si esta activo el efecto de fade @@ -73,6 +69,12 @@ private: // Muestra el contenido de Screen por pantalla void renderPresent(); + // Calcula el tamaño de la ventana + void calculateWindowSize(); + + // Ajusta game_canvas_rect_ + void adjustGameCanvasRect(); + // Constructor Screen(SDL_Window *window, SDL_Renderer *renderer);