Actualizadas librerias comunes a las ultimas versiones
This commit is contained in:
@@ -1,22 +1,35 @@
|
||||
#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 = 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,14 +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
|
||||
//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)
|
||||
{
|
||||
@@ -58,10 +70,7 @@ bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
|
||||
}
|
||||
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)
|
||||
{
|
||||
@@ -69,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
|
||||
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 != 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;
|
||||
this->width = width;
|
||||
this->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;
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user