500 lines
13 KiB
C++
500 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 "notifier.h" // Para Notify
|
|
|
|
// [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)
|
|
{
|
|
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
|
|
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_);
|
|
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, game_canvas_width_ + options_->borderWidth * 2, game_canvas_height_ + options_->borderHeight * 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_->videoMode);
|
|
|
|
// Muestra la ventana
|
|
SDL_ShowWindow(window);
|
|
}
|
|
|
|
// Destructor
|
|
Screen::~Screen()
|
|
{
|
|
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_);
|
|
}
|
|
|
|
// 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_->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)
|
|
{
|
|
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
|
|
{
|
|
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_, 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
|
|
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_, &window_width_, &window_height_);
|
|
|
|
// 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 (((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_->keepAspect)
|
|
{
|
|
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
|
|
SDL_RenderSetLogicalSize(renderer_, window_width_, window_height_);
|
|
|
|
// Actualiza las opciones
|
|
options_->videoMode = videoMode;
|
|
options_->screen.windowWidth = window_width_;
|
|
options_->screen.windowHeight = window_height_;
|
|
|
|
// 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()->get(glsl_file).c_str());
|
|
std::string source((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
|
|
|
if (options_->borderEnabled)
|
|
{
|
|
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_->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)
|
|
{
|
|
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_->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_ || fade_counter_ > 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Activa el spectrum fade
|
|
void Screen::setspectrumFade()
|
|
{
|
|
spectrum_fade_ = true;
|
|
}
|
|
|
|
// Comprueba si ha terminado el spectrum fade
|
|
bool Screen::spectrumFadeEnded()
|
|
{
|
|
if (spectrum_fade_ || spectrum_fade_counter_ > 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Inicializa las variables para el fade
|
|
void Screen::iniFade()
|
|
{
|
|
fade_ = false;
|
|
fade_counter_ = 0;
|
|
fade_lenght_ = 200;
|
|
}
|
|
|
|
// Actualiza el fade
|
|
void Screen::updateFade()
|
|
{
|
|
if (!fade_)
|
|
{
|
|
return;
|
|
}
|
|
|
|
fade_counter_++;
|
|
if (fade_counter_ > fade_lenght_)
|
|
{
|
|
iniFade();
|
|
}
|
|
}
|
|
|
|
// Dibuja el fade
|
|
void Screen::renderFade()
|
|
{
|
|
if (!fade_)
|
|
{
|
|
return;
|
|
}
|
|
|
|
const SDL_Rect rect = {0, 0, game_canvas_width_, game_canvas_height_};
|
|
color_t color = {0, 0, 0};
|
|
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);
|
|
}
|
|
|
|
// Inicializa las variables para el fade spectrum
|
|
void Screen::iniSpectrumFade()
|
|
{
|
|
spectrum_fade_ = false;
|
|
spectrum_fade_counter_ = 0;
|
|
spectrum_fade_lenght_ = 50;
|
|
|
|
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)
|
|
{
|
|
spectrum_color_.push_back(stringToColor(options_->palette, v));
|
|
}
|
|
}
|
|
|
|
// Actualiza el spectrum fade
|
|
void Screen::updateSpectrumFade()
|
|
{
|
|
if (!spectrum_fade_)
|
|
{
|
|
return;
|
|
}
|
|
|
|
spectrum_fade_counter_++;
|
|
if (spectrum_fade_counter_ > spectrum_fade_lenght_)
|
|
{
|
|
iniSpectrumFade();
|
|
SDL_SetTextureColorMod(game_canvas_, 255, 255, 255);
|
|
}
|
|
}
|
|
|
|
// Dibuja el spectrum fade
|
|
void Screen::renderSpectrumFade()
|
|
{
|
|
if (!spectrum_fade_)
|
|
{
|
|
return;
|
|
}
|
|
|
|
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 = spectrum_color_[index];
|
|
SDL_SetTextureColorMod(game_canvas_, 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()
|
|
{
|
|
Notifier::get()->update();
|
|
}
|
|
|
|
// Muestra una notificación de texto por pantalla;
|
|
void Screen::showNotification(std::string text1, std::string text2, int icon)
|
|
{
|
|
Notifier::get()->showText(text1, text2, icon);
|
|
}
|
|
|
|
// Dibuja las notificaciones
|
|
void Screen::renderNotifications()
|
|
{
|
|
if (Notifier::get()->active())
|
|
{
|
|
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, &dest_);
|
|
SDL_SetRenderTarget(renderer_, temp);
|
|
}
|
|
|
|
// Muestra el contenido de Screen por pantalla
|
|
void Screen::renderPresent()
|
|
{
|
|
SDL_SetRenderTarget(renderer_, nullptr);
|
|
SDL_SetRenderDrawColor(renderer_, border_color_.r, border_color_.g, border_color_.b, 0xFF);
|
|
SDL_RenderClear(renderer_);
|
|
|
|
if (options_->shaders)
|
|
{
|
|
// Aplica shaders y renderiza el contenido
|
|
shader::render();
|
|
}
|
|
else
|
|
{
|
|
if (options_->borderEnabled)
|
|
{
|
|
SDL_RenderCopy(renderer_, border_canvas_, nullptr, nullptr);
|
|
}
|
|
else
|
|
{
|
|
SDL_RenderCopy(renderer_, game_canvas_, nullptr, &dest_);
|
|
}
|
|
SDL_RenderPresent(renderer_);
|
|
}
|
|
}
|
|
|
|
// Cambia el estado de los shaders
|
|
void Screen::toggleShaders()
|
|
{
|
|
options_->shaders = !options_->shaders;
|
|
setVideoMode(options_->videoMode);
|
|
} |