Actualizado a la última versión de ltexture

This commit is contained in:
2022-09-25 21:11:17 +02:00
parent a74c8d5122
commit 6637ba9f10
7 changed files with 133 additions and 114 deletions

View File

@@ -1,28 +1,41 @@
#include "const.h"
#include "ltexture.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
LTexture::LTexture()
// Constructor
LTexture::LTexture(SDL_Renderer *renderer, std::string path)
{
// Initialize
mTexture = NULL;
mWidth = 0;
mHeight = 0;
// Copia punteros
this->renderer = renderer;
this->path = path;
// Inicializa
texture = nullptr;
width = 0;
height = 0;
// Carga el fichero en la textura
if (path != "")
{
loadFromFile(path, renderer);
}
}
// Destructor
LTexture::~LTexture()
{
// Deallocate
// Libera memoria
unload();
}
// Carga una imagen desde un fichero
bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
{
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 == NULL)
if (data == nullptr)
{
SDL_Log("Loading image failed: %s", stbi_failure_reason());
exit(1);
@@ -33,7 +46,7 @@ bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
if (req_format == STBI_rgb)
{
depth = 24;
pitch = 3 * width; // 3 bytes per pixel * pixels per row
pitch = 3 * width; // 3 bytes por pixel * pixels per linea
pixel_format = SDL_PIXELFORMAT_RGB24;
}
else
@@ -43,100 +56,99 @@ bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
pixel_format = SDL_PIXELFORMAT_RGBA32;
}
// Get rid of preexisting texture
// Limpia
unload();
// The final texture
SDL_Texture *newTexture = NULL;
// La textura final
SDL_Texture *newTexture = nullptr;
// Load image at specified path
//SDL_Surface *loadedSurface = IMG_Load(path.c_str());
// Carga la imagen desde una ruta específica
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!\n", path.c_str());
}
else
{
// Color key image
//SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, COLOR_KEY_R, COLOR_KEY_G, COLOR_KEY_B));
// Create texture from surface pixels
// Crea la textura desde los pixels de la surface
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());
}
else
{
// Get image dimensions
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
// Obtiene las dimensiones de la imagen
this->width = loadedSurface->w;
this->height = loadedSurface->h;
}
// Get rid of old loaded surface
// Elimina la textura cargada
SDL_FreeSurface(loadedSurface);
}
// Return success
mTexture = newTexture;
return mTexture != NULL;
texture = newTexture;
return texture != nullptr;
}
// Crea una textura en blanco
bool LTexture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access)
{
// Create uninitialized texture
mTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height);
if (mTexture == NULL)
// Crea una textura sin inicializar
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height);
if (texture == nullptr)
{
printf("Unable to create blank texture! SDL Error: %s\n", SDL_GetError());
}
else
{
mWidth = width;
mHeight = height;
this->width = width;
this->height = height;
}
return mTexture != NULL;
return texture != nullptr;
}
// Libera la memoria de la textura
void LTexture::unload()
{
// Free texture if it exists
if (mTexture != NULL)
// Libera la textura si existe
if (texture != nullptr)
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
SDL_DestroyTexture(texture);
texture = nullptr;
width = 0;
height = 0;
}
}
// Establece el color para la modulacion
void LTexture::setColor(Uint8 red, Uint8 green, Uint8 blue)
{
// Modulate texture rgb
SDL_SetTextureColorMod(mTexture, red, green, blue);
SDL_SetTextureColorMod(texture, red, green, blue);
}
// Establece el blending
void LTexture::setBlendMode(SDL_BlendMode blending)
{
// Set blending function
SDL_SetTextureBlendMode(mTexture, blending);
SDL_SetTextureBlendMode(texture, blending);
}
// Establece el alpha para la modulación
void LTexture::setAlpha(Uint8 alpha)
{
// Modulate texture alpha
SDL_SetTextureAlphaMod(mTexture, alpha);
SDL_SetTextureAlphaMod(texture, alpha);
}
// Renderiza la textura en un punto específico
void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_RendererFlip flip)
{
// Set rendering space and render to screen
SDL_Rect renderQuad = {x, y, mWidth, mHeight};
// Establece el destini de renderizado en la pantalla
SDL_Rect renderQuad = {x, y, width, height};
// Set clip rendering dimensions
if (clip != NULL)
// Obtiene las dimesiones del clip de renderizado
if (clip != nullptr)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
@@ -145,22 +157,30 @@ void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, floa
renderQuad.w = renderQuad.w * zoomW;
renderQuad.h = renderQuad.h * zoomH;
// Render to screen
SDL_RenderCopyEx(renderer, mTexture, clip, &renderQuad, angle, center, flip);
// Renderiza a pantalla
SDL_RenderCopyEx(renderer, texture, clip, &renderQuad, angle, center, flip);
}
// Establece la textura como objetivo de renderizado
void LTexture::setAsRenderTarget(SDL_Renderer *renderer)
{
// Make self render target
SDL_SetRenderTarget(renderer, mTexture);
SDL_SetRenderTarget(renderer, texture);
}
// Obtiene el ancho de la imagen
int LTexture::getWidth()
{
return mWidth;
return width;
}
// Obtiene el alto de la imagen
int LTexture::getHeight()
{
return mHeight;
return height;
}
// Recarga la textura
bool LTexture::reLoad()
{
return loadFromFile(path, renderer);
}