- [WIP] Modificaciones a la clase Textura

This commit is contained in:
2022-12-02 12:30:25 +01:00
parent 9bb9d26417
commit a61c2e8d46
2 changed files with 30 additions and 78 deletions

View File

@@ -3,23 +3,26 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
#include <iostream> #include <iostream>
#include "systempalette.h"
// Constructor // Constructor
Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose) Texture::Texture(SDL_Renderer *renderer, SDL_Texture *texture, std::string path, bool verbose)
{ {
// Copia punteros // Copia punteros
this->renderer = renderer; this->renderer = renderer;
this->texture = texture;
this->path = path; this->path = path;
// Inicializa // Inicializa
texture = nullptr; this->pixels = nullptr;
width = 0; this->width = 0;
height = 0; this->height = 0;
this->color = 0xffffffff;
// Carga el fichero en la textura // Carga el fichero en la textura
if (path != "") if (path != "")
{ {
loadFromFile(path, renderer, verbose); loadFromFile(path, verbose);
} }
} }
@@ -31,7 +34,7 @@ Texture::~Texture()
} }
// Carga una imagen desde un fichero // Carga una imagen desde un fichero
bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose) bool Texture::loadFromFile(std::string path, bool verbose)
{ {
const std::string filename = path.substr(path.find_last_of("\\/") + 1); const std::string filename = path.substr(path.find_last_of("\\/") + 1);
int req_format = STBI_rgb_alpha; int req_format = STBI_rgb_alpha;
@@ -50,90 +53,37 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer, bool verbos
} }
} }
int depth, pitch;
Uint32 pixel_format;
if (req_format == STBI_rgb)
{
depth = 24;
pitch = 3 * width; // 3 bytes por pixel * pixels per linea
pixel_format = SDL_PIXELFORMAT_RGB24;
}
else
{ // STBI_rgb_alpha (RGBA)
depth = 32;
pitch = 4 * width;
pixel_format = SDL_PIXELFORMAT_RGBA32;
}
// Limpia // Limpia
unload(); unload();
// La textura final const int size = width*height;
SDL_Texture *newTexture = nullptr; this->pixels = new uint8_t[size];
for (int i = 0; i < size; ++i) this->pixels[i] = SystemPalette::getEntry(data[i]);
// Carga la imagen desde una ruta específica
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
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;
}
// Elimina la textura cargada
SDL_FreeSurface(loadedSurface);
}
// Return success // Return success
stbi_image_free(data); stbi_image_free(data);
texture = newTexture; return this->pixels != nullptr;
return texture != nullptr;
} }
// Crea una textura en blanco // Crea una textura en blanco
bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) bool Texture::createBlank(int width, int height, SDL_TextureAccess access)
{ {
// Crea una textura sin inicializar // Crea una textura sin inicializar
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height); this->pixels = new uint8_t[width*height];
if (texture == nullptr) this->width = width;
{ this->height = height;
std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << std::endl;
}
else
{
this->width = width;
this->height = height;
}
return texture != nullptr; return this->pixels != nullptr;
} }
// Libera la memoria de la textura // Libera la memoria de la textura
void Texture::unload() void Texture::unload()
{ {
// Libera la textura si existe // Libera la textura si existe
if (texture != nullptr) if (pixels != nullptr)
{ {
SDL_DestroyTexture(texture); delete[] pixels;
texture = nullptr; pixels = nullptr;
width = 0; width = 0;
height = 0; height = 0;
} }
@@ -142,19 +92,19 @@ void Texture::unload()
// Establece el color para la modulacion // Establece el color para la modulacion
void Texture::setColor(Uint8 red, Uint8 green, Uint8 blue) void Texture::setColor(Uint8 red, Uint8 green, Uint8 blue)
{ {
SDL_SetTextureColorMod(texture, red, green, blue); this->color = (red << 24) + (green << 16) + (blue << 8) + 255;
} }
// Establece el blending // Establece el blending
void Texture::setBlendMode(SDL_BlendMode blending) void Texture::setBlendMode(SDL_BlendMode blending)
{ {
SDL_SetTextureBlendMode(texture, blending); //SDL_SetTextureBlendMode(texture, blending);
} }
// Establece el alpha para la modulación // Establece el alpha para la modulación
void Texture::setAlpha(Uint8 alpha) void Texture::setAlpha(Uint8 alpha)
{ {
SDL_SetTextureAlphaMod(texture, alpha); //SDL_SetTextureAlphaMod(texture, alpha);
} }
// Renderiza la textura en un punto específico // Renderiza la textura en un punto específico

View File

@@ -11,26 +11,28 @@ class Texture
{ {
private: private:
// Objetos y punteros // Objetos y punteros
SDL_Texture *texture; // La textura uint8_t *pixels; // Los pixels de esta textura
SDL_Texture *texture; // La textura SDL sobre la que pintaremos (debe haber sido creada con SDL_TEXTUREACCESS_STREAMING)
SDL_Renderer *renderer; // Renderizador donde dibujar la textura SDL_Renderer *renderer; // Renderizador donde dibujar la textura
// Variables // Variables
int width; // Ancho de la imagen int width; // Ancho de la imagen
int height; // Alto de la imagen int height; // Alto de la imagen
std::string path; // Ruta de la imagen de la textura std::string path; // Ruta de la imagen de la textura
uint32_t color; // Color para el pintado de 1 bit (sprites y tal)
public: public:
// Constructor // Constructor
Texture(SDL_Renderer *renderer, std::string path = "", bool verbose = false); Texture(SDL_Renderer *renderer, SDL_Texture *texture, std::string path = "", bool verbose = false);
// Destructor // Destructor
~Texture(); ~Texture();
// Carga una imagen desde un fichero // Carga una imagen desde un fichero
bool loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose = false); bool loadFromFile(std::string path, bool verbose = false);
// Crea una textura en blanco // Crea una textura en blanco
bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING); bool createBlank(int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);
// Libera la memoria de la textura // Libera la memoria de la textura
void unload(); void unload();