Ja tornen a funcionar els shaders

This commit is contained in:
2025-03-06 14:24:31 +01:00
parent 982db7f75b
commit d0e06e30c9
2 changed files with 46 additions and 7 deletions

View File

@@ -76,6 +76,17 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
} }
} }
// Crea la textura donde se dibuja el borde que rodea el area de juego
shaders_texture_ = 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 (!shaders_texture_)
{
// Registrar el error si está habilitado
if (options.console)
{
std::cerr << "Error: shaders_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
// Crea la surface donde se dibujan los graficos del juego // Crea la surface donde se dibujan los graficos del juego
game_surface_ = std::make_shared<Surface>(options.game.width, options.game.height); game_surface_ = std::make_shared<Surface>(options.game.width, options.game.height);
game_surface_->loadPalette(palettes_.at(current_palette_)); game_surface_->loadPalette(palettes_.at(current_palette_));
@@ -103,6 +114,7 @@ Screen::~Screen()
{ {
SDL_DestroyTexture(game_texture_); SDL_DestroyTexture(game_texture_);
SDL_DestroyTexture(border_texture_); SDL_DestroyTexture(border_texture_);
SDL_DestroyTexture(shaders_texture_);
} }
// Limpia la pantalla // Limpia la pantalla
@@ -225,6 +237,7 @@ void Screen::setBorderEnabled(bool value) { options.video.border.enabled = value
void Screen::toggleBorder() void Screen::toggleBorder()
{ {
options.video.border.enabled = !options.video.border.enabled; options.video.border.enabled = !options.video.border.enabled;
createShadersTexture();
setVideoMode(options.video.mode); setVideoMode(options.video.mode);
} }
@@ -320,7 +333,7 @@ void Screen::resetShaders()
std::ifstream f(Asset::get()->get(GLSL_FILE).c_str()); std::ifstream f(Asset::get()->get(GLSL_FILE).c_str());
std::string source((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); std::string source((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
shader::init(window_, options.video.border.enabled ? border_texture_ : game_texture_, source.c_str()); shader::init(window_, shaders_texture_, source.c_str());
} }
} }
@@ -383,18 +396,20 @@ void Screen::surfaceToTexture()
// Copia la textura al renderizador // Copia la textura al renderizador
void Screen::textureToRenderer() void Screen::textureToRenderer()
{ {
SDL_Texture *texture_to_render = options.video.border.enabled ? border_texture_ : game_texture_;
SDL_SetRenderTarget(renderer_, nullptr);
SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer_);
if (options.video.shaders) if (options.video.shaders)
{ {
SDL_SetRenderTarget(renderer_, shaders_texture_);
SDL_RenderCopy(renderer_, texture_to_render, nullptr, nullptr);
SDL_SetRenderTarget(renderer_, nullptr);
shader::render(); shader::render();
} }
else else
{ {
SDL_Texture *texture_to_render = options.video.border.enabled ? border_texture_ : game_texture_; SDL_SetRenderTarget(renderer_, nullptr);
SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer_);
SDL_RenderCopy(renderer_, texture_to_render, nullptr, nullptr); SDL_RenderCopy(renderer_, texture_to_render, nullptr, nullptr);
SDL_RenderPresent(renderer_); SDL_RenderPresent(renderer_);
} }
@@ -420,3 +435,23 @@ size_t Screen::findPalette(const std::string &name)
} }
return static_cast<size_t>(0); return static_cast<size_t>(0);
} }
// Recrea la textura para los shaders
void Screen::createShadersTexture()
{
if (shaders_texture_)
{
SDL_DestroyTexture(shaders_texture_);
}
// Crea la textura donde se dibuja el borde que rodea el area de juego
shaders_texture_ = 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 (!shaders_texture_)
{
// Registrar el error si está habilitado
if (options.console)
{
std::cerr << "Error: shaders_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
}

View File

@@ -30,6 +30,7 @@ private:
SDL_Renderer *renderer_; // El renderizador de la ventana SDL_Renderer *renderer_; // El renderizador de la ventana
SDL_Texture *game_texture_; // Textura donde se dibuja el juego SDL_Texture *game_texture_; // Textura donde se dibuja el juego
SDL_Texture *border_texture_; // Textura donde se dibuja el borde del juego SDL_Texture *border_texture_; // Textura donde se dibuja el borde del juego
SDL_Texture *shaders_texture_; // Textura para aplicar los shaders
std::shared_ptr<Surface> game_surface_; // Surface principal para manejar game_surface_data_ std::shared_ptr<Surface> game_surface_; // Surface principal para manejar game_surface_data_
std::shared_ptr<Surface> border_surface_; // Surface para pintar el el borde de la pantalla std::shared_ptr<Surface> border_surface_; // Surface para pintar el el borde de la pantalla
std::shared_ptr<std::shared_ptr<Surface>> renderer_surface_; // Puntero a la Surface que actua std::shared_ptr<std::shared_ptr<Surface>> renderer_surface_; // Puntero a la Surface que actua
@@ -71,7 +72,10 @@ private:
void renderOverlays(); void renderOverlays();
// Localiza la paleta dentro del vector de paletas // Localiza la paleta dentro del vector de paletas
size_t findPalette(const std::string& name); size_t findPalette(const std::string &name);
// Recrea la textura para los shaders
void createShadersTexture();
// Constructor // Constructor
Screen(SDL_Window *window, SDL_Renderer *renderer); Screen(SDL_Window *window, SDL_Renderer *renderer);