This commit is contained in:
2025-10-27 11:53:12 +01:00
parent 231dcd4b3b
commit 5d8811026d
69 changed files with 899 additions and 888 deletions

View File

@@ -20,10 +20,10 @@ Texture::Texture(SDL_Renderer* renderer, const std::string& path)
// Carga el fichero en la textura
if (!path_.empty()) {
// Obtiene la extensión
const std::string extension = path_.substr(path_.find_last_of(".") + 1);
const std::string EXTENSION = path_.substr(path_.find_last_of(".") + 1);
// .png
if (extension == "png") {
if (EXTENSION == "png") {
loadFromFile(path_);
}
}
@@ -42,14 +42,15 @@ bool Texture::loadFromFile(const std::string& file_path) {
}
int req_format = STBI_rgb_alpha;
int width, height, orig_format;
int width;
int height;
int orig_format;
unsigned char* data = stbi_load(file_path.c_str(), &width, &height, &orig_format, req_format);
if (!data) {
if (data == nullptr) {
std::cerr << "Error: Fichero no encontrado " << getFileName(file_path) << std::endl;
throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path));
} else {
printWithDots("Image : ", getFileName(file_path), "[ LOADED ]");
}
printWithDots("Image : ", getFileName(file_path), "[ LOADED ]");
int pitch;
SDL_PixelFormat pixel_format;
@@ -61,7 +62,7 @@ bool Texture::loadFromFile(const std::string& file_path) {
unloadTexture();
// La textura final
SDL_Texture* newTexture = nullptr;
SDL_Texture* new_texture = nullptr;
// Carga la imagen desde una ruta específica
auto* loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, static_cast<void*>(data), pitch);
@@ -69,8 +70,8 @@ bool Texture::loadFromFile(const std::string& file_path) {
std::cout << "Unable to load image " << file_path << std::endl;
} else {
// Crea la textura desde los pixels de la surface
newTexture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
if (newTexture == nullptr) {
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;
} else {
// Obtiene las dimensiones de la imagen
@@ -84,7 +85,7 @@ bool Texture::loadFromFile(const std::string& file_path) {
// Return success
stbi_image_free(data);
texture_ = newTexture;
texture_ = new_texture;
return texture_ != nullptr;
}
@@ -105,7 +106,7 @@ auto Texture::createBlank(int width, int height, SDL_PixelFormat format, SDL_Tex
// Libera la memoria de la textura
void Texture::unloadTexture() {
// Libera la textura
if (texture_) {
if (texture_ != nullptr) {
SDL_DestroyTexture(texture_);
texture_ = nullptr;
width_ = 0;
@@ -124,7 +125,7 @@ void Texture::setBlendMode(SDL_BlendMode blending) { SDL_SetTextureBlendMode(tex
void Texture::setAlpha(Uint8 alpha) { SDL_SetTextureAlphaMod(texture_, alpha); }
// Renderiza la textura en un punto específico
void Texture::render(float x, float y, SDL_FRect* clip, float zoomW, float zoomH, double angle, SDL_FPoint* center, SDL_FlipMode flip) {
void Texture::render(float x, float y, SDL_FRect* clip, float zoom_w, float zoom_h, double angle, SDL_FPoint* center, SDL_FlipMode flip) {
// Establece el destino de renderizado en la pantalla
SDL_FRect render_quad = {x, y, width_, height_};
@@ -135,11 +136,11 @@ void Texture::render(float x, float y, SDL_FRect* clip, float zoomW, float zoomH
}
// Calcula el zoom y las coordenadas
if (zoomH != 1.0f || zoomW != 1.0f) {
if (zoom_h != 1.0F || zoom_w != 1.0F) {
render_quad.x = render_quad.x + (render_quad.w / 2);
render_quad.y = render_quad.y + (render_quad.h / 2);
render_quad.w = render_quad.w * zoomW;
render_quad.h = render_quad.h * zoomH;
render_quad.w = render_quad.w * zoom_w;
render_quad.h = render_quad.h * zoom_h;
render_quad.x = render_quad.x - (render_quad.w / 2);
render_quad.y = render_quad.y - (render_quad.h / 2);
}
@@ -151,12 +152,6 @@ void Texture::render(float x, float y, SDL_FRect* clip, float zoomW, float zoomH
// Establece la textura como objetivo de renderizado
void Texture::setAsRenderTarget(SDL_Renderer* renderer) { SDL_SetRenderTarget(renderer, texture_); }
// Obtiene el ancho de la imagen
int Texture::getWidth() { return width_; }
// Obtiene el alto de la imagen
int Texture::getHeight() { return height_; }
// Recarga la textura
bool Texture::reLoad() { return loadFromFile(path_); }