la classe Texture ja pot tindre un numero indefinit de paletes 😌
This commit is contained in:
@@ -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<Uint32> Texture::loadPal(const char *filename)
|
||||
{
|
||||
std::vector<Uint32> 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user