407 lines
11 KiB
C++
407 lines
11 KiB
C++
#include "screen.h"
|
|
#include <SDL2/SDL_error.h> // Para SDL_GetError
|
|
#include <SDL2/SDL_events.h> // Para SDL_DISABLE, SDL_ENABLE
|
|
#include <SDL2/SDL_mouse.h> // Para SDL_ShowCursor
|
|
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
|
|
#include <algorithm> // Para max, min
|
|
#include <fstream> // Para basic_ostream, operator<<, basic_ifstream
|
|
#include <iostream> // Para cout
|
|
#include <iterator> // Para istreambuf_iterator, operator!=
|
|
#include <string> // Para basic_string, char_traits, string
|
|
#include "asset.h" // Para Asset
|
|
#include "jail_shader.h" // Para init, render
|
|
#include "notifier.h" // Para Notify
|
|
#include "options.h"
|
|
#include "mouse.h"
|
|
|
|
// [SINGLETON]
|
|
Screen *Screen::screen_ = nullptr;
|
|
|
|
// [SINGLETON] Crearemos el objeto con esta función estática
|
|
void Screen::init(SDL_Window *window, SDL_Renderer *renderer)
|
|
{
|
|
Screen::screen_ = new Screen(window, renderer);
|
|
}
|
|
|
|
// [SINGLETON] Destruiremos el objeto con esta función estática
|
|
void Screen::destroy()
|
|
{
|
|
delete Screen::screen_;
|
|
}
|
|
|
|
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
|
Screen *Screen::get()
|
|
{
|
|
return Screen::screen_;
|
|
}
|
|
|
|
// Constructor
|
|
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
|
|
: window_(window),
|
|
renderer_(renderer)
|
|
{
|
|
// Ajusta los tamaños
|
|
adjustGameCanvasRect();
|
|
adjustWindowSize();
|
|
|
|
// Define el color del borde para el modo de pantalla completa
|
|
border_color_ = {0x00, 0x00, 0x00};
|
|
|
|
// Establece el modo de escalado
|
|
SDL_RenderSetIntegerScale(renderer_, options.video.integer_scale ? SDL_TRUE : SDL_FALSE);
|
|
|
|
// Crea la textura donde se dibujan los graficos del juego
|
|
game_canvas_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, options.game.width, options.game.height);
|
|
if (game_canvas_ == nullptr)
|
|
{
|
|
if (options.console)
|
|
{
|
|
std::cout << "gameCanvas could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
|
}
|
|
}
|
|
|
|
// 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, options.game.width + options.video.border.width * 2, options.game.height + options.video.border.height * 2);
|
|
if (border_canvas_ == nullptr)
|
|
{
|
|
if (options.console)
|
|
{
|
|
std::cout << "borderCanvas could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
|
}
|
|
}
|
|
setBorderColor(border_color_);
|
|
|
|
// Establece el modo de video
|
|
setVideoMode(options.video.mode);
|
|
|
|
// Muestra la ventana
|
|
SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
|
show();
|
|
}
|
|
|
|
// Destructor
|
|
Screen::~Screen()
|
|
{
|
|
SDL_DestroyTexture(game_canvas_);
|
|
SDL_DestroyTexture(border_canvas_);
|
|
}
|
|
|
|
// Limpia la pantalla
|
|
void Screen::clean(Color color)
|
|
{
|
|
SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, 0xFF);
|
|
SDL_RenderClear(renderer_);
|
|
}
|
|
|
|
// Prepara para empezar a dibujar en la textura de juego
|
|
void Screen::start()
|
|
{
|
|
SDL_SetRenderTarget(renderer_, game_canvas_);
|
|
}
|
|
|
|
// Prepara para empezar a dibujar en la textura del borde
|
|
void Screen::startDrawOnBorder()
|
|
{
|
|
SDL_SetRenderTarget(renderer_, border_canvas_);
|
|
}
|
|
|
|
// Vuelca el contenido del renderizador en pantalla
|
|
void Screen::render()
|
|
{
|
|
// Renderiza sobre gameCanvas los overlays
|
|
renderNotifications();
|
|
|
|
// Si está el borde activo, vuelca gameCanvas sobre borderCanvas
|
|
if (options.video.border.enabled)
|
|
{
|
|
gameCanvasToBorderCanvas();
|
|
}
|
|
|
|
// Muestra el contenido por pantalla
|
|
renderPresent();
|
|
}
|
|
|
|
// Vuelca el contenido del renderizador en pantalla
|
|
void Screen::renderWithoutNotifier()
|
|
{
|
|
// Si está el borde activo, vuelca gameCanvas sobre borderCanvas
|
|
if (options.video.border.enabled)
|
|
{
|
|
gameCanvasToBorderCanvas();
|
|
}
|
|
|
|
// Muestra el contenido por pantalla
|
|
renderPresent();
|
|
}
|
|
|
|
// Establece el modo de video
|
|
void Screen::setVideoMode(int videoMode)
|
|
{
|
|
// Aplica el modo de video
|
|
|
|
// Modo ventana
|
|
if (videoMode == 0)
|
|
{
|
|
// Muestra el puntero
|
|
SDL_ShowCursor(SDL_ENABLE);
|
|
|
|
SDL_SetWindowFullscreen(window_, videoMode);
|
|
adjustWindowSize();
|
|
adjustGameCanvasRect();
|
|
adjustRenderLogicalSize();
|
|
}
|
|
|
|
// Modo pantalla completa
|
|
else
|
|
{
|
|
// Oculta el puntero
|
|
SDL_ShowCursor(SDL_DISABLE);
|
|
adjustWindowSize();
|
|
|
|
adjustGameCanvasRect();
|
|
SDL_SetWindowFullscreen(window_, videoMode);
|
|
}
|
|
|
|
// Actualiza las opciones
|
|
options.video.mode = videoMode;
|
|
|
|
// Reinicia los shaders
|
|
if (options.video.shaders)
|
|
{
|
|
const std::string glsl_file = window_height_ == 192 ? "crtpi_192.glsl" : "crtpi_240.glsl";
|
|
std::ifstream f(Asset::get()->get(glsl_file).c_str());
|
|
std::string source((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
|
|
|
if (options.video.border.enabled)
|
|
{
|
|
shader::init(window_, border_canvas_, source.c_str());
|
|
}
|
|
else
|
|
{
|
|
shader::init(window_, game_canvas_, source.c_str());
|
|
}
|
|
}
|
|
}
|
|
|
|
// Camibia entre pantalla completa y ventana
|
|
void Screen::toggleVideoMode()
|
|
{
|
|
options.video.mode = (options.video.mode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
|
|
setVideoMode(options.video.mode);
|
|
}
|
|
|
|
// Reduce el tamaño de la ventana
|
|
bool Screen::decWindowZoom()
|
|
{
|
|
if (options.video.mode == 0)
|
|
{
|
|
int previous_zoom = options.window.zoom;
|
|
--options.window.zoom;
|
|
options.window.zoom = std::max(options.window.zoom, 1);
|
|
|
|
if (options.window.zoom != previous_zoom)
|
|
{
|
|
adjustWindowSize();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
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;
|
|
options.window.zoom = std::min(options.window.zoom, options.window.max_zoom);
|
|
|
|
if (options.window.zoom != previous_zoom)
|
|
{
|
|
adjustWindowSize();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// Cambia el color del borde
|
|
void Screen::setBorderColor(Color color)
|
|
{
|
|
border_color_ = color;
|
|
auto temp = SDL_GetRenderTarget(renderer_);
|
|
SDL_SetRenderTarget(renderer_, border_canvas_);
|
|
SDL_SetRenderDrawColor(renderer_, border_color_.r, border_color_.g, border_color_.b, 0xFF);
|
|
SDL_RenderClear(renderer_);
|
|
SDL_SetRenderTarget(renderer_, temp);
|
|
}
|
|
|
|
// Cambia el tipo de mezcla
|
|
void Screen::setBlendMode(SDL_BlendMode blendMode)
|
|
{
|
|
SDL_SetRenderDrawBlendMode(renderer_, blendMode);
|
|
}
|
|
|
|
// Establece el tamaño del borde
|
|
void Screen::setBorderWidth(int s)
|
|
{
|
|
options.video.border.width = s;
|
|
}
|
|
|
|
// Establece el tamaño del borde
|
|
void Screen::setBorderHeight(int s)
|
|
{
|
|
options.video.border.height = s;
|
|
}
|
|
|
|
// Establece si se ha de ver el borde en el modo ventana
|
|
void Screen::setBorderEnabled(bool value) { options.video.border.enabled = value; }
|
|
|
|
// Cambia entre borde visible y no visible
|
|
void Screen::toggleBorder()
|
|
{
|
|
options.video.border.enabled = !options.video.border.enabled;
|
|
adjustWindowSize();
|
|
adjustGameCanvasRect();
|
|
adjustRenderLogicalSize();
|
|
}
|
|
|
|
// Dibuja las notificaciones
|
|
void Screen::renderNotifications()
|
|
{
|
|
Notifier::get()->render();
|
|
}
|
|
|
|
// Copia el gameCanvas en el borderCanvas
|
|
void Screen::gameCanvasToBorderCanvas()
|
|
{
|
|
auto temp = SDL_GetRenderTarget(renderer_);
|
|
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, &game_canvas_rect_);
|
|
SDL_SetRenderTarget(renderer_, temp);
|
|
}
|
|
|
|
// Muestra el contenido de Screen por pantalla
|
|
void Screen::renderPresent()
|
|
{
|
|
SDL_SetRenderTarget(renderer_, nullptr);
|
|
SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF);
|
|
SDL_RenderClear(renderer_);
|
|
|
|
if (options.video.shaders)
|
|
{
|
|
// Aplica shaders y renderiza el contenido
|
|
shader::render();
|
|
}
|
|
else
|
|
{
|
|
if (options.video.border.enabled)
|
|
{
|
|
SDL_RenderCopy(renderer_, border_canvas_, nullptr, nullptr);
|
|
}
|
|
else
|
|
{
|
|
SDL_RenderCopy(renderer_, game_canvas_, nullptr, &game_canvas_rect_);
|
|
}
|
|
SDL_RenderPresent(renderer_);
|
|
}
|
|
}
|
|
|
|
// Cambia el estado de los shaders
|
|
void Screen::toggleShaders()
|
|
{
|
|
options.video.shaders = !options.video.shaders;
|
|
setVideoMode(options.video.mode);
|
|
}
|
|
|
|
// Actualiza la lógica de la clase
|
|
void Screen::update()
|
|
{
|
|
Notifier::get()->update();
|
|
Mouse::updateCursorVisibility();
|
|
}
|
|
|
|
// Muestra la ventana
|
|
void Screen::show()
|
|
{
|
|
SDL_ShowWindow(window_);
|
|
}
|
|
|
|
// Oculta la ventana
|
|
void Screen::hide()
|
|
{
|
|
SDL_HideWindow(window_);
|
|
}
|
|
|
|
// Calcula el tamaño de la ventana
|
|
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)
|
|
{
|
|
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_, std::max(new_pos_x, WINDOWS_DECORATIONS_), std::max(new_pos_y, 0));
|
|
}
|
|
}
|
|
|
|
// Ajusta game_canvas_rect_
|
|
void Screen::adjustGameCanvasRect()
|
|
{
|
|
game_canvas_rect_ = {
|
|
options.video.border.enabled ? options.video.border.width : 0,
|
|
options.video.border.enabled ? options.video.border.height : 0,
|
|
options.game.width,
|
|
options.game.height};
|
|
}
|
|
|
|
// Ajusta el tamaño lógico del renderizador
|
|
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
|
|
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
|
|
void Screen::renderBlackFrame()
|
|
{
|
|
SDL_SetRenderTarget(renderer_, nullptr);
|
|
SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF);
|
|
SDL_RenderClear(renderer_);
|
|
SDL_RenderPresent(renderer_);
|
|
} |