Passant std::cout a SDL_Log
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
#include "texture.h"
|
||||
#include <SDL3/SDL_error.h> // Para SDL_GetError
|
||||
#include <SDL3/SDL_surface.h> // Para SDL_CreateRGBSurfaceWithFormatFrom
|
||||
#include <SDL3/SDL_log.h> // Para SDL_LogError, SDL_LogCategory, SDL_Log...
|
||||
#include <SDL3/SDL_surface.h> // Para SDL_CreateSurfaceFrom, SDL_DestroySurface
|
||||
#include <stdint.h> // Para uint32_t
|
||||
#include <cstring> // Para memcpy, size_t
|
||||
#include <fstream> // Para basic_ostream, operator<<, basic_ifstream
|
||||
#include <iostream> // Para cerr, cout
|
||||
#include <fstream> // Para basic_ifstream, basic_ios, ios, operator|
|
||||
#include <stdexcept> // Para runtime_error
|
||||
#include <string> // Para char_traits, operator<<, operator+
|
||||
#include <string> // Para operator+, char_traits, string, operat...
|
||||
#include <vector> // Para vector
|
||||
#include "gif.h" // Para Gif
|
||||
#include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_a...
|
||||
#include "utils.h" // Para getFileName, Color, printWithDots
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
@@ -67,7 +68,7 @@ bool Texture::loadFromFile(const std::string &file_path)
|
||||
unsigned char *data = stbi_load(file_path.c_str(), &width, &height, &orig_format, req_format);
|
||||
if (!data)
|
||||
{
|
||||
std::cerr << "Error: Fichero no encontrado " << getFileName(file_path) << std::endl;
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", getFileName(file_path).c_str());
|
||||
throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path));
|
||||
}
|
||||
else
|
||||
@@ -89,11 +90,10 @@ bool Texture::loadFromFile(const std::string &file_path)
|
||||
SDL_Texture *new_texture = nullptr;
|
||||
|
||||
// Carga la imagen desde una ruta específica
|
||||
/*auto loaded_surface = SDL_CreateRGBSurfaceWithFormatFrom(static_cast<void *>(data), width, height, depth, pitch, pixel_format);*/
|
||||
auto loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, static_cast<void *>(data), pitch);
|
||||
if (loaded_surface == nullptr)
|
||||
{
|
||||
std::cout << "Unable to load image " << file_path << std::endl;
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to load image %s", file_path.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -101,7 +101,7 @@ bool Texture::loadFromFile(const std::string &file_path)
|
||||
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
|
||||
if (new_texture == nullptr)
|
||||
{
|
||||
std::cout << "Unable to create texture from " << file_path << "! SDL Error: " << SDL_GetError() << std::endl;
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create texture from %s! SDL Error: %s", file_path.c_str(), SDL_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -127,7 +127,7 @@ bool Texture::createBlank(int width, int height, SDL_PixelFormat format, SDL_Tex
|
||||
texture_ = SDL_CreateTexture(renderer_, format, access, width, height);
|
||||
if (!texture_)
|
||||
{
|
||||
std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << std::endl;
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create blank texture! SDL Error: %s", SDL_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -249,7 +249,7 @@ std::shared_ptr<Surface> Texture::loadSurface(const std::string &file_path)
|
||||
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
||||
if (!file)
|
||||
{
|
||||
std::cerr << "Error: Fichero no encontrado " << file_path << std::endl;
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
|
||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ std::shared_ptr<Surface> Texture::loadSurface(const std::string &file_path)
|
||||
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;
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al leer el fichero %s", file_path.c_str());
|
||||
throw std::runtime_error("Error al leer el fichero: " + file_path);
|
||||
}
|
||||
|
||||
@@ -271,11 +271,11 @@ std::shared_ptr<Surface> Texture::loadSurface(const std::string &file_path)
|
||||
std::vector<Uint8> rawPixels = gif.loadGif(buffer.data(), w, h);
|
||||
if (rawPixels.empty())
|
||||
{
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudo cargar el GIF %s", file_path.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Si el constructor de Surface espera un std::shared_ptr<Uint8[]>,
|
||||
// reservamos un bloque dinámico y copiamos los datos del vector.
|
||||
// Si el constructor de Surface espera un std::shared_ptr<Uint8[]>:
|
||||
size_t pixelCount = rawPixels.size();
|
||||
auto pixels = std::shared_ptr<Uint8[]>(new Uint8[pixelCount], std::default_delete<Uint8[]>());
|
||||
std::memcpy(pixels.get(), rawPixels.data(), pixelCount);
|
||||
@@ -286,6 +286,7 @@ std::shared_ptr<Surface> Texture::loadSurface(const std::string &file_path)
|
||||
width_ = w;
|
||||
height_ = h;
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "GIF %s cargado correctamente.", file_path.c_str());
|
||||
return surface;
|
||||
}
|
||||
|
||||
@@ -325,8 +326,8 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
|
||||
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));
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
|
||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -340,8 +341,8 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
|
||||
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));
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudo leer completamente el fichero %s", file_path.c_str());
|
||||
throw std::runtime_error("Error al leer el fichero: " + file_path);
|
||||
}
|
||||
|
||||
// Usar la nueva función loadPalette, que devuelve un vector<uint32_t>
|
||||
@@ -349,6 +350,7 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
|
||||
std::vector<uint32_t> pal = gif.loadPalette(buffer.data());
|
||||
if (pal.empty())
|
||||
{
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Advertencia: No se encontró paleta en el archivo %s", file_path.c_str());
|
||||
return palette; // Devuelve un vector vacío si no hay paleta
|
||||
}
|
||||
|
||||
@@ -358,6 +360,7 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
|
||||
palette.push_back((color << 8) | 0xFF); // Resultado: 0xRRGGBBAA
|
||||
}
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Paleta cargada correctamente desde %s", file_path.c_str());
|
||||
return palette;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user