diff --git a/source/texture.cpp b/source/texture.cpp index de85f08..92df196 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -2,13 +2,13 @@ #include "texture.h" #include // Para SDL_GetError #include // Para SDL_CreateRGBSurfaceWithFormatFrom -#include // Para SEEK_END, SEEK_SET -#include // Para fseek, fclose, fopen, fread, ftell, FILE -#include // Para free, malloc #include // Para basic_ostream, operator<<, basic_ifstream #include // Para cerr, cout #include // Para runtime_error +#include // Para char_traits, operator<<, operator+ +#include // Para vector #include "gif.c" // Para LoadGif, LoadPalette +#include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_a... #include "utils.h" // Para getFileName, printWithDots #define STB_IMAGE_IMPLEMENTATION @@ -315,8 +315,8 @@ std::vector Texture::loadPaletteFromFile(const std::string &file_path) { std::vector palette; - FILE *f = fopen(file_path.c_str(), "rb"); - if (!f) + 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)); @@ -326,21 +326,22 @@ std::vector Texture::loadPaletteFromFile(const std::string &file_path) printWithDots("Image : ", getFileName(file_path), "[ LOADED ]"); } - fseek(f, 0, SEEK_END); - long size = ftell(f); - fseek(f, 0, SEEK_SET); - Uint8 *buffer = static_cast(malloc(size)); - fread(buffer, size, 1, f); - fclose(f); + auto size = file.tellg(); + file.seekg(0, std::ios::beg); - const auto *pal = LoadPalette(buffer); + std::vector buffer(size); + if (!file.read(reinterpret_cast(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; } - free(buffer); - for (int i = 0; i < 256; ++i) { palette.push_back((pal[i] << 8) + 255);