la classe Texture ja pot tindre un numero indefinit de paletes 😌

This commit is contained in:
2024-07-25 10:13:20 +02:00
parent 073dd2a904
commit 1a00a08300
5 changed files with 60 additions and 23 deletions

BIN
data/gfx/player1_pal1.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 B

View File

@@ -17,10 +17,8 @@ Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose)
texture = nullptr; texture = nullptr;
width = 0; width = 0;
height = 0; height = 0;
for (int i = 0; i < 256; ++i) paletteIndex = 0;
{ palettes.clear();
paleta[i] = 0;
}
// Carga el fichero en la textura // Carga el fichero en la textura
if (path != "") if (path != "")
@@ -38,8 +36,8 @@ Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose)
else if (extension == "gif") else if (extension == "gif")
{ {
surface = loadSurface(path.c_str()); surface = loadSurface(path.c_str());
loadPal(path.c_str()); addPalette(path.c_str());
setPal(0, 0x00000000); setPaletteColor(0, 0, 0x00000000);
createBlank(renderer, width, height); createBlank(renderer, width, height);
flipSurface(); flipSurface();
} }
@@ -301,29 +299,39 @@ Surface Texture::loadSurface(const char *filename)
// Vuelca la surface en la textura // Vuelca la surface en la textura
void Texture::flipSurface() 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; Uint32 *pixels;
int pitch; int pitch;
SDL_LockTexture(texture, nullptr, (void **)&pixels, &pitch); SDL_LockTexture(texture, nullptr, (void **)&pixels, &pitch);
for (int i = 0; i < width * height; ++i) for (int i = 0; i < width * height; ++i)
{ {
pixels[i] = paleta[surface->data[i]]; pixels[i] = palettes[paletteIndex][surface->data[i]];
} }
SDL_UnlockTexture(texture); SDL_UnlockTexture(texture);
} }
// Establece un color de la paleta // 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 // Carga una paleta desde un fichero
void Texture::loadPal(const char *filename) std::vector<Uint32> Texture::loadPal(const char *filename)
{ {
std::vector<Uint32> palette;
FILE *f = fopen(filename, "rb"); FILE *f = fopen(filename, "rb");
if (!f) if (!f)
{ {
return; return palette;
} }
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
@@ -334,15 +342,34 @@ void Texture::loadPal(const char *filename)
fclose(f); fclose(f);
Uint32 *pal = LoadPalette(buffer); Uint32 *pal = LoadPalette(buffer);
if (pal == NULL) if (pal == nullptr)
{ {
return; return palette;
} }
free(buffer); free(buffer);
for (int i = 0; i < 256; ++i) 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();
} }
} }

View File

@@ -3,6 +3,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <stdio.h> #include <stdio.h>
#include <string> #include <string>
#include <vector>
#ifndef TEXTURE_H #ifndef TEXTURE_H
#define TEXTURE_H #define TEXTURE_H
@@ -25,10 +26,11 @@ private:
Surface surface; // Surface para usar imagenes en formato gif con paleta Surface surface; // Surface para usar imagenes en formato gif con paleta
// Variables // Variables
int width; // Ancho de la imagen int width; // Ancho de la imagen
int height; // Alto de la imagen int height; // Alto de la imagen
std::string path; // Ruta de la imagen de la textura std::string path; // Ruta de la imagen de la textura
Uint32 paleta[256]; // Paleta para la surface std::vector<std::vector<Uint32>> palettes; // Vector con las diferentes paletas
int paletteIndex; // Indice de la paleta en uso
// Crea una nueva surface // Crea una nueva surface
Surface newSurface(int w, int h); Surface newSurface(int w, int h);
@@ -40,13 +42,10 @@ private:
Surface loadSurface(const char *filename); Surface loadSurface(const char *filename);
// Vuelca la surface en la textura // Vuelca la surface en la textura
void flipSurface(); void flipSurface();
// Establece un color de la paleta
void setPal(int index, Uint32 color);
// Carga una paleta desde un fichero // Carga una paleta desde un fichero
void loadPal(const char *filename); std::vector<Uint32> loadPal(const char *filename);
public: public:
// Constructor // Constructor
@@ -90,6 +89,15 @@ public:
// Obtiene la textura // Obtiene la textura
SDL_Texture *getSDLTexture(); 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 #endif

View File

@@ -364,6 +364,7 @@ bool Director::setFileList()
asset->add(prefix + "/data/gfx/title_dust.ani", t_data); 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.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/player2.gif", t_bitmap);
asset->add(prefix + "/data/gfx/player.ani", t_data); asset->add(prefix + "/data/gfx/player.ani", t_data);
asset->add(prefix + "/data/gfx/player1_power.gif", t_bitmap); asset->add(prefix + "/data/gfx/player1_power.gif", t_bitmap);

View File

@@ -454,6 +454,7 @@ void Game::loadMedia()
// Texturas - Player1 // Texturas - Player1
Texture *player1 = new Texture(renderer, asset->get("player1.gif")); Texture *player1 = new Texture(renderer, asset->get("player1.gif"));
player1->addPalette(asset->get("player1_pal1.gif"));
player1Textures.push_back(player1); player1Textures.push_back(player1);
Texture *player1Power = new Texture(renderer, asset->get("player1_power.gif")); Texture *player1Power = new Texture(renderer, asset->get("player1_power.gif"));