Retocs en Texture::loadPaletteFromFile
This commit is contained in:
@@ -2,13 +2,13 @@
|
|||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
#include <SDL2/SDL_error.h> // Para SDL_GetError
|
#include <SDL2/SDL_error.h> // Para SDL_GetError
|
||||||
#include <SDL2/SDL_surface.h> // Para SDL_CreateRGBSurfaceWithFormatFrom
|
#include <SDL2/SDL_surface.h> // Para SDL_CreateRGBSurfaceWithFormatFrom
|
||||||
#include <fcntl.h> // Para SEEK_END, SEEK_SET
|
|
||||||
#include <stdio.h> // Para fseek, fclose, fopen, fread, ftell, FILE
|
|
||||||
#include <stdlib.h> // Para free, malloc
|
|
||||||
#include <fstream> // Para basic_ostream, operator<<, basic_ifstream
|
#include <fstream> // Para basic_ostream, operator<<, basic_ifstream
|
||||||
#include <iostream> // Para cerr, cout
|
#include <iostream> // Para cerr, cout
|
||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
|
#include <string> // Para char_traits, operator<<, operator+
|
||||||
|
#include <vector> // Para vector
|
||||||
#include "gif.c" // Para LoadGif, LoadPalette
|
#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
|
#include "utils.h" // Para getFileName, printWithDots
|
||||||
|
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
@@ -315,8 +315,8 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
|
|||||||
{
|
{
|
||||||
std::vector<Uint32> palette;
|
std::vector<Uint32> palette;
|
||||||
|
|
||||||
FILE *f = fopen(file_path.c_str(), "rb");
|
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
||||||
if (!f)
|
if (!file)
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Fichero no encontrado " << getFileName(file_path) << std::endl;
|
std::cerr << "Error: Fichero no encontrado " << getFileName(file_path) << std::endl;
|
||||||
throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path));
|
throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path));
|
||||||
@@ -326,21 +326,22 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
|
|||||||
printWithDots("Image : ", getFileName(file_path), "[ LOADED ]");
|
printWithDots("Image : ", getFileName(file_path), "[ LOADED ]");
|
||||||
}
|
}
|
||||||
|
|
||||||
fseek(f, 0, SEEK_END);
|
auto size = file.tellg();
|
||||||
long size = ftell(f);
|
file.seekg(0, std::ios::beg);
|
||||||
fseek(f, 0, SEEK_SET);
|
|
||||||
Uint8 *buffer = static_cast<Uint8 *>(malloc(size));
|
|
||||||
fread(buffer, size, 1, f);
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
const auto *pal = LoadPalette(buffer);
|
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)
|
if (!pal)
|
||||||
{
|
{
|
||||||
return palette;
|
return palette;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(buffer);
|
|
||||||
|
|
||||||
for (int i = 0; i < 256; ++i)
|
for (int i = 0; i < 256; ++i)
|
||||||
{
|
{
|
||||||
palette.push_back((pal[i] << 8) + 255);
|
palette.push_back((pal[i] << 8) + 255);
|
||||||
|
|||||||
Reference in New Issue
Block a user