clang-format
This commit is contained in:
@@ -1,237 +1,201 @@
|
||||
|
||||
#include "texture.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <stdlib.h> // for exit
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, endl
|
||||
#include <stdlib.h> // for exit
|
||||
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, endl
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h" // for stbi_failure_reason, stbi_image_free
|
||||
#include "stb_image.h" // for stbi_failure_reason, stbi_image_free
|
||||
|
||||
SDL_ScaleMode Texture::currentScaleMode = SDL_SCALEMODE_NEAREST;
|
||||
|
||||
void Texture::setGlobalScaleMode(SDL_ScaleMode mode)
|
||||
{
|
||||
currentScaleMode = mode;
|
||||
void Texture::setGlobalScaleMode(SDL_ScaleMode mode) {
|
||||
currentScaleMode = mode;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose)
|
||||
{
|
||||
// Copia punteros
|
||||
this->renderer = renderer;
|
||||
this->path = path;
|
||||
Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose) {
|
||||
// Copia punteros
|
||||
this->renderer = renderer;
|
||||
this->path = path;
|
||||
|
||||
// Inicializa
|
||||
texture = nullptr;
|
||||
width = 0;
|
||||
height = 0;
|
||||
// Inicializa
|
||||
texture = nullptr;
|
||||
width = 0;
|
||||
height = 0;
|
||||
|
||||
// Carga el fichero en la textura
|
||||
if (path != "")
|
||||
{
|
||||
loadFromFile(path, renderer, verbose);
|
||||
}
|
||||
// Carga el fichero en la textura
|
||||
if (path != "") {
|
||||
loadFromFile(path, renderer, verbose);
|
||||
}
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Texture::~Texture()
|
||||
{
|
||||
// Libera memoria
|
||||
unload();
|
||||
Texture::~Texture() {
|
||||
// Libera memoria
|
||||
unload();
|
||||
}
|
||||
|
||||
// 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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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();
|
||||
// Limpia
|
||||
unload();
|
||||
|
||||
// La textura final
|
||||
SDL_Texture *newTexture = nullptr;
|
||||
// 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;
|
||||
// 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);
|
||||
}
|
||||
// Aplica el modo de escalado
|
||||
SDL_SetTextureScaleMode(newTexture, currentScaleMode);
|
||||
}
|
||||
|
||||
// Elimina la textura cargada
|
||||
SDL_DestroySurface(loadedSurface);
|
||||
}
|
||||
// Elimina la textura cargada
|
||||
SDL_DestroySurface(loadedSurface);
|
||||
}
|
||||
|
||||
// Return success
|
||||
stbi_image_free(data);
|
||||
texture = newTexture;
|
||||
return texture != nullptr;
|
||||
// Return success
|
||||
stbi_image_free(data);
|
||||
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
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
SDL_SetTextureScaleMode(texture, currentScaleMode);
|
||||
}
|
||||
bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) {
|
||||
// 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;
|
||||
} else {
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
SDL_SetTextureScaleMode(texture, currentScaleMode);
|
||||
}
|
||||
|
||||
return texture != nullptr;
|
||||
return texture != nullptr;
|
||||
}
|
||||
|
||||
// Libera la memoria de la textura
|
||||
void Texture::unload()
|
||||
{
|
||||
// Libera la textura si existe
|
||||
if (texture != nullptr)
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
texture = nullptr;
|
||||
width = 0;
|
||||
height = 0;
|
||||
}
|
||||
void Texture::unload() {
|
||||
// Libera la textura si existe
|
||||
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(Uint8 red, Uint8 green, Uint8 blue) {
|
||||
SDL_SetTextureColorMod(texture, red, green, blue);
|
||||
}
|
||||
|
||||
// Establece el blending
|
||||
void Texture::setBlendMode(SDL_BlendMode blending)
|
||||
{
|
||||
SDL_SetTextureBlendMode(texture, 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);
|
||||
void Texture::setAlpha(Uint8 alpha) {
|
||||
SDL_SetTextureAlphaMod(texture, alpha);
|
||||
}
|
||||
|
||||
// Renderiza la textura en un punto específico
|
||||
void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_FlipMode flip)
|
||||
{
|
||||
// Establece el destino de renderizado en la pantalla
|
||||
SDL_FRect renderQuad = {(float)x, (float)y, (float)width, (float)height};
|
||||
void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_FlipMode flip) {
|
||||
// Establece el destino de renderizado en la pantalla
|
||||
SDL_FRect renderQuad = {(float)x, (float)y, (float)width, (float)height};
|
||||
|
||||
// Obtiene las dimesiones del clip de renderizado
|
||||
if (clip != nullptr)
|
||||
{
|
||||
renderQuad.w = (float)clip->w;
|
||||
renderQuad.h = (float)clip->h;
|
||||
}
|
||||
// Obtiene las dimesiones del clip de renderizado
|
||||
if (clip != nullptr) {
|
||||
renderQuad.w = (float)clip->w;
|
||||
renderQuad.h = (float)clip->h;
|
||||
}
|
||||
|
||||
renderQuad.w = renderQuad.w * zoomW;
|
||||
renderQuad.h = renderQuad.h * zoomH;
|
||||
renderQuad.w = renderQuad.w * zoomW;
|
||||
renderQuad.h = renderQuad.h * zoomH;
|
||||
|
||||
// Convierte el clip a SDL_FRect
|
||||
SDL_FRect srcRect;
|
||||
SDL_FRect *srcRectPtr = nullptr;
|
||||
if (clip != nullptr)
|
||||
{
|
||||
srcRect = {(float)clip->x, (float)clip->y, (float)clip->w, (float)clip->h};
|
||||
srcRectPtr = &srcRect;
|
||||
}
|
||||
// Convierte el clip a SDL_FRect
|
||||
SDL_FRect srcRect;
|
||||
SDL_FRect *srcRectPtr = nullptr;
|
||||
if (clip != nullptr) {
|
||||
srcRect = {(float)clip->x, (float)clip->y, (float)clip->w, (float)clip->h};
|
||||
srcRectPtr = &srcRect;
|
||||
}
|
||||
|
||||
// Convierte el centro a SDL_FPoint
|
||||
SDL_FPoint fCenter;
|
||||
SDL_FPoint *fCenterPtr = nullptr;
|
||||
if (center != nullptr)
|
||||
{
|
||||
fCenter = {(float)center->x, (float)center->y};
|
||||
fCenterPtr = &fCenter;
|
||||
}
|
||||
// Convierte el centro a SDL_FPoint
|
||||
SDL_FPoint fCenter;
|
||||
SDL_FPoint *fCenterPtr = nullptr;
|
||||
if (center != nullptr) {
|
||||
fCenter = {(float)center->x, (float)center->y};
|
||||
fCenterPtr = &fCenter;
|
||||
}
|
||||
|
||||
// Renderiza a pantalla
|
||||
SDL_RenderTextureRotated(renderer, texture, srcRectPtr, &renderQuad, angle, fCenterPtr, flip);
|
||||
// Renderiza a pantalla
|
||||
SDL_RenderTextureRotated(renderer, texture, srcRectPtr, &renderQuad, angle, fCenterPtr, flip);
|
||||
}
|
||||
|
||||
// Establece la textura como objetivo de renderizado
|
||||
void Texture::setAsRenderTarget(SDL_Renderer *renderer)
|
||||
{
|
||||
SDL_SetRenderTarget(renderer, texture);
|
||||
void Texture::setAsRenderTarget(SDL_Renderer *renderer) {
|
||||
SDL_SetRenderTarget(renderer, texture);
|
||||
}
|
||||
|
||||
// Obtiene el ancho de la imagen
|
||||
int Texture::getWidth()
|
||||
{
|
||||
return width;
|
||||
int Texture::getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
// Obtiene el alto de la imagen
|
||||
int Texture::getHeight()
|
||||
{
|
||||
return height;
|
||||
int Texture::getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
// Recarga la textura
|
||||
bool Texture::reLoad()
|
||||
{
|
||||
return loadFromFile(path, renderer);
|
||||
bool Texture::reLoad() {
|
||||
return loadFromFile(path, renderer);
|
||||
}
|
||||
|
||||
// Obtiene la textura
|
||||
SDL_Texture *Texture::getSDLTexture()
|
||||
{
|
||||
return texture;
|
||||
SDL_Texture *Texture::getSDLTexture() {
|
||||
return texture;
|
||||
}
|
||||
Reference in New Issue
Block a user