518 lines
13 KiB
C++
518 lines
13 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 "notify.h" // Para Notify
|
|
|
|
// Constructor
|
|
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options)
|
|
{
|
|
// Inicializa variables
|
|
this->window = window;
|
|
this->renderer = renderer;
|
|
this->options = options;
|
|
this->asset = asset;
|
|
|
|
// Crea los objetos
|
|
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;
|
|
|
|
iniFade();
|
|
iniSpectrumFade();
|
|
|
|
// Define el color del borde para el modo de pantalla completa
|
|
borderColor = {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)
|
|
{
|
|
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)
|
|
{
|
|
if (options->console)
|
|
{
|
|
std::cout << "borderCanvas could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
|
}
|
|
}
|
|
setBorderColor(borderColor);
|
|
|
|
// Establece el modo de video
|
|
setVideoMode(options->videoMode);
|
|
|
|
// Inicializa variables
|
|
notifyActive = false;
|
|
|
|
// Muestra la ventana
|
|
SDL_ShowWindow(window);
|
|
}
|
|
|
|
// Destructor
|
|
Screen::~Screen()
|
|
{
|
|
delete notify;
|
|
SDL_DestroyTexture(gameCanvas);
|
|
SDL_DestroyTexture(borderCanvas);
|
|
}
|
|
|
|
// Limpia la pantalla
|
|
void Screen::clean(color_t 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, gameCanvas);
|
|
}
|
|
|
|
// Prepara para empezar a dibujar en la textura del borde
|
|
void Screen::startDrawOnBorder()
|
|
{
|
|
SDL_SetRenderTarget(renderer, borderCanvas);
|
|
}
|
|
|
|
// 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->borderEnabled)
|
|
{
|
|
gameCanvasToBorderCanvas();
|
|
}
|
|
|
|
// Muestra el contenido por pantalla
|
|
renderPresent();
|
|
}
|
|
|
|
// Establece el modo de video
|
|
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->borderEnabled)
|
|
{
|
|
windowWidth = gameCanvasWidth + options->borderWidth * 2;
|
|
windowHeight = gameCanvasHeight + options->borderHeight * 2;
|
|
dest = {options->borderWidth, options->borderHeight, gameCanvasWidth, gameCanvasHeight};
|
|
}
|
|
|
|
else
|
|
{
|
|
windowWidth = gameCanvasWidth;
|
|
windowHeight = gameCanvasHeight;
|
|
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
// Si está activo el modo de pantalla completa añade el borde
|
|
else if (videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
|
{
|
|
// Oculta el puntero
|
|
SDL_ShowCursor(SDL_DISABLE);
|
|
|
|
// Obten el alto y el ancho de la ventana
|
|
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
|
|
|
// Aplica el escalado al rectangulo donde se pinta la textura del juego
|
|
if (options->integerScale)
|
|
{
|
|
// Calcula el tamaño de la escala máxima
|
|
int scale = 0;
|
|
while (((gameCanvasWidth * (scale + 1)) <= windowWidth) && ((gameCanvasHeight * (scale + 1)) <= windowHeight))
|
|
{
|
|
scale++;
|
|
}
|
|
|
|
dest.w = gameCanvasWidth * scale;
|
|
dest.h = gameCanvasHeight * scale;
|
|
dest.x = (windowWidth - dest.w) / 2;
|
|
dest.y = (windowHeight - dest.h) / 2;
|
|
}
|
|
else if (options->keepAspect)
|
|
{
|
|
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
|
|
if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight))
|
|
{
|
|
dest.h = windowHeight;
|
|
dest.w = (int)((windowHeight * ratio) + 0.5f);
|
|
dest.x = (windowWidth - dest.w) / 2;
|
|
dest.y = (windowHeight - 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;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dest.w = windowWidth;
|
|
dest.h = windowHeight;
|
|
dest.x = dest.y = 0;
|
|
}
|
|
}
|
|
|
|
// Modifica el tamaño del renderizador
|
|
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
|
|
|
|
// Actualiza las opciones
|
|
options->videoMode = videoMode;
|
|
options->screen.windowWidth = windowWidth;
|
|
options->screen.windowHeight = windowHeight;
|
|
|
|
// Establece el tamaño de las notificaciones
|
|
setNotificationSize();
|
|
|
|
// Reinicia los 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());
|
|
std::string source((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
|
|
|
if (options->borderEnabled)
|
|
{
|
|
shader::init(window, borderCanvas, source.c_str());
|
|
}
|
|
else
|
|
{
|
|
shader::init(window, gameCanvas, source.c_str());
|
|
}
|
|
}
|
|
}
|
|
|
|
// Camibia entre pantalla completa y ventana
|
|
void Screen::toggleVideoMode()
|
|
{
|
|
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;
|
|
setVideoMode(0);
|
|
}
|
|
|
|
// Reduce el tamaño de la ventana
|
|
void Screen::decWindowSize()
|
|
{
|
|
--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);
|
|
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);
|
|
}
|
|
|
|
// 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->borderWidth = s;
|
|
}
|
|
|
|
// Establece el tamaño del borde
|
|
void Screen::setBorderHeight(int s)
|
|
{
|
|
options->borderHeight = s;
|
|
}
|
|
|
|
// Establece si se ha de ver el borde en el modo ventana
|
|
void Screen::setBorderEnabled(bool value)
|
|
{
|
|
options->borderEnabled = value;
|
|
}
|
|
|
|
// Cambia entre borde visible y no visible
|
|
void Screen::toggleBorder()
|
|
{
|
|
options->borderEnabled = !options->borderEnabled;
|
|
setVideoMode(0);
|
|
}
|
|
|
|
// Activa el fade
|
|
void Screen::setFade()
|
|
{
|
|
fade = true;
|
|
}
|
|
|
|
// Comprueba si ha terminado el fade
|
|
bool Screen::fadeEnded()
|
|
{
|
|
if (fade || fadeCounter > 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Activa el spectrum fade
|
|
void Screen::setspectrumFade()
|
|
{
|
|
spectrumFade = true;
|
|
}
|
|
|
|
// Comprueba si ha terminado el spectrum fade
|
|
bool Screen::spectrumFadeEnded()
|
|
{
|
|
if (spectrumFade || spectrumFadeCounter > 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Inicializa las variables para el fade
|
|
void Screen::iniFade()
|
|
{
|
|
fade = false;
|
|
fadeCounter = 0;
|
|
fadeLenght = 200;
|
|
}
|
|
|
|
// Actualiza el fade
|
|
void Screen::updateFade()
|
|
{
|
|
if (!fade)
|
|
{
|
|
return;
|
|
}
|
|
|
|
fadeCounter++;
|
|
if (fadeCounter > fadeLenght)
|
|
{
|
|
iniFade();
|
|
}
|
|
}
|
|
|
|
// Dibuja el fade
|
|
void Screen::renderFade()
|
|
{
|
|
if (!fade)
|
|
{
|
|
return;
|
|
}
|
|
|
|
const SDL_Rect rect = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
|
color_t color = {0, 0, 0};
|
|
const float step = (float)fadeCounter / (float)fadeLenght;
|
|
const int alpha = 0 + (255 - 0) * step;
|
|
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;
|
|
|
|
spectrumColor.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));
|
|
}
|
|
}
|
|
|
|
// Actualiza el spectrum fade
|
|
void Screen::updateSpectrumFade()
|
|
{
|
|
if (!spectrumFade)
|
|
{
|
|
return;
|
|
}
|
|
|
|
spectrumFadeCounter++;
|
|
if (spectrumFadeCounter > spectrumFadeLenght)
|
|
{
|
|
iniSpectrumFade();
|
|
SDL_SetTextureColorMod(gameCanvas, 255, 255, 255);
|
|
}
|
|
}
|
|
|
|
// Dibuja el spectrum fade
|
|
void Screen::renderSpectrumFade()
|
|
{
|
|
if (!spectrumFade)
|
|
{
|
|
return;
|
|
}
|
|
|
|
const float step = (float)spectrumFadeCounter / (float)spectrumFadeLenght;
|
|
const int max = spectrumColor.size() - 1;
|
|
const int index = max + (0 - max) * step;
|
|
const color_t c = spectrumColor[index];
|
|
SDL_SetTextureColorMod(gameCanvas, c.r, c.g, c.b);
|
|
}
|
|
|
|
// Actualiza los efectos
|
|
void Screen::updateFX()
|
|
{
|
|
updateFade();
|
|
updateSpectrumFade();
|
|
}
|
|
|
|
// Dibuja los efectos
|
|
void Screen::renderFX()
|
|
{
|
|
renderFade();
|
|
renderSpectrumFade();
|
|
}
|
|
|
|
// Actualiza el notificador
|
|
void Screen::updateNotifier()
|
|
{
|
|
notify->update();
|
|
notifyActive = 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);
|
|
}
|
|
|
|
// Dibuja las notificaciones
|
|
void Screen::renderNotifications()
|
|
{
|
|
if (notifyActive)
|
|
{
|
|
notify->render();
|
|
}
|
|
}
|
|
|
|
// Establece el tamaño de las notificaciones
|
|
void Screen::setNotificationSize()
|
|
{
|
|
if (options->videoMode == 0)
|
|
{
|
|
if (options->windowSize == 3)
|
|
{
|
|
notificationLogicalWidth = (windowWidth * 3) / 2;
|
|
notificationLogicalHeight = (windowHeight * 3) / 2;
|
|
}
|
|
else
|
|
{
|
|
notificationLogicalWidth = windowWidth * 2;
|
|
notificationLogicalHeight = windowHeight * 2;
|
|
}
|
|
}
|
|
|
|
if (options->videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
|
{
|
|
notificationLogicalWidth = windowWidth / 3;
|
|
notificationLogicalHeight = windowHeight / 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);
|
|
}
|
|
|
|
// 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);
|
|
|
|
if (options->shaders)
|
|
{
|
|
// Aplica shaders y renderiza el contenido
|
|
shader::render();
|
|
}
|
|
else
|
|
{
|
|
if (options->borderEnabled)
|
|
{
|
|
SDL_RenderCopy(renderer, borderCanvas, nullptr, nullptr);
|
|
}
|
|
else
|
|
{
|
|
SDL_RenderCopy(renderer, gameCanvas, nullptr, &dest);
|
|
}
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
}
|
|
|
|
// Cambia el estado de los shaders
|
|
void Screen::toggleShaders()
|
|
{
|
|
options->shaders = !options->shaders;
|
|
setVideoMode(options->videoMode);
|
|
} |