canvi de pc

This commit is contained in:
2025-02-21 19:45:29 +01:00
parent 5f68c6256f
commit 7a0bc5c9ae
18 changed files with 918 additions and 889 deletions

View File

@@ -12,55 +12,73 @@
#include "jail_shader.h" // Para init, render
#include "notify.h" // Para Notify
// Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options)
// [SINGLETON]
Screen *Screen::screen_ = nullptr;
// [SINGLETON] Crearemos el objeto con esta función estática
void Screen::init(SDL_Window *window, SDL_Renderer *renderer)
{
// Inicializa variables
this->window = window;
this->renderer = renderer;
this->options = options;
this->asset = asset;
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),
asset_(Asset::get())
{
// Crea los objetos
notify = new Notify(renderer, asset->get("notify.png"), asset->get("smb2.png"), asset->get("smb2.txt"), asset->get("notify.wav"), options);
notify_ = new Notify(renderer, asset_->get("notify.png"), asset_->get("smb2.png"), asset_->get("smb2.txt"), asset_->get("notify.wav"), options_);
gameCanvasWidth = options->gameWidth;
gameCanvasHeight = options->gameHeight;
notificationLogicalWidth = gameCanvasWidth;
notificationLogicalHeight = gameCanvasHeight;
game_canvas_width_ = options_->gameWidth;
game_canvas_height_ = options_->gameHeight;
notification_logical_width_ = game_canvas_width_;
notification_logical_height_ = game_canvas_height_;
iniFade();
iniSpectrumFade();
// Define el color del borde para el modo de pantalla completa
borderColor = {0x00, 0x00, 0x00};
border_color_ = {0x00, 0x00, 0x00};
// Crea la textura donde se dibujan los graficos del juego
gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight);
if (gameCanvas == nullptr)
game_canvas_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, game_canvas_width_, game_canvas_height_);
if (game_canvas_ == nullptr)
{
if (options->console)
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
borderCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth + options->borderWidth * 2, gameCanvasHeight + options->borderHeight * 2);
if (borderCanvas == nullptr)
border_canvas_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, game_canvas_width_ + options_->borderWidth * 2, game_canvas_height_ + options_->borderHeight * 2);
if (border_canvas_ == nullptr)
{
if (options->console)
if (options_->console)
{
std::cout << "borderCanvas could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
setBorderColor(borderColor);
setBorderColor(border_color_);
// Establece el modo de video
setVideoMode(options->videoMode);
setVideoMode(options_->videoMode);
// Inicializa variables
notifyActive = false;
notify_active_ = false;
// Muestra la ventana
SDL_ShowWindow(window);
@@ -69,28 +87,28 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
// Destructor
Screen::~Screen()
{
delete notify;
SDL_DestroyTexture(gameCanvas);
SDL_DestroyTexture(borderCanvas);
delete notify_;
SDL_DestroyTexture(game_canvas_);
SDL_DestroyTexture(border_canvas_);
}
// Limpia la pantalla
void Screen::clean(color_t color)
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
SDL_RenderClear(renderer);
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, gameCanvas);
SDL_SetRenderTarget(renderer_, game_canvas_);
}
// Prepara para empezar a dibujar en la textura del borde
void Screen::startDrawOnBorder()
{
SDL_SetRenderTarget(renderer, borderCanvas);
SDL_SetRenderTarget(renderer_, border_canvas_);
}
// Vuelca el contenido del renderizador en pantalla
@@ -100,7 +118,7 @@ void Screen::render()
renderNotifications();
// Si está el borde activo, vuelca gameCanvas sobre borderCanvas
if (options->borderEnabled)
if (options_->borderEnabled)
{
gameCanvasToBorderCanvas();
}
@@ -113,7 +131,7 @@ void Screen::render()
void Screen::setVideoMode(int videoMode)
{
// Aplica el modo de video
SDL_SetWindowFullscreen(window, videoMode);
SDL_SetWindowFullscreen(window_, videoMode);
// Modo ventana
if (videoMode == 0)
@@ -122,23 +140,23 @@ void Screen::setVideoMode(int videoMode)
SDL_ShowCursor(SDL_ENABLE);
// Modifica el tamaño de la ventana en función del borde
if (options->borderEnabled)
if (options_->borderEnabled)
{
windowWidth = gameCanvasWidth + options->borderWidth * 2;
windowHeight = gameCanvasHeight + options->borderHeight * 2;
dest = {options->borderWidth, options->borderHeight, gameCanvasWidth, gameCanvasHeight};
window_width_ = game_canvas_width_ + options_->borderWidth * 2;
window_height_ = game_canvas_height_ + options_->borderHeight * 2;
dest_ = {options_->borderWidth, options_->borderHeight, game_canvas_width_, game_canvas_height_};
}
else
{
windowWidth = gameCanvasWidth;
windowHeight = gameCanvasHeight;
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
window_width_ = game_canvas_width_;
window_height_ = game_canvas_height_;
dest_ = {0, 0, game_canvas_width_, game_canvas_height_};
}
// Modifica el tamaño de la ventana
SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize);
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
SDL_SetWindowSize(window_, window_width_ * options_->windowSize, window_height_ * options_->windowSize);
SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
}
// Si está activo el modo de pantalla completa añade el borde
@@ -148,74 +166,74 @@ void Screen::setVideoMode(int videoMode)
SDL_ShowCursor(SDL_DISABLE);
// Obten el alto y el ancho de la ventana
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
SDL_GetWindowSize(window_, &window_width_, &window_height_);
// Aplica el escalado al rectangulo donde se pinta la textura del juego
if (options->integerScale)
if (options_->integerScale)
{
// Calcula el tamaño de la escala máxima
int scale = 0;
while (((gameCanvasWidth * (scale + 1)) <= windowWidth) && ((gameCanvasHeight * (scale + 1)) <= windowHeight))
while (((game_canvas_width_ * (scale + 1)) <= window_width_) && ((game_canvas_height_ * (scale + 1)) <= window_height_))
{
scale++;
}
dest.w = gameCanvasWidth * scale;
dest.h = gameCanvasHeight * scale;
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
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->keepAspect)
else if (options_->keepAspect)
{
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight))
float ratio = (float)game_canvas_width_ / (float)game_canvas_height_;
if ((window_width_ - game_canvas_width_) >= (window_height_ - game_canvas_height_))
{
dest.h = windowHeight;
dest.w = (int)((windowHeight * ratio) + 0.5f);
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
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 = windowWidth;
dest.h = (int)((windowWidth / ratio) + 0.5f);
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
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 = windowWidth;
dest.h = windowHeight;
dest.x = dest.y = 0;
dest_.w = window_width_;
dest_.h = window_height_;
dest_.x = dest_.y = 0;
}
}
// Modifica el tamaño del renderizador
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
SDL_RenderSetLogicalSize(renderer_, window_width_, window_height_);
// Actualiza las opciones
options->videoMode = videoMode;
options->screen.windowWidth = windowWidth;
options->screen.windowHeight = windowHeight;
options_->videoMode = videoMode;
options_->screen.windowWidth = window_width_;
options_->screen.windowHeight = window_height_;
// Establece el tamaño de las notificaciones
setNotificationSize();
// Reinicia los shaders
if (options->shaders)
if (options_->shaders)
{
const std::string glsl_file = options->screen.windowHeight == 192 ? "crtpi_192.glsl" : "crtpi_240.glsl";
std::ifstream f(asset->get(glsl_file).c_str());
const std::string glsl_file = options_->screen.windowHeight == 192 ? "crtpi_192.glsl" : "crtpi_240.glsl";
std::ifstream f(asset_->get(glsl_file).c_str());
std::string source((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
if (options->borderEnabled)
if (options_->borderEnabled)
{
shader::init(window, borderCanvas, source.c_str());
shader::init(window_, border_canvas_, source.c_str());
}
else
{
shader::init(window, gameCanvas, source.c_str());
shader::init(window_, game_canvas_, source.c_str());
}
}
}
@@ -223,85 +241,85 @@ void Screen::setVideoMode(int videoMode)
// Camibia entre pantalla completa y ventana
void Screen::toggleVideoMode()
{
options->videoMode = (options->videoMode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
setVideoMode(options->videoMode);
options_->videoMode = (options_->videoMode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
setVideoMode(options_->videoMode);
}
// Cambia el tamaño de la ventana
void Screen::setWindowSize(int size)
{
options->windowSize = size;
options_->windowSize = size;
setVideoMode(0);
}
// Reduce el tamaño de la ventana
void Screen::decWindowSize()
{
--options->windowSize;
options->windowSize = std::max(options->windowSize, 1);
--options_->windowSize;
options_->windowSize = std::max(options_->windowSize, 1);
setVideoMode(0);
}
// Aumenta el tamaño de la ventana
void Screen::incWindowSize()
{
++options->windowSize;
options->windowSize = std::min(options->windowSize, 4);
++options_->windowSize;
options_->windowSize = std::min(options_->windowSize, 4);
setVideoMode(0);
}
// Cambia el color del borde
void Screen::setBorderColor(color_t color)
{
borderColor = color;
auto temp = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, borderCanvas);
SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
SDL_RenderClear(renderer);
SDL_SetRenderTarget(renderer, temp);
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);
SDL_SetRenderDrawBlendMode(renderer_, blendMode);
}
// Establece el tamaño del borde
void Screen::setBorderWidth(int s)
{
options->borderWidth = s;
options_->borderWidth = s;
}
// Establece el tamaño del borde
void Screen::setBorderHeight(int s)
{
options->borderHeight = s;
options_->borderHeight = s;
}
// Establece si se ha de ver el borde en el modo ventana
void Screen::setBorderEnabled(bool value)
{
options->borderEnabled = value;
options_->borderEnabled = value;
}
// Cambia entre borde visible y no visible
void Screen::toggleBorder()
{
options->borderEnabled = !options->borderEnabled;
options_->borderEnabled = !options_->borderEnabled;
setVideoMode(0);
}
// Activa el fade
void Screen::setFade()
{
fade = true;
fade_ = true;
}
// Comprueba si ha terminado el fade
bool Screen::fadeEnded()
{
if (fade || fadeCounter > 0)
if (fade_ || fade_counter_ > 0)
{
return false;
}
@@ -312,13 +330,13 @@ bool Screen::fadeEnded()
// Activa el spectrum fade
void Screen::setspectrumFade()
{
spectrumFade = true;
spectrum_fade_ = true;
}
// Comprueba si ha terminado el spectrum fade
bool Screen::spectrumFadeEnded()
{
if (spectrumFade || spectrumFadeCounter > 0)
if (spectrum_fade_ || spectrum_fade_counter_ > 0)
{
return false;
}
@@ -329,21 +347,21 @@ bool Screen::spectrumFadeEnded()
// Inicializa las variables para el fade
void Screen::iniFade()
{
fade = false;
fadeCounter = 0;
fadeLenght = 200;
fade_ = false;
fade_counter_ = 0;
fade_lenght_ = 200;
}
// Actualiza el fade
void Screen::updateFade()
{
if (!fade)
if (!fade_)
{
return;
}
fadeCounter++;
if (fadeCounter > fadeLenght)
fade_counter_++;
if (fade_counter_ > fade_lenght_)
{
iniFade();
}
@@ -352,65 +370,65 @@ void Screen::updateFade()
// Dibuja el fade
void Screen::renderFade()
{
if (!fade)
if (!fade_)
{
return;
}
const SDL_Rect rect = {0, 0, gameCanvasWidth, gameCanvasHeight};
const SDL_Rect rect = {0, 0, game_canvas_width_, game_canvas_height_};
color_t color = {0, 0, 0};
const float step = (float)fadeCounter / (float)fadeLenght;
const float step = (float)fade_counter_ / (float)fade_lenght_;
const int alpha = 0 + (255 - 0) * step;
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha);
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, alpha);
SDL_RenderFillRect(renderer_, &rect);
}
// Inicializa las variables para el fade spectrum
void Screen::iniSpectrumFade()
{
spectrumFade = false;
spectrumFadeCounter = 0;
spectrumFadeLenght = 50;
spectrum_fade_ = false;
spectrum_fade_counter_ = 0;
spectrum_fade_lenght_ = 50;
spectrumColor.clear();
spectrum_color_.clear();
// Inicializa el vector de colores
const std::vector<std::string> vColors = {"black", "blue", "red", "magenta", "green", "cyan", "yellow", "bright_white"};
for (auto v : vColors)
{
spectrumColor.push_back(stringToColor(options->palette, v));
spectrum_color_.push_back(stringToColor(options_->palette, v));
}
}
// Actualiza el spectrum fade
void Screen::updateSpectrumFade()
{
if (!spectrumFade)
if (!spectrum_fade_)
{
return;
}
spectrumFadeCounter++;
if (spectrumFadeCounter > spectrumFadeLenght)
spectrum_fade_counter_++;
if (spectrum_fade_counter_ > spectrum_fade_lenght_)
{
iniSpectrumFade();
SDL_SetTextureColorMod(gameCanvas, 255, 255, 255);
SDL_SetTextureColorMod(game_canvas_, 255, 255, 255);
}
}
// Dibuja el spectrum fade
void Screen::renderSpectrumFade()
{
if (!spectrumFade)
if (!spectrum_fade_)
{
return;
}
const float step = (float)spectrumFadeCounter / (float)spectrumFadeLenght;
const int max = spectrumColor.size() - 1;
const float step = (float)spectrum_fade_counter_ / (float)spectrum_fade_lenght_;
const int max = spectrum_color_.size() - 1;
const int index = max + (0 - max) * step;
const color_t c = spectrumColor[index];
SDL_SetTextureColorMod(gameCanvas, c.r, c.g, c.b);
const color_t c = spectrum_color_[index];
SDL_SetTextureColorMod(game_canvas_, c.r, c.g, c.b);
}
// Actualiza los efectos
@@ -430,89 +448,89 @@ void Screen::renderFX()
// Actualiza el notificador
void Screen::updateNotifier()
{
notify->update();
notifyActive = notify->active();
notify_->update();
notify_active_ = notify_->active();
}
// Muestra una notificación de texto por pantalla;
void Screen::showNotification(std::string text1, std::string text2, int icon)
{
notify->showText(text1, text2, icon);
notify_->showText(text1, text2, icon);
}
// Dibuja las notificaciones
void Screen::renderNotifications()
{
if (notifyActive)
if (notify_active_)
{
notify->render();
notify_->render();
}
}
// Establece el tamaño de las notificaciones
void Screen::setNotificationSize()
{
if (options->videoMode == 0)
if (options_->videoMode == 0)
{
if (options->windowSize == 3)
if (options_->windowSize == 3)
{
notificationLogicalWidth = (windowWidth * 3) / 2;
notificationLogicalHeight = (windowHeight * 3) / 2;
notification_logical_width_ = (window_width_ * 3) / 2;
notification_logical_height_ = (window_height_ * 3) / 2;
}
else
{
notificationLogicalWidth = windowWidth * 2;
notificationLogicalHeight = windowHeight * 2;
notification_logical_width_ = window_width_ * 2;
notification_logical_height_ = window_height_ * 2;
}
}
if (options->videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
if (options_->videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
{
notificationLogicalWidth = windowWidth / 3;
notificationLogicalHeight = windowHeight / 3;
notification_logical_width_ = window_width_ / 3;
notification_logical_height_ = window_height_ / 3;
}
}
// Copia el gameCanvas en el borderCanvas
void Screen::gameCanvasToBorderCanvas()
{
auto temp = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, borderCanvas);
SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, gameCanvas, nullptr, &dest);
SDL_SetRenderTarget(renderer, temp);
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, &dest_);
SDL_SetRenderTarget(renderer_, temp);
}
// Muestra el contenido de Screen por pantalla
void Screen::renderPresent()
{
SDL_SetRenderTarget(renderer, nullptr);
SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
SDL_RenderClear(renderer);
SDL_SetRenderTarget(renderer_, nullptr);
SDL_SetRenderDrawColor(renderer_, border_color_.r, border_color_.g, border_color_.b, 0xFF);
SDL_RenderClear(renderer_);
if (options->shaders)
if (options_->shaders)
{
// Aplica shaders y renderiza el contenido
shader::render();
}
else
{
if (options->borderEnabled)
if (options_->borderEnabled)
{
SDL_RenderCopy(renderer, borderCanvas, nullptr, nullptr);
SDL_RenderCopy(renderer_, border_canvas_, nullptr, nullptr);
}
else
{
SDL_RenderCopy(renderer, gameCanvas, nullptr, &dest);
SDL_RenderCopy(renderer_, game_canvas_, nullptr, &dest_);
}
SDL_RenderPresent(renderer);
SDL_RenderPresent(renderer_);
}
}
// Cambia el estado de los shaders
void Screen::toggleShaders()
{
options->shaders = !options->shaders;
setVideoMode(options->videoMode);
options_->shaders = !options_->shaders;
setVideoMode(options_->videoMode);
}