migrat a SDL3

This commit is contained in:
2025-03-24 12:48:32 +01:00
parent 111cbb2217
commit 4a2e5c27e2
13 changed files with 366 additions and 309 deletions
+34 -44
View File
@@ -1,24 +1,16 @@
#include <stdio.h>
#include <string>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <stdio.h>
#include <string>
#include "texture.h"
Texture::Texture(SDL_Renderer *renderer)
{
this->renderer = renderer;
texture = nullptr;
width = 0;
height = 0;
}
: renderer_(renderer), texture_(nullptr), width_(0), height_(0) {}
Texture::Texture(SDL_Renderer *renderer, std::string filepath)
Texture::Texture(SDL_Renderer *renderer, std::string file_path)
: renderer_(renderer), texture_(nullptr), width_(0), height_(0)
{
this->renderer = renderer;
texture = nullptr;
width = 0;
height = 0;
loadFromFile(filepath);
loadFromFile(file_path);
}
Texture::~Texture()
@@ -26,12 +18,12 @@ Texture::~Texture()
free();
}
bool Texture::loadFromFile(std::string path)
bool Texture::loadFromFile(std::string file_path)
{
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
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);
unsigned char *data = stbi_load(file_path.c_str(), &width, &height, &orig_format, req_format);
if (data == nullptr)
{
SDL_Log("Loading image failed: %s", stbi_failure_reason());
@@ -39,20 +31,18 @@ bool Texture::loadFromFile(std::string path)
}
else
{
std::cout << "Image loaded: " << filename.c_str() << std::endl;
std::cout << "Image loaded: " << filename.c_str() << std::endl;
}
int depth, pitch;
Uint32 pixel_format;
int pitch;
SDL_PixelFormat pixel_format;
if (req_format == STBI_rgb)
{
depth = 24;
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
pixel_format = SDL_PIXELFORMAT_RGB24;
}
else
{ // STBI_rgb_alpha (RGBA)
depth = 32;
pitch = 4 * width;
pixel_format = SDL_PIXELFORMAT_RGBA32;
}
@@ -61,69 +51,69 @@ bool Texture::loadFromFile(std::string path)
free();
// La textura final
SDL_Texture *newTexture = nullptr;
SDL_Texture *new_texture = nullptr;
// Carga la imagen desde una ruta específica
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
if (loadedSurface == nullptr)
SDL_Surface *loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, (void *)data, pitch);
if (loaded_surface == nullptr)
{
std::cout << "Unable to load image " << path.c_str() << std::endl;
std::cout << "Unable to load image " << file_path << std::endl;
}
else
{
// Crea la textura desde los pixels de la surface
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == nullptr)
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
if (new_texture == nullptr)
{
std::cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl;
std::cout << "Unable to create texture from " << file_path << "! SDL Error: " << SDL_GetError() << std::endl;
}
else
{
// Obtiene las dimensiones de la imagen
this->width = loadedSurface->w;
this->height = loadedSurface->h;
width_ = loaded_surface->w;
height_ = loaded_surface->h;
}
// Elimina la textura cargada
SDL_FreeSurface(loadedSurface);
SDL_DestroySurface(loaded_surface);
}
// Return success
stbi_image_free(data);
texture = newTexture;
return texture != nullptr;
texture_ = new_texture;
return texture_ != nullptr;
}
void Texture::free()
{
// Free texture if it exists
if (texture != nullptr)
if (texture_ != NULL)
{
SDL_DestroyTexture(texture);
texture = nullptr;
width = 0;
height = 0;
SDL_DestroyTexture(texture_);
texture_ = NULL;
width_ = 0;
height_ = 0;
}
}
void Texture::render(SDL_Rect *src, SDL_Rect *dst)
void Texture::render(SDL_FRect *src, SDL_FRect *dst)
{
// Render to screen
SDL_RenderCopy(renderer, texture, src, dst);
SDL_RenderTexture(renderer_, texture_, src, dst);
}
int Texture::getWidth()
{
return width;
return width_;
}
int Texture::getHeight()
{
return height;
return height_;
}
// Modulación de color
void Texture::setColor(int r, int g, int b)
{
SDL_SetTextureColorMod(texture, r, g, b);
SDL_SetTextureColorMod(texture_, r, g, b);
}