Mes recomanacions de cppcheck

This commit is contained in:
2024-10-13 19:26:27 +02:00
parent 46540ad7c3
commit babf02226c
22 changed files with 291 additions and 369 deletions

View File

@@ -11,7 +11,7 @@
#include "stb_image.h" // for stbi_failure_reason, stbi_image_free
// Constructor
Texture::Texture(SDL_Renderer *renderer, std::string path)
Texture::Texture(SDL_Renderer *renderer, const std::string &path)
: renderer_(renderer), path_(path)
{
// Inicializa
@@ -37,8 +37,8 @@ Texture::Texture(SDL_Renderer *renderer, std::string path)
// .gif
else if (extension == "gif")
{
surface_ = loadSurface(path_.c_str());
addPalette(path_.c_str());
surface_ = loadSurface(path_);
addPalette(path_);
setPaletteColor(0, 0, 0x00000000);
createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
SDL_SetTextureBlendMode(texture_, SDL_BLENDMODE_BLEND);
@@ -54,13 +54,13 @@ Texture::~Texture()
}
// Carga una imagen desde un fichero
bool Texture::loadFromFile(std::string path)
bool Texture::loadFromFile(const std::string &path)
{
const std::string file_name = path.substr(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);
if (data == nullptr)
if (!data)
{
#ifdef VERBOSE
std::cout << "Loading image failed: " << stbi_failure_reason() << std::endl;
@@ -70,19 +70,19 @@ bool Texture::loadFromFile(std::string path)
else
{
#ifdef VERBOSE
std::cout << "Image loaded: " << file_name.c_str() << std::endl;
std::cout << "Image loaded: " << file_name << std::endl;
#endif
}
int depth, pitch;
Uint32 pixel_format;
if (req_format == STBI_rgb)
/*if (req_format == STBI_rgb)
{
depth = 24;
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
pixel_format = SDL_PIXELFORMAT_RGB24;
}
else
else*/
{ // STBI_rgb_alpha (RGBA)
depth = 32;
pitch = 4 * width;
@@ -96,11 +96,11 @@ bool Texture::loadFromFile(std::string path)
SDL_Texture *newTexture = nullptr;
// Carga la imagen desde una ruta específica
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
auto loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom(static_cast<void*>(data), width, height, depth, pitch, pixel_format);
if (loadedSurface == nullptr)
{
#ifdef VERBOSE
std::cout << "Unable to load image " << path.c_str() << std::endl;
std::cout << "Unable to load image " << path << std::endl;
#endif
}
else
@@ -110,7 +110,7 @@ bool Texture::loadFromFile(std::string path)
if (newTexture == nullptr)
{
#ifdef VERBOSE
std::cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl;
std::cout << "Unable to create texture from " << path << "! SDL Error: " << SDL_GetError() << std::endl;
#endif
}
else
@@ -241,10 +241,10 @@ SDL_Texture *Texture::getSDLTexture()
// Crea una nueva surface
Surface Texture::newSurface(int w, int h)
{
Surface surf = (Surface)malloc(sizeof(surface_s));
Surface surf = static_cast<Surface>(malloc(sizeof(surface_s)));
surf->w = w;
surf->h = h;
surf->data = (Uint8 *)malloc(w * h);
surf->data = static_cast<Uint8 *>(malloc(w * h));
return surf;
}
@@ -265,29 +265,29 @@ void Texture::deleteSurface(Surface surface)
}
// Crea una surface desde un fichero .gif
Surface Texture::loadSurface(const char *file_name)
Surface Texture::loadSurface(const std::string &file_name)
{
FILE *f = fopen(file_name, "rb");
FILE *f = fopen(file_name.c_str(), "rb");
if (!f)
{
return NULL;
return nullptr;
}
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
Uint8 *buffer = (Uint8 *)malloc(size);
Uint8 *buffer = static_cast<Uint8 *>(malloc(size));
fread(buffer, size, 1, f);
fclose(f);
Uint16 w, h;
Uint8 *pixels = LoadGif(buffer, &w, &h);
if (pixels == NULL)
if (pixels == nullptr)
{
return NULL;
return nullptr;
}
Surface surface = (Surface)malloc(sizeof(surface_s));
Surface surface = static_cast<Surface>(malloc(sizeof(surface_s)));
surface->w = w;
surface->h = h;
surface->data = pixels;
@@ -312,7 +312,7 @@ void Texture::flipSurface()
// Vuelca los datos
Uint32 *pixels;
int pitch;
SDL_LockTexture(texture_, nullptr, (void **)&pixels, &pitch);
SDL_LockTexture(texture_, nullptr, reinterpret_cast<void**>(&pixels), &pitch);
for (int i = 0; i < width_ * height_; ++i)
{
pixels[i] = palettes_[paletteIndex_][surface_->data[i]];
@@ -327,11 +327,11 @@ void Texture::setPaletteColor(int palette, int index, Uint32 color)
}
// Carga una paleta desde un fichero
std::vector<Uint32> Texture::loadPal(const char *file_name)
std::vector<Uint32> Texture::loadPal(const std::string &file_name)
{
std::vector<Uint32> palette;
FILE *f = fopen(file_name, "rb");
FILE *f = fopen(file_name.c_str(), "rb");
if (!f)
{
return palette;
@@ -340,12 +340,12 @@ std::vector<Uint32> Texture::loadPal(const char *file_name)
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
Uint8 *buffer = (Uint8 *)malloc(size);
Uint8 *buffer = static_cast<Uint8 *>(malloc(size));
fread(buffer, size, 1, f);
fclose(f);
Uint32 *pal = LoadPalette(buffer);
if (pal == nullptr)
auto pal = LoadPalette(buffer);
if (!pal)
{
return palette;
}