diff --git a/CMakeLists.txt b/CMakeLists.txt index e3e31ae..b4761c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,6 @@ set(APP_SOURCES source/core/rendering/surface_moving_sprite.cpp source/core/rendering/surface_sprite.cpp source/core/rendering/text.cpp - source/core/rendering/texture.cpp # Core - Locale source/core/locale/locale.cpp diff --git a/source/core/rendering/texture.cpp b/source/core/rendering/texture.cpp deleted file mode 100644 index dfaebe2..0000000 --- a/source/core/rendering/texture.cpp +++ /dev/null @@ -1,163 +0,0 @@ - -#include "core/rendering/texture.hpp" - -#include - -#include // Para basic_ostream, operator<<, endl, cout -#include // Para runtime_error -#include // Para char_traits, operator<<, string, opera... -#include -#include // Para vector - -#include "utils/utils.hpp" // Para getFileName, Color, printWithDots - -#define STB_IMAGE_IMPLEMENTATION -#include "external/stb_image.h" // para stbi_failure_reason, stbi_image_free - -// Constructor -Texture::Texture(SDL_Renderer* renderer, std::string path) - : renderer_(renderer), - path_(std::move(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); - - // .png - if (EXTENSION == "png") { - loadFromFile(path_); - } - } -} - -// Destructor -Texture::~Texture() { - unloadTexture(); - palettes_.clear(); -} - -// Carga una imagen desde un fichero -auto Texture::loadFromFile(const std::string& file_path) -> bool { - if (file_path.empty()) { - return false; - } - - int req_format = STBI_rgb_alpha; - int width; - int height; - int orig_format; - unsigned char* data = stbi_load(file_path.c_str(), &width, &height, &orig_format, req_format); - if (data == nullptr) { - std::cerr << "Error: Fichero no encontrado " << getFileName(file_path) << '\n'; - throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path)); - } - printWithDots("Image : ", getFileName(file_path), "[ LOADED ]"); - - int pitch; - SDL_PixelFormat pixel_format; - // STBI_rgb_alpha (RGBA) - pitch = 4 * width; - pixel_format = SDL_PIXELFORMAT_RGBA32; - - // Limpia - unloadTexture(); - - // La textura final - SDL_Texture* new_texture = nullptr; - - // Carga la imagen desde una ruta específica - auto* loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, static_cast(data), pitch); - if (loaded_surface == nullptr) { - std::cout << "Unable to load image " << file_path << '\n'; - } else { - // Crea la textura desde los pixels de la surface - new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface); - if (new_texture == nullptr) { - std::cout << "Unable to create texture from " << file_path << "! SDL Error: " << SDL_GetError() << '\n'; - } else { - // Obtiene las dimensiones de la imagen - width_ = loaded_surface->w; - height_ = loaded_surface->h; - } - - // Elimina la textura cargada - SDL_DestroySurface(loaded_surface); - } - - // Return success - stbi_image_free(data); - texture_ = new_texture; - return texture_ != nullptr; -} - -// Crea una textura en blanco -auto Texture::createBlank(int width, int height, SDL_PixelFormat format, SDL_TextureAccess access) -> bool { - // Crea una textura sin inicializar - texture_ = SDL_CreateTexture(renderer_, format, access, width, height); - if (texture_ == nullptr) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create blank texture! SDL Error: %s", SDL_GetError()); - } else { - width_ = width; - height_ = height; - } - - return texture_ != nullptr; -} - -// Libera la memoria de la textura -void Texture::unloadTexture() { - // Libera la textura - if (texture_ != nullptr) { - SDL_DestroyTexture(texture_); - texture_ = nullptr; - width_ = 0; - height_ = 0; - } -} - -// Establece el color para la modulacion -void Texture::setColor(Uint8 red, Uint8 green, Uint8 blue) { SDL_SetTextureColorMod(texture_, red, green, blue); } -void Texture::setColor(Color color) { SDL_SetTextureColorMod(texture_, color.r, color.g, color.b); } - -// Establece el blending -void Texture::setBlendMode(SDL_BlendMode blending) { SDL_SetTextureBlendMode(texture_, blending); } - -// Establece el alpha para la modulación -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 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 = x, .y = y, .w = width_, .h = height_}; - - // Obtiene las dimesiones del clip de renderizado - if (clip != nullptr) { - render_quad.w = clip->w; - render_quad.h = clip->h; - } - - // Calcula el zoom y las coordenadas - 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 * 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); - } - - // Renderiza a pantalla - SDL_RenderTextureRotated(renderer_, texture_, clip, &render_quad, angle, center, flip); -} - -// Establece la textura como objetivo de renderizado -void Texture::setAsRenderTarget(SDL_Renderer* renderer) { SDL_SetRenderTarget(renderer, texture_); } - -// Recarga la textura -auto Texture::reLoad() -> bool { return loadFromFile(path_); } - -// Obtiene la textura -auto Texture::getSDLTexture() -> SDL_Texture* { return texture_; } - -// Obtiene el renderizador -auto Texture::getRenderer() -> SDL_Renderer* { return renderer_; } \ No newline at end of file diff --git a/source/core/rendering/texture.hpp b/source/core/rendering/texture.hpp deleted file mode 100644 index 81c6434..0000000 --- a/source/core/rendering/texture.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include - -#include // Para string -#include // Para vector -struct Color; // lines 11-11 - -class Texture { - public: - explicit Texture(SDL_Renderer* renderer, std::string path = std::string()); // Constructor - ~Texture(); // Destructor - - auto loadFromFile(const std::string& path) -> bool; // Carga una imagen desde un fichero - auto createBlank(int width, int height, SDL_PixelFormat format = SDL_PIXELFORMAT_RGBA8888, SDL_TextureAccess access = SDL_TEXTUREACCESS_STREAMING) -> bool; // Crea una textura en blanco - auto reLoad() -> bool; // Recarga la textura - - void setColor(Uint8 red, Uint8 green, Uint8 blue); // Establece el color para la modulacion - void setColor(Color color); // Establece el color para la modulacion - void setBlendMode(SDL_BlendMode blending); // Establece el blending - void setAlpha(Uint8 alpha); // Establece el alpha para la modulación - void setAsRenderTarget(SDL_Renderer* renderer); // Establece la textura como objetivo de renderizado - - void render(float x, float y, SDL_FRect* clip = nullptr, float zoom_w = 1, float zoom_h = 1, double angle = 0.0, SDL_FPoint* center = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE); // Renderiza la textura en un punto específico - - [[nodiscard]] auto getWidth() const -> int { return width_; } // Obtiene el ancho de la imagen - [[nodiscard]] auto getHeight() const -> int { return height_; } // Obtiene el alto de la imagen - auto getSDLTexture() -> SDL_Texture*; // Obtiene la textura - auto getRenderer() -> SDL_Renderer*; // Obtiene el renderizador - - private: - void unloadTexture(); // Libera la memoria de la textura - - // Objetos y punteros - SDL_Renderer* renderer_; // Renderizador donde dibujar la textura - SDL_Texture* texture_ = nullptr; // La textura - - // Variables - std::string path_; // Ruta de la imagen de la textura - float width_ = 0.0F; // Ancho de la imagen - float height_ = 0.0F; // Alto de la imagen - std::vector> palettes_; // Vector con las diferentes paletas -}; \ No newline at end of file