Implementats els shaders

This commit is contained in:
2025-02-20 08:48:55 +01:00
parent cc0f050c50
commit 9cb57e2ff2
20 changed files with 957 additions and 104 deletions

View File

@@ -1,5 +1,8 @@
#include "screen.h"
#include "jail_shader.h"
#include <string>
#include <fstream> // Para basic_ifstream, ifstream
#include <iterator> // Para istreambuf_iterator, operator==
#include <iostream>
// Constructor
@@ -51,6 +54,9 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
// Inicializa variables
notifyActive = false;
// Muestra la ventana
SDL_ShowWindow(window);
}
// Destructor
@@ -81,29 +87,19 @@ void Screen::startDrawOnBorder()
}
// Vuelca el contenido del renderizador en pantalla
void Screen::blit()
void Screen::render()
{
// Vuelve a dejar el renderizador en modo normal
SDL_SetRenderTarget(renderer, nullptr);
// Borra el contenido previo
// SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
// SDL_RenderClear(renderer);
// Copia la textura del borde en la ventana
if (options->borderEnabled)
{
SDL_RenderCopy(renderer, borderCanvas, nullptr, nullptr);
}
// Copia la textura de juego en la ventana en la posición adecuada
SDL_RenderCopy(renderer, gameCanvas, nullptr, &dest);
// Dibuja las notificaciones
// Renderiza sobre gameCanvas los overlays
renderNotifications();
// Muestra por pantalla el renderizador
SDL_RenderPresent(renderer);
// Si está el borde activo, vuelca gameCanvas sobre borderCanvas
if (options->borderEnabled)
{
gameCanvasToBorderCanvas();
}
// Muestra el contenido por pantalla
renderPresent();
}
// Establece el modo de video
@@ -118,9 +114,6 @@ void Screen::setVideoMode(int videoMode)
// Muestra el puntero
SDL_ShowCursor(SDL_ENABLE);
// Esconde la ventana
// SDL_HideWindow(window);
// Modifica el tamaño de la ventana en función del borde
if (options->borderEnabled)
{
@@ -139,9 +132,6 @@ void Screen::setVideoMode(int videoMode)
// 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);
// Muestra la ventana
// SDL_ShowWindow(window);
}
// Si está activo el modo de pantalla completa añade el borde
@@ -204,10 +194,27 @@ void Screen::setVideoMode(int videoMode)
// 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::switchVideoMode()
void Screen::toggleVideoMode()
{
options->videoMode = (options->videoMode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
setVideoMode(options->videoMode);
@@ -240,10 +247,11 @@ void Screen::incWindowSize()
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, nullptr);
SDL_SetRenderTarget(renderer, temp);
}
// Cambia el tipo de mezcla
@@ -271,7 +279,7 @@ void Screen::setBorderEnabled(bool value)
}
// Cambia entre borde visible y no visible
void Screen::switchBorder()
void Screen::toggleBorder()
{
options->borderEnabled = !options->borderEnabled;
setVideoMode(0);
@@ -428,14 +436,10 @@ void Screen::showNotification(std::string text1, std::string text2, int icon)
// Dibuja las notificaciones
void Screen::renderNotifications()
{
if (!notifyActive)
if (notifyActive)
{
return;
notify->render();
}
SDL_RenderSetLogicalSize(renderer, notificationLogicalWidth, notificationLogicalHeight);
notify->render();
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
}
// Establece el tamaño de las notificaciones
@@ -460,4 +464,48 @@ void Screen::setNotificationSize()
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);
}