Modificada la clase LTexture

This commit is contained in:
2022-09-01 19:30:56 +02:00
parent 114da1e381
commit 8f2e04b120
2 changed files with 94 additions and 75 deletions

View File

@@ -3,20 +3,33 @@
#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 = NULL;
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;
@@ -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,13 +56,13 @@ bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
pixel_format = SDL_PIXELFORMAT_RGBA32;
}
// Get rid of preexisting texture
// Limpia
unload();
// The final texture
// La textura final
SDL_Texture *newTexture = NULL;
// Load image at specified path
// Carga la imagen desde una ruta específica
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
if (loadedSurface == NULL)
{
@@ -57,7 +70,7 @@ bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
}
else
{
// Create texture from surface pixels
// Crea la textura desde los pixels de la surface
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == NULL)
{
@@ -65,73 +78,76 @@ bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
}
else
{
// Get image dimensions
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
// Obtiene las dimensiones de la imagen
width = loadedSurface->w;
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 != NULL;
}
// 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 == NULL)
{
printf("Unable to create blank texture! SDL Error: %s\n", SDL_GetError());
}
else
{
mWidth = width;
mHeight = height;
width = width;
height = height;
}
return mTexture != NULL;
return texture != NULL;
}
// Libera la memoria de la textura
void LTexture::unload()
{
// Free texture if it exists
if (mTexture != NULL)
// Libera la textura si existe
if (texture != NULL)
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
SDL_DestroyTexture(texture);
texture = NULL;
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
// Obtiene las dimesiones del clip de renderizado
if (clip != NULL)
{
renderQuad.w = clip->w;
@@ -141,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()
{
loadFromFile(path, renderer);
}

View File

@@ -1,4 +1,5 @@
#pragma once
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string>
@@ -6,61 +7,55 @@
#ifndef LTEXTURE_H
#define LTEXTURE_H
// Texture wrapper class
// Clase LTexture
class LTexture
{
public:
// Initializes variables
LTexture();
private:
SDL_Texture *texture; // La textura
SDL_Renderer *renderer; // Renderizador donde dibujar la textura
int width; // Ancho de la imagen
int height; // Alto de la imagen
std::string path; // Ruta de la imagen de la textura
// Deallocates memory
public:
// Constructor
LTexture(SDL_Renderer *renderer, std::string path = "");
// Destructor
~LTexture();
// Loads image at specified path
// Carga una imagen desde un fichero
bool loadFromFile(std::string path, SDL_Renderer *renderer);
// Creates blank texture
// Crea una textura en blanco
bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);
// Deallocates texture
// Libera la memoria de la textura
void unload();
// Set color modulation
// Establece el color para la modulacion
void setColor(Uint8 red, Uint8 green, Uint8 blue);
// Set blending
// Establece el blending
void setBlendMode(SDL_BlendMode blending);
// Set alpha modulation
// Establece el alpha para la modulación
void setAlpha(Uint8 alpha);
// Renders texture at given point
// Renderiza la textura en un punto específico
void render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = NULL, float zoomW = 1, float zoomH = 1, double angle = 0.0, SDL_Point *center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE);
// Set self as render target
// Establece la textura como objetivo de renderizado
void setAsRenderTarget(SDL_Renderer *renderer);
// Gets image dimensions
// Obtiene el ancho de la imagen
int getWidth();
// Obtiene el alto de la imagen
int getHeight();
// Pixel manipulators
bool lockTexture();
bool unlockTexture();
void *getPixels();
void copyPixels(void *pixels);
int getPitch();
Uint32 getPixel32(unsigned int x, unsigned int y);
private:
// The actual hardware texture
SDL_Texture *mTexture;
void *mPixels;
int mPitch;
// Image dimensions
int mWidth;
int mHeight;
// Recarga la textura
bool reLoad();
};
#endif