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;
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();
}
}

View File

@@ -3,6 +3,7 @@
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string>
#include <vector>
#ifndef TEXTURE_H
#define TEXTURE_H
@@ -28,7 +29,8 @@ private:
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
std::vector<std::vector<Uint32>> palettes; // Vector con las diferentes paletas
int paletteIndex; // Indice de la paleta en uso
// Crea una nueva surface
Surface newSurface(int w, int h);
@@ -42,11 +44,8 @@ private:
// Vuelca la surface en la textura
void flipSurface();
// Establece un color de la paleta
void setPal(int index, Uint32 color);
// Carga una paleta desde un fichero
void loadPal(const char *filename);
std::vector<Uint32> 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

View File

@@ -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);

View File

@@ -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"));