From 09ac9524043c141ba9081e617df39e16a8b40e2a Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 7 Mar 2025 14:32:35 +0100 Subject: [PATCH] Afegit efecte de fundit a negre per a la paleta principal i per a la secundaria. Implementat en Title --- source/logo.cpp | 2 +- source/surface.cpp | 35 ++++++++++++++++++++++++++++++----- source/surface.h | 9 ++++++++- source/title.cpp | 2 +- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/source/logo.cpp b/source/logo.cpp index a605ccd..e129f3b 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -21,7 +21,7 @@ Logo::Logo() { since_1998_sprite_->setClip(0, 0, since_1998_surface_->getWidth(), since_1998_surface_->getHeight()); since_1998_color_ = static_cast(PaletteColor::BLACK); - jailgames_color_ = static_cast(PaletteColor::WHITE); + jailgames_color_ = static_cast(PaletteColor::BRIGHT_WHITE); // Crea los sprites de cada linea for (int i = 0; i < jailgames_surface_->getHeight(); ++i) diff --git a/source/surface.cpp b/source/surface.cpp index 8c04242..dcda9c0 100644 --- a/source/surface.cpp +++ b/source/surface.cpp @@ -104,13 +104,15 @@ Palette readPalFile(const std::string &file_path) // Constructor Surface::Surface(int w, int h) : surface_data_(std::make_shared(w, h)), - transparent_color_(static_cast(PaletteColor::TRANSPARENT)) {} + transparent_color_(static_cast(PaletteColor::TRANSPARENT)) { initializeSubPalette(sub_palette_); } Surface::Surface(const std::string &file_path) : transparent_color_(static_cast(PaletteColor::TRANSPARENT)) { SurfaceData loadedData = loadSurface(file_path); surface_data_ = std::make_shared(std::move(loadedData)); + + initializeSubPalette(sub_palette_); } // Carga una superficie desde un archivo @@ -307,7 +309,7 @@ void Surface::render(int dx, int dy, int sx, int sy, int w, int h) Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width]; if (color != transparent_color_) { - surface_data->data[dest_x + dest_y * surface_data->width] = color; + surface_data->data[dest_x + dest_y * surface_data->width] = sub_palette_[color]; } } } @@ -354,7 +356,7 @@ void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip) Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width]; if (color != transparent_color_) { - surface_data_dest->data[dest_x + dest_y * surface_data_dest->width] = color; + surface_data_dest->data[dest_x + dest_y * surface_data_dest->width] = sub_palette_[color]; } } } @@ -411,7 +413,7 @@ void Surface::render(SDL_Rect *srcRect, SDL_Rect *dstRect, SDL_RendererFlip flip Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width]; if (color != transparent_color_) { - surface_data->data[dest_x + dest_y * surface_data->width] = color; + surface_data->data[dest_x + dest_y * surface_data->width] = sub_palette_[color]; } } } @@ -508,7 +510,7 @@ void Surface::copyToTexture(SDL_Renderer *renderer, SDL_Texture *texture) } } -// Realiza un efecto de fundido en la paleta +// Realiza un efecto de fundido en la paleta principal bool Surface::fadePalette() { // Verificar que el tamaño mínimo de palette_ sea adecuado @@ -529,4 +531,27 @@ bool Surface::fadePalette() // Devolver si el índice 15 coincide con el índice 0 return palette_[15] == palette_[0]; +} + +// Realiza un efecto de fundido en la paleta secundaria +bool Surface::fadeSubPalette() +{ + // Verificar que el tamaño mínimo de sub_palette_ sea adecuado + static constexpr int sub_palette_size = 19; + if (sizeof(sub_palette_) / sizeof(sub_palette_[0]) < sub_palette_size) + { + throw std::runtime_error("Palette size is insufficient for fadePalette operation."); + } + + // Desplazar colores (pares e impares) + for (int i = 18; i > 1; --i) + { + sub_palette_[i] = sub_palette_[i - 2]; + } + + // Ajustar el primer color + sub_palette_[1] = sub_palette_[0]; + + // Devolver si el índice 15 coincide con el índice 0 + return sub_palette_[15] == sub_palette_[0]; } \ No newline at end of file diff --git a/source/surface.h b/source/surface.h index 3a1c187..2442a7f 100644 --- a/source/surface.h +++ b/source/surface.h @@ -3,11 +3,13 @@ #include // Para SDL_Renderer #include // Para Uint8, Uint32 #include +#include #include #include // Alias using Palette = std::array; +using SubPalette = std::array; // Carga una paleta desde un archivo .gif Palette loadPalette(const std::string &file_path); @@ -71,6 +73,7 @@ class Surface private: std::shared_ptr surface_data_; // Datos a dibujar Palette palette_; // Paleta para volcar la SurfaceData a una Textura + SubPalette sub_palette_; // Paleta para reindexar colores int transparent_color_; // Indice de la paleta que se omite en la copia de datos public: @@ -105,8 +108,9 @@ public: // Vuelca la SurfaceData a una textura void copyToTexture(SDL_Renderer *renderer, SDL_Texture *texture); - // Realiza un efecto de fundido en la paleta + // Realiza un efecto de fundido en las paletas bool fadePalette(); + bool fadeSubPalette(); // Pone un pixel en la SurfaceData void putPixel(int x, int y, Uint8 color); @@ -137,4 +141,7 @@ public: // Paleta void setPalette(const std::array &palette) { palette_ = palette; } + + // Inicializa la sub paleta + void initializeSubPalette(SubPalette &palette) { std::iota(palette.begin(), palette.end(), 0); } }; diff --git a/source/title.cpp b/source/title.cpp index 64b040d..0bd7b93 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -202,7 +202,7 @@ void Title::update() case TitleState::FADE_LOADING_SCREEN: if (counter_ % 4 == 0) { - if (true) // poner aqui la condicion de "fin de fade = true" + if (loading_screen_surface_->fadeSubPalette()) { counter_ = 0; state_ = TitleState::SHOW_MENU;