From 1a00a08300612af8700f6a53422cbe923e73de71 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 25 Jul 2024 10:13:20 +0200 Subject: [PATCH] =?UTF-8?q?la=20classe=20Texture=20ja=20pot=20tindre=20un?= =?UTF-8?q?=20numero=20indefinit=20de=20paletes=20=F0=9F=98=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/gfx/player1_pal1.gif | Bin 0 -> 173 bytes source/common/texture.cpp | 55 ++++++++++++++++++++++++++++---------- source/common/texture.h | 26 +++++++++++------- source/director.cpp | 1 + source/game.cpp | 1 + 5 files changed, 60 insertions(+), 23 deletions(-) create mode 100644 data/gfx/player1_pal1.gif diff --git a/data/gfx/player1_pal1.gif b/data/gfx/player1_pal1.gif new file mode 100644 index 0000000000000000000000000000000000000000..00b23ee32e43c249dfbeb525b23164ce084e0138 GIT binary patch literal 173 zcmZ?wbh9u|6kuRtIKlt|ekOXc4(7Q&j-{ogd-v{r@Zdp*Q}8sW;9ZGfpK}s!P6>TB zH|o=}I3tICGq*X`-Ye__xALrZ%weUb+EWea{0PvOu=@dfXzI2fz}Mrbf{ literal 0 HcmV?d00001 diff --git a/source/common/texture.cpp b/source/common/texture.cpp index e9860d6..13fdc67 100644 --- a/source/common/texture.cpp +++ b/source/common/texture.cpp @@ -17,10 +17,8 @@ Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose) texture = nullptr; width = 0; height = 0; - for (int i = 0; i < 256; ++i) - { - paleta[i] = 0; - } + paletteIndex = 0; + palettes.clear(); // Carga el fichero en la textura if (path != "") @@ -38,8 +36,8 @@ Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose) else if (extension == "gif") { surface = loadSurface(path.c_str()); - loadPal(path.c_str()); - setPal(0, 0x00000000); + addPalette(path.c_str()); + setPaletteColor(0, 0, 0x00000000); createBlank(renderer, width, height); flipSurface(); } @@ -301,29 +299,39 @@ Surface Texture::loadSurface(const char *filename) // Vuelca la surface en la textura void Texture::flipSurface() { + // Limpia la textura + SDL_Texture *temp = SDL_GetRenderTarget(renderer); + SDL_SetRenderTarget(renderer, texture); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + SDL_RenderClear(renderer); + SDL_SetRenderTarget(renderer, temp); + + // Vuelca los datos Uint32 *pixels; int pitch; SDL_LockTexture(texture, nullptr, (void **)&pixels, &pitch); for (int i = 0; i < width * height; ++i) { - pixels[i] = paleta[surface->data[i]]; + pixels[i] = palettes[paletteIndex][surface->data[i]]; } SDL_UnlockTexture(texture); } // Establece un color de la paleta -void Texture::setPal(int index, Uint32 color) +void Texture::setPaletteColor(int palette, int index, Uint32 color) { - paleta[index] = color; + palettes.at(palette)[index] = color; } // Carga una paleta desde un fichero -void Texture::loadPal(const char *filename) +std::vector Texture::loadPal(const char *filename) { + std::vector palette; + FILE *f = fopen(filename, "rb"); if (!f) { - return; + return palette; } fseek(f, 0, SEEK_END); @@ -334,15 +342,34 @@ void Texture::loadPal(const char *filename) fclose(f); Uint32 *pal = LoadPalette(buffer); - if (pal == NULL) + if (pal == nullptr) { - return; + return palette; } free(buffer); for (int i = 0; i < 256; ++i) { - paleta[i] = (pal[i] << 8) + 255; + palette.push_back((pal[i] << 8) + 255); + } + + return palette; +} + +// Añade una paleta a la lista +void Texture::addPalette(std::string path) +{ + palettes.push_back(loadPal(path.c_str())); + setPaletteColor((int)palettes.size() - 1, 0, 0x00000000); +} + +// Cambia la paleta de la textura +void Texture::setPalette(int palette) +{ + if (palette < (int)palettes.size()) + { + paletteIndex = palette; + flipSurface(); } } \ No newline at end of file diff --git a/source/common/texture.h b/source/common/texture.h index a7fe7ea..aa38dfc 100644 --- a/source/common/texture.h +++ b/source/common/texture.h @@ -3,6 +3,7 @@ #include #include #include +#include #ifndef TEXTURE_H #define TEXTURE_H @@ -25,10 +26,11 @@ private: Surface surface; // Surface para usar imagenes en formato gif con paleta // Variables - int width; // Ancho de la imagen - int height; // Alto de la imagen - std::string path; // Ruta de la imagen de la textura - Uint32 paleta[256]; // Paleta para la surface + int width; // Ancho de la imagen + int height; // Alto de la imagen + std::string path; // Ruta de la imagen de la textura + std::vector> palettes; // Vector con las diferentes paletas + int paletteIndex; // Indice de la paleta en uso // Crea una nueva surface Surface newSurface(int w, int h); @@ -40,13 +42,10 @@ private: Surface loadSurface(const char *filename); // Vuelca la surface en la textura - void flipSurface(); - - // Establece un color de la paleta - void setPal(int index, Uint32 color); + void flipSurface(); // Carga una paleta desde un fichero - void loadPal(const char *filename); + std::vector loadPal(const char *filename); public: // Constructor @@ -90,6 +89,15 @@ public: // Obtiene la textura SDL_Texture *getSDLTexture(); + + // Añade una paleta a la lista + void addPalette(std::string path); + + // Establece un color de la paleta + void setPaletteColor(int palette, int index, Uint32 color); + + // Cambia la paleta de la textura + void setPalette(int palette); }; #endif diff --git a/source/director.cpp b/source/director.cpp index 09dae79..7836925 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -364,6 +364,7 @@ bool Director::setFileList() asset->add(prefix + "/data/gfx/title_dust.ani", t_data); asset->add(prefix + "/data/gfx/player1.gif", t_bitmap); + asset->add(prefix + "/data/gfx/player1_pal1.gif", t_bitmap); asset->add(prefix + "/data/gfx/player2.gif", t_bitmap); asset->add(prefix + "/data/gfx/player.ani", t_data); asset->add(prefix + "/data/gfx/player1_power.gif", t_bitmap); diff --git a/source/game.cpp b/source/game.cpp index bfaf6c0..2d80081 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -454,6 +454,7 @@ void Game::loadMedia() // Texturas - Player1 Texture *player1 = new Texture(renderer, asset->get("player1.gif")); + player1->addPalette(asset->get("player1_pal1.gif")); player1Textures.push_back(player1); Texture *player1Power = new Texture(renderer, asset->get("player1_power.gif"));