resource.pack

This commit is contained in:
2026-04-15 23:26:43 +02:00
parent c3534ace9c
commit 0faa605ad9
35 changed files with 1537 additions and 1851 deletions

View File

@@ -31,75 +31,86 @@ Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose) {
}
}
// Constructor desde bytes
Texture::Texture(SDL_Renderer *renderer, const std::vector<uint8_t> &bytes, bool verbose) {
this->renderer = renderer;
this->path = "";
texture = nullptr;
width = 0;
height = 0;
if (!bytes.empty()) {
loadFromMemory(bytes.data(), bytes.size(), renderer, verbose);
}
}
// Destructor
Texture::~Texture() {
// Libera memoria
unload();
}
// 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) {
const int pitch = 4 * w;
SDL_Surface *loadedSurface = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGBA32, (void *)data, pitch);
if (loadedSurface == nullptr) {
return nullptr;
}
SDL_Texture *newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture != nullptr) {
*out_w = loadedSurface->w;
*out_h = loadedSurface->h;
SDL_SetTextureScaleMode(newTexture, Texture::currentScaleMode);
}
SDL_DestroySurface(loadedSurface);
return newTexture;
}
// Carga una imagen desde un fichero
bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose) {
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
int req_format = STBI_rgb_alpha;
int width, height, orig_format;
unsigned char *data = stbi_load(path.c_str(), &width, &height, &orig_format, req_format);
int w, h, orig_format;
unsigned char *data = stbi_load(path.c_str(), &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;
}
} else if (verbose) {
std::cout << "Image loaded: " << filename.c_str() << std::endl;
}
int pitch;
SDL_PixelFormat pixel_format;
if (req_format == STBI_rgb) {
pitch = 3 * width; // 3 bytes por pixel * pixels per linea
pixel_format = SDL_PIXELFORMAT_RGB24;
} else { // STBI_rgb_alpha (RGBA)
pitch = 4 * width;
pixel_format = SDL_PIXELFORMAT_RGBA32;
}
// Limpia
unload();
// La textura final
SDL_Texture *newTexture = nullptr;
// Carga la imagen desde una ruta específica
SDL_Surface *loadedSurface = SDL_CreateSurfaceFrom(width, height, pixel_format, (void *)data, pitch);
if (loadedSurface == nullptr) {
if (verbose) {
std::cout << "Unable to load image " << path.c_str() << std::endl;
}
} else {
// Crea la textura desde los pixels de la surface
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == nullptr) {
if (verbose) {
std::cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl;
}
} else {
// Obtiene las dimensiones de la imagen
this->width = loadedSurface->w;
this->height = loadedSurface->h;
// Aplica el modo de escalado
SDL_SetTextureScaleMode(newTexture, currentScaleMode);
}
// Elimina la textura cargada
SDL_DestroySurface(loadedSurface);
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;
}
// Return success
stbi_image_free(data);
texture = newTexture;
return texture != nullptr;
}
// 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;
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());
return false;
}
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;
}
stbi_image_free(pixels);
texture = newTexture;
return texture != nullptr;
}
// Crea una textura en blanco
bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) {
// Crea una textura sin inicializar