la classe Texture ja suporta carregar gifs i paletes
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include "texture.h"
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
#include "gif.c"
|
||||
#include <iostream>
|
||||
|
||||
// Constructor
|
||||
@@ -12,14 +13,36 @@ Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose)
|
||||
this->path = path;
|
||||
|
||||
// Inicializa
|
||||
surface = nullptr;
|
||||
texture = nullptr;
|
||||
width = 0;
|
||||
height = 0;
|
||||
for (int i = 0; i < 256; ++i)
|
||||
{
|
||||
paleta[i] = 0;
|
||||
}
|
||||
|
||||
// Carga el fichero en la textura
|
||||
if (path != "")
|
||||
{
|
||||
loadFromFile(path, renderer, verbose);
|
||||
// Obtiene la extensión
|
||||
const std::string extension = path.substr(path.find_last_of(".") + 1);
|
||||
|
||||
// .png
|
||||
if (extension == "png")
|
||||
{
|
||||
loadFromFile(path, renderer, verbose);
|
||||
}
|
||||
|
||||
// .gif
|
||||
else if (extension == "gif")
|
||||
{
|
||||
surface = loadSurface(path.c_str());
|
||||
loadPal(path.c_str());
|
||||
setPal(0, 0x00000000);
|
||||
createBlank(renderer, width, height);
|
||||
flipSurface();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,10 +132,10 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer, bool verbos
|
||||
}
|
||||
|
||||
// Crea una textura en blanco
|
||||
bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access)
|
||||
bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_PixelFormatEnum format, SDL_TextureAccess access)
|
||||
{
|
||||
// Crea una textura sin inicializar
|
||||
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height);
|
||||
texture = SDL_CreateTexture(renderer, format, access, width, height);
|
||||
if (texture == nullptr)
|
||||
{
|
||||
std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << std::endl;
|
||||
@@ -129,7 +152,7 @@ bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_Tex
|
||||
// Libera la memoria de la textura
|
||||
void Texture::unload()
|
||||
{
|
||||
// Libera la textura si existe
|
||||
// Libera la textura
|
||||
if (texture != nullptr)
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
@@ -137,6 +160,13 @@ void Texture::unload()
|
||||
width = 0;
|
||||
height = 0;
|
||||
}
|
||||
|
||||
// Libera la surface
|
||||
if (surface != nullptr)
|
||||
{
|
||||
deleteSurface(surface);
|
||||
surface = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el color para la modulacion
|
||||
@@ -205,4 +235,114 @@ bool Texture::reLoad()
|
||||
SDL_Texture *Texture::getSDLTexture()
|
||||
{
|
||||
return texture;
|
||||
}
|
||||
|
||||
// Crea una nueva surface
|
||||
Surface Texture::newSurface(int w, int h)
|
||||
{
|
||||
Surface surf = (Surface)malloc(sizeof(surface_s));
|
||||
surf->w = w;
|
||||
surf->h = h;
|
||||
surf->data = (Uint8 *)malloc(w * h);
|
||||
return surf;
|
||||
}
|
||||
|
||||
// Elimina una surface
|
||||
void Texture::deleteSurface(Surface surface)
|
||||
{
|
||||
if (surface == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (surface->data != nullptr)
|
||||
{
|
||||
free(surface->data);
|
||||
}
|
||||
|
||||
free(surface);
|
||||
}
|
||||
|
||||
// Crea una surface desde un fichero .gif
|
||||
Surface Texture::loadSurface(const char *filename)
|
||||
{
|
||||
FILE *f = fopen(filename, "rb");
|
||||
if (!f)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
long size = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
Uint8 *buffer = (Uint8 *)malloc(size);
|
||||
fread(buffer, size, 1, f);
|
||||
fclose(f);
|
||||
|
||||
Uint16 w, h;
|
||||
Uint8 *pixels = LoadGif(buffer, &w, &h);
|
||||
if (pixels == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Surface surface = (Surface)malloc(sizeof(surface_s));
|
||||
surface->w = w;
|
||||
surface->h = h;
|
||||
surface->data = pixels;
|
||||
free(buffer);
|
||||
|
||||
this->width = w;
|
||||
this->height = h;
|
||||
|
||||
return surface;
|
||||
}
|
||||
|
||||
// Vuelca la surface en la textura
|
||||
void Texture::flipSurface()
|
||||
{
|
||||
Uint32 *pixels;
|
||||
int pitch;
|
||||
SDL_LockTexture(texture, nullptr, (void **)&pixels, &pitch);
|
||||
for (int i = 0; i < width * height; ++i)
|
||||
{
|
||||
pixels[i] = paleta[surface->data[i]];
|
||||
}
|
||||
SDL_UnlockTexture(texture);
|
||||
}
|
||||
|
||||
// Establece un color de la paleta
|
||||
void Texture::setPal(int index, Uint32 color)
|
||||
{
|
||||
paleta[index] = color;
|
||||
}
|
||||
|
||||
// Carga una paleta desde un fichero
|
||||
void Texture::loadPal(const char *filename)
|
||||
{
|
||||
FILE *f = fopen(filename, "rb");
|
||||
if (!f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
long size = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
Uint8 *buffer = (Uint8 *)malloc(size);
|
||||
fread(buffer, size, 1, f);
|
||||
fclose(f);
|
||||
|
||||
Uint32 *pal = LoadPalette(buffer);
|
||||
if (pal == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
|
||||
for (int i = 0; i < 256; ++i)
|
||||
{
|
||||
paleta[i] = (pal[i] << 8) + 255;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user