forked from jaildesigner-jailgames/jaildoctors_dilemma
Treballant en la caché de les paletes
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
|
||||
#include "texture.h"
|
||||
#include <SDL2/SDL_error.h> // for SDL_GetError
|
||||
#include <SDL2/SDL_surface.h> // for SDL_CreateRGBSurfaceWithFormatFrom
|
||||
#include <iostream> // for basic_ostream, operator<<, endl, cout
|
||||
#include <stdexcept> // for runtime_error
|
||||
#include <string> // for char_traits, operator<<, string, opera...
|
||||
#include <vector> // for vector
|
||||
#include "utils.h" // for getFileName, Color, printWithDots
|
||||
#include <SDL2/SDL_error.h> // for SDL_GetError
|
||||
#include <SDL2/SDL_surface.h> // for SDL_CreateRGBSurfaceWithFormatFrom
|
||||
#include <iostream> // for basic_ostream, operator<<, endl, cout
|
||||
#include <stdexcept> // for runtime_error
|
||||
#include <string> // for char_traits, operator<<, string, opera...
|
||||
#include <vector> // for vector
|
||||
#include "utils.h" // for getFileName, Color, printWithDots
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h" // para stbi_failure_reason, stbi_image_free
|
||||
@@ -27,22 +27,6 @@ Texture::Texture(SDL_Renderer *renderer, const std::string &path)
|
||||
{
|
||||
loadFromFile(path_);
|
||||
}
|
||||
|
||||
// .gif
|
||||
/*else if (extension == "gif")
|
||||
{
|
||||
// Crea la surface desde un fichero
|
||||
surface_ = loadSurface(path_);
|
||||
|
||||
// Añade la propia paleta del fichero a la lista
|
||||
addPaletteFromFile(path_);
|
||||
// setPaletteColor(0, 0, 0x00000000);
|
||||
|
||||
// Crea la textura, establece el BlendMode y copia la surface a la textura
|
||||
createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
|
||||
SDL_SetTextureBlendMode(texture_, SDL_BLENDMODE_BLEND);
|
||||
flipSurface();
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +34,6 @@ Texture::Texture(SDL_Renderer *renderer, const std::string &path)
|
||||
Texture::~Texture()
|
||||
{
|
||||
unloadTexture();
|
||||
// unloadSurface();
|
||||
palettes_.clear();
|
||||
}
|
||||
|
||||
@@ -58,7 +41,9 @@ Texture::~Texture()
|
||||
bool Texture::loadFromFile(const std::string &file_path)
|
||||
{
|
||||
if (file_path.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int req_format = STBI_rgb_alpha;
|
||||
int width, height, orig_format;
|
||||
@@ -75,18 +60,10 @@ bool Texture::loadFromFile(const std::string &file_path)
|
||||
|
||||
int depth, pitch;
|
||||
Uint32 pixel_format;
|
||||
/*if (req_format == STBI_rgb)
|
||||
{
|
||||
depth = 24;
|
||||
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
|
||||
pixel_format = SDL_PIXELFORMAT_RGB24;
|
||||
}
|
||||
else*/
|
||||
{ // STBI_rgb_alpha (RGBA)
|
||||
depth = 32;
|
||||
pitch = 4 * width;
|
||||
pixel_format = SDL_PIXELFORMAT_RGBA32;
|
||||
}
|
||||
// STBI_rgb_alpha (RGBA)
|
||||
depth = 32;
|
||||
pitch = 4 * width;
|
||||
pixel_format = SDL_PIXELFORMAT_RGBA32;
|
||||
|
||||
// Limpia
|
||||
unloadTexture();
|
||||
@@ -157,26 +134,14 @@ void Texture::unloadTexture()
|
||||
}
|
||||
|
||||
// Establece el color para la modulacion
|
||||
void Texture::setColor(Uint8 red, Uint8 green, Uint8 blue)
|
||||
{
|
||||
SDL_SetTextureColorMod(texture_, red, green, blue);
|
||||
}
|
||||
void Texture::setColor(Color color)
|
||||
{
|
||||
SDL_SetTextureColorMod(texture_, color.r, color.g, color.b);
|
||||
}
|
||||
void Texture::setColor(Uint8 red, Uint8 green, Uint8 blue) { SDL_SetTextureColorMod(texture_, red, green, blue); }
|
||||
void Texture::setColor(Color color) { SDL_SetTextureColorMod(texture_, color.r, color.g, color.b); }
|
||||
|
||||
// Establece el blending
|
||||
void Texture::setBlendMode(SDL_BlendMode blending)
|
||||
{
|
||||
SDL_SetTextureBlendMode(texture_, blending);
|
||||
}
|
||||
void Texture::setBlendMode(SDL_BlendMode blending) { SDL_SetTextureBlendMode(texture_, blending); }
|
||||
|
||||
// Establece el alpha para la modulación
|
||||
void Texture::setAlpha(Uint8 alpha)
|
||||
{
|
||||
SDL_SetTextureAlphaMod(texture_, alpha);
|
||||
}
|
||||
void Texture::setAlpha(Uint8 alpha) { SDL_SetTextureAlphaMod(texture_, alpha); }
|
||||
|
||||
// Renderiza la textura en un punto específico
|
||||
void Texture::render(int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_RendererFlip flip)
|
||||
@@ -207,177 +172,19 @@ void Texture::render(int x, int y, SDL_Rect *clip, float zoomW, float zoomH, dou
|
||||
}
|
||||
|
||||
// Establece la textura como objetivo de renderizado
|
||||
void Texture::setAsRenderTarget(SDL_Renderer *renderer)
|
||||
{
|
||||
SDL_SetRenderTarget(renderer, texture_);
|
||||
}
|
||||
void Texture::setAsRenderTarget(SDL_Renderer *renderer) { SDL_SetRenderTarget(renderer, texture_); }
|
||||
|
||||
// Obtiene el ancho de la imagen
|
||||
int Texture::getWidth()
|
||||
{
|
||||
return width_;
|
||||
}
|
||||
int Texture::getWidth() { return width_; }
|
||||
|
||||
// Obtiene el alto de la imagen
|
||||
int Texture::getHeight()
|
||||
{
|
||||
return height_;
|
||||
}
|
||||
int Texture::getHeight() { return height_; }
|
||||
|
||||
// Recarga la textura
|
||||
bool Texture::reLoad()
|
||||
{
|
||||
return loadFromFile(path_);
|
||||
}
|
||||
bool Texture::reLoad() { return loadFromFile(path_); }
|
||||
|
||||
// Obtiene la textura
|
||||
SDL_Texture *Texture::getSDLTexture()
|
||||
{
|
||||
return texture_;
|
||||
}
|
||||
|
||||
// Desencadenar la superficie actual
|
||||
/*void Texture::unloadSurface()
|
||||
{
|
||||
surface_.reset(); // Resetea el shared_ptr
|
||||
width_ = 0;
|
||||
height_ = 0;
|
||||
}*/
|
||||
|
||||
// Crea una surface desde un fichero .gif
|
||||
/*std::shared_ptr<Surface> Texture::loadSurface(const std::string &file_path)
|
||||
{
|
||||
// Desencadenar la superficie actual
|
||||
unloadSurface();
|
||||
|
||||
// Abrir el archivo usando std::ifstream para manejo automático del recurso
|
||||
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
||||
if (!file)
|
||||
{
|
||||
std::cerr << "Error: Fichero no encontrado " << file_path << std::endl;
|
||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||
}
|
||||
|
||||
// Obtener el tamaño del archivo
|
||||
std::streamsize size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
// Leer el contenido del archivo en un buffer
|
||||
std::vector<Uint8> buffer(size);
|
||||
if (!file.read(reinterpret_cast<char *>(buffer.data()), size))
|
||||
{
|
||||
std::cerr << "Error al leer el fichero " << file_path << std::endl;
|
||||
throw std::runtime_error("Error al leer el fichero: " + file_path);
|
||||
}
|
||||
|
||||
// Cerrar el archivo (automáticamente manejado por std::ifstream)
|
||||
file.close();
|
||||
|
||||
// Llamar a la función LoadGif
|
||||
Uint16 w, h;
|
||||
Uint8 *rawPixels = LoadGif(buffer.data(), &w, &h);
|
||||
if (!rawPixels)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Crear un std::shared_ptr con std::make_shared para pixels
|
||||
auto pixels = std::shared_ptr<Uint8[]>(rawPixels, std::default_delete<Uint8[]>());
|
||||
auto surface = std::make_shared<Surface>(w, h, pixels);
|
||||
|
||||
// Actualizar la anchura y altura
|
||||
width_ = w;
|
||||
height_ = h;
|
||||
|
||||
return surface;
|
||||
}*/
|
||||
|
||||
// Vuelca la surface en la textura
|
||||
/*void Texture::flipSurface()
|
||||
{
|
||||
// Limpia la textura
|
||||
auto 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, reinterpret_cast<void **>(&pixels), &pitch);
|
||||
for (int i = 0; i < width_ * height_; ++i)
|
||||
{
|
||||
pixels[i] = palettes_[current_palette_][surface_->data[i]];
|
||||
}
|
||||
SDL_UnlockTexture(texture_);
|
||||
}*/
|
||||
|
||||
// Establece un color de la paleta
|
||||
/*void Texture::setPaletteColor(int palette, int index, Uint32 color)
|
||||
{
|
||||
palettes_.at(palette)[index] = color;
|
||||
}*/
|
||||
|
||||
// Carga una paleta desde un fichero
|
||||
/*std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
|
||||
{
|
||||
std::vector<Uint32> palette;
|
||||
|
||||
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
||||
if (!file)
|
||||
{
|
||||
std::cerr << "Error: Fichero no encontrado " << getFileName(file_path) << std::endl;
|
||||
throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path));
|
||||
}
|
||||
else
|
||||
{
|
||||
printWithDots("Image : ", getFileName(file_path), "[ LOADED ]");
|
||||
}
|
||||
|
||||
auto size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<Uint8> buffer(size);
|
||||
if (!file.read(reinterpret_cast<char *>(buffer.data()), size))
|
||||
{
|
||||
std::cerr << "Error: No se pudo leer completamente el fichero " << getFileName(file_path) << std::endl;
|
||||
throw std::runtime_error("Error al leer el fichero: " + getFileName(file_path));
|
||||
}
|
||||
|
||||
const auto pal = LoadPalette(buffer.data());
|
||||
if (!pal)
|
||||
{
|
||||
return palette;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 256; ++i)
|
||||
{
|
||||
palette.push_back((pal[i] << 8) + 255);
|
||||
}
|
||||
|
||||
return palette;
|
||||
}*/
|
||||
|
||||
// Añade una paleta a la lista
|
||||
/*void Texture::addPaletteFromFile(const std::string &path)
|
||||
{
|
||||
palettes_.emplace_back(loadPaletteFromFile(path));
|
||||
setPaletteColor((int)palettes_.size() - 1, 0, 0x00000000);
|
||||
}
|
||||
|
||||
// Cambia la paleta de la textura
|
||||
void Texture::setPalette(int palette)
|
||||
{
|
||||
if (palette < (int)palettes_.size())
|
||||
{
|
||||
current_palette_ = palette;
|
||||
flipSurface();
|
||||
}
|
||||
}*/
|
||||
SDL_Texture *Texture::getSDLTexture() { return texture_; }
|
||||
|
||||
// Obtiene el renderizador
|
||||
SDL_Renderer *Texture::getRenderer()
|
||||
{
|
||||
return renderer_;
|
||||
}
|
||||
SDL_Renderer *Texture::getRenderer() { return renderer_; }
|
||||
Reference in New Issue
Block a user