neteja tidy a source/core i encamina Texture::loadFromFile pel ResourceHelper

This commit is contained in:
2026-05-14 20:22:54 +02:00
parent 88fa3f296f
commit 1912200b21
40 changed files with 699 additions and 578 deletions
+33 -22
View File
@@ -2,11 +2,12 @@
#include "core/rendering/texture.h"
#include <SDL3/SDL.h>
#include <stdlib.h> // for exit
#include <cstdlib> // for exit
#include <iostream> // for basic_ostream, operator<<, cout, endl
#define STB_IMAGE_IMPLEMENTATION
#include "external/stb_image.h" // for stbi_failure_reason, stbi_image_free
#include "core/resources/resource_helper.h" // for loadFile (pack + filesystem fallback)
#include "external/stb_image.h" // for stbi_failure_reason, stbi_image_free
SDL_ScaleMode Texture::currentScaleMode = SDL_SCALEMODE_NEAREST;
@@ -32,8 +33,7 @@ Texture::Texture(SDL_Renderer *renderer, const std::vector<uint8_t> &bytes, bool
: texture(nullptr),
renderer(renderer),
width(0),
height(0),
path("") {
height(0) {
if (!bytes.empty()) {
loadFromMemory(bytes.data(), bytes.size(), renderer, verbose);
}
@@ -46,7 +46,7 @@ Texture::~Texture() {
}
// Helper: convierte píxeles RGBA decodificados por stbi en SDL_Texture
static SDL_Texture *createTextureFromPixels(SDL_Renderer *renderer, unsigned char *data, int w, int h, int *out_w, int *out_h) {
static auto createTextureFromPixels(SDL_Renderer *renderer, unsigned char *data, int w, int h, int *out_w, int *out_h) -> SDL_Texture * {
const int pitch = 4 * w;
SDL_Surface *loadedSurface = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGBA32, static_cast<void *>(data), pitch);
if (loadedSurface == nullptr) {
@@ -62,23 +62,32 @@ static SDL_Texture *createTextureFromPixels(SDL_Renderer *renderer, unsigned cha
return newTexture;
}
// Carga una imagen desde un fichero
bool Texture::loadFromFile(const std::string &path, SDL_Renderer *renderer, bool verbose) {
// Carga una imagen desde un fichero (vía ResourceHelper: pack si està inicialitzat, filesystem si no)
auto Texture::loadFromFile(const std::string &path, SDL_Renderer *renderer, bool verbose) -> bool {
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
auto bytes = ResourceHelper::loadFile(path);
if (bytes.empty()) {
SDL_Log("Loading image failed: can't open %s", path.c_str());
exit(1);
}
int req_format = STBI_rgb_alpha;
int w, h, orig_format;
unsigned char *data = stbi_load(path.c_str(), &w, &h, &orig_format, req_format);
int w;
int h;
int orig_format;
unsigned char *data = stbi_load_from_memory(bytes.data(), static_cast<int>(bytes.size()), &w, &h, &orig_format, req_format);
if (data == nullptr) {
SDL_Log("Loading image failed: %s", stbi_failure_reason());
exit(1);
} else if (verbose) {
std::cout << "Image loaded: " << filename.c_str() << std::endl;
std::cout << "Image loaded: " << filename.c_str() << '\n';
}
unload();
SDL_Texture *newTexture = createTextureFromPixels(renderer, data, w, h, &this->width, &this->height);
if (newTexture == nullptr && verbose) {
std::cout << "Unable to load image " << path.c_str() << std::endl;
std::cout << "Unable to load image " << path.c_str() << '\n';
}
stbi_image_free(data);
@@ -87,8 +96,10 @@ bool Texture::loadFromFile(const std::string &path, SDL_Renderer *renderer, bool
}
// Carga una imagen desde bytes en memoria
bool Texture::loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *renderer, bool verbose) {
int w, h, orig_format;
auto Texture::loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *renderer, bool verbose) -> bool {
int w;
int h;
int orig_format;
unsigned char *pixels = stbi_load_from_memory(data, (int)size, &w, &h, &orig_format, STBI_rgb_alpha);
if (pixels == nullptr) {
SDL_Log("Loading image from memory failed: %s", stbi_failure_reason());
@@ -98,7 +109,7 @@ bool Texture::loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *ren
unload();
SDL_Texture *newTexture = createTextureFromPixels(renderer, pixels, w, h, &this->width, &this->height);
if (newTexture == nullptr && verbose) {
std::cout << "Unable to create texture from memory" << std::endl;
std::cout << "Unable to create texture from memory" << '\n';
}
stbi_image_free(pixels);
@@ -107,11 +118,11 @@ bool Texture::loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *ren
}
// Crea una textura en blanco
bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) {
auto Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) -> bool {
// Crea una textura sin inicializar
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height);
if (texture == nullptr) {
std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << std::endl;
std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << '\n';
} else {
this->width = width;
this->height = height;
@@ -165,7 +176,7 @@ void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float
SDL_FRect srcRect;
SDL_FRect *srcRectPtr = nullptr;
if (clip != nullptr) {
srcRect = {(float)clip->x, (float)clip->y, (float)clip->w, (float)clip->h};
srcRect = {.x = (float)clip->x, .y = (float)clip->y, .w = (float)clip->w, .h = (float)clip->h};
srcRectPtr = &srcRect;
}
@@ -173,7 +184,7 @@ void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float
SDL_FPoint fCenter;
SDL_FPoint *fCenterPtr = nullptr;
if (center != nullptr) {
fCenter = {(float)center->x, (float)center->y};
fCenter = {.x = (float)center->x, .y = (float)center->y};
fCenterPtr = &fCenter;
}
@@ -187,21 +198,21 @@ void Texture::setAsRenderTarget(SDL_Renderer *renderer) {
}
// Obtiene el ancho de la imagen
int Texture::getWidth() {
auto Texture::getWidth() const -> int {
return width;
}
// Obtiene el alto de la imagen
int Texture::getHeight() {
auto Texture::getHeight() const -> int {
return height;
}
// Recarga la textura
bool Texture::reLoad() {
auto Texture::reLoad() -> bool {
return loadFromFile(path, renderer);
}
// Obtiene la textura
SDL_Texture *Texture::getSDLTexture() {
auto Texture::getSDLTexture() -> SDL_Texture * {
return texture;
}