Eliminada dependencia de SDL2_image

This commit is contained in:
2024-08-22 20:51:40 +02:00
parent da0aa04f2d
commit 17b1e6a491
2 changed files with 7946 additions and 21 deletions

7897
source/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,5 @@
#include <SDL2/SDL_image.h> #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <stdio.h> #include <stdio.h>
#include <string> #include <string>
#include "texture.h" #include "texture.h"
@@ -27,43 +28,70 @@ Texture::~Texture()
bool Texture::loadFromFile(std::string path) bool Texture::loadFromFile(std::string path)
{ {
// Get rid of preexisting texture 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);
if (data == nullptr)
{
SDL_Log("Loading image failed: %s", stbi_failure_reason());
exit(1);
}
else
{
std::cout << "Image loaded: " << filename.c_str() << std::endl;
}
int depth, pitch;
Uint32 pixel_format;
if (req_format == STBI_rgb)
{
depth = 24;
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
pixel_format = SDL_PIXELFORMAT_RGB24;
}
else
{ // STBI_rgb_alpha (RGBA)
depth = 32;
pitch = 4 * width;
pixel_format = SDL_PIXELFORMAT_RGBA32;
}
// Limpia
free(); free();
// The final texture // La textura final
SDL_Texture *newTexture = NULL; SDL_Texture *newTexture = nullptr;
// Load image at specified path // Carga la imagen desde una ruta específica
SDL_Surface *loadedSurface = IMG_Load(path.c_str()); SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
if (loadedSurface == NULL) if (loadedSurface == nullptr)
{ {
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError()); std::cout << "Unable to load image " << path.c_str() << std::endl;
} }
else else
{ {
// Color key image // Crea la textura desde los pixels de la surface
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0, 0xFF, 0xFF));
// Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface); newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == NULL) if (newTexture == nullptr)
{ {
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); std::cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl;
} }
else else
{ {
// Get image dimensions // Obtiene las dimensiones de la imagen
width = loadedSurface->w; this->width = loadedSurface->w;
height = loadedSurface->h; this->height = loadedSurface->h;
} }
// Get rid of old loaded surface // Elimina la textura cargada
SDL_FreeSurface(loadedSurface); SDL_FreeSurface(loadedSurface);
} }
// Return success // Return success
stbi_image_free(data);
texture = newTexture; texture = newTexture;
return texture != NULL; return texture != nullptr;
} }
void Texture::free() void Texture::free()